Search code examples
pine-scriptpine-script-v5

Want persist variable like bought, exitedBuyPostion, sold, exitedSellPostion and use it later


I am trying to write some logic aroung Moving Average Convergence Divergence

I am new to Pine Script, but have programming experience.

The logic is simple, on the first Dark green trigger an alert for BUY, On the first light green exit buy postion, and goes on..

But the problem I am facing is, when I see a second Dark green also the BUY alert triggers, which I don't wanted it to trigger.

To do so I have used a variable called bought, which seem not helping in this case.

what am I doing wrong.

macd

//@version=5
indicator(title="Moving Average Convergence Divergence", shorttitle="MACD", timeframe="", timeframe_gaps=true)

// Getting inputs
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26)
src = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])

// Plot colors
col_macd = input(#2962FF, "MACD Line", group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "Signal Line", group="Color Settings", inline="Signal")
col_grow_above = input(#26A69A, "Above Grow", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")

// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal

hline(0, "Zero Line", color=color.new(#787B86, 50))
plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
plot(macd, title="MACD", color=col_macd)
plot(signal, title="Signal", color=col_signal)


// custome logic based on MACD
var buy = false
var bought = false
var exitBuyPostion = false
var exitedBuyPostion = false

var sell = false
var sold = false
var exitSellPostion = false
var exitedSellPostion = false

if (hist >= 0) // light or dark green
    if (hist[1] < hist) // dark green
        if (bought == false)
            buy := true // only buy on the first dark green
            bought := true
            exitBuyPostion := false
            exitedBuyPostion := false
            sell := false
            sold := false
            exitSellPostion := false
            exitedSellPostion := false
    else  // light green
        if (exitedBuyPostion == false)
            exitBuyPostion := true // only exit buy position on the first light green
            exitedBuyPostion := true
            buy := false
            bought := false
            sell := false
            sold := false
            exitSellPostion := false
            exitedSellPostion := false
else
 // light or dark red
    if (hist[1] < hist) // light red
        if (exitedSellPostion == false)
            exitSellPostion := true // only exit sell position on the first light red
            exitedSellPostion := true
            buy := false
            bought := false
            exitBuyPostion := false
            exitedBuyPostion := false
            sell := false
            sold := false
    else // dark red
        if (sold == false)
            sell := true // only sell on the first dark red
            sold := true
            buy := false
            bought := false
            exitBuyPostion := false
            exitedBuyPostion := false
            exitSellPostion := false
            exitedSellPostion := false

alertcondition(buy == true, title = "BUY", message = "Buying on the first dark green")
alertcondition(exitBuyPostion == true, title = "EXIT BUY POSITION", message = "Exiting buy position")

alertcondition(sell == true, title = "SELL", message = "Selling on the first dark red")
alertcondition(exitSellPostion == true, title = "EXIT SELL POSITION", message = "Exit sell position")

Solution

  • buy and sell should be defined without var.

    You want to control these values via bought and sold that you corretly defined with the var keyword meaning these values are retained for further calculations. Since you defined buy and sell also with var, once updated to true, they remain to be true for the coming candles.

    buy = false
    var bought = false
    ...
    sell = false
    var sold = false