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/

    Not all available trades taken

    General Code/Help
    2
    3
    336
    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.
    • H
      howardhh last edited by

      Hi there,

      I'm a newbie to algo trading and am running into some problems. I've been trying to backtest a trend-following strategy where I enter if the EMA50 > EMA200 and when the CCI20 crosses the -100. Sell when EMA50 and EMA200 cross lower.

      The problem is that backtrader is only taking some trades and not others for some reason. Can someone please help explain why this is the case and how I can solve this?

      Thanks, Howard

      Here is my code for the strategy:

      class maCross(bt.Strategy):
      '''
      For an official backtrader blog on this topic please take a look at:

      https://www.backtrader.com/blog/posts/2017-04-09-multi-example/multi-example.html
      
      oneplot = Force all datas to plot on the same master.
      '''
      params = (
      ('ema50', 50),
      ('ema200', 200),
      ('cci20', 20),
      ('oneplot', True)
      )
      def __init__(self):
          '''
          Create an dictionary of indicators so that we can dynamically add the
          indicators to the strategy using a loop. This mean the strategy will
          work with any number of data feeds.
          '''
          self.inds = dict()
          for i, d in enumerate(self.datas):
              self.inds[d] = dict()
              self.inds[d]['ema50'] = bt.indicators.ExponentialMovingAverage(
                  d.close, period=self.params.ema50)
              self.inds[d]['ema200'] = bt.indicators.ExponentialMovingAverage(
                  d.close, period=self.params.ema200)
              self.inds[d]['cci20'] = bt.indicators.CommodityChannelIndex(
                  period=self.params.cci20)
              self.inds[d]['crossUp'] = bt.indicators.CrossOver(self.inds[d]['cci20'], -100)  # crossup = 1
              self.inds[d]['EMAcross'] = bt.indicators.CrossOver(self.inds[d]['ema50'], self.inds[d]['ema200']) #crossdown = -1
      
              if i > 0:  # Check we are not on the first loop of data feed:
                  if self.p.oneplot == True:
                      d.plotinfo.plotmaster = self.datas[0]
      
      def prenext(self):
          self.next()
      
      def next(self):
          for i, d in enumerate(self.datas):
              dt, dn = self.datetime.date(), d._name
              pos = self.getposition(d).size
              if not pos:  # no market / no orders
                  if self.inds[d]['ema50'] >= self.inds[d]['ema200']:
                      if self.inds[d]['crossUp'][0] == 1:
                          self.buy(data=d, size=1000)
                      else:
                          pass
                  else:
                      pass
      
              else:
                  if self.inds[d]['EMAcross'][0] == -1:
                      self.close(data=d)
      
      def notify_trade(self, trade):
          dt = self.data.datetime.date()
          if trade.isclosed:
              print('{} {} Closed: PnL Gross {}, Net {}'.format(
                  dt,
                  trade.data._name,
                  round(trade.pnl, 2),
                  round(trade.pnlcomm, 2)))
      
      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

        Maybe sometime you don't have enough money to buy 1000 shares? Just a first guess.

        • If my answer helped, hit reputation up arrow at lower right corner of the post.
        • Python Debugging With Pdb
        • New to python and bt - check this out
        H 1 Reply Last reply Reply Quote 2
        • H
          howardhh @ab_trader last edited by

          @ab_trader That was actually it haha. Thanks for that. I'm literally just starting out and it's really tough.

          But cheers for the assistance, very much appreciated.

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