Pine Script 无状态函数实现
Pine Script stateless function implementation
在 tradingview 的 pine-script 中,我试图找到高于上次收盘价的先前分形 (HF1),并绘制 HF1 的水平
到目前为止,我的代码并没有真正起作用:
//@version=4
strategy("Fractal Breakout Strategy", overlay=true)
HF = bar_index>5 and high[2]>high[1] and high[2]>high and high[2]>high[3] and high[2]>high[4] ? -1 : 0
LF = bar_index>5 and low[2]<low[1] and low[2]<low and low[2]<low[3] and low[2]<low[4] ? 1 : 0
tot = HF + LF
pl = abs(tot)>=1 ? 1 : 0
//Plot fractal arrows:
plotarrow(pl==1 ? tot : na, colorup=color.teal, colordown=color.orange,offset=-2,maxheight=15)
HF1 = 0.0
for i = 0 to 40
HF1_temp = valuewhen(HF, high[2],i)
if (HF1_temp > close[1])
HF1 := HF1_temp
break
plot(HF1, color = color.blue)
我怀疑它与文档中的最后一段有关here
To avoid this you may use your own, stateless function implementation.
This is the list of built-in functions which have the same behavior:
sma(source, length): length is stateful.
ema(source, length): length is stateful.
sum(source, length): length is stateful.
valuewhen(condition, source, occurrence): occurrence is stateful.
rsi(x, y): when y is of type integer and behaves like a length, y is stateful.
..但真的不知道如何实现这个...任何帮助将不胜感激,谢谢!
你的猜测是对的。需要测试,但认为这可以完成工作:
//@version=4
strategy("Fractal Breakout Strategy", overlay=true)
ofst = 2
HF = bar_index>5 and high[2]>high[1] and high[2]>high and high[2]>high[3] and high[2]>high[4] ? -1 : 0
LF = bar_index>5 and low[2]<low[1] and low[2]<low and low[2]<low[3] and low[2]<low[4] ? 1 : 0
// Save fractal's hi/lo when we find one, offset `ofst` bars later.
// This could also be done in the 2 lines above but didn't want to change them
// in case you will need their -1 and 1 values elsewhere in your code.
HFval = HF != 0 ? high[ofst] : na
LFval = LF != 0 ? low[ofst] : na
tot = HF + LF
pl = abs(tot)>=1 ? 1 : 0
//Plot fractal arrows:
plotarrow(pl==1 ? tot : na, colorup=color.teal, colordown=color.orange,offset=-ofst,maxheight=15)
float HF1 = na
for i = 0 to 40
if (HFval[i] > close[1])
HF1 := HFval[i]
break
// Plotting circles eliminates the inelegant joins between non `na` values.
plot(HF1, "HF1", color.blue, 2, plot.style_circles)
在 tradingview 的 pine-script 中,我试图找到高于上次收盘价的先前分形 (HF1),并绘制 HF1 的水平
到目前为止,我的代码并没有真正起作用:
//@version=4
strategy("Fractal Breakout Strategy", overlay=true)
HF = bar_index>5 and high[2]>high[1] and high[2]>high and high[2]>high[3] and high[2]>high[4] ? -1 : 0
LF = bar_index>5 and low[2]<low[1] and low[2]<low and low[2]<low[3] and low[2]<low[4] ? 1 : 0
tot = HF + LF
pl = abs(tot)>=1 ? 1 : 0
//Plot fractal arrows:
plotarrow(pl==1 ? tot : na, colorup=color.teal, colordown=color.orange,offset=-2,maxheight=15)
HF1 = 0.0
for i = 0 to 40
HF1_temp = valuewhen(HF, high[2],i)
if (HF1_temp > close[1])
HF1 := HF1_temp
break
plot(HF1, color = color.blue)
我怀疑它与文档中的最后一段有关here
To avoid this you may use your own, stateless function implementation. This is the list of built-in functions which have the same behavior:
sma(source, length): length is stateful.
ema(source, length): length is stateful.
sum(source, length): length is stateful.
valuewhen(condition, source, occurrence): occurrence is stateful.
rsi(x, y): when y is of type integer and behaves like a length, y is stateful.
..但真的不知道如何实现这个...任何帮助将不胜感激,谢谢!
你的猜测是对的。需要测试,但认为这可以完成工作:
//@version=4
strategy("Fractal Breakout Strategy", overlay=true)
ofst = 2
HF = bar_index>5 and high[2]>high[1] and high[2]>high and high[2]>high[3] and high[2]>high[4] ? -1 : 0
LF = bar_index>5 and low[2]<low[1] and low[2]<low and low[2]<low[3] and low[2]<low[4] ? 1 : 0
// Save fractal's hi/lo when we find one, offset `ofst` bars later.
// This could also be done in the 2 lines above but didn't want to change them
// in case you will need their -1 and 1 values elsewhere in your code.
HFval = HF != 0 ? high[ofst] : na
LFval = LF != 0 ? low[ofst] : na
tot = HF + LF
pl = abs(tot)>=1 ? 1 : 0
//Plot fractal arrows:
plotarrow(pl==1 ? tot : na, colorup=color.teal, colordown=color.orange,offset=-ofst,maxheight=15)
float HF1 = na
for i = 0 to 40
if (HFval[i] > close[1])
HF1 := HFval[i]
break
// Plotting circles eliminates the inelegant joins between non `na` values.
plot(HF1, "HF1", color.blue, 2, plot.style_circles)