Pono Smart Moving Averages

PonoTrading · study · 201 行 · 点赞 22 · TradingView 原页

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

Pine Script

//@version=6
indicator("Pono Smart Moving Averages", shorttitle="Pono Smart MAs", overlay=true)
 
// Pine can never reference more than 5000 historical bars (RE10004).
const int MAX_PERIOD = 5000
 
GOLDENROD = #DAA520
 
movingAverage(float source, string averageType, int period) =>
    averageType == "EMA" ? ta.ema(source, period) : ta.sma(source, period)
 
// ── Warning message inputs ──────────────────────────────────────────────
showWarning      = input.bool(true,  "Show warning when an MA can't be displayed", group="Warning Message")
hideAllOnError   = input.bool(false, "Hide ALL MAs if any one MA is blocked",      group="Warning Message")
warnOnLowHistory = input.bool(true,  "Also warn if chart has fewer bars than the period needs", group="Warning Message")
warningSize      = input.string("Normal", "Warning text size", options=["Small", "Normal", "Large"], group="Warning Message")
 
// ── Cloud inputs ────────────────────────────────────────────────────────
showCloud         = input.bool(true, "Show cloud between MAs", group="MA Cloud")
cloudMA_A         = input.string("MA 1", "Cloud MA A (fast)", options=["MA 1", "MA 2", "MA 3", "MA 4"], group="MA Cloud")
cloudMA_B         = input.string("MA 2", "Cloud MA B (slow)", options=["MA 1", "MA 2", "MA 3", "MA 4"], group="MA Cloud")
bullishColor      = input.color(color.new(color.rgb(8, 153, 129), 82),   "Bullish (A above B)", group="MA Cloud")
bearishColor      = input.color(color.new(color.rgb(220, 50, 50), 82),   "Bearish (A below B)", group="MA Cloud")
neutralColor      = input.color(color.new(color.rgb(255, 170, 0), 82),   "Neutral (A ≈ B)",     group="MA Cloud")
neutralTolerance  = input.float(0.05, "Neutral tolerance (%)", minval=0.0, step=0.01, group="MA Cloud")
useRegimeMAColors = input.bool(true,  "Color MAs by bullish/bearish regime", group="MA Cloud")
regimeColorAllMAs = input.bool(false, "Apply regime color to ALL MAs (not just cloud pair)", group="MA Cloud")
 
// Moving Average 1
enabled1   = input.bool(true, "Enabled", group="1. Moving Average 1")
type1      = input.string("SMA", "Type", options=["SMA", "EMA"], group="1. Moving Average 1")
source1    = input.source(close, "Source", group="1. Moving Average 1")
smart1     = input.bool(true, "Smart adjust", group="1. Moving Average 1")
timeframe1 = input.timeframe("5", "Reference timeframe", group="1. Moving Average 1")
period1    = input.int(390, "Period", minval=1, group="1. Moving Average 1")
color1     = input.color(color.rgb(30, 144, 255), "Color", group="1. Moving Average 1")
width1     = input.int(2, "Width", minval=1, maxval=10, group="1. Moving Average 1")
style1     = input.string("Solid", "Style", options=["Solid", "Dashed", "Dotted"], group="1. Moving Average 1")
 
// Moving Average 2
enabled2   = input.bool(true, "Enabled", group="2. Moving Average 2")
type2      = input.string("SMA", "Type", options=["SMA", "EMA"], group="2. Moving Average 2")
source2    = input.source(close, "Source", group="2. Moving Average 2")
smart2     = input.bool(true, "Smart adjust", group="2. Moving Average 2")
timeframe2 = input.timeframe("5", "Reference timeframe", group="2. Moving Average 2")
period2    = input.int(780, "Period", minval=1, group="2. Moving Average 2")
color2     = input.color(color.rgb(147, 112, 219), "Color", group="2. Moving Average 2")
width2     = input.int(2, "Width", minval=1, maxval=10, group="2. Moving Average 2")
style2     = input.string("Solid", "Style", options=["Solid", "Dashed", "Dotted"], group="2. Moving Average 2")
 
// Moving Average 3
enabled3   = input.bool(false, "Enabled", group="3. Moving Average 3")
type3      = input.string("SMA", "Type", options=["SMA", "EMA"], group="3. Moving Average 3")
source3    = input.source(close, "Source", group="3. Moving Average 3")
smart3     = input.bool(true, "Smart adjust", group="3. Moving Average 3")
timeframe3 = input.timeframe("5", "Reference timeframe", group="3. Moving Average 3")
period3    = input.int(1560, "Period", minval=1, group="3. Moving Average 3")
color3     = input.color(color.rgb(255, 140, 0), "Color", group="3. Moving Average 3")
width3     = input.int(2, "Width", minval=1, maxval=10, group="3. Moving Average 3")
style3     = input.string("Solid", "Style", options=["Solid", "Dashed", "Dotted"], group="3. Moving Average 3")
 
// Moving Average 4
enabled4   = input.bool(false, "Enabled", group="4. Moving Average 4")
type4      = input.string("SMA", "Type", options=["SMA", "EMA"], group="4. Moving Average 4")
source4    = input.source(close, "Source", group="4. Moving Average 4")
smart4     = input.bool(true, "Smart adjust", group="4. Moving Average 4")
timeframe4 = input.timeframe("5", "Reference timeframe", group="4. Moving Average 4")
period4    = input.int(3120, "Period", minval=1, group="4. Moving Average 4")
color4     = input.color(color.rgb(105, 105, 105), "Color", group="4. Moving Average 4")
width4     = input.int(2, "Width", minval=1, maxval=10, group="4. Moving Average 4")
style4     = input.string("Solid", "Style", options=["Solid", "Dashed", "Dotted"], group="4. Moving Average 4")
 
// ── Effective (smart-adjusted) periods ──────────────────────────────────
chartSeconds      = timeframe.in_seconds()
referenceSeconds1 = timeframe.in_seconds(timeframe1)
referenceSeconds2 = timeframe.in_seconds(timeframe2)
referenceSeconds3 = timeframe.in_seconds(timeframe3)
referenceSeconds4 = timeframe.in_seconds(timeframe4)
 
canSmart1 = smart1 and not na(chartSeconds) and not na(referenceSeconds1) and chartSeconds > 0 and referenceSeconds1 > 0
canSmart2 = smart2 and not na(chartSeconds) and not na(referenceSeconds2) and chartSeconds > 0 and referenceSeconds2 > 0
canSmart3 = smart3 and not na(chartSeconds) and not na(referenceSeconds3) and chartSeconds > 0 and referenceSeconds3 > 0
canSmart4 = smart4 and not na(chartSeconds) and not na(referenceSeconds4) and chartSeconds > 0 and referenceSeconds4 > 0
 
effective1 = canSmart1 ? math.max(1, int(math.round(period1 * referenceSeconds1 / chartSeconds))) : period1
effective2 = canSmart2 ? math.max(1, int(math.round(period2 * referenceSeconds2 / chartSeconds))) : period2
effective3 = canSmart3 ? math.max(1, int(math.round(period3 * referenceSeconds3 / chartSeconds))) : period3
effective4 = canSmart4 ? math.max(1, int(math.round(period4 * referenceSeconds4 / chartSeconds))) : period4
 
// ── Error detection (BEFORE any ta.* call sees the raw period) ──────────
blocked1 = enabled1 and effective1 > MAX_PERIOD
blocked2 = enabled2 and effective2 > MAX_PERIOD
blocked3 = enabled3 and effective3 > MAX_PERIOD
blocked4 = enabled4 and effective4 > MAX_PERIOD
anyBlocked = blocked1 or blocked2 or blocked3 or blocked4
 
barsAvailable = last_bar_index + 1
lowHist1 = enabled1 and not blocked1 and effective1 > barsAvailable
lowHist2 = enabled2 and not blocked2 and effective2 > barsAvailable
lowHist3 = enabled3 and not blocked3 and effective3 > barsAvailable
lowHist4 = enabled4 and not blocked4 and effective4 > barsAvailable
anyLowHist = warnOnLowHistory and (lowHist1 or lowHist2 or lowHist3 or lowHist4)
 
// ── Calculate with CLAMPED periods so ta.sma/ta.ema can never error ────
ma1 = movingAverage(source1, type1, math.min(effective1, MAX_PERIOD))
ma2 = movingAverage(source2, type2, math.min(effective2, MAX_PERIOD))
ma3 = movingAverage(source3, type3, math.min(effective3, MAX_PERIOD))
ma4 = movingAverage(source4, type4, math.min(effective4, MAX_PERIOD))
 
lineStyle1 = style1 == "Dashed" ? plot.linestyle_dashed : style1 == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid
lineStyle2 = style2 == "Dashed" ? plot.linestyle_dashed : style2 == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid
lineStyle3 = style3 == "Dashed" ? plot.linestyle_dashed : style3 == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid
lineStyle4 = style4 == "Dashed" ? plot.linestyle_dashed : style4 == "Dotted" ? plot.linestyle_dotted : plot.linestyle_solid
 
suppressAll = hideAllOnError and anyBlocked
show1 = enabled1 and not blocked1 and not suppressAll
show2 = enabled2 and not blocked2 and not suppressAll
show3 = enabled3 and not blocked3 and not suppressAll
show4 = enabled4 and not blocked4 and not suppressAll
 
// ── Cloud + regime selection (calculated BEFORE plotting) ───────────────
cloudValueA = cloudMA_A == "MA 2" ? ma2 : cloudMA_A == "MA 3" ? ma3 : cloudMA_A == "MA 4" ? ma4 : ma1
cloudValueB = cloudMA_B == "MA 2" ? ma2 : cloudMA_B == "MA 3" ? ma3 : cloudMA_B == "MA 4" ? ma4 : ma1
cloudShowA  = cloudMA_A == "MA 2" ? show2 : cloudMA_A == "MA 3" ? show3 : cloudMA_A == "MA 4" ? show4 : show1
cloudShowB  = cloudMA_B == "MA 2" ? show2 : cloudMA_B == "MA 3" ? show3 : cloudMA_B == "MA 4" ? show4 : show1
 
cloudActive = showCloud and cloudMA_A != cloudMA_B and cloudShowA and cloudShowB
 
cloudMidDenom = (math.abs(cloudValueA) + math.abs(cloudValueB)) / 2
cloudDiffPct  = cloudMidDenom > 0 ? math.abs(cloudValueA - cloudValueB) / cloudMidDenom * 100 : 0.0
 
// One regime color drives BOTH the cloud fill and (optionally) the MA lines.
color regimeColor = na(cloudValueA) or na(cloudValueB) ? na :
     cloudDiffPct <= neutralTolerance ? neutralColor :
     cloudValueA > cloudValueB ? bullishColor : bearishColor
 
color cloudColor = cloudActive ? regimeColor : na
 
// Solid version of the regime color for the lines (cloud colors are ~82% transparent).
color regimeLineColor = na(regimeColor) ? na : color.new(regimeColor, 0)
 
bool regimeValid = useRegimeMAColors and cloudMA_A != cloudMA_B and cloudShowA and cloudShowB and not na(regimeLineColor)
 
bool isCloudPair1 = cloudMA_A == "MA 1" or cloudMA_B == "MA 1"
bool isCloudPair2 = cloudMA_A == "MA 2" or cloudMA_B == "MA 2"
bool isCloudPair3 = cloudMA_A == "MA 3" or cloudMA_B == "MA 3"
bool isCloudPair4 = cloudMA_A == "MA 4" or cloudMA_B == "MA 4"
 
color plotColor1 = regimeValid and (regimeColorAllMAs or isCloudPair1) ? regimeLineColor : color1
color plotColor2 = regimeValid and (regimeColorAllMAs or isCloudPair2) ? regimeLineColor : color2
color plotColor3 = regimeValid and (regimeColorAllMAs or isCloudPair3) ? regimeLineColor : color3
color plotColor4 = regimeValid and (regimeColorAllMAs or isCloudPair4) ? regimeLineColor : color4
 
// ── Plots ────────────────────────────────────────────────────────────────
plot(show1 ? ma1 : na, "MA 1", color=plotColor1, linewidth=width1, linestyle=lineStyle1)
plot(show2 ? ma2 : na, "MA 2", color=plotColor2, linewidth=width2, linestyle=lineStyle2)
plot(show3 ? ma3 : na, "MA 3", color=plotColor3, linewidth=width3, linestyle=lineStyle3)
plot(show4 ? ma4 : na, "MA 4", color=plotColor4, linewidth=width4, linestyle=lineStyle4)
 
cloudPlotA = plot(cloudActive ? cloudValueA : na, "Cloud A", color=color.new(color.white, 100), editable=false)
cloudPlotB = plot(cloudActive ? cloudValueB : na, "Cloud B", color=color.new(color.white, 100), editable=false)
fill(cloudPlotA, cloudPlotB, color=cloudColor, title="MA Cloud")
 
// ── Warning table ────────────────────────────────────────────────────────
warningTextSize = warningSize == "Small" ? size.small : warningSize == "Large" ? size.large : size.normal
 
string warningText = ""
if blocked1
    warningText += "• MA 1 hidden: needs " + str.tostring(effective1) + " bars (Pine limit: " + str.tostring(MAX_PERIOD) + ")\n"
if blocked2
    warningText += "• MA 2 hidden: needs " + str.tostring(effective2) + " bars (Pine limit: " + str.tostring(MAX_PERIOD) + ")\n"
if blocked3
    warningText += "• MA 3 hidden: needs " + str.tostring(effective3) + " bars (Pine limit: " + str.tostring(MAX_PERIOD) + ")\n"
if blocked4
    warningText += "• MA 4 hidden: needs " + str.tostring(effective4) + " bars (Pine limit: " + str.tostring(MAX_PERIOD) + ")\n"
if anyLowHist
    if lowHist1
        warningText += "• MA 1: needs " + str.tostring(effective1) + " bars, only " + str.tostring(barsAvailable) + " loaded\n"
    if lowHist2
        warningText += "• MA 2: needs " + str.tostring(effective2) + " bars, only " + str.tostring(barsAvailable) + " loaded\n"
    if lowHist3
        warningText += "• MA 3: needs " + str.tostring(effective3) + " bars, only " + str.tostring(barsAvailable) + " loaded\n"
    if lowHist4
        warningText += "• MA 4: needs " + str.tostring(effective4) + " bars, only " + str.tostring(barsAvailable) + " loaded\n"
 
if warningText != ""
    warningText := "⚠ Pono Smart MAs — not enough data on the " + timeframe.period + " chart\n" + warningText +
         "Fix: use a HIGHER chart timeframe, or lower the Period / Reference timeframe."
 
var table warningTable = table.new(position.top_right, 1, 1)
 
if barstate.islast
    if showWarning and warningText != ""
        table.cell(warningTable, 0, 0, warningText, bgcolor=color.rgb(178, 34, 34), text_color=color.white, text_size=warningTextSize, text_halign=text.align_left)
    else
        table.clear(warningTable, 0, 0, 0, 0)
 
// ── PonoTrading watermark ────────────────────────────────────────────────
var table watermark = table.new(position.bottom_center, 1, 1, bgcolor = color.new(color.black, 100), frame_width = 0, border_width = 0)
if barstate.islast
    table.cell(watermark, 0, 0, 'PonoTrading', text_color = color.new(GOLDENROD, 50), text_size = size.small)

← 返回列表