0
votes

Strategy Code

I have a pine code which sells and buys depending on moving average condition. The code are like:

study("MAS_Alerts")

qty = input(10000, "Buy quantity")


ma1 = input( "SMA",title="Select MA", options=["SMA", "EMA","TEMA", "WMA","HMA"])


len1 = input(7, minval=1, title="Period")

s=sma(close,len1)

e=ema(close,len1)


xEMA1 = ema(close, len1)
xEMA2 = ema(xEMA1, len1)
xEMA3 = ema(xEMA2, len1)
t = 3 * xEMA1 - 3 * xEMA2 + xEMA3


f_hma(_src, _length)=>
    _return = wma((2 * wma(_src, _length / 2)) - wma(_src, _length), round(sqrt(_length)))

h = f_hma(close, len1)

w = wma(close, len1)

ma = ma1 == "SMA" ? s : ma1 == "EMA" ? e : ma1 == "WMA" ? w : ma1 == "HMA" ? h : ma1 == "TEMA" ? t : na

Alert Code

Now here I am trying to make an alert feature out of the above code by adding further code which you can see below:

long_condition = 0
long_count = 1
green = color.green
red = color.red
if(s)
    if(long_count)
        long_count := long_count - 1
        if(s < close)
            long_condition := long_condition + 1
        else
            long_condition := long_condition - 1    

plot(long_condition, "Long", color=green)   

short_condition = 0
short_count = 1

if(s)
    if(short_count)
        short_count := short_count - 1
        if(s > close)
            short_condition := short_condition + 1
        else
            short_condition := short_condition - 1    

plot(short_condition, "Short", color=red)    

I was planning to generate an alert for one time only whenever the condition mets for buy:

if(s)
    if(long_count)
        long_count := long_count - 1
        if(s < close)
            long_condition := long_condition + 1
        else
            long_condition := long_condition - 1    

plot(long_condition, "Long", color=green)  

or for sell

if(s)
    if(short_count)
        short_count := short_count - 1
        if(s > close)
            short_condition := short_condition + 1
        else
            short_condition := short_condition - 1    

plot(short_condition, "Short", color=red)   

Whenever one condition is met let suppose say, current price was higher than the SMA value: if(s < close) we will successfully make a long plot as it gets valid first. The main issue now why I had to write this post was that because price stays above the SMA for a long time depending on market trend my Alert Code keeps on firing the same long plot as many times the condition is valid. I just want to print a plot for alert either it is long or short just for one time and stop repeating it (I mean no double action if once a long plot has already been proceeded I don't want it to repeat until a new short plot condition if(s > close) is valid) and vise versa if once a short plot has already proceeded I don't want it to repeat the short plot alert again and until if a new long plot condition if(s < close) is valid. How can we make it possible?

1

1 Answers

0
votes

This uses barssince() to check eliminate consecutive entry signals:

//@version=4
study("MAS_Alerts2")

qty = input(10000, "Buy quantity")


ma1 = input( "SMA",title="Select MA", options=["SMA", "EMA","TEMA", "WMA","HMA"])


len1 = input(7, minval=1, title="Period")

s=sma(close,len1)

e=ema(close,len1)


xEMA1 = ema(close, len1)
xEMA2 = ema(xEMA1, len1)
xEMA3 = ema(xEMA2, len1)
t = 3 * xEMA1 - 3 * xEMA2 + xEMA3


f_hma(_src, _length)=>
    _return = wma((2 * wma(_src, _length / 2)) - wma(_src, _length), round(sqrt(_length)))

h = f_hma(close, len1)

w = wma(close, len1)

ma = ma1 == "SMA" ? s : ma1 == "EMA" ? e : ma1 == "WMA" ? w : ma1 == "HMA" ? h : ma1 == "TEMA" ? t : na
long_condition = 0
long_count = 1
green = color.green
red = color.red

long_trigger = s < close
short_trigger = s > close
b_since_long = barssince(long_trigger)[1]
b_since_short = barssince(short_trigger)[1]
if(s)
    if(long_count)
        long_count := long_count - 1
        if long_trigger and b_since_long > b_since_short
            long_condition := long_condition + 1
        else
            long_condition := long_condition - 1    

plot(long_condition, "Long", color=green)   

short_condition = 0
short_count = 1

if(s)
    if(short_count)
        short_count := short_count - 1
        if short_trigger and b_since_short > b_since_long
            short_condition := short_condition + 1
        else
            short_condition := short_condition - 1    

plot(short_condition, "Short", color=red)    
bgcolor(long_trigger ? color.green : short_trigger ? color.red : na)
plotchar(b_since_long, "b_since_long", "", location.top, size = size.tiny)
plotchar(b_since_short, "b_since_short", "", location.top, size = size.tiny)

Yours above, the new version below: enter image description here