strategy · own pane · open source
CMF Trend Strategy
Running…
Loading chart…
About
Chaikin Money Flow trend strategy: long when CMF (sum of MFM×volume / sum of volume) crosses above the threshold, exit on the reverse cross.
- CMF > 0 = net buying pressure; < 0 = net distribution.
- Combine with a price trend filter for confirmation.
- 20-period is the canonical CMF lookback.
Source code
//@version=5
strategy("CMF Trend Strategy", overlay=false, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.05)
len = input.int(20, "CMF Length", minval=5, maxval=50, step=1)
threshold = input.float(0, "CMF Threshold", minval=-0.5, maxval=0.5, step=0.05)
stopPct = input.float(2, "Stop Loss %", minval=0.5, maxval=10, step=0.1)
tpPct = input.float(6, "Take Profit %", minval=0, maxval=20, step=0.1)
mfm = ((close - low) - (high - close)) / (high - low)
mflow = mfm * volume
cmf = math.sum(mflow, len) / math.sum(volume, len)
longSignal = strategy.position_size == 0 and ta.crossover(cmf, threshold)
exitSignal = strategy.position_size > 0 and ta.crossunder(cmf, threshold)
if longSignal
strategy.entry("Long", strategy.long)
if strategy.position_size > 0
strategy.exit("Risk", stop=stopPct > 0 ? strategy.position_avg_price * (1 - stopPct / 100) : na, limit=tpPct > 0 ? strategy.position_avg_price * (1 + tpPct / 100) : na)
if exitSignal
strategy.close("Long", comment="CMF cross down")
hline(0, "Zero", color=color.gray, linestyle=hline.style_dotted)
p_CMF = plot(cmf, "CMF", color=color.orange, linewidth=2)
plotshape(longSignal, "Long", style=shape.triangleup, location=location.belowbar, color=color.green, text="L")
plotshape(exitSignal, "Exit", style=shape.triangledown, location=location.abovebar, color=color.red, text="X")
alertcondition(longSignal, "CMF long entry", "CMF crossed above threshold")
alertcondition(exitSignal, "CMF exit", "CMF crossed below threshold")
No comments yet.
Sign in to comment.