writting in CSV file with loop not working

 
 //+------------------------------------------------------------------+
//|                                         multi_chart_analyzer.mq4 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
 
  double GBPUSD_array[][6];
    double USDCHF_array[][6];
     double EURGBP_array[][6];
       double EURCHF_array[][6];
         double EURUSD_array[][6];
       
        int error_number=-1;
        int GBP_strenght=0;
        int USD_strenght=0;
        int EUR_strenght=0;
 
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
  
  int e = ArrayCopyRates(GBPUSD_array,"GBPUSD", PERIOD_D1); 
  e = ArrayCopyRates(USDCHF_array,"USDCHF", PERIOD_D1);
  e = ArrayCopyRates(EURUSD_array,"EURUSD", PERIOD_D1); 
  e = ArrayCopyRates(EURGBP_array,"EURGBP", PERIOD_D1); 
  e = ArrayCopyRates(EURCHF_array,"EURCHF", PERIOD_D1); 
   
  if (e>error_number)
  
  for (int i=0;i<=50;i++)
      {
      Sleep(15);
      
      
      if ((GBPUSD_array[i][0]==EURGBP_array[i][0])&&(EURGBP_array[i][0]==EURUSD_array[i][0])&&(GBPUSD_array[i][0]==EURUSD_array[i][0]))
   
      
       {
   if (GBPUSD_array[i][1]>GBPUSD_array[i][4])
   {
   GBP_strenght = GBP_strenght-1;
     USD_strenght=USD_strenght+1;  
     
    }
    
   if (GBPUSD_array[i][1]<GBPUSD_array[i][4])
 {
    GBP_strenght = GBP_strenght+1;
      USD_strenght=USD_strenght-1;
     }
     
        
   if (EURGBP_array[i][1]<EURGBP_array[i][4])
   {
   EUR_strenght = EUR_strenght+1;
     GBP_strenght = GBP_strenght-1;
    }
       if (EURGBP_array[i][1]>EURGBP_array[i][4])
      {
   EUR_strenght = EUR_strenght-1;
     GBP_strenght = GBP_strenght+1;
    }
        if (EURUSD_array[i][1]<EURUSD_array[i][4])
   {
   EUR_strenght = EUR_strenght+1;
     USD_strenght = USD_strenght-1;
    }
       if (EURUSD_array[i][1]>EURUSD_array[i][4])
      {
   EUR_strenght = EUR_strenght-1;
     USD_strenght = USD_strenght+1;
    }

int handle;

  handle=FileOpen("pair_strenght.csv", FILE_CSV|FILE_WRITE, ';');
  if(handle>0)
    {
     FileWrite(handle, TimeToStr(EURGBP_array[i][0]),EUR_strenght,USD_strenght, GBP_strenght );
     FileClose(handle);
    }
     
        if ((GBP_strenght==0)&&(EUR_strenght==0)&&(USD_strenght==0))
     {
      Alert ("History data not found.STOPING");
       break;
     }
    
     GBP_strenght=0;
     EUR_strenght=0;
     USD_strenght=0;
     
     }
     
     else
     Alert ("Datatime mismatch. STOPING");
       }
      
   

//----End of file writting
   return(0);
   
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+

int start()
  {
//----

//----
   return(0);
  }
//+----------

Description: I want to write inside csv file each days Symbol strengths each in separate row so it would be asc or desc by date time . Everything works fine but it just writes the first row with necessary data. I understand that there is problem with loop. I tried to put in different places the code that should write in csv file each cycle strengths, but without success. If you could just point me in right direction, It would be fantastic.

Also question: How can I insert that data inside multidimensional array ? Is there some way to do that. The code would be inside the place off that file writing in csv file, so at the end of each cycle Symbol strengths are added to array and moved on?

Thanks

 
edgars.rworks:

Description: I want to write inside csv file each days Symbol strengths each in separate row so it would be asc or desc by date time . Everything works fine but it just writes the first row with necessary data. I understand that there is problem with loop. I tried to put in different places the code that should write in csv file each cycle strengths, but without success. If you could just point me in right direction, It would be fantastic.

Also question: How can I insert that data inside multidimensional array ? Is there some way to do that. The code would be inside the place off that file writing in csv file, so at the end of each cycle Symbol strengths are added to array and moved on?

Thanks

Don't keep opening and closing the file . . . each time you open it with FILE_CSV|FILE_WRITE it writes a new file because you haven't allowed the code to FILE_READ and seek to the end of the file . . . read the documentation for FileOpen(), it says . . . "If FILE_WRITE does not combine with FILE_READ, a zero-length file will be opened. If even the file contains some data, they will be deleted."

Open the file, write everything you want to the file, then close the file . . .

Also . . . move your code from init() to start() . . .
 

You may find useful "StringArrayLoad" function here: https://www.mql5.com/en/code/8701

This reads csv file into an array.

 
szgy74:

You may find useful "StringArrayLoad" function here: https://www.mql5.com/en/code/8701

This reads csv file into an array.

Why would I find that useful ?
 
RaptorUK:
Why would I find that useful ?


Edgar, for the second question. To read the data from csv into an array.

 
szgy74:

Edgar, for the second question. To read the data from csv into an array.

Ah, your comment was aimed at edgars, you didn't quote him at all so it looked like you were replying to me.
 
RaptorUK:
Ah, your comment was aimed at edgars, you didn't quote him at all so it looked like you were replying to me.


sorry for the misunderstanding
 
szgy74:

sorry for the misunderstanding
 
edgars.rworks:

Description: I want to write inside csv file each days Symbol strengths each in separate row so it would be asc or desc by date time . Everything works fine but it just writes the first row with necessary data. I understand that there is problem with loop. I tried to put in different places the code that should write in csv file each cycle strengths, but without success. If you could just point me in right direction, It would be fantastic.

Here is a working version of the code you posted:

int init() {
   FWTest1();
   return(0);
}

void FWTest1 () {
   double GBPUSD_array[][6], USDCHF_array[][6], EURGBP_array[][6], EURCHF_array[][6], EURUSD_array[][6];
   int GBP_strenght = 0, USD_strenght = 0, EUR_strenght = 0;
        
   string filename = "pair_strenght.csv";
   FileDelete(filename);
   GetLastError();
        
   if (IsHistoryCurrent("GBPUSD", PERIOD_D1, false))
      if (ArrayCopyRates(GBPUSD_array, "GBPUSD", PERIOD_D1) < 0) {
         Print ("Error occurred while copying rates for GBPUSD");
         return;
      }
   if (IsHistoryCurrent("USDCHF", PERIOD_D1, false))
      if (ArrayCopyRates(USDCHF_array, "USDCHF", PERIOD_D1) < 0) {
         Print ("Error occurred while copying rates for USDCHF");
         return;
      }
   if (IsHistoryCurrent("EURUSD", PERIOD_D1, false))
      if (ArrayCopyRates(EURUSD_array, "EURUSD", PERIOD_D1) < 0) {
         Print ("Error occurred while copying rates for EURUSD");
         return;
      }
   if (IsHistoryCurrent("EURGBP", PERIOD_D1, false))
      if (ArrayCopyRates(EURGBP_array, "EURGBP", PERIOD_D1) < 0) {
         Print ("Error occurred while copying rates for EURGBP");
         return;
      }
   if (IsHistoryCurrent("EURCHF", PERIOD_D1, false))
      if (ArrayCopyRates(EURCHF_array, "EURCHF", PERIOD_D1) < 0) {
         Print ("Error occurred while copying rates for EURCHF");
         return;
      }
        
   for (int i = 0; i < 50; i++) {
      if (GBPUSD_array[i][0] == EURGBP_array[i][0] && EURGBP_array[i][0] == EURUSD_array[i][0]) {   // if a = b and b = c, then a = c
         if (GBPUSD_array[i][1] > GBPUSD_array[i][4]) 
            { GBP_strenght--; USD_strenght++; }
         if (GBPUSD_array[i][1] < GBPUSD_array[i][4]) 
            { GBP_strenght++; USD_strenght--; }
         if (EURGBP_array[i][1] < EURGBP_array[i][4]) 
            { EUR_strenght++; GBP_strenght--; }
         if (EURGBP_array[i][1] > EURGBP_array[i][4]) 
            { EUR_strenght--; GBP_strenght++; }
         if (EURUSD_array[i][1] < EURUSD_array[i][4]) 
            { EUR_strenght++; USD_strenght--; }
         if (EURUSD_array[i][1] > EURUSD_array[i][4]) 
            { EUR_strenght--; USD_strenght++; }
                        
         int handle=FileOpen(filename, FILE_CSV|FILE_WRITE|FILE_READ, ';');
         if(handle > 0) {
            FileSeek(handle, 0, SEEK_END);
            FileWrite(handle, TimeToStr(EURGBP_array[i][0]), EUR_strenght, USD_strenght, GBP_strenght );
            FileClose(handle);
         }
         else
            Print ("FileOpen failed. Error # ", GetLastError());
                        
         GBP_strenght=0;
         EUR_strenght=0;
         USD_strenght=0;
      }
      else 
         Alert ("Datatime mismatch. STOPING");
        
   }
   return;
}

bool IsHistoryCurrent(string curpair = "EURUSD", int tf = PERIOD_H1, bool verbose = true) {
   datetime latest_history_time = iTime(curpair, tf, 0);
   int error = GetLastError();
   if (error > 0) {
      if (verbose) {
         if (error == 4066)
            Print (curpair, " history is updating . . . ");
         else
            Print ("An error occurred in IsHistoryCurrent(). Error # ", error);
      }
      return (false);
   }
   if (TimeCurrent() < latest_history_time + (tf * 60))
      return (true);
   else
      return (false);
}
Reason: