0

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())
  • Welcome to [Stack Overflow.](https://stackoverflow.com/ "Stack Overflow") This is not a code-writing or tutoring service. It is extremely difficult to answer your question without sample data and an idea of what you expect. That being said, you don't seem to be instantiating an instance of SuperTrend, so you aren't invoking your code. See the [How To Ask a Good Question](https://stackoverflow.com/help/how-to-ask "How To Ask a Good Question") page for details on how to best help us help you. – itprorh66 Apr 19 '22 at 14:07
  • Stack Overflow is not a 24/7 code service (I thought this too at first myself) but a giant q&a for programmers, which is community run. Each question and answer should contribute to the giant library. – Leo Apr 19 '22 at 14:11
  • @iprorh66 I did in my original program, the attached csv is the df mentioned in my code. I will be appreciated it if you can try the attached sample data to test the program. – IU iuuu Apr 19 '22 at 15:11
  • @LeostandswithRussia with Russia if my question is not a good question for this giant community, can u tell me where can I find the answer of this problem? Coz I have no fds can help me with this. – IU iuuu Apr 19 '22 at 15:21
  • If you want to make a good question read [this](https://stackoverflow.com/help/how-to-ask), like @itprorh66 said – Leo Apr 22 '22 at 13:28

0 Answers0