How to get data from new bar function

 
Hi. I'm new here and i have a problem with taking data from new bar function. 
   if (newBar==true){  
    datetime startTime = CreateDateTime(14,0);
    datetime 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);


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?

 
Mariusz Kuduk:
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);
 
Yashar Seyyedin #:

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 ) { ...
Dealing with Time (Part 2): The Functions
Dealing with Time (Part 2): The Functions
  • www.mql5.com
Determing the broker offset and GMT automatically. Instead of asking the support of your broker, from whom you will probably receive an insufficient answer (who would be willing to explain a missing hour), we simply look ourselves how they time their prices in the weeks of the time changes — but not cumbersome by hand, we let a program do it — why do we have a PC after all.
 
Mariusz Kuduk: i have a problem with taking data from new bar function. 
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)

 
William Roeder #:

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)

 Thank you for idea. Now I create datetime with "00" sec value on every tick. Works fine. :)
 
We didn't see your newbar function. It looks as if you are just writing the words newbar there but it doesn't call any function. Have you looked at the function chapter of mql5 documentation?
Reason: