Search code examples
jsonpine-scriptpine-script-v5tradingview-api

PineScript: Multiple 'alert_message' on multiple 'strategy.exit'


I am trying to give SignalStack a try. They need their commands relayed to them, through alerts, in JSON format and can only receive single commands - that is I can only place either a buy or sell order, I cannot have TP and SL commands within in the same JSON alert.

The problem here, per TD's PineScript language, I can only have one 'strategy.exit' condition per "if" statement.

here is a snip of my simple entry and exist logic:

//Enter Bull position
if ta.crossover(Bull, Bear) and not na(Time) and strategy.opentrades <= 0
    strategy.entry("Bullish Entry", strategy.long, qty = CS)
    strategy.exit("Bullish Exit", "Bullish Entry", profit = 43, alert_message = "{JSON Command"})
    strategy.exit("Bullish Exit", "Bullish Entry", loss = 39, alert_message = "{JSON Command"})
    strategy.close("Bearish Entry")
//Enter Bear position
if ta.crossunder(Bull, Bear) and not na(Time) and strategy.opentrades <= 0
    strategy.entry("Bearish Entry", strategy.short, qty = CS) 
    strategy.exit("Bearish Exit", "Bearish Entry", profit = 43, alert_message = "{JSON Command"})
    strategy.exit("Bearish Exit", "Bearish Entry", loss = 39, alert_message = "{JSON Command"})
    strategy.close("Bullish Entry")
if na(Time)
    strategy.close_all("CLOSED FOR THE DAY")

Reading this, you should get the logic of what I am trying to affect here. but that second "strategy.exit" per "if" statement will not work. But in order to use SignalStacks automation services, I have to have the a JSON command per "buy" and "sell" alert.

Any help to my situation here would be great!


Solution

  • You can combine your exits into one strategy.exit() and use dedicated exit alert messages.

    strategy.exit(id, from_entry, qty, qty_percent, profit, limit, loss, stop, trail_price, trail_points, trail_offset, oca_name, comment, comment_profit, comment_loss, comment_trailing, alert_message, alert_profit, alert_loss, alert_trailing) → void

    alert_profit (series string) Text that will replace the '{{strategy.order.alert_message}}' placeholder when one is used in the "Message" field of the "Create Alert" dialog. Only replaces the text if the exit was triggered by crossing profit or limit specifically. Optional. The default is na.

    alert_loss (series string) Text that will replace the '{{strategy.order.alert_message}}' placeholder when one is used in the "Message" field of the "Create Alert" dialog. Only replaces the text if the exit was triggered by crossing stop or loss specifically. Optional. The default is na.

    It would look like:

    strategy.exit("Bullish Exit", "Bullish Entry", profit = 43, loss = 39, alert_profit = "{JSON Command}", alert_loss = "{JSON Command}")