I am new to the field of programming, and recently I am trying to write a backtesting program by using backtrader. But it didn't enter into any position, can anyone tell me why?
CSV doc to run this program The csv doc is attached for your reference. For ['signal'] column, 1 = entry to long, 2 = entry to short.
I will be really appreciated it if anyone can give me a hand. QAQ
import backtrader as bt
import backtrader.analyzers as btay
import matplotlib
import backtrader.feeds as btf
class Supertrend(bt.Strategy):
def __init___(self):
signal = self.signal
tp = self.take_profit
sl = self.stop_loss
high = self.High
low = self.Low
if self.position:
temp = 0
def next(self):
if not self.position:
if signal == 1: #做多
temp = 1
self.order = self.buy()
elif signal == 2: #做空
temp = 2
self.order = self.sell()
else:
if temp == 1 and (high >= tp or low <=sl ):
self.order = self.close()
elif temp == 2 and (low <= tp or high >=sl ):
self.order = self.close()
cerebro = bt.Cerebro()
cerebro.addstrategy(Supertrend)
data = bt.feeds.PandasData(dataname=df)
cerebro.adddata(data)
cerebro.broker.set_cash(8700)
cerebro.addsizer(bt.sizers.PercentSizer, percents = 50)
cerebro.broker.setcommission(commission=0.002)
print('Starting Portfolio Value : %0.2f' % cerebro.broker.getvalue())
cerebro.run()
cerebro.plot()
print('Final Portfolio Value : %0.2f' % cerebro.broker.getvalue())