Hi. I'm new here and i have a problem with taking data from new bar function.
startTime and endTime is process once when new candle is created. But i dont want to trade function do the same. It is possible to get this data in OnTick function?
Hello. You should define startTime and endTime variables out of if block. Only then they are accessible out of it.
datetime startTime, endTime; if (newBar==true){ startTime = CreateDateTime(14,0); endTime = CreateDateTime(23,0); } (Comment("Current: ",TimeCurrent(),"\nStart: ",startTime,"\nEnd: ",endTime)); if(high1>Ask>low1 && TimeCurrent()==startTime && PositionsTotal()<2) Trade.BuyStop(Lot,high1,_Symbol,low1,high1+TakeProfit,ORDER_TIME_SPECIFIED,endTime,NULL); if(high1>Bid>low1 && TimeCurrent()==startTime && PositionsTotal()<2) Trade.SellStop(Lot,low1,_Symbol,high1,low1-TakeProfit,ORDER_TIME_SPECIFIED,endTime,NULL);
Hello. You should define startTime and endTime variables out of if block. Only then they are accessible out of it.
When i do this
datetime startTime, endTime;
when this
datetime startTime = NULL; datetime endTime = NULL;
I try this before and this isnt working with New bar. :( i must go with another solution of create datetime
You can use my articles about using time:
https://www.mql5.com/en/articles/9926
https://www.mql5.com/en/articles/9929
like from 14:00
if ( HoD(TimeCurent()) >= 14 ) { ...
- www.mql5.com
TimeCurrent()==startTime
Unless you get a tick that exact second, your code fails.
When dealing with time, a lot of people use strings; they can not be optimized. Using (int) seconds or (double) hours and fraction can be inputs.
See also Dealing with Time (Part 1): The Basics - MQL5 Articles (2021.10.01)
Dealing with Time (Part 2): The Functions - MQL5 Articles (2021.10.08)
MQL5 Programming Basics: Time - MQL5 Articles (2013.04.26)
Unless you get a tick that exact second, your code fails.
When dealing with time, a lot of people use strings; they can not be optimized. Using (int) seconds or (double) hours and fraction can be inputs.
See also Dealing with Time (Part 1): The Basics - MQL5 Articles (2021.10.01)
Dealing with Time (Part 2): The Functions - MQL5 Articles (2021.10.08)
MQL5 Programming Basics: Time - MQL5 Articles (2013.04.26)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
startTime and endTime is process once when new candle is created. But i dont want to trade function do the same. It is possible to get this data in OnTick function?