Metadrader handling weirdly data read from different time frame
@Moderators @Community
Do you have any idea here? Should I send directly to MQL5 technical team via the service desk?
Regards
Gege
if tempZ is Long max it is > price and if it is Long min it is < price .
Yes I agree with you :( unfortunately, this is leaving me with no solution other than cutting out those backtests in order not to show any erroneous results , unless I found another workaround for this
Yes I agree with you :( unfortunately, this is leaving me with no solution other than cutting out those backtests in order not to show any erroneous results , unless I found another workaround for this
Organize the download and control of data using a timer, and then you will be able to control the entire process. In my trading systems, this is exactly what happens. Only I download only M1 and ticks, the rest is formed from this data. Therefore, my trading systems do not depend on the TimeFrames
Till today, I couldn't yet find a valid root cause, below the EA with default Trade.mqh used y mql5
#property copyright "Copyright 2024, Grap" #property link "https://www.google.com" #property version "1.00" //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ #include <Trade\Trade.mqh> input ENUM_TIMEFRAMES InpHighTimeFrameTP = 0; input int stress = 1; double ext = 0; int mHandle; double mBuffer[]; CTrade mTrade; int OnInit() { //--- mTrade.SetExpertMagicNumber(1); mHandle = 0; ArraySetAsSeries(mBuffer,true); mHandle = iCustom(Symbol(),InpHighTimeFrameTP,"Examples\\ZigZag.ex5",8,5,3); if(mHandle == INVALID_HANDLE) { return INIT_FAILED; } return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- IndicatorRelease(mHandle); } double GetValue(int index){ int result = CopyBuffer(mHandle, 0, index, 1, mBuffer); if(result < 1 ) { return(0);} return(mBuffer[0]); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if(!IsNewBar()) return; if(PositionsTotal() == 0) { double TPbuy = GetTP(SymbolInfoDouble(Symbol(),SYMBOL_BID), ORDER_TYPE_BUY); double TPSell = GetTP(SymbolInfoDouble(Symbol(),SYMBOL_BID), ORDER_TYPE_SELL); Print("TP Buy: ",TPbuy); Print("TP Sell: ",TPSell); ResetLastError(); if(!mTrade.Buy(0.01,Symbol(),SymbolInfoDouble(Symbol(),SYMBOL_ASK),SymbolInfoDouble(Symbol(),SYMBOL_ASK)-0.00100,TPbuy)) Print(GetLastError()); ResetLastError(); if(!mTrade.Sell(0.01,Symbol(),SymbolInfoDouble(Symbol(),SYMBOL_BID),SymbolInfoDouble(Symbol(),SYMBOL_ASK)-0.00100,TPSell)) Print(GetLastError()); } } //+------------------------------------------------------------------+ double GetTP(double price,ENUM_ORDER_TYPE type, int startAt=2) { double zigzag = -1; int i=startAt; if(type == ORDER_TYPE_BUY) { while(zigzag ==-1) { ResetLastError(); double tempZ = GetValue(i); if(GetLastError() !=0) { Print("ERROR: ",GetLastError()); } if( tempZ == EMPTY_VALUE || tempZ == 0) { i++; continue; } if(tempZ > price) { zigzag = tempZ; } i++; } } if(type == ORDER_TYPE_SELL) { while(zigzag ==-1) { ResetLastError(); double tempZ = GetValue(i); if(GetLastError() !=0) { Print("ERROR: ",GetLastError()); } if( tempZ == EMPTY_VALUE || tempZ == 0) { i++; continue; } if(tempZ < price) { zigzag = tempZ; } i++; } } return zigzag; } bool IsNewBar() { static bool result = false; static datetime previous_time = 0; datetime current_time =iTime(Symbol(), Period(), 0); result = false; if(previous_time != current_time) { previous_time = current_time; result = true; } return (result); }
Hi,
I was optimizing an EA and noticed that with the same parameters, I had different results. I wanted to explain my case and benefit from your experience to move on with this. I firmly believe a limitation in how metatrader handles threads and loads data.
For a week, I did my investigations. It is related to reading indicator data from a higher timeframe. The metatrader 5 is sending/reading the wrong data randomly!
So, below is a simplified version of my code. I went through the forums and applied different approaches, like capturing the last error to check if anything related to the data loaded; I also forced loading the data using CheckLoadHistory shared in this forum; the load is correct, but again, the data reading is false. I did this in the EA and the Indicator.
After multiple tests, I realized I always have this error randomly and with high probability when opening metatrader and starting a fresh new test or loading my CPU/RAM with other programs.
Take the code below, open metatrader, and run it for the first time. You will have the error, run it a second time, and there will be no more errors.
Any help is welcome.
My idea is simple: I get a swing high/low of a higher timeframe from zigzag to use as a TP.
When I have the error, the values are returned as follows: for example, it looks like LONG_MAX and LONG_MIN are involved.
I have done a stress test, just an optimization on an int that is not used in the logic, and when I load my PC I start having those errors, and it happens on a nonregular time. Example 3 occurrences over 500 tests.
My code is the following