Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    1. Home
    2. developer
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
    D
    • Profile
    • Following 0
    • Followers 0
    • Topics 2
    • Posts 2
    • Best 0
    • Controversial 0
    • Groups 0

    developer

    @developer

    0
    Reputation
    10
    Profile views
    2
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    developer Unfollow Follow

    Latest posts made by developer

    • Run strategy only once indicators are warmed up

      I have written a strategy that relies on a 200-day SMA. Therefore, during the first 200 days in my strategy, this indicator simply is not available.
      How can I make it that backtrader only runs my strategy once it has gathered the data for 200 days so that my SMA 200 indicator is available?

      posted in General Code/Help backtraderv20 indicator sma warmup strategy
      D
      developer
    • Backtrader doesn`t execute trades / buy and sell don´t work

      I have written a simple Python script using backtrader that is supposed to sell an ETF (SPY in my case) when the price of the ETF is over its 200-day simple moving average (SMA200). The code is as follows:

      import backtrader as bt
      import yfinance
      
      stock = yfinance.Ticker("SPY")
      stock_data = stock.history(period="4y")
      
      # print(stock_data.head())
      
      
      class SMA200Strategy(bt.Strategy):
          def __init__(self):
              self.etf = self.datas[0]
              self.etf_sma = bt.ind.SMA(self.etf.lines.close, period=200)
              self.crossover = bt.ind.CrossOver(self.etf, self.etf_sma)
      
          def next(self):
              if self.crossover > 0:
                  self.order_target_percent(self.etf, target=1)
              elif self.crossover < 0:
                  self.order_target_percent(self.etf, target=0)
      
      
      cerebro = bt.Cerebro()
      
      cerebro.broker.setcash(100000)
      cerebro.adddata(
          bt.feeds.PandasData(dataname=stock_data))
      cerebro.addstrategy(SMA200Strategy)
      
      cerebro.run()
      cerebro.plot()
      

      The crossover occurs at the correct points, however, backtrader doesnt execute my sell/buy command (self.order_target_percent(self.etf, target=1)/self.order_target_percent(self.etf, target=0))

      You can see in this image outputted by backtrader that the crossover signals are generated, but then no buy/sell orders are executed in many cases: Imgur

      I have tried using other data but I checked and the data loaded by yfinance is correct and in the fitting format (a pandas dataframe).

      I have also tried self.buy and self.close, but even those commands don´t work reliably.

      How can I make backtrader actually execute my buy/sell commands in all cases?

      posted in General Code/Help
      D
      developer