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/

    Orders not getting filled on data1

    General Code/Help
    1
    2
    67
    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.
    • A
      adamb032 last edited by

      i am having an issue getting orders based on data1 to get filled. I init cerebro with

      cerebro.adddata(bt.feeds.PandasData(dataname=get_eod_data(instrument, 
                                                                strat_start_date, 
                                                                end_date),                                                  
                                          openinterest=None),
                                          name=instrument)
      
      cerebro.adddata(bt.feeds.PandasData(dataname=df_call_data,
                                          open=None,
                                          high=None,
                                          low=None,
                                          volume=None,
                                          openinterest=None),
                                          name='atm_call_price')
      
      cerebro.broker.setcash(100000)
      cerebro.addsizer(bt.sizers.PercentSizer, percents=2)
      
      

      essentially data0 is the o/h/l/c of the underlying index and data1 is just the close for a synthetic atm call price with a given rfRate and impliedVol.

      in my strategy i have the following basic code:

          def next(self):
              # General Logic
              # sell a delta=50 (atm) 30day call
              # re-balance the delta daily 
              
              if self.orders['option'] and self.orders['underlying']:
                  return
      
              if self.day_counter % self.p.num_hold_days == 0:
                  if self.position:
                      self.logger.info(f'Need to close the option position')
                  else:
                      self.logger.info(f'Need to establish the initial option position')
                      self.logger.info(f'{self.data0.datetime.datetime()} ::  \
                                                                  {self.data1.datetime.datetime()}')
                      self.logger.info(f'{self.data0.close[0]} :: {self.data1.close[0]}')
                      
                      option_order = self.sell(data=self.data1, 
                                               exectype=Order.Market)
      
      

      my notify_order looks generic, I am just outputting stuff at this point:

          def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
                  # Buy/Sell order submitted/accepted to/by broker - Nothing to do
                  self.logger.info(f'Order {order.Status[order.status]} :: {order.ordtypename()} {order.size} {order.price}')
                  return
      
              if order.status == Order.Completed:
                  self.logger.info("[{}] FILLED {} {} @ {}".format(bt.num2date(order.executed.dt).date(),
                                                                   'Buy' * order.isbuy() or 'Sell',
                                                                   order.executed.size,
                                                                   order.executed.price))
      
                  if order.is_option:
                      self.orders['option'] = None
                  
              elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                  self.logger.info(f'Order {order.Status[order.status]} :: {order.ordtypename()} 
                                  {order.size} {order.price}')
                  self.orders['option'] = None
                  return
      

      and i can see in my strategy outout:

      14:25:15 [  strat]:INFO: Need to establish the initial option position
      14:25:15 [  strat]:INFO: 2015-06-21 00:00:00 :: 2015-06-21 00:00:00
      14:25:15 [  strat]:INFO: 243.944000244141 :: 21.058958519199706
      14:25:15 [  strat]:INFO: Order Submitted :: Sell -94.97145825975088 None
      14:25:15 [  strat]:INFO: Order Margin :: Sell -94.97145825975088 None
      

      i can see that

      • the dates of self.data0, self.data1 are inline
      • the prices of data0.close[0] and data1.close[0] are what i would expect
      • the position size is correct, im using a 2% Sizer, so if the initial broker cash is 100k, thats 2000/data1.close[0] = 94. and that is my size.

      whats weird is that its automatically saying I need to get margin, i have no idea why, nor does the order.status ever change to completed. does anyone have any idea what is happening here ? Thanks-

      A 1 Reply Last reply Reply Quote 0
      • A
        adamb032 @adamb032 last edited by

        @adamb032 I solved the issue, the data needs to have at least an open and close value in it for the simulated broker to work, once i made an open/high/low/close dataframe of all close values it works as expected.

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