Dynamic Money Flow Index [SMRT Algo]

SMRTAlgo · study · 119 行 · 点赞 97 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © SMRTAlgo
 
 
 //@version=6
indicator("Dynamic Money Flow Index [SMRT Algo]", shorttitle="DMFI [SMRT Algo]", overlay=false)
 
//────────────────────────────────────────────
// INPUT GROUPS
//────────────────────────────────────────────
const string GROUP_CALC   = "01. Calculation"
const string GROUP_LEVELS = "02. Levels"
const string GROUP_SMOOTH = "03. Smoothing"
const string GROUP_STYLE  = "04. Appearance"
 
//────────────────────────────────────────────
// CALCULATION INPUTS
//────────────────────────────────────────────
int mfiLength = input.int(30, "MFI Length", minval=1, group=GROUP_CALC)
 
//────────────────────────────────────────────
// SMOOTHING INPUTS
//────────────────────────────────────────────
int smoothingLength = input.int(4, "MFI Smoothing", minval=1, group=GROUP_SMOOTH)
int trendSmoothingLength = input.int(14, "Trend Smoothing", minval=1, group=GROUP_SMOOTH)
 
//────────────────────────────────────────────
// LEVEL INPUTS
//────────────────────────────────────────────
int oversoldLevel = input.int(35, "Oversold Level", minval=1, maxval=49, group=GROUP_LEVELS)
int overboughtLevel = input.int(65, "Overbought Level", minval=51, maxval=99, group=GROUP_LEVELS)
 
//────────────────────────────────────────────
// APPEARANCE INPUTS
//────────────────────────────────────────────
color bullishColor = input.color(#0DF1C6, "Bullish Color", group=GROUP_STYLE)
color bearishColor = input.color(#E91E63, "Bearish Color", group=GROUP_STYLE)
color neutralColor = input.color(color.gray, "Neutral Color", group=GROUP_STYLE)
 
bool showSignals = input.bool(true, "Show Reversal Signals", group=GROUP_STYLE)
bool showLevelsFill = input.bool(true, "Show Extreme Zone Background", group=GROUP_STYLE)
bool showTrendFill = input.bool(true, "Show Dynamic Trend Fill", group=GROUP_STYLE)
 
//────────────────────────────────────────────
// MONEY FLOW CALCULATION
//────────────────────────────────────────────
float rawMoneyFlow = close * volume
float positiveMoneyFlow = ta.change(hlc3) > 0 ? rawMoneyFlow : 0.0
float negativeMoneyFlow = ta.change(hlc3) < 0 ? rawMoneyFlow : 0.0
 
float averagePositiveFlow = ta.sma(positiveMoneyFlow, mfiLength)
float averageNegativeFlow = ta.sma(negativeMoneyFlow, mfiLength)
 
float moneyFlowRatio = averagePositiveFlow / averageNegativeFlow
float rawMFI = 100.0 * moneyFlowRatio / (1.0 + moneyFlowRatio)
 
//────────────────────────────────────────────
// SMOOTHING
//────────────────────────────────────────────
float smoothedMFI = ta.sma(rawMFI, smoothingLength)
float dynamicMFI = smoothedMFI
float smoothedTrend = ta.sma(dynamicMFI, trendSmoothingLength)
 
//────────────────────────────────────────────
// DYNAMIC COLOR FUNCTION
//────────────────────────────────────────────
getTrendColor(float value) =>
    value >= overboughtLevel ? bearishColor :
     value <= oversoldLevel ? bullishColor :
     value > 50 ? color.from_gradient(value, 50, overboughtLevel, chart.bg_color, bearishColor) :
     color.from_gradient(value, oversoldLevel, 50, bullishColor, chart.bg_color)
 
//────────────────────────────────────────────
// PLOT COLORS
//────────────────────────────────────────────
color oscillatorColor = getTrendColor(dynamicMFI)
 
color gradientColor = color.from_gradient(dynamicMFI, oversoldLevel, overboughtLevel, bullishColor, bearishColor)
 
//────────────────────────────────────────────
// REVERSAL SIGNALS
// Original conditions preserved
//────────────────────────────────────────────
bool bullishReversal = oversoldLevel > dynamicMFI and oversoldLevel[1] <= dynamicMFI[1] and oversoldLevel[2] <= dynamicMFI[2]
bool bearishReversal = overboughtLevel < dynamicMFI and overboughtLevel[1] >= dynamicMFI[1] and overboughtLevel[2] >= dynamicMFI[2]
 
//────────────────────────────────────────────
// OSCILLATOR PLOTS
//────────────────────────────────────────────
oscillatorPlot = plot(dynamicMFI, title="Dynamic MFI", color=oscillatorColor, linewidth=2)
centerlinePlot = plot(50, title="Dynamic Centerline", color=gradientColor, linewidth=2)
 
// hline() requires input-compatible levels.
// Keep the original adjustable inputs for these levels.
overboughtPlot = hline(65, "Overbought", color=color.gray)
oversoldPlot = hline(35, "Oversold", color=color.gray)
 
//────────────────────────────────────────────
// EXTREME ZONE BACKGROUND
//────────────────────────────────────────────
fill(overboughtPlot, oversoldPlot, color=showLevelsFill ? color.new(neutralColor, 90) : na, title="Neutral Zone Background")
 
//────────────────────────────────────────────
// DYNAMIC TREND FILL
//────────────────────────────────────────────
fill(oscillatorPlot, centerlinePlot, dynamicMFI, 50, chart.bg_color, dynamicMFI > 50 ? color.new(bearishColor, 0) : color.new(bullishColor, 0), title="Dynamic Trend Fill", editable=false, fillgaps=true)
 
barcolor(getTrendColor(dynamicMFI))
//────────────────────────────────────────────
// REVERSAL MARKERS
//────────────────────────────────────────────
plotshape(showSignals and bullishReversal, title="Bullish Reversal", style=shape.triangleup, location=location.bottom, color=bullishColor, size=size.tiny)
plotshape(showSignals and bearishReversal, title="Bearish Reversal", style=shape.triangledown, location=location.top, color=bearishColor, size=size.tiny)
 
//────────────────────────────────────────────
// ALERT CONDITIONS
//────────────────────────────────────────────
alertcondition(bullishReversal, title="DMFI Bullish Reversal", message="DMFI Bullish Reversal | {{ticker}} | TF: {{interval}} | Price: {{close}}")
alertcondition(bearishReversal, title="DMFI Bearish Reversal", message="DMFI Bearish Reversal | {{ticker}} | TF: {{interval}} | Price: {{close}}")

← 返回列表