Golden/Death Cross Highlighter

dripvesting · study · 47 行 · 点赞 64 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
indicator("Golden/Death Cross Highlighter", overlay=true)
 
// Input for Moving Averages
fastLength = input.int(50, title="Fast MA Length")
slowLength = input.int(200, title="Slow MA Length")
goldenOpacity = input.int(90, title="Golden Cross Highlight Opacity", minval=0, maxval=100)
deathOpacity = input.int(90, title="Death Cross Highlight Opacity", minval=0, maxval=100)
 
// Calculate Moving Averages
fastMA = ta.sma(close, fastLength)
slowMA = ta.sma(close, slowLength)
 
// Detect Golden and Death crosses
goldenCross = ta.crossover(fastMA, slowMA)
deathCross = ta.crossunder(fastMA, slowMA)
 
// Initialize variables to track cross state
var inGoldenCross = false
var inDeathCross = false
 
// Update cross state
if goldenCross
    inGoldenCross := true
    inDeathCross := false
else if deathCross
    inGoldenCross := false
    inDeathCross := true
 
// Define coloring conditions
bullishCondition = inGoldenCross and close > fastMA and close > slowMA
bearishCondition = inDeathCross and close < fastMA and close < slowMA
 
// Create colors with opacity
goldenColor = color.new(color.yellow, goldenOpacity)
deathColor = color.new(color.red, deathOpacity)
 
// Apply background colors based on conditions
bgcolor(bullishCondition ? goldenColor : bearishCondition ? deathColor : na)
 
// Plot Moving Averages
plot(fastMA, color=color.blue, linewidth=2, title="Fast MA")
plot(slowMA, color=color.red, linewidth=2, title="Slow MA")
 
// Optional alerts
alertcondition(goldenCross, title="Golden Cross", message="Golden Cross Detected!")
alertcondition(deathCross, title="Death Cross", message="Death Cross Detected!")

← 返回列表