有关一次发出警报的Pinescript问题

时间:2020-06-03 12:23:40

标签: pine-script algorithmic-trading trading

策略代码

我有一个根据移动平均条件买卖的松木码。代码如下:

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

警报代码

现在,我想通过添加下面可以在下面看到的更多代码来使上面的代码具有警报功能:

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)    

我计划仅在满足购买条件时才生成一次警报:

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)  

或出售

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)   

每当满足一个条件时,假设当前价格高于SMA值:if(s < close),我们将成功地制作长图,因为它首先有效。 现在的主要问题,我之所以不得不写这篇文章,是因为根据市场趋势,价格长期保持在SMA之上,所以我的警报代码一直持续触发绘制条件有效的次数。我只想打印一个情节以提醒它是长线还是短线,并停止重复一次(我的意思是,如果已经进行了长的情节,那么就不会重复操作,我不希望它重复直到新的空头情节出现情节条件if(s > close)有效),反之亦然,如果短情节已经开始,我不希望它再次重复短情节警报,直到新的长情节条件if(s < close)有效。我们如何使其成为可能?

1 个答案:

答案 0 :(得分:0)

这使用barssince()来检查消除连续输入信号:

//@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)

您的上方,以下是新版本: enter image description here