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/

    How could I have both of the StopTrail and siginal Close() ?

    General Discussion
    2
    2
    72
    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.
    • J
      jeff lee last edited by jeff lee

      how can I have both of the two exist condition ?
      1 normal 'self.sell()' with sma condition meet.
      2 'self.sell(exectype=bt.Order.StopTrail, price=self.buyprice * (1 - 0.10))'
      in function notify_order(), i set the stop tail order.

      when 1) is meet and trigered, how to cancel the 2) automaticlly?

      thanks

      
      # Create a Stratey
      class TestDoubleSMAStrategy(bt.Strategy):
          params = (
              ('maperiod1', 5),
              ('maperiod2', 20),
          )
          def log(self, txt, dt=None):
              dt = dt or self.datas[0].datetime.date(0)
              print('%s, %s' % (dt.isoformat(), txt))
      
          def __init__(self):
              self.dataclose = self.datas[0].close
      
              self.sma1 = bt.indicators.SimpleMovingAverage(
                  self.datas[0], period=self.params.maperiod1)
      
              self.sma2 = bt.indicators.SimpleMovingAverage(
                  self.datas[0], period=self.params.maperiod2)
      
          def notify_order(self, order):
              if order.status in [order.Submitted, order.Accepted]:
                  # Buy/Sell order submitted/accepted to/by broker - Nothing to do
                  return
      
              # Check if an order has been completed
              # Attention: broker could reject order if not enougth cash
              if order.status in [order.Completed, order.Canceled, order.Margin]:
                  if order.isbuy():
                      self.log(
                          'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                          (order.executed.price,
                           order.executed.value,
                           order.executed.comm))
                      pass
                      buyprice = order.executed.price
                      self.sell(exectype=bt.Order.StopTrail, price=buyprice * (1 - 0.20))   #### sell  2)
                  elif order.issell():
                      self.log(
                          'SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %
                          (order.executed.price,
                           order.executed.value,
                           order.executed.comm))
                      pass
      
                  self.bar_executed = len(self)
      
              # Write down: no pending order
              self.order = None
      
          def next(self):
              # Simply log the closing price of the series from the reference
              # print(self.datas[0], self.sma0[0], self.sma1[0])
              # print(self.datas[0])
      
              # Check if we are in the market
              if not self.position:
      
                  # Not yet ... we MIGHT BUY if ...
                  if self.sma1[0] > self.sma2[0]:
                      # BUY, BUY, BUY!!! (with all possible default parameters)
                      self.log('BUY CREATE, %.2f' % self.dataclose[0])
      
                      # Keep track of the created order to avoid a 2nd order
                      self.order = self.buy(exectype=bt.Order.Market)
      
              else:
      
                  if self.sma1[0] < self.sma2[0]:
                      # SELL, SELL, SELL!!! (with all possible default parameters)
                      self.log('SELL CREATE, %.2f' % self.dataclose[0])
      
                      # Keep track of the created order to avoid a 2nd order
                      self.order = self.sell(exectype=bt.Order.Market)     #### sell  1)
              pass
      
      
      
      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by ab_trader

        Cancel the 2nd order as soon as 1st order is issued or executed using self.cancel(2nd_order_variable).

        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(); }); }