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/

    Ask on the logic of notify order and next to execute strategy

    Indicators/Strategies/Analyzers
    2
    2
    599
    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.
    • Nguyễn Sơn
      Nguyễn Sơn last edited by

      Hi all,
      I have the logic inside the function next() as below.
      if sme > sma then open to buy_bracket with 3 orders to open and close with limit and stop. As I understand, stopprice is stoploss and limitprice is the target profit.

      def next(self):
              if self.order:
                  return
              if not self.position:
                  if self.ema[0] > self.sma[0]:
                      self.log('BUY at {}'. format(self.dataopen[0]))
                      self.order = self.buy_bracket(stopprice = self.dataopen[0] * (1 - 0.02),
                                                    limitprice = self.dataopen[0] * (1 + 0.04))
                  elif self.ema[0] == self.sma[0]:
                      self.order = self.close()
                      self.log('CLOSED at {}'. format(self.dataclose[0]))
              else:
                  if self.ema[0] < self.sma[0]:
                      self.order = self.sell_bracket(stopprice = self.dataopen[0] * (1 + 0.02),
                                                     limitprice = self.dataopen[0] * (1 - 0.04))
                  elif self.ema[0] == self.sma[0]:
                      self.order = self.close()
                      self.log('CLOSED at {}'. format(self.dataclose[0]))
      

      and the notify order is the same as the quick guide

      def notify_order(self, order):
              if order.status in [order.Completed]:
                  if order.isbuy():
                      self.log(
                          'BUY EXECUTED at {}, cost {}, com {}'.format(order.executed.price,
                                                                      order.executed.value,
                                                                      order.executed.comm)
                      )
                      self.buyprice = order.executed.price
                      self.buycom = order.executed.comm
                  else:
                      self.log(
                          'SELL EXECUTED at price {}, cost {}, com {}'.format(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
              self.log('OPERATION PROFIT, GROSS {}, NET{}'.format(trade.pnl,
                                                                 trade.pnlcomm))
      

      However, the logger have a lot of trade without return the notify_trade() even I have detailed the condition to close the trade in next() function. How to solve this problem?

      2019-10-24 12:58:00 BUY EXECUTED at 108.59200000000001, cost 108.59200000000001, com 0.0010859200000000002
      2019-10-24 13:00:00 SELL EXECUTED at price 108.581, cost 108.59200000000001, com 0.0010858100000000002
      2019-10-24 13:00:00 OPERATION PROFIT, GROSS -0.01100000000000989, NET-0.013171730000009891
      ...
      2019-10-25 02:30:00 SELL EXECUTED at price 108.664, cost -108.664, com 0.00108664
      2019-10-25 02:31:00 SELL EXECUTED at price 108.662, cost -108.662, com 0.0010866200000000002
      2019-10-25 02:33:00 SELL EXECUTED at price 108.65100000000001, cost -108.65100000000001, com 0.0010865100000000002
      2019-10-25 02:34:00 SELL EXECUTED at price 108.656, cost -108.656, com 0.00108656
      2019-10-25 02:35:00 SELL EXECUTED at price 108.65299999999999, cost -108.65299999999999, com 0.00108653
      
      

      Where is the wrong here?

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        @Nguyễn-Sơn said in Ask on the logic of notify order and next to execute strategy:

        Where is the wrong here?

        I think it is obvious. Your logic sells several times in a row.

        @Nguyễn-Sơn said in Ask on the logic of notify order and next to execute strategy:

                else:
                    if self.ema[0] < self.sma[0]:
                        self.order = self.sell_bracket(stopprice = self.dataopen[0] * (1 + 0.02),
                                                       limitprice = self.dataopen[0] * (1 - 0.04))
        

        @Nguyễn-Sơn said in Ask on the logic of notify order and next to execute strategy:

        How to solve this problem?

        Correct your logic to something which wont sell several times in a row.

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