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/

    Am I using StopTrail correctly?

    General Code/Help
    1
    1
    172
    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.
    • Unreal CyberFly
      Unreal CyberFly last edited by

      My goal is to activate a trailing order when the price hits a certain level. For example, when the price goes up to 1000 start trailing on SMA level to secure/maximize profit.

      After looking around the documentation and the forum it seems that StopTrail is the right solution. Here is a topic on the same issue. However, the StopTrail seems to trigger like a regular Stop instead of at the trigger price.

      Console Output

      Candle: 24479
      Current close: 378.55
      Stop Price: 328.55
      Entry Price: 368.55
      Target Price: 1000
      
      
      2020-10-20 11:00:00, Enter Long at: 368.55, Amount: 2.48534226, P.size: 915.97, Order: Long Entry, capital: 9939.29, cash: 9024.71
      2020-10-20 12:00:00, Exit Long at: 367.98, Amount: -2.48534226, P.size: 915.97, Order: OCO Target, capital: 9938.58, cash: 9938.58
      2020-10-20 12:00:00, OCO Stoploss was Canceled/Margin/Rejected, capital: 9938.58, cash: 9938.58
      2020-10-20 12:00:00, Long Results, ETHUSDT. Gross: -1.42, Net: -2.79 capital: 9938.58, cash: 9938.58
      

      As you can see the StopTrail order was executed at the next candle (I'm using 1 hour candles). I did not expect this to happen since the trigger price was set to 1000, this is why I suspect that StopTrail triggers like a regular Stop instead.

      Am I using StopTrail correctly or should I use something else?

      Bracket Order Creation

      def create_order_bracket(self, valid=60):
              """
              Create order bracket, the bracket contains the following order:
              - Entry order, LIMIT order with expiration date
              - Target order, trailing MARKET order
              - Stoploss, MARKET order
      
              Info:
              When the Entry order expires all orders are cancelled.
              The Target and Stoploss behave like an OCO order.
      
              Documentation:
              https://www.backtrader.com/docu/order-creation-execution/bracket/bracket/
              https://www.backtrader.com/docu/order-creation-execution/trail/stoptrail/
      
              Args:
                  valid (int, optional): How many minutes the entry order stays valid. Defaults to 60.
              """
              ######################
              # CREATE ENTRY ORDER #
              ######################
      
              # Set expiration date for entry order
              dt = self.data.datetime.datetime()
              valid = dt + datetime.timedelta(minutes=600)
      
              # Create Limit order to enter the market
              self.order_size = self.size_order()
              entry_order = self.buy(
                  exectype=bt.Order.Limit,
                  size=self.order_size,
                  price=self.entry_price,
                  valid=valid,  # expiration date
                  transmit=False,  # transmit must be False
              )
              entry_order.addinfo(name=LONG_ENTRY)
      
              # Log order creation in CSV
              self.log_order(entry_order, 'create', self.entry_price)
              # add order list
              self.order_list.append(entry_order)
      
              #####################
              # CREATE STOP ORDER #
              #####################
      
              # Create stoploss Market order
              stoploss_order = self.sell(
                  exectype=bt.Order.Stop,
                  size=self.order_size,
                  price=self.stoploss_price,
                  parent=entry_order,
                  transmit=False,  # transmit must be False
              )
              stoploss_order.addinfo(name=OCO_STOPLOSS)
      
              # Log order creation in CSV
              self.log_order(stoploss_order, 'create', self.stoploss_price)
              # add order list
              self.order_list.append(stoploss_order)
      
              #######################
              # CREATE TARGET ORDER #
              #######################
      
              # Create trailing Market order as target order
              target_order = self.sell(
                  exectype=bt.Order.StopTrail,
                  size=self.order_size,
                  price=self.target_price,
                  trailpercent=0.01,  # trail 1%
                  parent=entry_order,
                  transmit=True,  # transmit must be True
              )
              target_order.addinfo(name=OCO_TARGET)
      
              # Log order creation in CSV
              self.log_order(target_order, 'create', self.target_price)
              # add order list
              self.order_list.append(target_order)
      
              # Set awaiting_entry to True
              self.awaiting_entry = True
      
      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors