Problem with FileWriteStruct

Back to topics list To post a new topic, please log in or register
avatar
81
texasnomad 2015.12.18 21:40
 

I'm trying to write a script which writes my trading history to a binary file. I tried to follow the example at https://docs.mql4.com/files/filewritestruct, but I keep getting the compile error "'tradeBuff' - parameter conversion not allowed OSR Trade History Recorder Binary.mq4 72 33"

 I can't figure out what the compiler is unhappy. Do you know why this won't compile?

 

#property strict

struct trade 
{
   string symbol;
   int direction;
   double lots;
   datetime openTime;
   datetime closeTime;
   double sl;
   double tp;
};


void OnStart()
{  
   trade tradeBuff[];
   string symbols[];
   int directions[];
   double lots[];
   datetime openTimes[];
   datetime closeTimes[];
   
   int size = OrdersHistoryTotal();
   ArrayResize(tradeBuff,size);
   ArrayResize(symbols,size);
   ArrayResize(directions,size);
   ArrayResize(lots,size);
   ArrayResize(openTimes,size);
   ArrayResize(closeTimes,size);
   
   for(int i = size - 1; i >= 0; i--)
   {
      if( OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) )
      {
         if(OrderType()==6)
            continue;
         
         symbols[i] = OrderSymbol();
         directions[i] = OrderType();        
         lots[i] = OrderLots();
         openTimes[i] = OrderOpenTime();
         closeTimes[i] = OrderCloseTime();
      }
   }
   
   for(int i = size - 1; i >= 0; i--)
   {
      tradeBuff[i].symbol = symbols[i];
      tradeBuff[i].direction = directions[i];
      tradeBuff[i].lots = lots[i];
      tradeBuff[i].openTime = openTimes[i];
      tradeBuff[i].closeTime = closeTimes[i];
   }
 
   string file = "tradeHistory.bin";
   
   int handle = FileOpen(file,FILE_READ|FILE_WRITE|FILE_BIN|FILE_COMMON);
   
   if( handle != INVALID_HANDLE )
   {
      for(int i = size - 1; i >= 0; i--)
      {
         FileWriteStruct(handle,tradeBuff[i]);  // THIS IS WHERE THE COMPILE ERROR OCCURS
	// 'tradeBuff' - parameter conversion not allowed	OSR Trade History Recorder Binary.mq4	72	33
      }
   }   
   FileClose(handle);  
}

 

avatar
1
MartinT 2015.12.22 20:20 #
 
FileWriteStruct doesn't work with arrays that "contain strings, dynamic arrays or virtual functions.". To make it compile you need to remove string symbol; from trade struct.
avatar
12985
WHRoeder 2015.12.22 22:02 #
 
MartinT: FileWriteStruct doesn't work with arrays that "contain strings, dynamic arrays or virtual functions.". To make it compile you need to remove string symbol; from trade struct.
Exactly. next time RTFM FileWriteStruct - MQL4 Documentation

struct_object

[in] Reference to the object of this structure. The structure should not contain strings, dynamic arrays or virtual functions.


avatar
81
texasnomad 2015.12.22 22:49 #
 

Can I convert the string to bytes or another data type so that I don't lose the information?

 WHRoeder, there's no reason to be rude. It was an honest oversight. I modeled my code on the documentation... I obviously read the manual. Snotty remarks like yours are the reason people don't ask questions when they need help. 

avatar
1109
Ovo 2015.12.22 22:56 #
 
Use char[12] instead, 12 is max length of a symbol.
avatar
126
Demos 2015.12.22 23:58 #
 
texasnomad:

Can I convert the string to bytes or another data type so that I don't lose the information?

 WHRoeder, there's no reason to be rude. It was an honest oversight. I modeled my code on the documentation... I obviously read the manual. Snotty remarks like yours are the reason people don't ask questions when they need help. 

hello,

regarding RTFM i may say that i find it to be in the funny side - at least for me ; some years before (ahem) whenever i was facing that epxression, I got the impression that it was used in a friendly manner , coming from a positive stance and also humorous :) I could use it in a similar manner on my own - a rather geeky approach if you ask me but quite common to computer users , but again, not in a bad attitude ( i guess :) )

 

avatar
1109
Ovo 2015.12.23 13:57 #
 
Demos:

hello,

regarding RTFM i may say that i find it to be in the funny side - at least for me ; some years before (ahem) whenever i was facing that epxression, I got the impression that it was used in a friendly manner , coming from a positive stance and also humorous :) I could use it in a similar manner on my own - a rather geeky approach if you ask me but quite common to computer users , but again, not in a bad attitude ( i guess :) )

 

Well, you may use it both ways, I think. A good advice and expressing contempt.

Anyway, I still have a bug report pending at Service Desk since 2014.08.28 - the third parameter always gets trimmed to the sizeof of declared structure. I.e. it invalidates structure inheritance, and actually the parameter becomes very useless.

avatar
81
texasnomad 2015.12.28 17:58 #
 
Ovo:
Use char[12] instead, 12 is max length of a symbol.
Thanks!
Back to topics list  

To add comments, please log in or register