首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有关发出一次警告的笔录问题

有关发出一次警告的笔录问题
EN

Stack Overflow用户
提问于 2020-06-03 12:23:40
回答 1查看 850关注 0票数 0

策略代码

我有一个松树代码,根据移动平均条件销售和购买。守则如下:

代码语言:javascript
复制
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

警报码

现在,我试图从上面的代码中添加一个警报特性,添加更多代码,如下所示:

代码语言:javascript
复制
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)    

我计划只在购买条件为mets时发出一次警告:

代码语言:javascript
复制
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)  

或为了出售

代码语言:javascript
复制
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)是有效的。我们如何才能使之成为可能?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-04 00:19:32

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

代码语言:javascript
复制
//@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)

以下是您的新版本:

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62172763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档