需要确定最后一个信号是买入还是抛售 [英] Need to establish if the last signal was toBuy or toSell

查看:19
本文介绍了需要确定最后一个信号是买入还是抛售的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于缓慢移动平均线指标的斜率来创建买入和抛售信号。我的代码的问题是,即使没有事先的买入信号,我也会得到卖出信号。在卖出信号发出之前,有没有办法检查之前的信号是不是买入,这样它们就成对出现了?我想使用valueWhen()函数,但不知道如何应用它。 请帮帮😢

//@version=4
study(title="Bridger MMI ", shorttitle="MMI", overlay=true)

r = rsi(close, 21)                                                 // RSI of Close
slowMA = sma(r, 2)                                          // Moving Average of RSI 7 bars back *** this is the green one I'm watching
                                                         

angleSlowMA= (atan(slowMA[0]-slowMA[1]))*180/3.14159   // atan gives angle in radians given opposite side/adjacent, adjacent =1, therefore atan(opposite) will give angle
                                                       // to convert radians to degrees, multiply by 180 and divide by pi

toBuy = angleSlowMA >= 15 and angleSlowMA <= 30
lastWasToBuy = valuewhen(toBuy, angleSlowMA, 1)      // this gives the value of the angle at the last toBuy

toSell = angleSlowMA <15 

plotshape(toBuy, title="Slope Positive", location=location.belowbar, color=color.lime, transp=0, style=shape.triangleup, text="BUY")            
plotshape(toSell, title="Slope Negative", location=location.abovebar, color=color.yellow, transp=0, style=shape.triangledown, text="SELL")

推荐答案

我们在此处使用var保存条形图中的状态,并且仅绘制转换时的买入/卖出标记:

//@version=4
study(title="Bridger MMI ", shorttitle="MMI", overlay=true)

r = rsi(close, 21)                                      // RSI of Close
slowMA = sma(r, 2)                                      // Moving Average of RSI 7 bars back *** this is the green one I'm watching
                                                         
angleSlowMA= (atan(slowMA[0]-slowMA[1]))*180/3.14159    // atan gives angle in radians given opposite side/adjacent, adjacent =1, therefore atan(opposite) will give angle

// Use `var` to save state of vars across bars.
var bool toBuy = false
var bool toSell = false
if angleSlowMA >= 15 and angleSlowMA <= 30
    toBuy  := true
    toSell := false
else if toBuy and angleSlowMA <15
    toSell := true
    toBuy  := false

lastWasToBuy = valuewhen(toBuy, angleSlowMA, 1)      // this gives the value of the angle at the last toBuy

// Only show markers on transitions.
plotshape(toBuy and not toBuy[1], title="Slope Positive", location=location.belowbar, color=color.lime, transp=0, style=shape.triangleup, text="BUY")            
plotshape(toSell and not toSell[1], title="Slope Negative", location=location.abovebar, color=color.yellow, transp=0, style=shape.triangledown, text="SELL")

[编辑:2021.05.10 13:24-LucF]

调试曲线图:

plotchar(toBuy, "toBuy", "▲", location.top, size = size.tiny)
plotchar(toSell, "toSell", "▼", location.bottom, size = size.tiny)

这篇关于需要确定最后一个信号是买入还是抛售的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆