### ---- Author: Logan Crist
### ---- BaconTrading Codes
### ---- Free Version 1.0
### ---- Release Date: 08/07/2017
### ---- Visual Normal Distribution
#Hint DataType: <li><b>ChartSymbol</b> automatically normalize and align the chart data with the distribution curve. <li><b>AdvanceDecline</b> automatically normalize and align the Advance Decline & Unchanged inputs and calculates the normal distribution.
declare lower;
### Inputs ###
Input DataType = {Default AdvanceDecline, ChartSymbol};
Input Advance = "$ADVN";
Input Decline = "$DECN";
Input Unchanged = "$UNCN";
### Main Data References ###
def ADV = close(Advance);
def DEC = close(Decline);
def UNC = close(Unchanged);
def C = Close;
### Variables ###
plot UpperDistribution;
plot LowerDistribution;
plot Upper;
plot Lower;
Plot Data;
plot One;
plot Zero;
plot NegOne;
Def Mean;
def Distribution;
def CurrentBarNumber;
def CountUp;
### Definitions ###
Zero = 0;
One = 1;
NegOne =-1;
CurrentBarNumber = HighestAll(if IsNaN(c[-1]) and !IsNaN(c) then BarNumber() else Double.NaN);
CountUp = if !IsNaN(c) then 0 else CountUp[1] + 1;
Data = if DataType == DataType.ChartSymbol then ((C - HighestAll(C)) + (C - LowestAll(C)))/(HighestAll(C) - LowestAll(C)) else (ADV - DEC) / (ADV + DEC + UNC);
### Calculations ###
Mean = GetValue(TotalSum(Data) / CurrentBarNumber, -(CurrentBarNumber - BarNumber()));
def StDev = Sqrt(GetValue(TotalSum(Sqr(Data - Mean)), -(CurrentBarNumber - BarNumber())) / CurrentBarNumber);
Distribution = if CountUp > 101 then Double.NaN else (fold d = 0 to CurrentBarNumber with dSum do
dSum + if GetValue(AbsValue(Data + Mean), d + CountUp) <= ((100 - CountUp) / 100) then 1 else 0) / (CurrentBarNumber);
### Display Flow Control ###
if !isnan(C)
{
UpperDistribution = Double.nan;
LowerDistribution = Double.nan;
Upper = + StDev;
Lower = - StDev;
} else
{
UpperDistribution = ( + Distribution);
LowerDistribution = ( - Distribution);
Upper = if (( + StDev) - UpperDistribution) > 0
then UpperDistribution
else
if CountUp >= 100
then Double.NaN
else + StDev;
Lower = if ((- StDev) - LowerDistribution) < 0
then LowerDistribution
else
if CountUp >= 100
then Double.NaN
else - StDev;
}
#### Labels and Vertical Lines ####
AddLabel(yes," Visual Normal Distribution -- BaconTrading ",Color.Gray);
AddCloud(Upper, Lower, CreateColor(150, 150, 250));
AddVerticalLine(CurrentBarNumber == BarNumber(), Color = Color.GRAY);
AddVerticalLine((100 - CountUp) == 0, Color = Color.GRAY);
### Graphic Settings ###
Zero.SetStyle(Curve.LONG_DASH);
Zero.SetDefaultColor(Color.GRAY);
One.SetStyle(Curve.LONG_DASH);
One.SetDefaultColor(Color.GRAY);
NegOne.SetStyle(Curve.LONG_DASH);
NegOne.SetDefaultColor(Color.GRAY);
Data.SetPaintingStrategy(PaintingStrategy.POINTS);
Data.SetDefaultColor(Color.GRAY);
Upper.SetDefaultColor(Color.WHITE);
Lower.SetDefaultColor(Color.WHITE);
UpperDistribution.SetDefaultColor(Color.WHITE);
LowerDistribution.SetDefaultColor(Color.WHITE);
Zero.HideBubble();
Data.HideTitle();
One.HideTitle();
NegOne.HideTitle();
Zero.HideTitle();
Upper.HideBubble();
Lower.HideBubble();
UpperDistribution.HideBubble();
LowerDistribution.HideBubble();
Upper.HideTitle();
Lower.HideTitle();
UpperDistribution.HideTitle();
LowerDistribution.HideTitle();
### ---- BaconTrading Codes
### ---- Free Version 1.0
### ---- Release Date: 08/07/2017
### ---- Visual Normal Distribution
#Hint DataType: <li><b>ChartSymbol</b> automatically normalize and align the chart data with the distribution curve. <li><b>AdvanceDecline</b> automatically normalize and align the Advance Decline & Unchanged inputs and calculates the normal distribution.
declare lower;
### Inputs ###
Input DataType = {Default AdvanceDecline, ChartSymbol};
Input Advance = "$ADVN";
Input Decline = "$DECN";
Input Unchanged = "$UNCN";
### Main Data References ###
def ADV = close(Advance);
def DEC = close(Decline);
def UNC = close(Unchanged);
def C = Close;
### Variables ###
plot UpperDistribution;
plot LowerDistribution;
plot Upper;
plot Lower;
Plot Data;
plot One;
plot Zero;
plot NegOne;
Def Mean;
def Distribution;
def CurrentBarNumber;
def CountUp;
### Definitions ###
Zero = 0;
One = 1;
NegOne =-1;
CurrentBarNumber = HighestAll(if IsNaN(c[-1]) and !IsNaN(c) then BarNumber() else Double.NaN);
CountUp = if !IsNaN(c) then 0 else CountUp[1] + 1;
Data = if DataType == DataType.ChartSymbol then ((C - HighestAll(C)) + (C - LowestAll(C)))/(HighestAll(C) - LowestAll(C)) else (ADV - DEC) / (ADV + DEC + UNC);
### Calculations ###
Mean = GetValue(TotalSum(Data) / CurrentBarNumber, -(CurrentBarNumber - BarNumber()));
def StDev = Sqrt(GetValue(TotalSum(Sqr(Data - Mean)), -(CurrentBarNumber - BarNumber())) / CurrentBarNumber);
Distribution = if CountUp > 101 then Double.NaN else (fold d = 0 to CurrentBarNumber with dSum do
dSum + if GetValue(AbsValue(Data + Mean), d + CountUp) <= ((100 - CountUp) / 100) then 1 else 0) / (CurrentBarNumber);
### Display Flow Control ###
if !isnan(C)
{
UpperDistribution = Double.nan;
LowerDistribution = Double.nan;
Upper = + StDev;
Lower = - StDev;
} else
{
UpperDistribution = ( + Distribution);
LowerDistribution = ( - Distribution);
Upper = if (( + StDev) - UpperDistribution) > 0
then UpperDistribution
else
if CountUp >= 100
then Double.NaN
else + StDev;
Lower = if ((- StDev) - LowerDistribution) < 0
then LowerDistribution
else
if CountUp >= 100
then Double.NaN
else - StDev;
}
#### Labels and Vertical Lines ####
AddLabel(yes," Visual Normal Distribution -- BaconTrading ",Color.Gray);
AddCloud(Upper, Lower, CreateColor(150, 150, 250));
AddVerticalLine(CurrentBarNumber == BarNumber(), Color = Color.GRAY);
AddVerticalLine((100 - CountUp) == 0, Color = Color.GRAY);
### Graphic Settings ###
Zero.SetStyle(Curve.LONG_DASH);
Zero.SetDefaultColor(Color.GRAY);
One.SetStyle(Curve.LONG_DASH);
One.SetDefaultColor(Color.GRAY);
NegOne.SetStyle(Curve.LONG_DASH);
NegOne.SetDefaultColor(Color.GRAY);
Data.SetPaintingStrategy(PaintingStrategy.POINTS);
Data.SetDefaultColor(Color.GRAY);
Upper.SetDefaultColor(Color.WHITE);
Lower.SetDefaultColor(Color.WHITE);
UpperDistribution.SetDefaultColor(Color.WHITE);
LowerDistribution.SetDefaultColor(Color.WHITE);
Zero.HideBubble();
Data.HideTitle();
One.HideTitle();
NegOne.HideTitle();
Zero.HideTitle();
Upper.HideBubble();
Lower.HideBubble();
UpperDistribution.HideBubble();
LowerDistribution.HideBubble();
Upper.HideTitle();
Lower.HideTitle();
UpperDistribution.HideTitle();
LowerDistribution.HideTitle();
Comments
Post a Comment