How to extract date stamp from YfinanceData
-
Hi..
I am learning the Backtrader codes.. I used below code to check the data cross over from YFinance... But the date returned are weird..
def next(self):
if self.data.close[0] > self.limit:
crossed_time = self.data.datetime[0]dt = datetime.fromtimestamp(crossed_time) # Crossing the limit, perform your action here print("Limit crossed!",dt,"Take action.")
What are the attrributes returned by YFinance and which is the right one to use..?
-
@Chandrahas-Patel ignoring the rest of the code for a moment (so as to do things step-by-step), you can access the date of your datafeed like this:
self.data.datetime.date()
To ensure that this is working correct we can print the date everytime 'next' is called:
def next(self): print(self.data.datetime.date())
I believe this solves your issue if I have understood it correctly. Let me know if this helps!
-
@Z Yes.. Wow.. It does print as below for a given range..
Will try full code and check.. Thank you so much
2023-06-30
2023-07-03
2023-07-05
2023-07-06 -
@Z Yes it worked... I was using
self.data.datetime[0] instead of self.data.datetime.time()
Quite resourcefull thinking by you... Thanks again.. -
@Chandrahas-Patel no problem! I found the information in the documentation. It might also be useful for you to know that you can access the actual time of the date using self.data.datetime.time() (Formatted as such: 23:59:59.999989). Similarly, you can access both at the same time using self.data.datetime.datetime() (Formatted as such: 2022-12-30 23:59:59.999989).
Glad to be able to help!