Persistence array - MQL4

To add comments, please log in or register
zaro123
41
zaro123  

Hey guys,

I manage my trades using an array, the array contains the next Take profit level, how much lots to close at certain points and so on.

The only problem I'm facing is if somehow my EA will crush? Or I will write a new version and need to redeploy?  I'll lose my unrecoverable array.

If I would program the EA in Python I will use the pickle library (Binary reading and writing easily) or maybe an external database, But everything seems much more complicated in MQL.

Any recommendation?

Thanks!

Keith Watford
Moderator
20336
Keith Watford  
Use Global Variables of the terminal or a file.
zaro123
41
zaro123  
Keith Watford:
Use Global Variables of the terminal or a file.

Global Variables supports only double values, So it's not an option

writing to a file a complicated array is also a mess. (Or maybe there is a Deserialization and Serialization library that I'm missing)

Keith Watford
Moderator
20336
Keith Watford  
zaro123:

Global Variables supports only double values, So it's not an option

writing to a file a complicated array is also a mess. (Or maybe there is a Deserialization and Serialization library that I'm missing)

What do you need to save that cannot be expressed as a double?

zaro123
41
zaro123  
Keith Watford:

What do you need to save that cannot be expressed as a double?

I have this list on my EA : (Vector<OrderDetails*> list)

(using https://github.com/dingmaotu/mql4-lib/blob/master/Collection/Vector.mqh)

OrderDetails:


 could the list convert to a double's only?

Philipp Negreshniy
4074
Philipp Negreshniy  
zaro123:

Hey guys,

I manage my trades using an array, the array contains the next Take profit level, how much lots to close at certain points and so on.

The only problem I'm facing is if somehow my EA will crush? Or I will write a new version and need to redeploy?  I'll lose my unrecoverable array.

If I would program the EA in Python I will use the pickle library (Binary reading and writing easily) or maybe an external database, But everything seems much more complicated in MQL.

Any recommendation?

Thanks!

If you already have experience in python, then it is most reasonable to use these skills in MQL.

Python is a good language and has so many features and libraries, and all this can be used.

For example, I write python blocks of code into my MQL4 and MQL5 EA's and send it to the execution console via named pipes.

nicholi shen
2392
nicholi shen  
You'll probably want to subclass CObject and override the Save Load methods. Then override the CreateElement object in CArrayObj to implement collection serialization. This will save the collection to a bin file and quickly restore it by passing the file handle back to the CarrayObj Load method. 
zaro123
41
zaro123  
nicholi shen:
You'll probably want to subclass CObject and override the Save Load methods. Then override the CreateElement object in CArrayObj to implement collection serialization. This will save the collection to a bin file and quickly restore it by passing the file handle back to the CarrayObj Load method. 

Thank you guys!

Nicholi answers seem like the one I looked for.

Nicholi, Maybe you have a code example? This could really help

nicholi shen
2392
nicholi shen  
zaro123:

Thank you guys!

Nicholi answers seem like the one I looked for.

Nicholi, Maybe you have a code example? This could really help

Something along the lines of this... (this is obviously lazy code) Look at the ChartObjects libs for better examples. 


#include <arrays/arrayobj.mqh>

class LotLevel : public CObject
{
public:
   int ticket;
   double price;
   double lots;
   virtual bool Save(const int h) override { 
      FileWriteInteger(h, ticket);
      FileWriteDouble(h, price);
      FileWriteDouble(h, lots);
      return true;
   }
   virtual bool Load( const int h) override {
      ticket = FileReadInteger(h);
      price = FileReadDouble(h);
      lots = FileReadDouble(h);
      return true;
   }
};

class LotLevelArray : public CArrayObj
{
   public: virtual bool CreateElement(const int index) override { 
      m_data[index] = new LotLevel();
      return true;   
   }
};



Also checkout this https://www.mql5.com/en/forum/213003

[MQL4/5] How to save/load/sort dynamic object collections?
[MQL4/5] How to save/load/sort dynamic object collections?
  • 2017.08.09
  • www.mql5.com
I wasn't able to find any documentation on how to override the CObject virtual methods Load, Save, and Compare to make use of(CList and CArrayObj...
Mladen Rakic
159742
Mladen Rakic  
zaro123:

I have this list on my EA : (Vector<OrderDetails*> list)

(using https://github.com/dingmaotu/mql4-lib/blob/master/Collection/Vector.mqh)

OrderDetails:


 could the list convert to a double's only?

If you do not intend to add string element to your structure, you can simply use FileWriteStruct() and FileReadStruct() to save and retrieve the values

Anthony Garot
3259
Anthony Garot  
Mladen Rakic:

If you do not intend to add string element to your structure, you can simply use FileWriteStruct() and FileReadStruct() to save and retrieve the values

@Mladen's advice is what I do with any Simple Structures.

Simple Structures

Structures that do not contain strings, class objects, pointers and objects of dynamic arrays are called simple structures.

And if you have an arrays of Simple Structures, use FileLoad() and FileSave(). FileReadArray() and FileWriteArray()

12
To add comments, please log in or register