# // ==========================
#
# Technical Analysis of Stocks & Commodities
# August, 2008
# Premier Stochastic Oscillator by Lee Leibfarth
# pp 30 - 36
# ported by R Houser
# Changed to histogram display by Horserider 10/11/219
declare lower;
# inputs:
input Line1 = 0.9;
input Line2 = 0.2;
input StochLength = 8;
input Period = 25;
input agg = AggregationPeriod.HOUR;
input AggregationPeriod1 = {"MONTH", "WEEK", "DAY", "4 HOURS", "1 HOUR", "30 MIN", Default "15 MIN","10 MIN", "5 MIN", "3 MIN"};
input AggregationPeriod2 = {"MONTH", "WEEK", "DAY", "4 HOURS", "1 HOUR", "30 MIN", Default "15 MIN","10 MIN", "5 MIN", "3 MIN"};
# variables:
# oFastK(0)= Average((close(period = AggregationPeriod1) - Lowest(low(period = AggregationPeriod1), KPeriod1)) / (Highest(high(period = AggregationPeriod1), KPeriod1) - Lowest(low(period = AggregationPeriod1), KPeriod1)) * 100, slowing_period1);
# oFastD(0)= Average(Average((close(period = AggregationPeriod1) - Lowest(low(period = AggregationPeriod1), DPeriod1)) / (Highest(high(period = AggregationPeriod1), DPeriod1) - Lowest(low(period = AggregationPeriod1), DPeriod1)) * 100, slowing_period1), DPeriod1);
# oSlowK(0)= Average((close(period = AggregationPeriod2) - Lowest(low(period = AggregationPeriod2), KPeriod2)) / (Highest(high(period = AggregationPeriod2), KPeriod2) - Lowest(low(period = AggregationPeriod2), KPeriod2)) * 100, slowing_period2);
# oSlowD(0)= Average(Average((close(period = AggregationPeriod2) - Lowest(low(period = AggregationPeriod2), DPeriod2)) / (Highest(high(period = AggregationPeriod2), DPeriod2) - Lowest(low(period = AggregationPeriod2), DPeriod2)) * 100, slowing_period2), DPeriod2);
# Length(0)
# NormStoch(0)
# SmoothStoch(0)
#Premier(0);
# Value1 = Stochastic( h, l, c, StochLength, 1, 3, 1, oFastK, oFastD, oSlowK, oSlowD);
def oFastK = 100 * ( ( close - Lowest( low, StochLength ) ) / ( Highest( high, StochLength ) - Lowest( low, StochLength ) ) );
def oFastD = Average( oFastK, 1 );
def oSlowK = oFastD;
def oSlowD = Average( oSlowK, 3 );
# Length = iff(Period < 0, 1, squareroot(Period));
def Length = if Period < 0 then 1 else Sqrt( Period );
# NormStoch = .1 * (oslowK - 50);
def NormStoch = 0.1 * ( oSlowK - 50 );
# SmoothStoch = xaverage(xaverage(NormStoch, Length), Length);
def SmoothStoch = ExpAverage( ExpAverage( NormStoch, Length ), Length );
plot Premier = ( Exp( 1 * SmoothStoch ) - 1 ) / ( Exp( 1 * SmoothStoch ) + 1 );
plot pLine1 = Line1;
pLine1.SetDefaultColor( Color.BLACK );
plot pLine2 = Line2;
pLine2.SetDefaultColor( Color.GRAY );
plot nLine1 = -1 * Line1;
nLine1.SetDefaultColor( Color.BLACK );
plot nLine2 = -1 * Line2;
nLine2.SetDefaultColor( Color.GRAY );
#Premier.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Premier.SetLineWeight(3);
Premier.DefineColor("Positive and Up", Color.GREEN);
Premier.DefineColor("Positive and Down", Color.DARK_GREEN);
Premier.DefineColor("Negative and Down", Color.RED);
Premier.DefineColor("Negative and Up", Color.DARK_RED);
Premier.AssignValueColor(if Premier >= 0 then if Premier >Premier[1] then Premier.color("Positive and Up") else Premier.color("Positive and Down") else if Premier < Premier[1] then Premier.color("Negative and Down") else Premier.color("Negative and Up"));