请帮助找到代码中的错误,或者确认CopyTicks()没有正确工作。
问题:当用CopyTicks()复制时,跳过跳针。
操作顺序。
编码
//+------------------------------------------------------------------+ //| ABL_Collector.mq5 | //| Copyright 2021, prostotrader | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2021, prostotrader" #property link "https://www.mql5.com" #property version "1.00" //--- input string StTime = "07:00:00"; //Начало сбора тиков input string EndTime = "23:50:00"; //Конец сбора тиков //--- struct MARKET_DATA { int cnt; datetime time[]; ulong time_msc[]; double ask[]; double bid[]; double last[]; long volume[]; string flags[]; ulong mem_time; int skip_cnt; MqlTick ticks[]; } m_data; int f_handle; datetime start_time, end_time; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { m_data.cnt = 0; m_data.mem_time = 0; ArrayResize(m_data.time, 5000000, 5000000); ArrayResize(m_data.time_msc, 5000000, 5000000); ArrayResize(m_data.ask, 5000000, 5000000); ArrayResize(m_data.bid, 5000000, 5000000); ArrayResize(m_data.last, 5000000, 5000000); ArrayResize(m_data.volume, 5000000, 5000000); ArrayResize(m_data.flags, 5000000, 5000000); f_handle = FileOpen("ABL_Colletor.csv", FILE_WRITE|FILE_CSV); if(f_handle == INVALID_HANDLE) { Alert("Не создан *.CSV файл!"); return(INIT_FAILED); } else { FileWrite(f_handle, "Symbol: ", Symbol()); FileWrite(f_handle, "Tick num", "Date", "Tick time", "ASK", "BID", "LAST", "VOLUME", "Tick Flags"); } start_time = StringToTime(StTime); end_time = StringToTime(EndTime); return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { if(f_handle != INVALID_HANDLE) { ulong a_time = m_data.time_msc[0]; for(int i = 0; i<m_data.cnt; i++) { if(a_time != m_data.time_msc[i]) { FileWrite(f_handle, ""); a_time = m_data.time_msc[i]; } FileWrite(f_handle, IntegerToString(i + 1), TimeToString(m_data.time[i], TIME_DATE), TimeToString(m_data.time[i], TIME_SECONDS) + "." + StringFormat("%03i", m_data.time_msc[i]%1000), DoubleToString(m_data.ask[i], Digits()), DoubleToString(m_data.bid[i], Digits()), DoubleToString(m_data.last[i], Digits()), string(m_data.volume[i]), m_data.flags[i]); } } ArrayResize(m_data.time, 0, -1); ArrayResize(m_data.time_msc, 0, -1); ArrayResize(m_data.ask, 0, -1); ArrayResize(m_data.bid, 0, -1); ArrayResize(m_data.last, 0, -1); ArrayResize(m_data.volume, 0, -1); ArrayResize(m_data.flags, 0, -1); } //+------------------------------------------------------------------+ //| Expert fill data function | //+------------------------------------------------------------------+ void FillData(const int skip, const int t_cnt) { string c_flags; m_data.skip_cnt = 0; //Обнуляем счетчик тиков с одним временем (последним) for(int i = skip; i < t_cnt; i++) { c_flags = ""; m_data.ask[m_data.cnt] = m_data.ticks[i].ask; //Сохраняем значения m_data.bid[m_data.cnt] = m_data.ticks[i].bid; m_data.last[m_data.cnt] = m_data.ticks[i].last; m_data.volume[m_data.cnt] = long(m_data.ticks[i].volume_real); m_data.time[m_data.cnt] = m_data.ticks[i].time; m_data.time_msc[m_data.cnt] = m_data.ticks[i].time_msc; //Собираем все флаги if((m_data.ticks[i].flags&TICK_FLAG_ASK) == TICK_FLAG_ASK) c_flags += " TICK_FLAG_ASK,"; if((m_data.ticks[i].flags&TICK_FLAG_BID) == TICK_FLAG_BID) c_flags += " TICK_FLAG_BID,"; if((m_data.ticks[i].flags&TICK_FLAG_LAST) == TICK_FLAG_LAST) c_flags += " TICK_FLAG_LAST,"; if((m_data.ticks[i].flags&TICK_FLAG_BUY) == TICK_FLAG_BUY) c_flags += " TICK_FLAG_BUY,"; if((m_data.ticks[i].flags&TICK_FLAG_SELL) == TICK_FLAG_SELL) c_flags += " TICK_FLAG_SELL,"; if((m_data.ticks[i].flags&TICK_FLAG_VOLUME) == TICK_FLAG_VOLUME) c_flags += " TICK_FLAG_VOLUME,"; int f_len = StringLen(c_flags); //Кастрируем последнюю запятую в строке if(f_len > 1) { StringSetCharacter(c_flags, f_len - 1, ushort(" ")); StringTrimRight(c_flags); } m_data.flags[m_data.cnt] = c_flags + " (" + string(m_data.ticks[i].flags) + ")"; //Записываем флаги m_data.cnt++; //Увеличиваем счетчик всех записей if(m_data.mem_time == ulong(m_data.ticks[i].time_msc)) m_data.skip_cnt++; //Увеличиваем счетчик тиков с последним (одинаковым) временем } } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { datetime cur_time = TimeTradeServer(); //Берем текущее время сервера if((cur_time >=start_time)&&(cur_time<end_time)) //Проверка времени торговли { int result = 0; if(m_data.mem_time == 0) //Проверка на 1-ю запись { result = CopyTicks(Symbol(), m_data.ticks, COPY_TICKS_ALL, 0, 20); //Копируем последние 20 тиков if(result >= 1) { m_data.mem_time = ulong(m_data.ticks[result-1].time_msc); //Запоминаем время последнего тика FillData(0, result); //Сохраняем данные } } else //Последующие записи { result = CopyTicks(Symbol(), m_data.ticks, COPY_TICKS_ALL, m_data.mem_time, 1000); //Копируем тики с запомненного времени if(result >= 1) //плюс последующие тики (если есть) { if(m_data.mem_time < ulong(m_data.ticks[result-1].time_msc)) //Проверяем, изменилось ли время последнего тика { m_data.mem_time = ulong(m_data.ticks[result-1].time_msc); //Запоминаем время последнего тика FillData(m_data.skip_cnt, result); //Сохраняем данные, минус уже сохраненные с } //предыдущим последнем временем (m_data.skip_cnt) } } } } //+------------------------------------------------------------------+
在专家顾问工作后,我们得到文件ABL_Colletor.csv(在zip文件中)。
我从终端保存蜱虫
我得到了GAZR-12.21.csv(在 压缩包中)。
比较GAZR-12.21_mix.csv.xlsx中的文件(在 压缩文件中)。
是的,有了COPY_TICKS_TRADE标志,就不会跳过,但我需要交易和出价的询问。
又是一个拐杖?
result = CopyTicks(Symbol(), m_data.ticks, COPY_TICKS_TRADE, m_data.mem_time, 1000); //плюс result = CopyTicks(Symbol(), m_data.ticks, COPY_TICKS_INFO, m_data.mem_time, 1000);
但马上就出现了2个大问题。
1.如何 "同步 "时间
2.如何检查ASK和BID的正确性
新MetaTrader 5 build 3081:MQL5服务的改进和更新设计
fxsaber, 2021.10.19 16:30
两个不同的线程(INFO和LAST)被人为地合并为一个ALL-flow。
在同一毫秒内,交易所在信息流中给出了36800的买入价格。而在最后的流动中,有很多交易。很明显,如果时间是以纳秒为单位的,那么INFO-价格将比交易晚。
MT5花费时间来合并和同步数据流。正因为如此,在建立当前tick历史时,可能会有非常体面的实时滞后。可能会损失几毫秒的时间。
新MetaTrader 5 build 3081:MQL5服务的改进和更新设计
fxsaber, 2021.10.19 16:38
我认为,如果你在终端监测图书流,并与新的CopyTicks进行比较,会有很多不一致的地方。也就是说,事后看来,这将是相当体面的。
交易所的交易是单独进行的,这就对了。
如同询问和投标。
COPY_TICKS_ALL是从这两个数据流中取样的,只是做得不正确。
而这个样本不可能是毫秒级的,因为两个线程都有 "直通 "的时间,而不是在不同的线程中!
做了一个拐杖,似乎很有效。
//+------------------------------------------------------------------+ //| ABL_Collector.mq5 | //| Copyright 2021, prostotrader | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2021, prostotrader" #property link "https://www.mql5.com" #property version "1.00" //--- input string StTime = "07:00:00"; //Начало сбора тиков input string EndTime = "23:50:00"; //Конец сбора тиков //--- struct MARKET_DATA { datetime time; ulong time_msc; double ask; double bid; double last; long volume; string flags; }; struct A_DATA { int cnt; ulong m_time_info; ulong m_time_trade; int s_cnt_info; int s_cnt_trade; MqlTick ticks_trade[]; MqlTick ticks_info[]; MARKET_DATA m_data[]; }a_data; int f_handle; datetime start_time, end_time; //+------------------------------------------------------------------+ //| Expert Array fast sort function | //+------------------------------------------------------------------+ void ArrayFastSort(MARKET_DATA &array[], const int size) { ulong msc_val; MARKET_DATA temp; MARKET_DATA tmp_arr[]; ArrayResize(tmp_arr, size, size); for(int i = 0; i < size; i++) { tmp_arr[i] = array[i]; } int n[] = {9,5,3,2,1}; int i, j, z, y; for(z = 0;z < 5;z++) { y = n[z]; for(i = y;i < size;i++) { msc_val = tmp_arr[i].time_msc; temp = tmp_arr[i]; for(j = i - y; j >= 0 && msc_val < tmp_arr[j].time_msc; j -= y) { tmp_arr[j + y] = tmp_arr[j]; } tmp_arr[j + y] = temp; } } for(i = 0; i < size; i++) { msc_val = tmp_arr[i].time_msc; for(j = 0; j < size; j++) { if(array[j].time_msc == 0) continue; if(msc_val == array[j].time_msc) { tmp_arr[i] = array[j]; array[j].time_msc = 0; break; } } } for(i = 0; i < size; i++) { array[i] = tmp_arr[i]; } } //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { a_data.cnt = 0; a_data.m_time_info = 0; a_data.m_time_trade = 0; ArrayResize(a_data.m_data, 5000000, 5000000); f_handle = FileOpen("ABL_Colletor.csv", FILE_WRITE|FILE_CSV); if(f_handle == INVALID_HANDLE) { Alert("Не создан *.CSV файл!"); return(INIT_FAILED); } else { FileWrite(f_handle, "Symbol: ", Symbol()); FileWrite(f_handle, "Tick num", "Date", "Tick time", "ASK", "BID", "LAST", "VOLUME", "Tick Flags"); } start_time = StringToTime(StTime); end_time = StringToTime(EndTime); int result = CopyTicks(Symbol(), a_data.ticks_info, COPY_TICKS_ALL, 0, 1); if(result >= 1) { a_data.m_time_info = a_data.ticks_info[0].time_msc; a_data.m_time_trade = a_data.ticks_info[0].time_msc; } return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { if(f_handle != INVALID_HANDLE) { ArrayFastSort(a_data.m_data, a_data.cnt); ulong a_time = a_data.m_data[0].time_msc; for(int i = 0; i < a_data.cnt; i++) { if(a_time != a_data.m_data[i].time_msc) { FileWrite(f_handle, ""); a_time = a_data.m_data[i].time_msc; } FileWrite(f_handle, IntegerToString(i + 1), TimeToString(a_data.m_data[i].time, TIME_DATE), TimeToString(a_data.m_data[i].time, TIME_SECONDS) + "." + StringFormat("%03i", a_data.m_data[i].time_msc%1000), DoubleToString(a_data.m_data[i].ask, Digits()), DoubleToString(a_data.m_data[i].bid, Digits()), DoubleToString(a_data.m_data[i].last, Digits()), string(a_data.m_data[i].volume), a_data.m_data[i].flags); } } ArrayResize(a_data.m_data, 0, -1); Print("Collect ticks DONE!"); } //+------------------------------------------------------------------+ //| Expert fill data function | //+------------------------------------------------------------------+ void FillData(const int skip, const int t_cnt, ulong &mem_time, int &skip_cnt, MqlTick &ticks[]) { string c_flags; skip_cnt = 0; for(int i = skip; i < t_cnt; i++) { c_flags = ""; a_data.m_data[a_data.cnt].ask = ticks[i].ask; a_data.m_data[a_data.cnt].bid = ticks[i].bid; a_data.m_data[a_data.cnt].last = ticks[i].last; a_data.m_data[a_data.cnt].volume = long(ticks[i].volume_real); a_data.m_data[a_data.cnt].time = ticks[i].time; a_data.m_data[a_data.cnt].time_msc = ticks[i].time_msc; //--- if((ticks[i].flags&TICK_FLAG_ASK) == TICK_FLAG_ASK) c_flags += " TICK_FLAG_ASK,"; if((ticks[i].flags&TICK_FLAG_BID) == TICK_FLAG_BID) c_flags += " TICK_FLAG_BID,"; if((ticks[i].flags&TICK_FLAG_LAST) == TICK_FLAG_LAST) c_flags += " TICK_FLAG_LAST,"; if((ticks[i].flags&TICK_FLAG_BUY) == TICK_FLAG_BUY) c_flags += " TICK_FLAG_BUY,"; if((ticks[i].flags&TICK_FLAG_SELL) == TICK_FLAG_SELL) c_flags += " TICK_FLAG_SELL,"; if((ticks[i].flags&TICK_FLAG_VOLUME) == TICK_FLAG_VOLUME) c_flags += " TICK_FLAG_VOLUME,"; int f_len = StringLen(c_flags); if(f_len > 1) { StringSetCharacter(c_flags, f_len - 1, ushort(" ")); StringTrimRight(c_flags); } a_data.m_data[a_data.cnt].flags = c_flags + " (" + string(ticks[i].flags) + ")"; a_data.cnt++; if(mem_time == ulong(ticks[i].time_msc)) skip_cnt++; } } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { datetime cur_time = TimeTradeServer(); if((cur_time >=start_time)&&(cur_time<end_time)) { int result = 0; result = CopyTicks(Symbol(), a_data.ticks_info, COPY_TICKS_INFO, a_data.m_time_info, 1000); if(result >= 1) { if(a_data.m_time_info < ulong(a_data.ticks_info[result -1].time_msc)) { a_data.m_time_info = a_data.ticks_info[result -1].time_msc; FillData(a_data.s_cnt_info, result, a_data.m_time_info, a_data.s_cnt_info, a_data.ticks_info); } } result = CopyTicks(Symbol(), a_data.ticks_trade, COPY_TICKS_TRADE, a_data.m_time_trade, 1000); if(result >= 1) { if(a_data.m_time_trade < ulong(a_data.ticks_trade[result -1].time_msc)) { a_data.m_time_trade = a_data.ticks_trade[result -1].time_msc; FillData(a_data.s_cnt_trade, result, a_data.m_time_trade, a_data.s_cnt_trade, a_data.ticks_trade); } } } } //+------------------------------------------------------------------+
如果有人需要收集报价,你可以使用它。
太糟糕了,"请求 "和 "出价 "不能对照交易所进行检查。
总结一下对CopyTicks()的调查CopyTicksRange()。
图片3101,真实,开放
1.如果使用了COPY_TICKS_ALL标志--有跳过的交易。
当使用COPY_TICKS_TRADE 标志时 - 正确显示。
2.难以想象的事情发生在报价上 (ASK/BID)
交易是以不存在的价格执行的
交易以相反的价格执行(而不是ASK,他们使用BID)。
跳过引言
有一个大问题。
如果有巨大的价格差异,那么在终端的是什么?
我们的交易价格是多少?
ZIP档案中的源文件
在一个MQ们不会忽视的主题里,以该主题的第一个帖子的方式来做。
亲爱的版主!
请将 "在merits????*上清算 "这一主题中的帖子移出。
与清算无关,在此。