Latest posts made by rajthakur
-
RE: Handle date format in GenericCSVData [Beginner]
@RandyT Can you help please ?
-
RE: Unable to print close price correctly. [Beginner]
@rajthakur How do i close this discussion ?
-
RE: Handle date format in GenericCSVData [Beginner]
@backtrader @backtraderadmin Please help me, Im stuck on this for a long time.
-
Handle date format in GenericCSVData [Beginner]
Hi Team,
I'm new to backtrader. I was trying a simple example that prints datetime and close price from my csv. But I'm unable to do so.
Its printing date correctly but for time its printing "23:59:59" only.
Can you please help me to check what am I doing wrong?Here is my code:
import datetime import backtrader as bt import backtrader.feeds as btfeeds class PrintClose(bt.Strategy): def __init__(self): self.dataclose = self.datas[0].close def log(self, txt, dt=None): dt = dt or self.datas[0].datetime.datetime(0).strftime('%Y-%m-%d %H:%M:%S%z') print(f'{dt} {txt}') # Print date and close def next(self): self.log(self.dataclose[0]) if __name__ == '__main__': cerebro = bt.Cerebro() cerebro.addstrategy(PrintClose) datapath = './RELIANCE_formatted.csv' data = btfeeds.GenericCSVData( dataname=datapath, dtformat=('%Y-%m-%d %H:%M:%S%z'), timestamp=0, open=1, high=2, low=3, close=4, volume=5, openinterest=-1 ) cerebro.adddata(data) cerebro.run()
Output :
2016-07-04 23:59:59 489.1 2016-07-04 23:59:59 491.45 2016-07-04 23:59:59 489.8 2016-07-04 23:59:59 488.75 2016-07-04 23:59:59 488.75 2016-07-04 23:59:59 489.15 2016-07-04 23:59:59 488.55 2016-07-05 23:59:59 486.95 2016-07-05 23:59:59 486.9
Expected Output:
2016-07-04 09:15:00+05:30 489.1 2016-07-04 10:15:00+05:30 491.45 2016-07-04 11:15:00+05:30 489.8 2016-07-04 12:15:00+05:30 488.75 2016-07-04 13:15:00+05:30 488.75 2016-07-04 14:15:00+05:30 489.15 2016-07-04 15:15:00+05:30 488.55 2016-07-05 09:15:00+05:30 486.95 2016-07-05 10:15:00+05:30 486.9
Here is my csv:
date,open,high,low,close,volume 2016-07-04 09:15:00+05:30,483.4,490.35,483.4,489.1,950630 2016-07-04 10:15:00+05:30,489.1,492.05,488.55,491.45,603618 2016-07-04 11:15:00+05:30,491.7,491.95,489.4,489.8,514331 2016-07-04 12:15:00+05:30,489.8,490.65,488.15,488.75,374728 2016-07-04 13:15:00+05:30,488.85,489.55,488.25,488.75,31432 2016-07-04 14:15:00+05:30,488.75,490.3,487.45,489.15,511010 2016-07-04 15:15:00+05:30,489.1,489.25,487.95,488.55,323005 2016-07-05 09:15:00+05:30,488.55,490.1,486.5,486.95,441230 2016-07-05 10:15:00+05:30,486.9,488.05,485.55,486.9,320247
Can you please guide me why is my date and time not getting printed correctly ? Any help is appreciated.
Thanks in advance
-
RE: Unable to print close price correctly. [Beginner]
@rajthakur I figured out the issue. Closing this thread.
-
RE: Unable to print close price correctly. [Beginner]
@backtrader @backtraderadmin Any help please sir ?
-
Unable to print close price correctly. [Beginner]
Hi Team,
I'm new to backtrader. I was trying a simple example that prints close price from my csv. But I'm unable to do so. Can you please help me to check what am I doing wrong?Here is my code:
import backtrader as bt import backtrader.feeds as btfeeds class PrintClose(bt.Strategy): def __init__(self): self.dataclose = self.datas[0].close def log(self, txt, dt=None): dt = dt or self.datas[0].datetime.date(0) print(f'{dt} {txt}') # Print date and close def next(self): self.log('Close: ', self.dataclose[0]) if __name__ == '__main__': cerebro = bt.Cerebro() cerebro.addstrategy(PrintClose) datapath = './RELIANCE_formatted.csv' data = btfeeds.GenericCSVData( dataname=datapath, dtformat=('%Y-%m-%d %H:%M:%S%z'), timestamp=0, high=2, low=3, open=1, close=4, volume=5, openinterest=-1 ) cerebro.adddata(data) cerebro.run()
Output:
1992.9 Close:
1989.1 Close:
1988.8 Close:
1992.75 Close:
1989.7 Close:
1991.55 Close:
1986.7 Close:
1979.15 Close:
... and so on .Here is a part of my csv :
date,open,high,low,close,volume 2016-07-04 09:15:00+05:30,483.4,490.35,483.4,489.1,950630 2016-07-04 10:15:00+05:30,489.1,492.05,488.55,491.45,603618 2016-07-04 11:15:00+05:30,491.7,491.95,489.4,489.8,514331 2016-07-04 12:15:00+05:30,489.8,490.65,488.15,488.75,374728 2016-07-04 13:15:00+05:30,488.85,489.55,488.25,488.75,314632 2016-07-04 14:15:00+05:30,488.75,490.3,487.45,489.15,511010 2016-07-04 15:15:00+05:30,489.1,489.25,487.95,488.55,323005
Please help me out. I have been banging my head for hours on this. Thanks in Advance