Pinescript中的打开和关闭开关

时间:2020-06-13 16:51:15

标签: pine-script

我希望做一些没有for循环的事情,所以要么我要查询的东西是不可能的,要么需要多个var命令以及打开和关闭开关

study("On/Off condition", "", true)
upBar = close > open
// On/off conditions.
triggerOn = upBar and upBar[1] and upBar[2]
triggerOff = not upBar and not upBar[1]
// Switch state is implicitly saved across bars thanks to initialize-only-once keyword "var".
var onOffSwitch = false
// Turn the switch on when triggerOn is true. If it is already on,
// keep it on unless triggerOff occurs.
onOffSwitch := triggerOn or (onOffSwitch and not triggerOff)
bgcolor(onOffSwitch ? color.green : na)
plotchar(triggerOn, "triggerOn", "▲", location.belowbar, color.lime, 0, size = size.tiny, text = "On")
plotchar(triggerOff, "triggerOff", "▼", location.abovebar, color.red, 0, size = size.tiny, text = "Off")

基本上,如果您有一个移动平均线交叉,我正在研究如何发布该移动平均线的源,将其移动至低位或高位而不是收盘价,分别取决于其是否穿越或低于。这表现为原油波动或多或少停止,使用低/高使得它的表现像是较慢的移动平均线。

例如,发生交叉,打开了开关状态,并且它从pinecode常见问题中知道该交叉发生在前面的3个小节中,如上例所示,但随后重新定义了源变量。现在的问题是交叉状态必须在之前出现,然后才能使用close作为源更改切换状态,而我试图“跳回”并更改它,但是这种类型的在pine代码中,不存在循环的跳回功能并不明显。

是否有任何方法可以在警报条件,事件,开关状态发生之后“重新定义”源变量?

让我尝试阐明为什么开和关开关可能比以下开关更好,为什么需要使用var;如下所示,即使此方法也需要在绘制波动停止线之前发生该事件。您可以在各种条件下更改源变量基于,但不能更改其本身已经变长或变短的条件,因为代码中尚不知道此信息。也就是说,您如何在脚本开头-在脚本结尾更改a变量?


Factor = slot5a2
Up = ma - Factor * calcS4()
Dn = ma + Factor * calcS4()
TrendUp = 0.0
TrendUp := alttstep[1] > TrendUp[1] ? max(Up, TrendUp[1]) : Up
TrendDown = 0.0
TrendDown := alttstep[1] < TrendDown[1] ? min(Dn, TrendDown[1]) : Dn
Trend = 0.0
Trend := (src555) > TrendDown[1] ? 1 : (src555) < TrendUp[1] ? -1 : nz(Trend[1], 1)
TslMain = Trend == 1 ? TrendUp : TrendDown
linecolor = Trend == 1 ? color.green : color.red

barcolor(Trend == 1 ? color.green : color.red)

//======================================================================================//

//===== PLOTS ===========================================================================//

plot(TslMain, color=linecolor, style=plot.style_line, linewidth=2, title="SuperTrend")

plotarrow(Trend == 1 and Trend[1] == -1 ? Trend : na, title="Up Entry Arrow", colorup=color.lime, maxheight=60, minheight=50, transp=0)
plotarrow(Trend == -1 and Trend[1] == 1 ? Trend : na, title="Down Entry Arrow", colordown=color.red, maxheight=60, minheight=50, transp=0)


//===== ALERTS ==========================================================================//

//--- alerts using variables ---//

sLong = Trend == 1 and Trend[1] == -1
alertcondition(sLong, title='Long', message='SuperTrend Alert Long')

sShort = Trend == -1 and Trend[1] == 1
alertcondition(sShort, title='Short', message='SuperTrend Alert Short')

2 个答案:

答案 0 :(得分:1)

关于定义转换的条件,您无需多说。这假定在不同时期close上2个MA的交叉点上进行转换。由于过渡条件是在一小节后检测到的,因此我们在高/低的另外两个MA之间进行切换:

//@version=4
study("Hi/Lo MAs", "", true)
emaHi   = sma(high, 30)
emaLo   = sma(low,  30)
bull    = sma(close, 20) > sma(close, 100)
stop    = bull ? emaLo : emaHi
stopChg = bull != bull[1]
c_stop  = bull ? color.green : color.fuchsia
plot(stop, "Stop", stopChg ? na : c_stop)
plotchar(stopChg ? stop : na, "stopChg", "•", location.absolute, c_stop, size = size.tiny)
plot(emaLo, "emaLo", color.green,   4, transp = 90)
plot(emaHi, "emaHi", color.fuchsia, 4, transp = 90)

enter image description here

答案 1 :(得分:0)

您应该让您的问题更明确,但据我所知,您正在寻找这样的东西:

os = 0
os := on ? 1 : off ? 0 : os[1]

尾随止损情况给出:

os = 0
mah = sma(high,14)
mal = sma(low,14)
os := cross(close,mah) ? 1 : cross(close,mal) ? 0 : os[1]
stop = os*mal+(1-os)*mah