Using ParabolicSAR inside strategy
-
Good morning,
I'm having difficulty implementing the ParabolicSAR in my strategy. I believe I've done the params and initialization correctly (not for sure, though), but I'm not sure how to add the conditions in. Am I supposed to build the calculations and use a variable for the condition? Or is that done inside backtrader? I want to include a psar condition inside the buy/sell conditions.Params:
('period', 2), # psar period ('start' in pine) ('af', 0.005), #psar af ('increment' in pine) ('afmax', 1), #psar afmax ('maximum' in pine)
init:
self.psar = bt.indicators.ParabolicSAR( period = self.p.period,# Period plot = False, af = self.p.af, afmax = self.p.afmax, )
I am new, thank you for your patience.
-
I have searched google and backtrader references with no luck. I may just need a point in the right direction.
-
@switchfire start in pine can be float but prriod in python can not be float.
-
@nima021 how can put float as integer?
-
@switchfire I had a similar script on file so I will simply share for your reference.
import backtrader as bt class Strategy(bt.Strategy): params = ( ("period", 2), # psar period ('start' in pine) ("af", 0.005), # psar af ('increment' in pine) ("afmax", 1), # psar ) def log(self, txt, dt=None): """ Logging function fot this strategy""" dt = dt or self.data.datetime[0] if isinstance(dt, float): dt = bt.num2date(dt) print("%s, %s" % (dt.date(), txt)) def print_signal(self): self.log( f"o {self.datas[0].open[0]:7.2f} " f"h {self.datas[0].high[0]:7.2f} " f"l {self.datas[0].low[0]:7.2f} " f"c {self.datas[0].close[0]:7.2f} " f"v {self.datas[0].volume[0]:7.0f} " f"psar {self.psar[0]:5.0f}" ) def __init__(self): self.psar = bt.ind.PSAR(period=self.p.period, af=self.p.af, afmax=self.p.afmax) def next(self): self.log(f"period: {self.p.period}, af: {self.p.af}, afmax: {self.p.afmax}") self.print_signal() if __name__ == "__main__": import time cerebro = bt.Cerebro() start = time.time() data = bt.feeds.GenericCSVData( dataname="data/2006-day-001.txt", dtformat=("%Y-%m-%d"), timeframe=bt.TimeFrame.Days, compression=1, ) print(f"time {(time.time() - start)}") cerebro.adddata(data) kwargs = dict( period=3, af=.004, afmax=.9 ) cerebro.addstrategy(Strategy, **kwargs) # Execute cerebro.run()
-
@run-out thanx, I had red this before but I want to put 0.11 for period but it errored period can not be float.
but in pine script it can be
start=0.11
please read my question completely
best regards -
@nima021 I can't comment on what pinescript is doing.
In backtrader period is basically the number of bars you wish to calculate over. There are no partial bars in this scenario, hence the requirement for an
int
-
@run-out said in Using ParabolicSAR inside strategy:
int
Yes I searched and found QuantConnect. this is exactly I said. for live trading you should have orginal PSAR that is float. in pineScript and QuantConnct this is float but in backtrader because of it's own definition. and when its float you can reach more different takeprofit.