2

I am trying to create an alert in the alert box as below:

Test Signal (Nifty Spot price) {{time}}: 
{{ticker}} Recommendation {{strategy.order.action}} at CMP: {{close}} with SL:{{plot("StopLoss")}} Target(with 1:1 Risk-to-Reward Ratio): {{plot("Target")}}.

However the StopLoss and Target are not replaced in the final message.

The final alert message is coming as:

enter image description here

My code is like:

buyTarget = close + ((close - atrLow) * rrRatio)
sellTarget = close - ((atrHigh - close) * rrRatio)

// Trading
var float sl = na
var float tgt = na
h = hour(time('1'), syminfo.timezone)
m = minute(time('1'), syminfo.timezone)
hourVal = h * 100 + m
if (mktAlwaysOn or (hourVal < endOfDay))
    // Entry
    var string alert_msg = na
    if (sureBuyInTrend)
        alert_msg := 'Buy: ' + syminfo.ticker + ' , SL: ' + str.tostring(math.floor(atrLow[0])) + ', Target(RR=1:1):' + str.tostring(math.floor(buyTarget)) + '. CMP: ' + str.tostring(close)
        strategy.entry("enter long", strategy.long, lotSize, limit=na, stop=na, comment="Enter Long", alert_message=alert_msg)
        sl := atrLow
        tgt := buyTarget
        // alert('Buy:' + syminfo.ticker + ' ,SL:' + str.tostring(math.floor(atrLow[0])) + ', Target:' + str.tostring(math.floor(buyTarget)), alert.freq_once_per_bar)
    if (sureSellInTrend)
        alert_msg := 'Sell: ' + syminfo.ticker + ' , SL: ' + str.tostring(math.floor(atrHigh[0])) + ', Target(RR=1:1): ' + str.tostring(math.floor(sellTarget)) + '. CMP: ' + str.tostring(close)
        strategy.entry("enter short", strategy.short, lotSize, limit=na, stop=na, comment="Enter Short", alert_message=alert_msg)
        sl := atrHigh
        tgt := sellTarget
        // alert('Sell:' + syminfo.ticker + ' ,SL:' + str.tostring(math.floor(atrHigh[0])) + ', Target:' + str.tostring(math.floor(sellTarget)), alert.freq_once_per_bar)
    
    alert(alert_msg, alert.freq_once_per_bar)

    // Exit: target or SL
    shortExitComment = (close > sl) ? "Short SL hit" : sureBuyInTrend ? "Short target hit" : na
    var string exit_msg = na
    if (sureSellInTrend or (close < sl) or (close > buyTarget))
        exit_msg := (close < sl) ? "Long SL hit. Sorry" : (close > buyTarget) ? 'Long target hit. Enjoy': sureSellInTrend ? 'Next Sell signal came. Exiting' : na
        strategy.close("enter long", comment=close < sl ? "Long SL hit" : "Long target hit", alert_message=exit_msg)

    if (sureBuyInTrend or (close > sl) or (close < sellTarget))
        exit_msg := (close > sl) ? "Short SL hit. Sorry" : (close < sellTarget) ? 'Short target hit. Enjoy': sureBuyInTrend ? 'Next Buy signal came. Exiting' : na
        strategy.close("enter short", comment=close > sl ? "Short SL hit" : "Short target hit", alert_message=exit_msg)

else if(not mktAlwaysOn)
    // Close all open position at the end if Day
    strategy.close_all(comment = "Close all entries", alert_message="Closing all the pending open positions as market close is near. Thanks.")

// Hack for alerts
targetPrice = sureBuyInTrend ? buyTarget : sureSellInTrend ? sellTarget : na
plot(math.round(sl,2), title="StopLoss", display = display.all)
plot(math.round(targetPrice,2), title="Target", display = display.all)


So the logic is pretty simple:

For SL, I am considering ATR and based on that deriving the buySL and sellSL. For target, we are just considering the R:R = 1:1 and add/substract from the price. But some mistake I am doing for which it's not coming up.

Any help will be appreciated.

Pradip
  • 509
  • 1
  • 5
  • 22
  • How many plots do you have? Are you sure your plots have a valid value (not `na`) at the time of your alerts? – vitruvius Sep 22 '22 at 05:33
  • Yes. Because I just changed the colors and saw that the plots are properly coming in the chart. One is SL another is target. Both are plotting in chart with values. – Pradip Sep 22 '22 at 06:08
  • Perhaps you are using two single quotes instead of one double quote. Check in your strategy code and in the alert settings – vgladkov Sep 22 '22 at 17:59

1 Answers1

0

I solved it a little different ways. In strategy.entry, there is an argument as alert_message. I use it and then refer to it in the Alert Settings dialog box as this:

Alert: 
{{strategy.order.alert_message}}

Action:
{{strategy.order.action}} @ {{strategy.order.contracts}} filled on {{ticker}}. New strategy position is {{strategy.position_size}}.
Order number: {{strategy.order.id}}

The code is like this:

    // Entry
    var string alert_msg = na
    if (sureBuyInTrend)
        alert_msg := 'Buy: ' + syminfo.ticker + ' , SL: ' + str.tostring(math.floor(atrLow[0])) + ', Target(RR=1:1):' + str.tostring(math.floor(buyTarget)) + '. CMP: ' + str.tostring(close)
        strategy.entry("enter long", strategy.long, lotSize, limit=na, stop=na, comment="Enter Long", alert_message=alert_msg)
        sl := atrLow
        tgt := buyTarget
    if (sureSellInTrend)
        alert_msg := 'Sell: ' + syminfo.ticker + ' , SL: ' + str.tostring(math.floor(atrHigh[0])) + ', Target(RR=1:1): ' + str.tostring(math.floor(sellTarget)) + '. CMP: ' + str.tostring(close)
        strategy.entry("enter short", strategy.short, lotSize, limit=na, stop=na, comment="Enter Short", alert_message=alert_msg)
        sl := atrHigh
        tgt := sellTarget
Pradip
  • 509
  • 1
  • 5
  • 22