Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    trades not registering, but strategy is plotting

    General Code/Help
    2
    2
    124
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      BMccoy last edited by

      For a buy only and exit strategy, when I run the following code I am able to see the chart and data and the indicator. However it doesnt appear to run any trades. Is this because I need trade analyzers. I did this with a strategy before and it worked as designed. But when I changed the data to intraday it gave me loads of problems.

      I end up with the following.

      SMA.JPG

      import backtrader as bt
      from datetime import datetime

      class SmaCross(bt.Strategy):

      params = dict(
          pfast=10,  
          pslow=30
      )
      
      def __init__(self):
          sma1 = bt.ind.SMA(period=self.p.pfast)
          sma2 = bt.ind.SMA(period=self.p.pslow)  
          self.crossover = bt.ind.CrossOver(sma1, sma2)  
      
      def next(self):
          if not self.position:  
              if self.crossover > 0:
                  self.buy() 
      
              elif self.crossover < 0:  
                  self.close() 
      

      In[8]:

      data = bt.feeds.GenericCSVData(
      dataname="test.csv",
      dtformat='%Y-%m-%d %H:%M:%S',
      open=1,
      high=2,
      low=3,
      close=4,
      volume=5,
      fromdate=datetime(2020, 1, 1),
      todate=datetime(2021, 11, 4)

      )
      

      In[9]:

      cerebro = bt.Cerebro()

      cerebro.adddata(data)

      cerebro.addstrategy(SmaCross)
      cerebro.run()
      cerebro.plot(style='Candlestick')

      1 Reply Last reply Reply Quote 0
      • ultra1971
        ultra1971 last edited by

        Pls include some data to check if you have enough cash to buy,

        Try changing initial cash, set up a size in the buy function and define a notify_trade method (lot of examples just google) .

        The crossover analyzer is working so if the buy fuction is executed then order is rejected for any reason.

        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors