0

策略代码

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

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)有效。我们怎样才能使它成为可能?

4

1 回答 1

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)

上面是你的,下面是新版本: 在此处输入图像描述

于 2020-06-04T00:19:32.477 回答