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/

    STOP TRAIL order questions.

    General Discussion
    2
    5
    831
    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.
    • ciceron
      ciceron last edited by

      Hi all,

      First, thanks for all the contributors' work on Backtrader project, this is a very nice framework compare with what I used before.

      I have a question with the build in stop trail order. I use OANDA api before, maybe this influence my understanding on STOP Orders.

      In OANDA, when I pose a buy market order with trailing stop, the order will be executed as soon. Then if the price go down in a certain percent, then a sell market order active. If price go up, the order will keep trailing.

          mktOrder = MarketOrderRequest(instrument=instruments, units=units, trailingStopLossOnFill=trailingStopLossOnFill.data)
      
      • Question 1
      self.order = self.buy(exectype=bt.Order.StopTrail, trailpercent=self.p.trailingPercent, valid = valid1)
      

      After investigate the API doc of backtrader, in my understanding (if correct), the above buy order with a stop trail type means the order will be actived as market order once the price go up exceed a certain percent = self.p.trailingPercent, within time delta = valid1. And if the price go down within valid time, the buy order won't be executed. Once the order exceed valid time, this order will be cancelled.

      • Question 3
        To achieve what OANDA API do. In backtrader, I need to pose a buy order, and then pose a sell stop trail order (as the example below):
      self.buy(size=1)
      self.sell(size=1, exectype=bt.Order.StopTrail, price=10.50, trailpercent=0.0.02)
      
      • Question 4
        If I do this,
      if not self.position:
           if Signal = buy_signal:
                self.order = self.buy(exectype=bt.Order.StopTrail, trailpercent=self.p.trailingPercent, valid = valid1)
      else:
           if Signal = sell_signal:
                self.order = self.close()
      

      This means,
      If we have a buy signal, we pose a buy trail stop order in the market. Then,

      • a: if the price continue go up to a certain threshold, a buy market order will be posed. Once the buy order active, we wait for sell signal to close the position.

      • b: if the price continue go down, the buy order won't become market buy until exceed time threshold valid1, the order will be cancelled.

      • c: During the price going down period, the trail price will be updated.

      Thanks,
      Ciceron

      1 Reply Last reply Reply Quote 0
      • ciceron
        ciceron last edited by

        Is above understanding correct?

        1 Reply Last reply Reply Quote 0
        • Nikolai
          Nikolai last edited by

          Hi Ciceron.

          As i see Oanda broker doesn't have trailing stop implementation. Only:

          # Order type matching with oanda
          _ORDEREXECS = {
              bt.Order.Market: 'MARKET',
              bt.Order.Limit: 'LIMIT',
              bt.Order.Stop: 'STOP',
              bt.Order.StopLimit: 'STOP',
          }
          

          https://github.com/ftomassetti/backtrader-oandav20/blob/master/btoandav20/stores/oandav20store.py

          ciceron 1 Reply Last reply Reply Quote 0
          • ciceron
            ciceron @Nikolai last edited by

            @nikolai Hi Nikolai, Thanks for the answer, the OANDA API I used before come from oandapyV20, the original oanda v20 also support trailing stop.

            [from oandapyV20.contrib.requests import PositionCloseRequest, MarketOrderRequest, TrailingStopLossDetails](https://oanda-api-v20.readthedocs.io/en/latest/contrib/orders/marketorderrequest.html)
            

            What confused me is in oandapyv20, if I open a market order with a trailing stop loss, I just need one command:

            mktOrder = MarketOrderRequest(instrument=instruments, units=units, trailingStopLossOnFill=trailingStopLossOnFill.data)
            

            But in backtrader, if I want to do the same thing I need to open two order:

            self.buy(size=1)
            self.sell(size=1, exectype=bt.Order.StopTrail, price=10.50, trailpercent=0.0.02)
            

            Is my understanding correct?
            Best
            Ciceron

            1 Reply Last reply Reply Quote 0
            • Nikolai
              Nikolai last edited by

              @ciceron hi again.
              I was wrong. Oanda broker has StopTrail implementation.

              if order.exectype == bt.Order.StopTrail:
                  okwargs['distance'] = order.trailamount 
              

              https://github.com/ftomassetti/backtrader-oandav20/blob/master/btoandav20/stores/oandav20store.py

              I didn't have any practice with Oanda broker, so i don't know how stoptrail works on Oanda side. But your understanding is correct for backtesting.

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