策略代码
我有一个根据移动平均线条件买卖的松代码。代码如下:
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)有效。我们怎样才能使它成为可能?
