Store data by key and value in mql5

 
Hello friends, I want to save the highest price after opening a new position. This is easy when only one position is open, but it is difficult for me to implement it when several positions are open.
In Python, I use a dictionary for a similar situation, for example, the { tiket : highPrice}.
  But in MQL5, I don't know how to solve this problem. Can you suggest me a solution?
 
Your topic has been moved to the section: Expert Advisors and Automated Trading — Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
mansour afshar:
Hello friends, I want to save the highest price after opening a new position. This is easy when only one position is open, but it is difficult for me to implement it when several positions are open.
In Python, I use a dictionary for a similar situation, for example, the { tiket : highPrice}.
  But in MQL5, I don't know how to solve this problem. Can you suggest me a solution?
You could use a hash map to do that.

 
Dominik Egert #: You could use a hash map to do that. https://www.mql5.com/en/docs/standardlibrary/generic/imap
if there are lots of datas to store, another way is to consider using nosql Engine.such as Redis. because it is more effective than mql.
 
Guo Zheng Feng #:
if there are lots of datas to store, another way is to consider using nosql Engine.such as Redis. because it is more effective than mql.
Do you have a redis implementation in MQL? Could you please link it to your post as well, because it's not helpful to suggest a solution without giving access to the same...
 
Dominik Egert #:
Do you have a redis implementation in MQL? Could you please link it to your post as well, because it's not helpful to suggest a solution without giving access to the same...
not yet,because i dont need to using that feature for current moment. but it should be quiet easy to do that.just some command and it is just almost pure text transfer base socket.
 

hey guy,had you got a library that can help you?

here is my solution,which i used to story *a few* key pares in my MQL4(it shoud work also in 5)

class clsRuntimeHash{
      private:
            struct _HashTable{
               string   key;
               string   value;
               datetime LastModifyTime;
               datetime LastAccessTime;
               long      TimeToLive_Minutes;
            };
            _HashTable HashTable[];
            string FieldSeperator/*="@#$||"*/;
      public:
            string FileName;
      int RETURN_STATUS_TTLExpired;
      int RETURN_STATUS_KeyNotFound;
      int RETURN_STATUS_OK;
      int RETURN_STATUS_KeyAlreadyExists;
      clsRuntimeHash():RETURN_STATUS_OK(0),RETURN_STATUS_TTLExpired(-1),RETURN_STATUS_KeyNotFound(-2),RETURN_STATUS_KeyAlreadyExists(-3),FieldSeperator("@#$||"){};
      clsRuntimeHash(string SyncFilename):RETURN_STATUS_OK(0),RETURN_STATUS_TTLExpired(-1),RETURN_STATUS_KeyNotFound(-2),RETURN_STATUS_KeyAlreadyExists(-3),FieldSeperator("@#$||"){FileName=SyncFilename;if (FileName!="") tryLoadHashTableFromFile(FileName);};
      int getValueByKey(const string key,string& value){
            int i;
            for (i=0;i<ArraySize(HashTable);i++){
                  if ( HashTable[i].TimeToLive_Minutes && TimeCurrent()-HashTable[i].LastModifyTime > HashTable[i].TimeToLive_Minutes ){
                          //键值对生命超时:删除键值对并返回空
                          HashTable[i].key="";
                          HashTable[i].value="";
                          HashTable[i].LastModifyTime=0;
                          HashTable[i].LastAccessTime=0;
                          HashTable[i].TimeToLive_Minutes=0;
                  }
                  if (HashTable[i].key==key&&HashTable[i].key!=""){ //匹配到键名
                              value=HashTable[i].value;
                              HashTable[i].LastAccessTime=TimeCurrent();
                              return(RETURN_STATUS_OK);
                  }
            }
            //匹配不到键名
            if (FileName!="") tryDumpHashTableToFile(FileName);
            value="";return(RETURN_STATUS_KeyNotFound);
            
      } 
      int setValueByKey(const string key,const string value,int TimeToLiveMinutes=0){
            bool keyAlreadySet=false; int iPos=-1; //用于标记是否已发生写操作,以及写操作所在的位置
            int i;
            for (i=0;i<ArraySize(HashTable);i++){
                  if ( HashTable[i].key==key) {  //发现有重复
                        if (keyAlreadySet) {  //若发现重复之前已在空白位置上作过写操作,则撤消操作
                              HashTable[iPos].LastAccessTime=0;
                              HashTable[iPos].LastModifyTime=0;
                              HashTable[iPos].TimeToLive_Minutes=0;
                              HashTable[iPos].key="";
                              HashTable[iPos].value="";
                        }
                        return ( RETURN_STATUS_KeyAlreadyExists );
                  }
                  if (HashTable[i].key=="" && !keyAlreadySet ){ //发现空白位置且未做过写操作--先写上,若后面发现有重复再撤消
                        //1.写操作
                        HashTable[i].key=key;
                        HashTable[i].value=value;
                        HashTable[i].LastAccessTime=TimeCurrent();
                        HashTable[i].LastModifyTime=TimeCurrent();
                        HashTable[i].TimeToLive_Minutes=TimeToLiveMinutes;
                        //2.标记已写操作并记录写操作所在位置
                        keyAlreadySet=true;iPos=i;
                  }                  
            }
            //遍历完成
            if (!keyAlreadySet){  //若仍未发生写操作,说明中间没有空白位置,没有重复
                  int ArraySize_Old=ArraySize(HashTable);
                  //新增一栏并填入
                  ArrayResize(HashTable,ArraySize_Old+1);
                  HashTable[ArraySize_Old].key=key;
                  HashTable[ArraySize_Old].value=value;
                  HashTable[ArraySize_Old].LastAccessTime=TimeCurrent();
                  HashTable[ArraySize_Old].LastModifyTime=TimeCurrent();
                  HashTable[ArraySize_Old].TimeToLive_Minutes=TimeToLiveMinutes;
            }    
            if (FileName!="") tryDumpHashTableToFile(FileName);
            return(RETURN_STATUS_OK);
            
      
      }
      void deleteKey(const string key){
           for (int i=0;i<ArraySize(HashTable);i++){
                  if ( HashTable[i].key==key) {  //发现有重复
                              HashTable[i].LastAccessTime=0;
                              HashTable[i].LastModifyTime=0;
                              HashTable[i].TimeToLive_Minutes=0;
                              HashTable[i].key="";
                              HashTable[i].value="";
                  }      
           }
           if (FileName!="") tryDumpHashTableToFile(FileName);
      }
      void tryDumpHashTableToFile(const string filename){
            int handle=FileOpen(filename,FILE_CSV|FILE_WRITE);
            if (handle==INVALID_HANDLE) return;
            string tmp;
            string sep=FieldSeperator;
            for (int i=0;i<ArraySize(HashTable);i++){
                  if (HashTable[i].key!="") {
                  tmp=StringConcatenate(
                      HashTable[i].key,sep,
                      HashTable[i].value,sep,
                      TimeToString(HashTable[i].LastModifyTime),sep, 
                      TimeToString(HashTable[i].LastAccessTime),sep,
                      HashTable[i].TimeToLive_Minutes,"\n"
                      );
                   FileWriteString(handle,tmp);
                   }
            }
            
            FileClose(handle);
      }
      void tryLoadHashTableFromFile(const string filename){
            int handle=FileOpen(filename,FILE_CSV|FILE_READ);
            if (handle==INVALID_HANDLE) return;
            string tmp;
            string out[];
            int hts;
            ArrayResize(HashTable,0);
            while (FileTell(handle)<FileSize(handle)){
                  tmp=FileReadString(handle);
                  ArrayResize(out,0);
                  MyStrProc.split(tmp,FieldSeperator,out);
                  hts=ArraySize(HashTable);
                  ArrayResize(HashTable,hts+1);
                  HashTable[hts].key=out[0];
                  HashTable[hts].value=out[1];
                  HashTable[hts].LastModifyTime=StringToTime(out[2]);
                  HashTable[hts].LastAccessTime=StringToTime(out[3]);
                  HashTable[hts].TimeToLive_Minutes=StringToInteger(out[4]);
             }
            FileClose(handle);                
      }
};

clsRuntimeHash RuntimeHash;


simple example

#include <clsRuntimeHash.mqh>


void OnStart()
{
  //set key
  RuntimeHash.setValueByKey("myname","feng");
  //get key value
  string keyval;
  RuntimeHash.getValueByKey("myname",keyval);
  
}