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?



  • 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.



  • 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
    


  • @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.


Log in to reply
 

});