user23400508
user23400508

Reputation: 1

strategy.exit is not being executed in long position while does executed in short position base on the same calculation

I'm trying to manually set a trailing take profit price. I am calculating my trailing take profit price every time I hit a new high (while in long) and every time I hit a new low (in short position).

For some reason, for short position it works perfectly fine, while long positions not.

if strategy.position_size > 0
        trade_entry_price := strategy.opentrades.entry_price(0)
        longStopPrice := trade_entry_price * (1 - longSL)
        longTakePrice := trade_entry_price * (1 + longTP)
        strategy.exit("Exit", from_entry = "LONG", stop = longStopPrice, comment = "Long Stop Loss " + str.tostring(longStopPrice) + " / " + str.tostring(trade_entry_price))
        if trail_longTP == 0.0
            if high > longTakePrice
                trail_longTP := high * (1 - longTO)
                strategy.exit("Exit", "LONG", stop = trail_longTP, comment = "Long Trail TP Hit, on first calc")
        else 
            if (high * (1 - longTO)) > trail_longTP
                b_long_to_tp_set := true
                trail_longTP := high * (1 - longTO)
                strategy.exit("Exit", "LONG", stop = trail_longTP, comment = "Long Trail TP Hit after re-calc")
                

For some reason, the following is not being executed:

strategy.exit("Exit", "LONG", stop = trail_longTP, comment = "Long Trail TP Hit after re-calc")

I've added a debug variable to see that i am actually getting into the condition to update the trailing price "b_long_to_tp_set := true"

Attached a picture of the graph to show that it is being updated and trail_longTP price is updated (blue line), and as can be seen the order is not being executed in the marked red bar as it should because the price of the bar crossed my price.

Any help would be much appreciated. enter image description here

i've tried adding another condition which eventually looked like this (as a workaround) and this did trigger, but this is not what i want to use, and this make my exit order later than i want. note that the order was filled using the latter exit function rather the first. (marked blue arrow) enter image description here

2 lines added at the bottom

else if high[1] > high or close < trail_longTP
                strategy.exit("Exit", "LONG", stop = trail_longTP, comment = "Long Trail TP Hit close price < trail long price")
if strategy.position_size > 0
        trade_entry_price := strategy.opentrades.entry_price(0)
        longStopPrice := trade_entry_price * (1 - longSL)
        longTakePrice := trade_entry_price * (1 + longTP)
        strategy.exit("Exit", from_entry = "LONG", stop = longStopPrice, comment = "Long Stop Loss " + str.tostring(longStopPrice) + " / " + str.tostring(trade_entry_price))
        if trail_longTP == 0.0
            if high > longTakePrice
                trail_longTP := high * (1 - longTO)
                strategy.exit("Exit", "LONG", stop = trail_longTP, comment = "Long Trail TP Hit, on first calc")
        else 
            if (high * (1 - longTO)) > trail_longTP
                b_long_to_tp_set := true
                trail_longTP := high * (1 - longTO)
                strategy.exit("Exit", "LONG", stop = trail_longTP, comment = "Long Trail TP Hit after re-calc")
            else if high[1] > high or close < trail_longTP
                strategy.exit("Exit", "LONG", stop = trail_longTP, comment = "Long Trail TP Hit close price < trail long price")

Upvotes: 0

Views: 37

Answers (1)

Rakesh Poluri
Rakesh Poluri

Reputation: 355

The "Long Trail TP Hit after re-calc" exit condition is not triggering because you only update the trail_longTP variable when it is zero and high > longTakePrice. This means that only the first exit condition will ever be hit.

You will need to update trail_longTP outside of the second if statement (high > longTakePrice) if you want the second exit condition to have a chance of triggering.

Upvotes: 0

Related Questions