declare upper;
input price = ohlc4;
input length = 20;
input InnerValue = 1.618;
input OuterValue = 2.618;
input ExtremeValue = 4.236;
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;
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;
UpperExtremeBand.SetDefaultColor(GetColor(4));
UpperExtremeBand.SetLineWeight(1);
UpperExtremeBand.SetStyle(Curve.SHORT_DASH);
LowerExtremeBand.SetDefaultColor(GetColor(4));
LowerExtremeBand.SetLineWeight(1);
LowerExtremeBand.SetStyle(Curve.SHORT_DASH);
#UpperExtremeBand.hide();
#LowerExtremeBand.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);