Hash Line
Hash Line

Reputation: 35

Separate exit for stop loss and target

I am coding a strategy in pine script where i have set the exit at a certain stoploss and target price.It looks like this:

if useStopLoss
    strategy.exit("Stop Loss/Profit Long","Long", stop=stop_level, limit=take_level,comment="exit_buy")
    strategy.exit("Stop Loss/Profit Short","Short", stop=stop_level_short, limit=take_level_short,comment="exit_sell")

The issue here is I am using {{strategy.order.comment}} in my alerts but i am not able to differentiate if the stoploss is triggered or It has reached the target. I tried doing the following:

if useStopLoss
    strategy.exit("Stop Loss/Profit Long","Long", stop=stop_level,comment="stop_buy")
    strategy.exit("Stop Loss/Profit Short","Short", stop=stop_level_short,comment="stop_sell")
    strategy.exit("Stop Loss/Profit Long","Long", limit=take_level,comment="target_buy")
    strategy.exit("Stop Loss/Profit Short","Short",limit=take_level_short,comment="target_sell")

here i made separate exits for stop loss and target. But for some reason it doesn't work as intended. Any help is appreciated

Upvotes: 0

Views: 898

Answers (1)

Seba Vasile
Seba Vasile

Reputation: 19

Try to use strategy.order to exit your positions. Also, in the meantime you can use alert_message instead of comment and in the alert use {{strategy.order.alert_message}}.

Something like this should work; the direction based on IS_LONG variable is the reverse direction versus initial orders that opened position:

strategy.order(id="take_profit", limit=take_profit_price, long=(IS_LONG?false:true), qty=strategy.position_size, when=(strategy.position_size != 0), alert_message="write here")

Upvotes: 1

Related Questions