目标:动态获取最新K线并通过DLL发送出去,symbols和periods可配置。

//+------------------------------------------------------------------+
//| kline.mq4 |
//| Copyright 2018, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict string symbols[]={"USDJPY","USDCHF","USDCAD","NZDUSD","GBPUSD","EURUSD","AUDUSD",
"AUDCAD","AUDJPY","EURCAD","GBPAUD","GBPJPY","XAUUSD","XAGUSD","Brent","WTI"}; int periods[]={PERIOD_M1,PERIOD_M5,PERIOD_M15,PERIOD_M30,PERIOD_H1,PERIOD_H4,PERIOD_D1,PERIOD_W1,PERIOD_MN1};
//PERIOD_M1,PERIOD_M5,PERIOD_M15,PERIOD_M30,PERIOD_H1,PERIOD_H4,PERIOD_D1,PERIOD_W1,PERIOD_MN1 // int gCount = 0; struct RateInfo
{
int time;
int ctm; // rate time
int open; // open price: 11987=119.87
int high, low, close; // high,low,close shift from open
double vol; // volume
}; //DLL
#import "TradeQuot.dll"
void UnInit();
int Init(int,int);
void OnKineDataChange(string symbol, int period, RateInfo &data[],int kinesize);
void OnKineDataChangeW(string symbol, int period, RateInfo &data[], int kinesize);
void OnKineDataChangeExW(string symbol, int period,RateInfo &data);
#import
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//Print(PERIOD_M1," ",PERIOD_M5," ",PERIOD_M15," ",PERIOD_M30," ",PERIOD_H1," ",PERIOD_H4," ",PERIOD_D1," ",PERIOD_W1," ",PERIOD_MN1);
int ret = Init(1,0);
if(ret!=1){
Print("init dll failed code=",ret);
return INIT_FAILED;
} long begin=TimeLocal();
GetKline(0);
long end=TimeLocal();
Print("OnInit use:"+(end-begin)+"s");
//--- create timer
EventSetTimer(60);
//---
return(INIT_SUCCEEDED);
} void GetKline(int type)
{
//long t=TimeLocal();
int maxBars = 2;
if(type==0){
maxBars = 5000;
}
for(int i=0;i<ArraySize(symbols);i++){
for(int j=0;j<ArraySize(periods);j++){
string symbol=symbols[i];
int period = periods[j]; MqlRates rates[];
ArraySetAsSeries(rates,true);
int copied=CopyRates(symbol,period,0,maxBars,rates);
Print("GetKline:["+symbol+":"+period+"] - "+copied);
if(copied>0){
RateInfo item[];
ArrayResize(item,copied);
for(int m=0;m<copied;m++)
{
item[m].time = rates[m].time;
item[m].ctm = rates[m].time;
item[m].open = rates[m].open;
item[m].high = rates[m].high;
item[m].low = rates[m].low;
item[m].close = rates[m].close;
item[m].vol = rates[m].tick_volume;
gCount=gCount+1;
if(gCount%100000==0){
printf("%s %d %s %lf %lf %lf %lf %d",symbol,period,TimeToString(rates[m].time),
rates[m].open,rates[m].high,rates[m].low,rates[m].close,rates[m].tick_volume);
}
}
OnKineDataChangeW(symbol,period,item,copied);
ArrayFree(item);
}else{
Print("Failed to get history data for the symbol ",symbol," period ",period," code ",GetLastError());
}
}
}
} //+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- destroy timer
EventKillTimer(); UnInit();
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- }
//+------------------------------------------------------------------+
//| Timer function |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
Print("onTimer...");
GetKline(1);
}
//+------------------------------------------------------------------+
05-11 18:05