#COG_inertia
#
# [email protected]
# April 5 2014, find mid point (COG) using first order linear regression and slope
# removed repainting
# Modified code from;
# Hurst_Channels
# www.trendxplorer.info
# [email protected]
# Build: July 25, 2012
# Rev 1: July 27, 2012: ExtrapolatedMA based on Kirills code
# Rev 2: July 29, 2012: FlowPrice value for better extremes
# Rev 3: Sept 9, 2012: Coloring for ExtrapolatedCMA added
#
#
# --- script begin ----
#
declare upper;
input price = ohlc4;
input length = 20;
input InnerValue = 1.618;
input OuterValue = 2.618;
input ExtremeValue = 4.236;
input OuterExtremeValue = 6.854;
input showFlowPrice = NO;
input showPriceBar = YES;
input smooth = 1;
def dPrice = if !IsNaN(price) then Inertia(price, length) else Inertia(dPrice[1], length - 1);
rec LRS = 6 * ( WMA(dPrice, length) - Average(dPrice, length) ) / (length - 1);
rec CMA = if !IsNaN(dPrice) then Inertia(dPrice, length) - LRS * (length / 2) else Inertia(CMA[1], length - 1) - LRS * (length / 2);
plot CenteredMA = if !IsNaN(dPrice) then CMA else Double.NaN;
CenteredMA.SetDefaultColor(GetColor(1));
CenteredMA.SetLineWeight(2);
def sterr = if !IsNaN(dprice - CMA) then StDev (dprice - CMA , 4 * length) else sterr[1];
;
def ExtremeBand = sterr * ExtremeValue;
def OuterBand = sterr * OuterValue;
def InnerBand = sterr * InnerValue;
def OuterExtremeBand = sterr * OuterExtremeValue;
plot UpperOuterExtremeBand = if !IsNaN(price) then CMA + OuterExtremeBand else Double.NaN;
plot LowerOuterExtremeBand = if !IsNaN(price) then CMA - OuterExtremeBand else Double.NaN;
plot UpperExtremeBand = if !IsNaN(price) then CMA + ExtremeBand else Double.NaN;
plot LowerExtremeBand = if !IsNaN(price) then CMA - ExtremeBand else Double.NaN;
plot UpperOuterBand = if !IsNaN(price) then CMA + OuterBand else Double.NaN;
plot LowerOuterBand = if !IsNaN(price) then CMA - OuterBand else Double.NaN;
plot UpperInnerBand = if !IsNaN(price) then CMA + InnerBand else Double.NaN;
plot LowerInnerBand = if !IsNaN(price) then CMA - InnerBand else Double.NaN;
UpperOuterExtremeBand.SetDefaultColor(GetColor(4));
UpperOuterExtremeBand.SetLineWeight(1);
UpperOuterExtremeBand.SetStyle(Curve.SHORT_DASH);
UpperExtremeBand.SetDefaultColor(GetColor(4));
UpperExtremeBand.SetLineWeight(1);
UpperExtremeBand.SetStyle(Curve.SHORT_DASH);
LowerOuterExtremeBand.SetDefaultColor(GetColor(4));
LowerOuterExtremeBand.SetLineWeight(1);
LowerOuterExtremeBand.SetStyle(Curve.SHORT_DASH);
LowerExtremeBand.SetDefaultColor(GetColor(4));
LowerExtremeBand.SetLineWeight(1);
LowerExtremeBand.SetStyle(Curve.SHORT_DASH);
#UpperExtremeBand.hide();
#LowerExtremeBand.hide();
#UpperOuterExtremeBand.hide();
#LowerOuterExtremeBand.hide();
UpperOuterBand.SetDefaultColor(GetColor(5));
UpperOuterBand.SetLineWeight(2);
LowerOuterBand.SetDefaultColor(GetColor(6));
LowerOuterBand.SetLineWeight(2);
UpperInnerBand.SetDefaultColor(GetColor(5));
UpperInnerBand.SetLineWeight(1);
UpperInnerBand.SetStyle(Curve.SHORT_DASH);
LowerInnerBand.SetDefaultColor(GetColor(6));
LowerInnerBand.SetLineWeight(1);
LowerInnerBand.SetStyle(Curve.SHORT_DASH);
# Turn AddClouds off by putting a #-sign at the first position of the lines
AddCloud(UpperOuterBand, UpperInnerBand, Color.RED);
AddCloud(LowerInnerBand, LowerOuterBand, Color.GREEN);
#Rev 2:
#def FlowValue = if close > close[1] then high else if close < close[1] then low else (high + low)/2;
def FlowValue =
if high >= high[1] and low <= low[1]
then
if close >= close[1] #or high >= high[2]
then high
else low
else
if high > high[1]
then high
else
if low < low[1]
then low
else
if close > close[1]
then high
else
if close < close[1]
then low
else (high + low) / 2;
plot FlowPrice = if showFlowPrice then Average(FlowValue, smooth) else Double.NaN;
FlowPrice.SetDefaultColor(GetColor(9));
FlowPrice.SetLineWeight(2);
HidePricePlot(!showPriceBar);
#
# --- script end ----