file

 
Hello geniuses
I have a code that hedges a some of orders and stores them in a file.

If the robot is removed from the chart, after restarting, the robot can read the hedges from the file, but if for any reason the Meta-trader program is closed, after restarting the robot, it will display all of them as zero. Can anyone guide me???

 
st.khalili:
but if for any reason the Meta-trader program is closed

Is the file still there with valid data once you close, and re-open MetaTrader? Or does it somehow get deleted or overwritten?

It would help if you share the code you use to re-read the file when the EA is re-started

 
R4tna C #:

Is the file still there with valid data once you close, and re-open MetaTrader? Or does it somehow get deleted or overwritten?

It would help if you share the code you use to re-read the file when the EA is re-started

when i closed Metatrader, checked the file and its data are correct, but after reopen it changes and would be 0.00000000


void Write_file()
  {
   string hedges = "";
   string filename = Symbol()+(string)Period()+"hadge.csv";
   FileDelete(filename);
   int filehandle = FileOpen(filename,FILE_COMMON|FILE_READ|FILE_WRITE|FILE_CSV|FILE_ANSI,",");
   if(filehandle != INVALID_HANDLE)
     {
      FileSeek(filehandle,0,SEEK_SET);
      for(int i=0; i<ArraySize(hedgeArray); i++)
        {
         if(!hedgeArray[i].released)
           {
            for(int j=0; j<ArraySize(hedgeArray[i].tickets); j++)
              {
               hedges += (string)hedgeArray[i].tickets[j] + ",";
               // Print(hedges);                // check result
              }
            hedges = StringSubstr(hedges,0,StringLen(hedges)-1);     // strip last delimiter
            Print(hedges);                                           // check result
            hedges += "\n";                                          // add newline chr
            uint writing = FileWriteString(filehandle, hedges, StringLen(hedges));
            if(writing == 0)
              {
               Alert("error", GetLastError());
              }
            else
              {
               hedges="";
              }
           }
        }
      FileClose(filehandle);
     }
   else
      Print("Failed to open and write ",filename," due to ",GetLastError());
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Readfile()
  {
   int row = 0;
   int col = 0;
   for(int N=0; N<ArraySize(Pairhedge); N++)
     {
      Pairhedge[N].pairreleased = true;
      Pairhedge[N].pairsymbol = "";
      Pairhedge[N].pairprofit = 0;
      ArrayFree(Pairhedge[N].pairmember);
     }
   FileName();
   for(int j=0; j<ArraySize(names); j++)
     {
      string filename = names[j];
      if(StringFind(filename,Symbol())==-1)
        {
         int filehandle =FileOpen(filename,FILE_COMMON|FILE_READ|FILE_CSV,',');
         if(filehandle != INVALID_HANDLE)
           {
            while(!FileIsEnding(filehandle))
              {
               string cell = FileReadString(filehandle);
               cell= StrToInteger(cell);
               if(cell!=0)
                 {
                  ArrayResize(Pairhedge[row].pairmember, ArraySize(Pairhedge[row].pairmember)+1);
                  Pairhedge[row].pairmember[ArraySize(Pairhedge[row].pairmember)-1] = (cell);
                  if(FileIsLineEnding(filehandle))
                    {
                     col =0;
                     row++;
                    }
                  else
                     col++;
                 }
              }
            FileClose(filehandle);
           }
         else
            Print("Failed to open ",filename," due to ",GetLastError());

         for(int L=0; L<ArraySize(Pairhedge); L++)
           {
            for(int t=0; t<ArraySize(Pairhedge[L].pairmember); t++)
              {
               if(OrderSelect(Pairhedge[L].pairmember[t],SELECT_BY_TICKET))
                 {
                  Pairhedge[L].pairreleased=false;
                 }
              }
           }
        }
     }
  }
 
st.khalili #:

when i closed Metatrader, checked the file and its data are correct, but after reopen it changes and would be 0.00000000


Without debugging I cannot say for sure, but I think you need to debug this section and check you are loading the data as expected:

while(!FileIsEnding(filehandle))
              {
               string cell = FileReadString(filehandle);
               cell= StrToInteger(cell);
               if(cell!=0)
                 {
                  ArrayResize(Pairhedge[row].pairmember, ArraySize(Pairhedge[row].pairmember)+1);
                  Pairhedge[row].pairmember[ArraySize(Pairhedge[row].pairmember)-1] = (cell);
                  if(FileIsLineEnding(filehandle))
                    {
                     col =0;
                     row++;
                    }
                  else
                     col++;
                 }

You are calling the file open/close functions within a loop which struck me as peculiar, unless you are calling different files each time.

It appears to be the same file so it would be better to open once, and close at the end I think


 
R4tna C #:

Without debugging I cannot say for sure, but I think you need to debug this section and check you are loading the data as expected:

You are calling the file open/close functions within a loop which struck me as peculiar, unless you are calling different files each time.

It appears to be the same file so it would be better to open once, and close at the end I think


yes its different file each time

 
st.khalili #:

yes its different file each time

Ok - debugging would be the next step

 
R4tna C #:

Ok - debugging would be the next step

thanks alot for your answer

 
st.khalili #:

thanks alot for your answer

good luck