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 to get the StopTrail order price returned by the broker from time to time?

    General Code/Help
    4
    5
    491
    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.
    • hei jai
      hei jai last edited by

      I searched through the forum seems no one asked it before.

      As StopTrail order price is a dynamic value that will be updated by broker to a new value when the is new high in long position, may I know how to get StopTrail order value in the next()

      Is there a simple code to do this ? Thanks in advance.

      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        When you create the order make your order equal to a variable, say: self.order Also keep track of the latest traillimit price with another variable, in this case self.trail_price.

        You will need to initialize them as None. The changing limit can be found in the order as the created price, so order.created.price.

        Just compare the created price to your tracking trail_price and you can print or log whenever they are not equal.

        Here's full code implementation:

        def next(self):
        
            if not self.position and self.buy_sig:
                self.order = self.buy(size=1, exectype=bt.Order.StopTrail, trailamount=5)
                self.trail_price = self.order.created.price
                self.log(
                    f"ORDER MADE: close {self.data.close[0]}, trail price {self.trail_price}"
                )
            elif self.position and self.sell_sig:
                self.sell()
            else:
                pass
        
            if self.trail_price and self.order is not None:
                if self.trail_price != self.order.created.price:
                    self.log(
                        f"TRAIL PRICE CHANGES: \tclose {self.data.close[0]:.2f},"
                        f"\told trail price {self.trail_price:.2f},"
                        f"\tnew trail price {self.order.created.price:.2f}"
                    )
                    self.trail_price = self.order.created.price
        
        

        Output:

        2020-01-29 12:10:00, ORDER MADE: close 3274.75, trail price 3279.75
        2020-01-29 12:25:00, TRAIL PRICE CHANGES: 	close 3274.50, 	old trail price 3279.75, 	new trail price 3279.50
        2020-01-29 12:35:00, TRAIL PRICE CHANGES: 	close 3273.75, 	old trail price 3279.50, 	new trail price 3278.75
        2020-01-29 13:00:00, TRAIL PRICE CHANGES: 	close 3273.25, 	old trail price 3278.75, 	new trail price 3278.25
        2020-01-29 13:10:00, TRAIL PRICE CHANGES: 	close 3272.00, 	old trail price 3278.25, 	new trail price 3277.00
        2020-01-29 13:30:00, BUY EXECUTED, Price: 3277.00, Cost: -3308.50, Comm 0.00
        

        RunBacktest.com

        KonstantinKlychkov Jens Halsberghe 2 Replies Last reply Reply Quote 3
        • hei jai
          hei jai last edited by

          @run-out , thanks a lot for your detailed explanation. You helped me to fix my days of headache. I never notice order.created.price will store the updated price.

          Now I can use order.created.price to track the StopTrail price changes.

          1 Reply Last reply Reply Quote 1
          • KonstantinKlychkov
            KonstantinKlychkov @run-out last edited by

            @run-out hello!

            I've tried to use that recommendation and change stop-limit price, but some issues have happened and my strategy shows the same results.

            My code is:

               def notify_order(self, order):
                    ...
            
                    # notification for STOP-LOSS orders
                    elif order.issell() and order.exectype==bt.Order.Stop:
                        print('{}: Order № {} | STOP-LOSS - {} | Size {} | Price {}'.
                        format(
                            self.data.datetime.datetime(0),
                            order.ref,
                            order.getstatusname(),
                            order.size,
                            order.price))
            
            
                def notify_trade(self, trade):
                    if trade.isclosed:
                        self.log(f'OPERATION RESULT --- Gross: {trade.pnl:.2f}, Net: {trade.pnlcomm:.2f} \n')
                
            
                def next(self):
                    ...
            
                    self.price_SL_order = self.price_B_order - self.p.bargain_risk * self.data.close[0]
                    
                    ...
            
                    if self.SL_order:
                        if self.SL_order.alive and (self.SL_order.price < self.price_SL_order):
                            self.SL_orderprice = self.price_SL_order
                        else:
                            self.SL_order = None
            

            With changing stop-loss price I have last stop-loss price 5008.725 and OPERATION RESULT --- Gross: -8932.00

            2019-08-16 18:00:00: Order № 4 | BUY - Submitted | Size 116 | Price 5055.0
            2019-08-16 18:00:00: Order № 5 | STOP-LOSS - Submitted | Size -116 | Price 4979.175
            2019-08-16 18:00:00: Order № 6 | TAKE-PROFIT - Submitted | Size -116 | Price 6571.5
            2019-08-16 18:00:00: Order № 4 | BUY - Accepted | Size 116 | Price 5055.0
            2019-08-16 18:00:00: Order № 5 | STOP-LOSS - Accepted | Size -116 | Price 4979.175
            2019-08-16 18:00:00: Order № 6 | TAKE-PROFIT - Accepted | Size -116 | Price 6571.5
            2019-08-16 18:00:00: Order № 4 | BUY - Completed | Size 116 | Price 5055.0
            2019-08-26 11:00:00: Order № 5 | STOP-LOSS - Completed | Size -116 | Price 5008.725
            2019-08-26 11:00:00: Order № 6 | TAKE-PROFIT - Canceled | Size -116 | Price 6571.5
            2019-08-26 11:00:00, OPERATION RESULT --- Gross: -8932.00, Net: -8932.00 
            

            Without changing stop-loss price I have last stop-loss price 4979.175 and OPERATION RESULT --- Gross: -8932.00

            2019-08-16 18:00:00: Order № 4 | BUY - Submitted | Size 116 | Price 5055.0
            2019-08-16 18:00:00: Order № 5 | STOP-LOSS - Submitted | Size -116 | Price 4979.175
            2019-08-16 18:00:00: Order № 6 | TAKE-PROFIT - Submitted | Size -116 | Price 6571.5
            2019-08-16 18:00:00: Order № 4 | BUY - Accepted | Size 116 | Price 5055.0
            2019-08-16 18:00:00: Order № 5 | STOP-LOSS - Accepted | Size -116 | Price 4979.175
            2019-08-16 18:00:00: Order № 6 | TAKE-PROFIT - Accepted | Size -116 | Price 6571.5
            2019-08-16 18:00:00: Order № 4 | BUY - Completed | Size 116 | Price 5055.0
            2019-08-26 11:00:00: Order № 5 | STOP-LOSS - Completed | Size -116 | Price 4979.175
            2019-08-26 11:00:00: Order № 6 | TAKE-PROFIT - Canceled | Size -116 | Price 6571.5
            2019-08-26 11:00:00, OPERATION RESULT --- Gross: -8932.00, Net: -8932.00 
            

            Where is a mistake?

            1 Reply Last reply Reply Quote 0
            • Jens Halsberghe
              Jens Halsberghe @run-out last edited by

              @run-out This actually doesn't make that much sense to me. My thought would be that when we buy at let's say 3277. the initial stoploss would be 3272 (because trailamount = 5). then when price moves up, it would keep trailing 5 behind. it would then stay the same if price comes back down and would trigger a sell order once it was hit.

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