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/

    Adding indicator causes order to be rejected when using fractional size

    General Code/Help
    2
    4
    33
    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.
    • M
      MohMah last edited by

      I have this simple buy and hold strategy:

      from imports import *
      from indicators import BBW
      
      
      # noinspection PyArgumentList
      from utils.fractional_commision_info import CommInfoFractional
      
      
      class BuyAndHoldTarget(bt.Strategy):
          def __init__(self):
              # bt.indicators.BollingerBands(self.datas[0])
              # bbw = BBW(self.datas[0])
              # VLI_fast = btind.SmoothedMovingAverage(bbw, period=200)
              # btind.ExponentialMovingAverage(self.datas[0])
      
              # btind.Highest(period=20).plotinfo.subplot = False
              # btind.Lowest(period=20).plotinfo.subplot = False
      
              # self.val_start = self.broker.get_cash()  # keep the starting cash
              self.BTC = self.datas[0]
      
          def nextstart(self):
              # Buy all the available cash
              self.broker.addcommissioninfo(CommInfoFractional())
              size = self.broker.get_cash() / self.data
              self.buy(size=size)
              # self.order_target_value(target=self.broker.get_cash())
      
          def notify_order(self, order):
              if order.status in [bt.Order.Submitted, bt.Order.Accepted]:
                  return  # Await further notifications
      
              if order.status == order.Completed:
                  otypetxt = 'Buy ' if order.isbuy() else 'Sell'
                  self.log(
                      ('%s Order Completed - '
                       'Size: %.2f @Price: %.2f '
                       'Value: %.2f Comm: %.2f') %
                      (otypetxt, order.executed.size, order.executed.price,
                       order.executed.value, order.executed.comm)
                  )
      
          def log(self, txt, dt=None):
              dt = dt or self.BTC.datetime.datetime()
              print('%s %s' % (dt.isoformat(), txt))
      
      

      As you can see it uses fractional sizing to buy. When all the indicators are commented out, it successfully buys:

      2015-04-01T02:00:00 Buy  Order Completed - Size: 0.41 @Price: 244.20 Value: 99.97 Comm: 0.00
      

      but as soon as I uncomment the indicators, the order gets margin status and does not get completed!

      Here's the order details with indicators commented:

      Ref: 1
      OrdType: 0
      OrdType: Buy
      Status: 4
      Status: Completed
      Size: 0.4093830597289884
      Price: None
      Price Limit: None
      TrailAmount: None
      TrailPercent: None
      ExecType: 0
      ExecType: Market
      CommInfo: <utils.fractional_commision_info.CommInfoFractional object at 0x7fab218ecc10>
      End of Session: 735689.9999999999
      Info: AutoOrderedDict()
      Broker: None
      Alive: False
      

      and here's the order details when indicators are uncommented:

      Ref: 1
      OrdType: 0
      OrdType: Buy
      Status: 7
      Status: Margin
      Size: 0.4081799257112535
      Price: None
      Price Limit: None
      TrailAmount: None
      TrailPercent: None
      ExecType: 0
      ExecType: Market
      CommInfo: None
      End of Session: 735689.9999999999
      Info: AutoOrderedDict()
      Broker: None
      Alive: False
      

      uncommenting only the first indicator will do the job

      1 Reply Last reply Reply Quote 0
      • hghhgghdf dfdf
        hghhgghdf dfdf last edited by

        Add commission info before running cerebro. Not inside of the strategy.

        M 1 Reply Last reply Reply Quote 0
        • M
          MohMah @hghhgghdf dfdf last edited by

          @hghhgghdf-dfdf That is irrelevant and doesn't change the behaviour

          1 Reply Last reply Reply Quote 0
          • M
            MohMah last edited by

            Just found out this fixes it:
            https://community.backtrader.com/topic/3234/issue-with-percentsizer-2020-11-11-order-canceled-margin-rejected

            Still don't know why adding an indicator causes this in the first place

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
            $(document).ready(function () { app.coldLoad(); }); }