Files

43 lines
1.5 KiB
Plaintext
Raw Permalink Normal View History

// Licensed under the Apache License, Version 2.0
// © mihakralj
//@version=6
indicator("Chaikin Money Flow (CMF)", "CMF", overlay=false)
//@function Calculates the Chaikin Money Flow (CMF), measuring buying and selling pressure through price and volume
//@param len Lookback period length (default: 20)
//@param src_high The high price (default: built-in high)
//@param src_low The low price (default: built-in low)
//@param src_close The close price (default: built-in close)
//@param src_vol The volume (default: built-in volume)
//@returns float CMF value between -1 and 1
cmf(len = 20, src_high = high, src_low = low, src_close = close, src_vol = volume) =>
// Calculate Money Flow Multiplier
float mfm = 0.0
if not na(src_high) and not na(src_low) and not na(src_close)
mfm := (src_close - src_low) - (src_high - src_close)
mfm := src_high != src_low ? mfm / (src_high - src_low) : 0.0
// Calculate Money Flow Volume
float mfv = na(src_vol) ? 0.0 : mfm * src_vol
// Calculate CMF
var float sum_mfv = 0.0
var float sum_vol = 0.0
// Update rolling sums
sum_mfv := sum_mfv + mfv - (bar_index >= len ? mfv[len] : 0)
sum_vol := sum_vol + src_vol - (bar_index >= len ? src_vol[len] : 0)
// Calculate CMF value
float cmf_value = sum_vol != 0 ? sum_mfv / sum_vol : 0.0
cmf_value
// ---------- Inputs ----------
i_length = input.int(20, "Length", minval=1)
// ---------- Calculations ----------
cmf_val = cmf(i_length)
// ---------- Plotting ----------
plot(cmf_val, "CMF", color=color.yellow, linewidth=2)