本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=4
// Author: vladimir.kamba
study(title="Golden/Death Cross Prediction", precision=5)
priceChangePercents = input(title="Future Price change %", defval="0", options=["50", "40", "30", "20", "10", "0", "-10", "-20", "-30", "-40", "-50"])
futureSMA(length, futureOffset) =>
x=futureOffset
if (futureOffset>length-1)
x:=length-1
float total=0.0
float sma=0.0
for j = 0 to length-1-x
total := total + close[j]
(total+x*close*(100+tonumber(priceChangePercents))/100)/length
isCross(futureOffset1, futureOffset2) =>
diff1=futureSMA(50, futureOffset1) - futureSMA(200, futureOffset1)
diff2=futureSMA(50, futureOffset2) - futureSMA(200, futureOffset2)
diff1*diff2<0
fastSMA = sma(close, 50)
slowSMA = sma(close, 200)
distance = fastSMA - slowSMA
hline(0, "0", color=color.white, linestyle=hline.style_solid, editable=false)
plot(distance)
plot(cross(fastSMA, slowSMA) ? 0 : na, style=plot.style_cross, linewidth=5, color=fastSMA > slowSMA ? color.green : color.red, editable=false)
// draw 50 line segments (TradingView doesn't allow more lines at the same time)
n=50
// segments are 3 bars long in order to extend the timeframe
step=3
// draw prediction on reaching the last bar
if (barstate.islast)
lastDiff=distance
for k = 1 to n-1
i=k*step
x1=bar_index+i-step
x2=bar_index+i
diff=futureSMA(50, i) - futureSMA(200, i)
line.new(x1,lastDiff,x2, diff, color=color.yellow, style=line.style_dotted)
if (lastDiff*diff<0)
// cross detected in interval <i-step, i> - find the exact position with step=1
crossPosition=0.0
for j = i-step to i
if (isCross(j, j+1))
crossPostionIndex=j
crossPosition:=bar_index+crossPostionIndex
xt=time+crossPostionIndex*(time[1]-time[2])
xts=str.format("{0,date,MMM d, YYYY HH:mm z}", xt)
// Info Box
InfoBox=(diff>lastDiff?"Golden" : "Death") + " cross expected: \n" + xts
lbl = label.new(int(crossPosition),0,text = InfoBox, style=diff<lastDiff?label.style_label_upper_right:label.style_label_lower_right,textcolor=color.white, size=size.small, textalign=text.align_left, color=color.new(diff>lastDiff?color.green : color.red, 70))
lastDiff:=diff