• Memorial Day Sale! Claim the biggest discount of the year with $50 off VIP using code MEM50. Sign up here.

Chaikin Money Flow Index Indicator

WayneG

New member
VIP
I'm modifying the standard Chaikin Money Flow Index according to it interpretation rules. The area between -0.05 to 0.05, the gray area, is considered to be a no mans land because of false signals. In the code I've written, see below, I am having trouble getting the red not to fill the gray to +0.5. I've tried several variation from different example, but I'm just no catching the hank of it.

Code:
declare lower;
input length = 21;
input upperLine = 1;
input lowerLine = -1;
input pgrayline = 0.05;
input ngrayline = -0.05;
def tmp_var =
if high == low then
  volume
else
  (close - low - (high - close)) / (high - low) * volume
;

def sum_close = Sum(tmp_var, length);
def total = Sum(volume, length);

plot CMF =
if total == 0 then
  0
else
  sum_close / total
;
CMF.SetDefaultColor(Color.BLACK);

plot ThirtyLine = .30;
ThirtyLine.SetDefaultColor(GetColor(5));

plot TwentyLine = -.20;
TwentyLine.SetDefaultColor(GetColor(5));

plot ngray = ngrayline;
ngray.SetDefaultColor(Color.GRAY);

plot pgray = pgrayline;
pgray.SetDefaultColor(Color.GRAY);


plot ZeroLine = 0;
ZeroLine.SetDefaultColor(GetColor(5));


def BullPrice = ChaikinMoneyFlow(21);

def BearPrice = -chaikinMoneyFlow(21);
#
#THE NEXT TO LINES OF CODE ARE PROBLEM
#
AddCloud(BullPrice, 0.05 ,color.green, Color.red);
#AddCloud(BearPrice, -0.05, Color.red, Color.red);
#
#
#

AddCloud(ngrayline, pgrayline, Color.GRAY, Color.GRAY);

AddLabel( 1, "Short < -0.05 Long > 0.05 Gray area Choppy", Color.BLACK);

#end code

Thanks for you help,
Wayne
 

horserider

Well-known member
VIP
Messy but may do.

Code:
plot ngray = .05;
ngray.SetDefaultColor(Color.GRAY);

plot pgray = -.05;
pgray.SetDefaultColor(Color.GRAY);

#
#THE NEXT TO LINES OF CODE ARE PROBLEM
#
AddCloud(cmf, ngray, color.green, Color.pink);
AddCloud(cmf, pgray, color.pink, Color.pink);
#
#

AddCloud(ngray, pgray, Color.DARK_RED, Color.DARK_RED);

AddLabel( 1, "Short < -0.05 Long > 0.05 Gray area Choppy", Color.BLACK);

#end code
 

DeusMecanicus

Active member
Try this:

Code:
#THE NEXT TO LINES OF CODE ARE PROBLEM

plot uppercmf = if cmf > .05 then cmf else double.nan;
uppercmf.hide();
plot lowercmf = if cmf < -.05 then cmf else double.nan;
lowercmf.hide();
AddCloud(uppercmf, pgray ,color.green, Color.green);
AddCloud(lowercmf, ngray, Color.red, Color.red);
#
#
#

AddCloud(ngrayline, pgrayline, Color.GRAY, Color.GRAY);

AddLabel( 1, "Short < -0.05 Long > 0.05 Gray area Choppy", Color.BLACK);

#end code

You lose some shading on the edges when it dips above/below the .05/-.05 levels.
 
ChaikinMoneyFlow is helpful in finding strength or weakness in stocks or sectors. The following link will give you a visual impression of that.

https://stockcharts.com/freecharts/...,XLK,XLI,XLB,XLE,XLP,XLV,XLU,XLF,XLRE|B|C20|1

Can you please help in building a watchlist in thinkorswim platform that will display the parameters helpful in identifying the trend (hypothetical example given below)

symbol CMF

AAPL 0.3 ( bullish 10 bars)
MSFT 0.2 ( bullish 8 bars)
AMD 0.8 (bullish 3 bars)
NVDA 0.9 (bullish 1 bar)
AMZN -0.9 (bearish 1 bar)

I have the following thinkscript designed to show the CMF value with RED colour for bearish and green colour for bullish signal. I need help in counting the "bars ago" or number of bars.

plot CMF = ChaikinMoneyFlow();
AssignBackgroundColor(if CMF > 0 then Color.DARK_GREEN else if CMF < 0 then Color.DARK_RED else Color.Dark_ORANGE);
 

Slippage

Well-known member
I left out the words bullish and bearish to conserve space since the colors and the CMF value already indicate the direction.

Ruby:
def CMF = ChaikinMoneyFlow();

def bullCount = if CMF > 0 then if IsNaN(bullCount[1]) then 1 else bullCount[1] + 1 else 0;
def bearCount = if CMF < 0 then if IsNaN(bearCount[1]) then 1 else bearCount[1] + 1 else 0;
def count = Max(bullCount, bearCount);

AddLabel(yes, Round(CMF, 1) + " (" + count + " bars)");

AssignBackgroundColor(
  if CMF > 0 then Color.DARK_GREEN
  else if CMF < 0 then Color.DARK_RED
  else Color.DARK_ORANGE
);
 

ThinkingVan

New member
VIP
Hey all, can someone make a label code for Chaikin Money Flow? I've tried, but failed. I'm not that good with coding.
Basic Label: If it passes zeroline going up, color.GREEN or UPTICK... If it passes zeroline going down, color.RED or UPTICK... Text- CMF: Numerical Value
Thanks in advance. I appreciate it!
 

SleepyZ

Well-known member
VIP
Lifetime
Hey all, can someone make a label code for Chaikin Money Flow? I've tried, but failed. I'm not that good with coding.
Basic Label: If it passes zeroline going up, color.GREEN or UPTICK... If it passes zeroline going down, color.RED or UPTICK... Text- CMF: Numerical Value
Thanks in advance. I appreciate it!
Here are 2 versions based upon your criteria. The first one will only show the color to green/red at the bar where the cross occurred, and white when there is not a cross. The second one will not only show the cross color, it will maintain that color until a cross in the opposite direction.

Add the label codes that you choose to the bottom of a copy of the TOS Chaikin Money Flow Indicator

Code:
#1 cross bar
addlabel(1,"CMF",if cmf crosses above 0 then color.green else if cmf crosses below 0 then color.red else color.white);
#2 aboe/below zero
addlabel(1,"CMF",if cmf > 0 then color.green else color.red);

Here is a stand alone version that you could use to display the labels in the upper price panel.

Ruby:
def cmf = reference chaikinMoneyFlow().cmf;

#1 cross bar
addlabel(1,"CMF",if cmf crosses above 0 then color.green else if cmf crosses below 0 then color.red else color.white);
#2 aboe/below zero
addlabel(1,"CMF",if cmf > 0 then color.green else color.red);
 

ThinkingVan

New member
VIP
Here are 2 versions based upon your criteria. The first one will only show the color to green/red at the bar where the cross occurred, and white when there is not a cross. The second one will not only show the cross color, it will maintain that color until a cross in the opposite direction.

Add the label codes that you choose to the bottom of a copy of the TOS Chaikin Money Flow Indicator



Here is a stand alone version that you could use to display the labels in the upper price panel.
Hey Sleepy
Thanks for taking time to develop the code. The stand-only works perfectly. The 1st one had some issues.

 

SleepyZ

Well-known member
VIP
Lifetime
Hey Sleepy
Thanks for taking time to develop the code. The stand-only works perfectly. The 1st one had some issues.


Both sets of code worked. The first set had to be added to the TOS version of Chaikin Money flow. See original post.

Here is the first code added to the TOS version

Ruby:
#
# TD Ameritrade IP Company, Inc. (c) 2007-2021
#

declare lower;
input length = 21;

def tmp_var =
if high == low then
  volume
else
  (close - low - (high - close)) / (high - low) * volume
;

def sum_close = sum(tmp_var, length);
def total = sum(volume, length);

plot CMF =
if total == 0 then
  0
else
  sum_close / total
;
CMF.SetDefaultColor(GetColor(1));

plot ZeroLine = 0;
ZeroLine.SetDefaultColor(GetColor(5));

#1 cross bar
addlabel(1,"CMF",if cmf crosses above 0 then color.green else if cmf crosses below 0 then color.red else color.white);
#2 aboe/below zero
addlabel(1,"CMF",if cmf > 0 then color.green else color.red);
 

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
Mobius version of the Chaiken Oscillator
# Chaikin Oscillator with signal lines
# Mobius
# V01.20138

declare lower;

input longLength = 10;
input shortLength = 3;

def h = high;
def l = low;
def c = close;
def v = volume;
def CLV = if h != l then ((c - l) - (h - c)) / (h - l) else 1;
def AccDist = totalSum(v * CLV);
plot COSC = ExpAverage(AccDist, shortLength) - ExpAverage(AccDist, longLength);
plot ZeroLine = 0;
plot avgLongCOSC = Average(COSC, longLength);
plot avgShortCOSC = Average(COSC, shortLength);
COSC.SetDefaultColor(GetColor(1));
ZeroLine.SetDefaultColor(GetColor(5));
plot ArrowUP = if(COSC < 0 and avgShortCOSC crosses above avgLongCOSC, avgShortCOSC, double.nan);
ArrowUp.SetPaintingStrategy(PaintingStrategy.Arrow_UP);
ArrowUP.SetLineWeight(3);
ArrowUP.SetDefaultColor(Color.Green);
plot ArrowDN = if(COSC > 0 and avgShortCOSC crosses below avgLongCOSC, avgShortCOSC, double.nan);
ArrowDN.SetPaintingStrategy(PaintingStrategy.Arrow_DOWN);
ArrowDN.SetLineWeight(3);
ArrowDN.SetDefaultColor(Color.RED);
AddCloud(avgLongCOSC, avgShortCOSC, color.red, color.green);
# End Code
 
Top