1

我正在寻找一种将包含 MqlTick 结构(由 CopyTicksRange() 返回)“columnwise”的数组解析为 JSON 的有效方法。

生成的 JSON 格式为

{        "ticks": [       // column-based two-dimensional array
          [<string>],     // time    
          [<float>],      // bid    
          [<float>],      // ask    
          [<float>],      // last    
          [<long>],       // volume    
          [<long>],       // epoc time in ms    
          [<int>]         // flags    
        ]
}

我目前的分钟。示例采用 2018 年 3 月 24 日的所有 24 小时刻度,组成一个 9.5m 字符的 JSON:

void OnStart()  {


MqlTick ticks[];
long pStart = 1521759600000;
long pEnd = 1521846000000;
int err = CopyTicksRange("EURUSD", ticks, COPY_TICKS_ALL, pStart, pEnd);

string c_time = "\"" + ticks[0].time + "\""; 
string c_bid = ""  + ticks[0].bid; 
string c_ask = ""  + ticks[0].ask; 
string c_last = ""  + ticks[0].last;
string c_volume = ""  + ticks[0].volume; 
string c_time_msc = "" + ticks[0].time_msc; 
string c_flags = ""  + ticks[0].flags;

for(int i = 1; i < ArraySize(ticks); i++) {

   c_time += ",\"" + ticks[i].time  + "\""; 
   c_bid += ","  + ticks[i].bid; 
   c_ask += ","  + ticks[i].ask; 
   c_last += ","  + ticks[i].last;
   c_volume += ","  + ticks[i].volume; 
   c_time_msc += "," + ticks[i].time_msc; 
   c_flags += ","  + ticks[i].flags;    
}
    json =    "{" +
              "\"ticks\":[[" + 
                  c_time + "],[" +
                  c_bid + "],[" +
                  c_ask + "],[" +
                  c_last + "],[" +
                  c_volume + "],[" +
                  c_time_msc + "],[" +
                  c_flags +
              "]]}"; 


}
Print("done");

}

这肯定比每列的循环要好,但我仍然想知道是否有更好的方法?

4

1 回答 1

0

最好的方法是使用现成的 jason.mqh 库,这里提供: https ://www.mql5.com/en/code/13663

CJAVAL json(NULL,jtUNDEF);
json["time"]=ticks[i].time; and so on

CJAVAL jarray(NULL,jtUNDEF);
jarray[IntegerToString(i)].Set(json); - for adding elements into array

最后,将 json 转换为字符串(以防你也需要)

string result;  
jarray.Serialize(result);
于 2018-03-24T16:54:05.053 回答