'MQL4 Time function miscalculation
I have the following function where tries to calculate the time between the last closed trade and the current time. if the time difference is <> that a certain number of bars it gives a different result.
that being said, during the backtest at some point the bar calculation begins to fail and i do not find the error. Any thoughts?
int LastOrderTicket()
{
datetime TicketTime = 0;
int LastOT;
datetime
Timeorder = LastOrderTime(),
Timeahora = TimeCurrent(),
Times = Timeahora - Timeorder;
int
m = TimeMinute(Times),
h = TimeHour(Times),
d = TimeDay(Times),
bars = (((d - 1) * 24 * 60) + (h * 60) + m) / 240;
for(int ix = 0; ix < OrdersHistoryTotal(); ix++)
{
if(OrderSelect(ix, SELECT_BY_POS, MODE_HISTORY)
&& OrderMagicNumber() == MagicNumber
&& OrderSymbol() == Symbol())
{
if(bars > barsback)
{
LastOT = -1;
}
else
{
if(bars < barsback)
{
LastOT = OrderType(); //For testing
}
}
}
else
Print("OrderSelect returned the error of ",GetLastError());
}
Print("TicketN ",LastOT);
Print("bars ",bars);
Print("Timeorder=",TimeToStr(Timeorder, TIME_MINUTES|TIME_SECONDS|TIME_DATE));
Print("Timeahora=",TimeToStr(Timeahora, TIME_MINUTES|TIME_SECONDS|TIME_DATE));
Print("Times=",TimeToStr(Times, TIME_MINUTES|TIME_SECONDS|TIME_DATE));
Print("m ",m);
Print("h ",h);
Print("d ",d);
return(LastOT); // Buy==0, Sell==1, Others==2 through 5
}
Here where i find the error:
0 18:02:11.243 2021.08.06 23:59:59 BBReverse V1 EURUSD,H4: bars 182
0 18:02:11.243 2021.08.06 23:59:59 BBReverse V1 EURUSD,H4: Timeorder=2021.03.09 12:00:00
0 18:02:11.243 2021.08.06 23:59:59 BBReverse V1 EURUSD,H4: Timeahora=2021.08.06 23:59:59
0 18:02:11.243 2021.08.06 23:59:59 BBReverse V1 EURUSD,H4: Times=1970.05.31 11:59:59
0 18:02:11.243 2021.08.06 23:59:59 BBReverse V1 EURUSD,H4: m 59
0 18:02:11.243 2021.08.06 23:59:59 BBReverse V1 EURUSD,H4: h 11
**0 18:02:11.243 2021.08.06 23:59:59 BBReverse V1 EURUSD,H4: d 31**
0 18:02:11.243 2021.08.08 23:05:00 BBReverse V1 EURUSD,H4: bars 8
0 18:02:11.243 2021.08.08 23:05:00 BBReverse V1 EURUSD,H4: Timeorder=2021.03.09 12:00:00
0 18:02:11.243 2021.08.08 23:05:00 BBReverse V1 EURUSD,H4: Timeahora=2021.08.08 23:05:00
0 18:02:11.243 2021.08.08 23:05:00 BBReverse V1 EURUSD,H4: Times=1970.06.02 11:05:00
0 18:02:11.243 2021.08.08 23:05:00 BBReverse V1 EURUSD,H4: m 5
0 18:02:11.243 2021.08.08 23:05:00 BBReverse V1 EURUSD,H4: h 11
**0 18:02:11.243 2021.08.08 23:05:00 BBReverse V1 EURUSD,H4: d 2**
the function time keep summing up but the day value not...
thank you.
Solution 1:[1]
i've used ibarshif and it worked.
thank you anyway.
Solution 2:[2]
datetime lastTime = TimeCurrent()-OrderOpenTime();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pinto André |
| Solution 2 | Jeremy Caney |
