Persistence array - MQL4 - page 2

 
zaro123:

(Or maybe there is a Deserialization and Serialization library that I'm missing)

https://www.mql5.com/en/code/13663

JSON Serialization and Deserialization (native MQL)
JSON Serialization and Deserialization (native MQL)
  • www.mql5.com
ForecastOscilator_HTF The ForecastOscilator indicator with the timeframe selection option available in the input parameters. Flat_HTF The Flat indicator with the timeframe selection option available in the input parameters. FX5_SelfAdjustingRSI_HTF The FX5_SelfAdjustingRSI...
 
nicholi shen:

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




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

Thanks all!

I decided to go with your way.

I have one last problem with this way, maybe you could help me out.

On MyObj I also have an array of take profit levels (   double         tpList[];)


and I'm having a hard time realizing why I'm wrong


#property strict
#include <Arrays\ArrayObj.mqh>
//+------------------------------------------------------------------+
class MyObj : public CObject
{
public:
   string         name;
   double         price;
   datetime       time;
   double         tpList[];
   
   virtual bool   Save(const int file_handle) override   // MUST BE OVERRIDDEN FOR LOAD/SAVE          
   { 
      if(!FileWriteString(file_handle,name,10)) return false;
      if(!FileWriteDouble(file_handle,price))   return false; 
      if(!FileWriteLong(file_handle,(long)time))return false;  
      if(!FileWriteArray(file_handle,tpList))return false;  
      return(true);   
   }
   virtual bool   Load(const int file_handle) override   // MUST BE OVERRIDDEN FOR LOAD/SAVE                   
   { 
      name = FileReadString(file_handle,10);
      price= FileReadDouble(file_handle); 
      time = (datetime)FileReadLong(file_handle);
      FileReadArray(file_handle, tpList);
      return(true);   
   }
   virtual int    Compare(const CObject *node,const int mode=0)const override // MUST BE OVERRIDDEN FOR SORT
   {
      MyObj *other = (MyObj*)node; 
      if(this.price > other.price) return 1;
      if(this.price < other.price) return -1;
      return 0;
   }
   string ToString(){ return name+" - "+string(time)+" - "+string(price);}
};
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
class MyObjArray : public CArrayObj
{
public:
   virtual bool CreateElement(const int index) override //MUST BE OVERRIDDEN FOR LOAD/SAVE
   {
      m_data[index] = new MyObj;
      return true;
   }
   
   MyObj* operator[](const int i)const
   {
      return (MyObj*)At(i);
   }
   
   bool Load()
   { 
      int h = FileOpen("MyTest.bin",FILE_READ|FILE_BIN);
      bool res = CArrayObj::Load(h);
      FileClose(h);
      
      return res;
   }
   bool Save()
   { 
      int h = FileOpen("MyTest.bin",FILE_WRITE|FILE_BIN);
      bool res = CArrayObj::Save(h);
      FileClose(h);
      
      return res;
   }
};

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
//---
   srand(GetTickCount());
   MyObjArray *arr = new MyObjArray;
   
   int num = 1;
   for(int i=0;i<SymbolsTotal(false)&&i<10;i++)
   {
      double tpList1[] = {1,2};
   
      MyObj *obj = new MyObj;
      obj.name = SymbolName(i,false);
      obj.price= double(rand()%1000);
      obj.time = TimeCurrent();
      ArrayCopy(obj.tpList, tpList1);
      arr.Add(obj);
   }
   arr.Save();

   delete arr;
   arr = new MyObjArray;
   arr.Load();
   arr.Sort();
   
   for(int i=0;i<arr.Total();i++)
   {
      string rb = arr[i].ToString();
   
      Print(arr[i].ToString());   
   }
   
   delete arr;
}
//+------------------------------------------------------------------+



Edit : I Succeed after changing the array to CArrayDouble :) 

 
Taras Slobodyanik:

https://www.mql5.com/en/code/13663

I like this idea also! If you're not already using OOP and object collections then this is the way to go, however, if you are using object collections then just override the methods.