我希望在没有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")基本上,如果你有一个移动平均线交叉,我在观察如何将移动平均线的来源改变为低或高,而不是接近,这取决于它是越过还是低于。这表现为原油波动或多或少停止,使用低/高使其表现为一个较慢的周期移动平均。
例如,交叉发生,开关状态被打开,并且它知道这个交叉发生在前面的3个条形上,就像上面的松树faq中的例子一样,但是它随后重新定义了源变量。现在的问题是,在切换状态改变之前,必须先发生交叉状态,然后使用close作为源,我正在尝试“跳回去”并更改它,但是这种循环缺少的跳转特性在松树代码中并不明显。
是否有任何方法来重新定义源变量‘后,警报条件,事件,开关状态已经发生?
让我来解释一下为什么开和关开关可能比下面的更好,为什么需要使用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')发布于 2020-06-13 19:48:37
对于定义您的转换的条件,您不会说太多。这一假设是在不同时期的close上2 MAs杂交的过渡。由于转换条件是在一条之后检测到的,所以我们在高/低的另外两个MAs之间切换时:
//@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)

发布于 2020-06-13 19:30:21
你应该把你的问题说得更清楚,但据我所知,你正在寻找这样的东西:
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)*mahhttps://stackoverflow.com/questions/62362931
复制相似问题