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/

    Market Order Submitted, but not executed...

    General Code/Help
    2
    2
    114
    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.
    • mama1baba
      mama1baba last edited by

      As the title suggests, I placed my bracket order as Market Order, but it wasn't executed. Can anyone help?

      Strategy Snippet:

      class TurtleStrategy(bt.Strategy):
          
          # Set the parameters
          params = (
              ('fast_ema_period', 25),
              ('slow_ema_period', 350),
              ('donchian_period', 20),
              ('ATR_period', 20),
              ('ATR_dist', 2.0),
              ('percrisk', 0.01),
              ('multi', 1000.0)
              )
          
          def log(self, txt, dt = None):
              ''' Logging Function for this strategy'''
              dt = dt or self.datas[0].datetime.date(0)
              dt1 = self.datas[0].datetime.time(0)
              print('%s, %s, %s' % (dt.isoformat(), dt1.isoformat(), txt))
          
          def __init__(self):
              
              # Set up the indicators
              self.emafast = bt.indicators.ExponentialMovingAverage(self.data.close,
                  period = self.p.fast_ema_period)
              self.emaslow = bt.indicators.ExponentialMovingAverage(self.data.close,
                  period = self.p.slow_ema_period)
              self.donchian = DonchianChannels()
              self.atr = bt.indicators.ATR(self.data,
                                           period=self.p.ATR_period)
              
              # Fast-Slow EMA Crossover signal
              self.CrossOver = bt.indicators.CrossOver(self.emafast, self.emaslow)
              
              # Keep track of pending order + price + commission
              self.order = None 
              self.buyprice = None
              self.buycomm = None
              
          def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
                  # Buy/Sell order submitted/accepted by broker. Do nothing
                  self.log('ORDER ACCEPTED/SUBMITTED')
                  return 
              
              #Check if order has been completed
              if order.status in [order.Completed]:
                  if order.isbuy():
                      self.log(
                          'LONG EXECUTED, Price: %.2f, Cost: %.2f, Comm: %.2f' % 
                          (order.executed.price,
                           order.executed.value,
                           order.executed.comm))
                      
                      self.buyprice = order.executed.price
                      self.buycomm = order.executed.comm
                  
                  else: # Sell
                      self.log(
                          'SHORT EXECUTED, Price: %.2f, Cost: %.2f, Comm: %.2f' % 
                          (order.executed.price,
                           order.executed.value,
                           order.executed.comm))
                      
                  self.bar_executed = len(self)
                  
                  
                  
              elif order.status in [order.Canceled, order.Margin, order.Rejected]:
                  self.log('Order Canceled/Margin/Rejected')
                  
              
              self.order = None
              
          def notify_trade(self, trade):
              if not trade.isclosed:
                  return
              
              # open position PnL
              self.log('OPERATION PROFIT, Gross %.2f, Net %.2f' %
                       (trade.pnl, trade.pnlcomm))
              self.log('PORTFOLIO VALUE, %.2f' % self.broker.get_value())
                      
      
          def next(self):
              close = self.data.close[0]
              # Log closing price for reference
              self.log('Close, %.2f' % close)
              
              # Check if an order is pending, if yes then we do nothing
              if self.order:
                  return
              
              # set up parameters for SL and TP
              pdist = self.atr[0] * self.p.ATR_dist
              long_stop = close - pdist
              short_stop = close + pdist
              long_profit = close + pdist
              short_profit = close - pdist
              Size = (0.01 * self.broker.get_value()) / (self.atr[0] * 50000)
              
              # Check if we are in the market
              if not self.position: # not in the market
                  # Trade Logic
                  if close > self.donchian.l.dch[0]:
                      if self.emafast[0] > self.emaslow[0]:
                          # Place BUY trade : 1% of portfolio value
                          self.log('BUY CREATE, {}, DONCHIAN HIGH, {}, ATR, {}'.format(
                                  close, self.donchian.l.dch[0], self.atr[0]))
                          self.buy_bracket(
                              stopprice = long_stop,
                              limitprice = long_profit,
                              exectype = bt.Order.Market,
                              size = Size
                              )
                          
                  
                  elif close < self.donchian.l.dcl[0]:
                      if self.emafast[0] < self.emaslow[0]:
                          # Place SELL trade : 1% of portfolio value
                          self.log('SELL CREATE, {}, DONCHIAN LOW, {}, ATR, {}'.format(
                                  close, self.donchian.l.dcl[0], self.atr[0]))
                          self.sell_bracket(
                              stopprice = short_stop,
                              limitprice = short_profit,
                              exectype = bt.Order.Market,
                              size = Size
                              )
      

      My Output:

      2020-02-24, 23:59:59.999989, Close, 2.55
      2020-02-24, 23:59:59.999989, Close, 2.57
      2020-02-24, 23:59:59.999989, Close, 2.58
      2020-02-22, 23:59:59.999989, Close, 2.64
      2020-02-22, 23:59:59.999989, BUY CREATE, 2.642, DONCHIAN HIGH, 2.611, ATR, 0.053519956567191586
      2020-02-21, 23:59:59.999989, ORDER ACCEPTED/SUBMITTED
      2020-02-21, 23:59:59.999989, ORDER ACCEPTED/SUBMITTED
      2020-02-21, 23:59:59.999989, ORDER ACCEPTED/SUBMITTED
      2020-02-21, 23:59:59.999989, ORDER ACCEPTED/SUBMITTED
      2020-02-21, 23:59:59.999989, ORDER ACCEPTED/SUBMITTED
      2020-02-21, 23:59:59.999989, ORDER ACCEPTED/SUBMITTED
      2020-02-21, 23:59:59.999989, Close, 2.63
      2020-02-21, 23:59:59.999989, Close, 2.64
      2020-02-21, 23:59:59.999989, Close, 2.59
      2020-02-21, 23:59:59.999989, Close, 2.63
      2020-02-21, 23:59:59.999989, Close, 2.62
      2020-02-21, 23:59:59.999989, Close, 2.66
      2020-02-20, 23:59:59.999989, Close, 2.68
      2020-02-20, 23:59:59.999989, Close, 2.69
      
      1 Reply Last reply Reply Quote 0
      • C
        CyberCowboy last edited by

        Maybe there's no volume tradeable, you can check the origin data first

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