pine-scriptpine-script-v5tradingview-apitradingalgorithmic-trading

AlertCondition() is not triggering the buy and sell alerts even after handling the state management


Below is the script for plotting option premium as candles in a seperate pane (not in main chart/pane) and triggering buy and sell alerts. But the script is not trigerring any alerts or alerts are triggering for every candle close even without condition change buy to sell or sell to buy. Can you please help me correct the script to trigger alert only once per condition change (Buy/Sell)

  1. Tried this ----- Below logic is triggering alerts are triggering for every candle close even without condition change buy to sell or sell to buy
if buy_condition1 and buy_condition2 and buy_condition3 and buy_condition4
        buy := true
        sell := false
        buyC := 1
        sellC := 0
    else if sell_condition1 or sell_condition2 or sell_condition3 or sell_condition4
        sell := true
        buy := false
        sellC := 1
        buyC := 0

    // Add alert conditions for buy and sell
    alertcondition(buy, title="Buy Alert", message="All 4 Buy conditions met!")
    alertcondition(sell, title="Sell Alert", message="Any 1 Sell condition met!")
  1. Tried this ----- But the script is not trigerring any alerts
// Buy and Sell Logic: Green when all 4 buy conditions are met, Red when any 1 sell condition is met
if buy_condition1 and buy_condition2 and buy_condition3 and buy_condition4
    buy := true
    sell := false
    
else if sell_condition1 or sell_condition2 or sell_condition3 or sell_condition4
    sell := true
    buy := false

if buy 
    buyC := 1
    sellC := 0
if sell 
    sellC := 1
    buyC := 0

// **State management for triggering alerts only once**
var bool buyAlertTriggered = false
var bool sellAlertTriggered = false

// **New Logic to trigger alerts only once per state change**
if buy and not buyAlertTriggered
    buyAlertTriggered := true
    sellAlertTriggered := false  // Reset sell alert flag

if sell and not sellAlertTriggered
    sellAlertTriggered := true
    buyAlertTriggered := false  // Reset buy alert flag

// **Alert conditions to show up in TradingView alerts dropdown**
alertcondition(buy and not buyAlertTriggered, title="Buy Alert", message="All 4 Buy conditions met!")
alertcondition(sell and not sellAlertTriggered, title="Sell Alert", message="Any 1 Sell condition 
met!")
  1. Expected result is - trigger alert only once per condition change (Buy/Sell)

**Updated plotshape() and alertcondition() logic

// Define buy and sell states
var bool inBuyState = false  // Is currently in Buy state
var bool inSellState = false  // Is currently in Sell state
var bool prevBuyState = false  // Previous Buy state
var bool prevSellState = false  // Previous Sell state

// Detect Buy and Sell signals
buySignal = buy_condition1 and buy_condition2 and buy_condition3 and buy_condition4 and not inBuyState
sellSignal = sell_condition1 or sell_condition2 or sell_condition3 or sell_condition4 and not inSellState

// Update states based on signals
if buySignal
    inBuyState := true
    inSellState := false
else if sellSignal
    inBuyState := false
    inSellState := true

// Trigger alerts only when state changes
triggerBuyAlert = inBuyState and not prevBuyState
triggerSellAlert = inSellState and not prevSellState

// Register alerts
alertcondition(triggerBuyAlert, title="Buy Alert", message="Buy Signal Triggered")
alertcondition(triggerSellAlert, title="Sell Alert", message="Sell Signal Triggered")

// Plot Buy and Sell signals on the chart as vertical markers
plotshape(triggerBuyAlert ? combined_close : na, title="Buy Signal", location=location.absolute, style=shape.triangleup, color=color.green, size=size.small, text="BUY", offset=0)
plotshape(triggerSellAlert ? combined_close : na, title="Sell Signal", location=location.absolute, style=shape.triangledown, color=color.red, size=size.small, text="SELL", offset=0)

Solution

  • Your logic (order of some events) in your code is off.

    Let's break it down for the buy alert.

    Your alert condition is buy and not buyAlertTriggered. This means, when it reaches to that line, buy should be true and buyAlertTriggered should be false.

    However, just above that alertcondition() code, you have and if block as below.

    if buy and not buyAlertTriggered
        buyAlertTriggered := true
        sellAlertTriggered := false  // Reset sell alert flag
    

    This is the same condition as your alert condition. However, there is a problem here.

    If buy is true and buyAlertTriggered is false, you will enter into this if block and set buyAlertTriggered to true.

    So, when it reaches to alertcondition() statement a few lines below, buyAlertTriggered is now true. Therefore, the conditions (buy and not buyAlertTriggered) for the alert is no longer satisfied at that line.

    Move your alertcondition() to above those if blocks and it will be fine.

    var bool buyAlertTriggered = false
    var bool sellAlertTriggered = false
    
    alertcondition(buy and not buyAlertTriggered, title="Buy Alert", message="All 4 Buy conditions met!")
    alertcondition(sell and not sellAlertTriggered, title="Sell Alert", message="Any 1 Sell condition met!")
    
    // **New Logic to trigger alerts only once per state change**
    if buy and not buyAlertTriggered
        buyAlertTriggered := true
        sellAlertTriggered := false  // Reset sell alert flag
    
    if sell and not sellAlertTriggered
        sellAlertTriggered := true
        buyAlertTriggered := false  // Reset buy alert flag
    

    You should also learn some basic debugging skills.

    At least plot the conditions for the alert as a shape or similar so you can have some visual indication.

    alert_buy_cond = buy and not buyAlertTriggered
    alert_sell_cond = sell and not sellAlertTriggered
    
    plotshape(alert_buy_cond, "Buy", shape.labelup, location.belowbar, color.green, 0, "Buy", color.white)
    plotshape(alert_sell_cond, "Sell", shape.labeldown, location.abovebar, color.red, 0, "Sell", color.white)