战略代码
我有一个松码,根据移动平均线的状况来卖和买。该代码是这样的。
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)
只要满足一个条件,比如说,当前价格高于均线值。if(s < close)
我们就会成功地做多,因为它首先有效。在 当前的主要问题 我为什么要写这篇文章,因为价格长期停留在均线之上,根据市场走势,我的。警报代码 在条件有效的情况下,会不断发射相同的长图。我只想打印一个警报图,无论是长图还是短图,只打印一次,然后停止重复(我的意思是,如果一个长图已经被执行,我不想让它重复,直到一个新的短图条件 if(s > close)
是有效的),反之亦然,如果一个短情节已经进行了,我不希望它再次重复短情节警报,直到如果一个新的长情节条件 if(s < close)
是有效的。如何才能实现?
这使用了 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)