@rajanprabu said in logging timestamp produces an entirely different number:
dt = dt or self.datas[0].datetime.date(0)
thank you so much for the help, it does work now!
@rajanprabu said in logging timestamp produces an entirely different number:
dt = dt or self.datas[0].datetime.date(0)
thank you so much for the help, it does work now!
hi, i'm still pretty new to using backtrader and haven't really gotten the grasp of it. I've been trying to log my order executions but the log time is always completely off. My data was fed in using this code
fromdate = datetime.datetime.strptime('2020-1-01', '%Y-%m-%d')
todate = datetime.datetime.strptime('2021-01-08', '%Y-%m-%d')
data = bt.feeds.GenericCSVData(
dataname='2021_hourly.csv',
dtformat=2,
compression=30,
timeframe=bt.TimeFrame.Minutes,
fromdate=fromdate,
todate=todate)
cerebro.adddata(data)
and my data looks something like this. The first column corresponds to the datetime for each row
but somehow when i try to log with the following code
def log(self, txt, dt=None):
''' logging function for this strat ''
dt = datetime.datetime.fromtimestamp(self.data.datetime[0])
print('%s, %s' % (dt, txt))
print('')
def notify_order(self, order):
if order.status in [order.Submitted, order.Accepted]:
return
if order.status in [order.Completed, order.Canceled, order.Margin]:
if order.isbuy():
self.log('Buy Executed, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm))
self.buyprice = order.executed.price
self.buycomm = order.executed.comm
else:
self.log('Sell Executed, Price: %.2f, Cost: %.2f, Comm %.2f' % (order.executed.price, order.executed.value, order.executed.comm))
self.bar_executed = len(self)
self.order = None
it produces an output like this in which the dates and time are completely off
if i change dt to the following instead
dt = self.data.datetime[0]
the output will produce the date as this number which is completely different from the unix datetime fed into cerebro
could anyone help identify what the problem is? would really appreciate it, thanks!
@vladisld said in signals stop showing after a certain date:
cerebro.broker.setcash(1000000)
thanks for the help, it does work now!
pretty new to this. followed some guide online and i managed to write out this code which seems to work fine until after july. I'm just trying to create a chart with the 14 day RSI as an indicator to signal buy (rsi<30) and sell (rsi>70) points.
here is my code
import backtrader as bt
import datetime
class RSIStrategy(bt.Strategy):
def __init__(self):
self.rsi = bt.talib.RSI(self.data, period=14)
def next(self):
if self.rsi < 30 and not self.position:
self.buy(size=1)
if self.rsi > 70 and self.position:
self.close()
cerebro = bt.Cerebro()
fromdate = datetime.datetime.strptime('2020-01-01', '%Y-%m-%d')
todate = datetime.datetime.strptime('2020-12-25', '%Y-%m-%d')
data = bt.feeds.GenericCSVData(dataname='2020_hourly.csv', dtformat=2, compression=60, timeframe=bt.TimeFrame.Minutes, fromdate=fromdate, todate=todate)
cerebro.adddata(data)
cerebro.addstrategy(RSIStrategy)
cerebro.run()
cerebro.plot()
the data import is fine, double checked that so it shouldn't be the problem. but the chart it produces looks like this
as you can see, after around end july, the signals stop appearing even though there are certain points where the rsi is clearly below 30 (some even below 20) or over 70.
could anyone help identify what the problem is? would really appreciate it, thanks!