Date and time of the bar

Hi,



I want to reset the barcount to 1 at the first bar of the day.  I want to write this logic inside handle_data.

Iam not able to access this information in  data.history,



logic: if date<>date of previous bar then barcount=1. how to go about writing this?



I also want to access the time of bar. if time = 1510, i would like to exit my open position. how to go about writing this?

Check out the API function get_datetime(). You need to import it first of course. It returns the current simulation date-time in a Pandas Timestamp format. You can get the only date part or the time part using the methods date() or time() respectively. These functions will return python built-in datetime formats. Example (inside handle_data or any other:



current_dt =  get_datetime()

date_part = current_dt.date()

time_part = current_dt.time()



Refer the links above for more on how to manipulate these values.

 

Thankyou Sir.

Also I suggest you use g.t. or l.t.  (or g.t.e/ l.t.e) for comparison, instead of eq. In a realistic scenario (live trading, for example), we do not know the exact time of call of the function where you have this comparison and an eq condition may not ever get fired.