本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Moon Phases (Full / New)", overlay = true)
showFull = input.bool(true, "Show full moons")
showNew = input.bool(true, "Show new moons")
showLabels = input.bool(true, "Show text labels")
useEmoji = input.bool(true, "Use emoji icons (off = plain circles)")
tint = input.bool(false, "Tint background on moon bars")
// Mean synodic month and a known new moon (2000-01-06 18:14 UTC)
const float SYN_MS = 29.530588853 * 86400000.0
const float HALF = SYN_MS / 2.0
const float REF = 947182440000.0
// Index of the latest half-cycle event at or before each bar's close.
// Even index = new moon, odd index = full moon.
kNow = math.floor((time_close - REF) / HALF)
kPrev = math.floor((nz(time_close[1], time_close) - REF) / HALF)
diff = kNow - kPrev // events since previous bar's close (catches weekend/session gaps)
isNew = diff >= 2 or (diff == 1 and kNow % 2 == 0)
isFull = diff >= 2 or (diff == 1 and kNow % 2 != 0)
showF = isFull and showFull
showN = isNew and showNew
txtF = showLabels ? color.new(color.yellow, 0) : color.new(color.yellow, 100)
txtN = showLabels ? color.new(color.gray, 0) : color.new(color.gray, 100)
plotchar(showF and useEmoji, "Full Moon", "🌕", location.bottom, color.white, text = "Full", textcolor = txtF, size = size.small)
plotchar(showN and useEmoji, "New Moon", "🌑", location.bottom, color.white, text = "New", textcolor = txtN, size = size.small)
plotchar(showF and not useEmoji, "Full Moon", "●", location.bottom, color.yellow, text = "Full", textcolor = txtF, size = size.small)
plotchar(showN and not useEmoji, "New Moon", "●", location.bottom, color.gray, text = "New", textcolor = txtN, size = size.small)
bgcolor(tint and showF ? color.new(color.yellow, 88) : na)
bgcolor(tint and showN ? color.new(color.gray, 85) : na)
alertcondition(isFull, "Full Moon", "Full moon on {{ticker}}")
alertcondition(isNew, "New Moon", "New moon on {{ticker}}")