Navigation

    Backtrader Community

    • Register
    • 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/

    profit calculation seems to be wrong

    General Code/Help
    2
    3
    87
    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.
    • J
      jambhala last edited by

      I'am doing my first steps with backtrader and I tried from https://www.backtrader.com/home/helloalgotrading/:

      import backtrader as bt
      import yfinance as yf
      
      start_date = "2020-01-03"
      end_date = "2021-09-28"
      
      # Create a subclass of Strategy to define the indicators and logic
      
      class SmaCross(bt.Strategy):
          # list of parameters which are configurable for the strategy
          params = dict(
              pfast=10,  # period for the fast moving average
              pslow=30   # period for the slow moving average
          )
      
          def __init__(self):
              sma1 = bt.ind.SMA(period=self.p.pfast)  # fast moving average
              sma2 = bt.ind.SMA(period=self.p.pslow)  # slow moving average
              self.crossover = bt.ind.CrossOver(sma1, sma2)  # crossover signal
      
          def next(self):
              if not self.position:  # not in the market
                  if self.crossover > 0:  # if fast crosses slow to the upside
                      self.buy()  # enter long
      
              elif self.crossover < 0:  # in the market & cross to the downside
                  self.close()  # close long position
      
      
      cerebro = bt.Cerebro()  # create a "Cerebro" engine instance
      
      # Create a data feed
      data = bt.feeds.PandasData(dataname=yf.download('MSFT', start_date, end_date))
      
      
      cerebro.adddata(data)  # Add the data feed
      
      
      cerebro.addstrategy(SmaCross)  # Add the trading strategy
      for cash in [10000, 100000, 1000000]:
          cerebro.broker.setcash(cash)
          initial_cash = cerebro.broker.getvalue()
          cerebro.run()  # run it all
          print(f"initial cash = {initial_cash} profit = {cerebro.broker.getvalue() - initial_cash}")
      
      

      The simulation results in a profit of 58 no matter how large the initial cash is. What is wrong here?

      R 1 Reply Last reply Reply Quote 1
      • R
        rajanprabu @jambhala last edited by

        @jambhala

        change size of the order.. by default its ordering 1 qty..

        self.buy() 
        
        J 1 Reply Last reply Reply Quote 2
        • J
          jambhala @rajanprabu last edited by

          @rajanprabu Thank you for your prompt response

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