如何在阵列MQL4中初始化,读取和写入变量名存储

时间:2019-12-09 05:51:02

标签: mql4

我仍然不是一个好的编码员,请给我一些建议,如何初始化,读取和写入Array MQL4中的变量名存储。 的目的,我想尝试在单个窗口中使EA多对,并在整个循环过程中保持ALL-STATUS的状态,例如以下代码:

如何简化:


                     if  (pairs[cmp] == "EURUSD") 
                               { 
                                 VAR1 =  str01EURUSD;
                                 VAR2 =  int01EURUSD;
                                 VAR3 = date01EURUSD;
                                 VAR4 =  dou01EURUSD;
                                 .....
                                 .....
                                 VARN =  xxx01EURUSD;
                               }


                      if (symbolx == "EURUSD")
                               {
                                   str01EURUSD = VAR1;
                                   int01EURUSD = VAR2;
                                  date01EURUSD = VAR3;
                                   dou01EURUSD = VAR4;
                                  .....
                                  .....
                                   xxx01EURUSD = VARN
                               }

在数组操作中。因为如果还有其他状态码,这将花费时间并增加行数。

示例代码:



     //multipair

     string pairs[] = 
         {
         "EURUSD",
         "GBPUSD",
         "AUDUSD",
         "USDCAD",
         "CHFJPY",
         .........
         .........
         "XAUUSD"
         };

       //Variable name pair


       string     str01EURUSD = "BUY";
       ..
       ..
       string     str01XAUUSD = "BUY";


       int    int01EURUSD = 0;
       ..
       ..
       int    int01XAUUSD = 0;


       datetime  date01EURUSD;
       ..
       ..
       datetime  date01XAUUSD;


       double    dou01EURUSD = 0;
       ..
       ..
       double    dou01XAUUSD = 0;



       int start()

       for(int cmp = 0; cmp < ArraySize(pairs); cmp++)
        {



             if  (pairs[cmp] == "EURUSD") 
                   { 
                       VAR1 =  str01EURUSD;
                       VAR2 =  int01EURUSD;
                       VAR3 = date01EURUSD;
                       VAR4 =  dou01EURUSD;
                       .....
                       .....
                       VARN =  xxx01EURUSD;
                   }

             .............
             .............

             if  (pairs[cmp] == "XAUUSD") 
                  { 
                       VAR1 =  str01XAUUSD;
                       VAR2 =  int01XAUUSD;
                       VAR3 = date01XAUUSD;
                       VAR4 =  dou01XAUUSD;
                       .....
                       .....
                       VARN =  xxx01XAUUSD;
                   }


     // call function checkdata
     checkdata(pair[cmp], VAR1, VAR2, VAR3, VAR4, VARN);


         if (symbolx == "EURUSD")
          {
             str01EURUSD = VAR1;
             int01EURUSD = VAR2;
            date01EURUSD = VAR3;
             dou01EURUSD = VAR4;
            .....
            .....
             xxx01EURUSD = VARN
          }
         if (symbolx == "XAUUSD")
          {
             str01XAUUSD = VAR1;
             int01XAUUSD = VAR2;
            date01XAUUSD = VAR3;
             dou01XAUUSD = VAR4;
            .....
            .....
             xxx01XAUUSD = VARN
          }
     }

       //checkdata function 
       checkdata(string symbol, string code01, int code02, datetime code03, double code04, ....., .....,   xxxx codeN)  
       {

         all checking process ...
         ............
         ............

         symbolx = symbol;
         VAR1 = code01;
         VAR2 = code02;
         VAR3 = code03;
         VAR4 = code04;
         ....
         ....
         VARN = codeN;

         return(symbolx);
         return(VAR1);
         return(VAR2);
         return(VAR3);
         return(VAR4);
         .....
         .....
         return(VARN);

        }

1 个答案:

答案 0 :(得分:0)

使用继承CObject.mqh的类并将它们全部放入CArrayObj.mqh列表中。通过填充类实例的字段来进行初始化(类似于pairs[cmp] == "EURUSD") { VAR1 = str01EURUSD; ... },然后在OnInit()中将类添加到列表中,然后使用该列表遍历OnTick()中的参数

下面的示例代码


    #include <Arrays/ArrayObj.mqh>
    class CParams : public CObject
      {
       string      m_symbol;
       datetime    m_date01;
       double      m_double01;

       bool        checkData()
         {
          //do not know what you may need here, return true if need to proceed;
          // or signature change to int that returns -1 (SELL), 0 (pass) or 1(BUY)
          return false;
         }

    public:
       CParams(const string smb,const datetime dt01,const double d01):m_symbol(smb),m_date01(dt01),m_double01(d01){}
      ~CParams(){}
       void        onTick()
         {
          if(!checkData())
             return;
          //open another trade?
         }

      };
    CArrayObj *params;


    int OnInit()
      {
       params=new CArrayObj();
       params.Add(new CParams("EURUSD",date01EURUSD,dou01EURUSD));
       // add more instances of CParams
       return(INIT_SUCCEEDED);
      }
    void OnDeinit(const int reason)
      {
       delete(params);
      }
    void OnTick()
      {
       for(int i=params.Total()-1;i>=0;i--)
         {
          CParams *param=params.At(i);
          param.onTick();
         }
      }