_SECTION_BEGIN( "Mittens" );
SetChartOptions( 2, chartWrapTitle );
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Functions - Perry Kaufman Adaptive Moving Average
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function KAMA( series, period )
{
fast = 2 / 3;
slow = 2 / 31;
efficiencyRatio = ( abs( series - Ref( series, -1 * period ) ) ) / ( Sum( abs( series - Ref( series, -1 ) ), period ) );
smoothingConstant = ( efficiencyRatio * ( fast - slow ) + slow ) ^ 2;
return AMA( series, smoothingConstant );
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// The price plot
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Plot( Close, "Close", colorBlack, styleNoTitle | GetPriceStyle(), Null, Null, Null );
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Pivots
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
pivotBars = 3;
start = Max( BarCount - 503, 5 );
pivotHigh = Null;
pivotLow = Null;
line = Null;
for ( barNumber = start;barNumber < BarCount - pivotBars;barNumber++ )
{
i = pivotBars;
lowCount = 0;
highCount = 0;
while ( i > 0 )
{
if ( Low[barNumber] <= Low[barNumber-i] && Low[barNumber] <= Low[barNumber+i] )
lowCount++;
if ( High[barNumber] >= High[barNumber-i] && High[barNumber] >= High[barNumber+i] )
highCount++;
i--;
}
if ( lowCount == pivotBars )
{
pivotLow[barNumber] = Low[barNumber];
line = LineArray( barNumber - pivotBars, pivotLow[barNumber], barNumber + pivotBars, pivotLow[barNumber], 0 );
Plot( line, "", colorDarkYellow, styleNoLabel | styleDashed | styleNoRescale, Null, Null, pivotBars, -3 );
}
else
pivotLow[barNumber] = pivotLow[barNumber-1];
if ( highCount == pivotBars )
{
pivotHigh[barNumber] = High[barNumber];
line = LineArray( barNumber - pivotBars, pivotHigh[barNumber], barNumber + pivotBars, pivotHigh[barNumber], 0 );
Plot( line, "", colorDarkYellow, styleNoLabel | styleDashed | styleNoRescale, Null, Null, pivotBars, -3 );
}
else
pivotHigh[barNumber] = pivotHigh[barNumber-1];
}
// Removing Inside pivots
for ( barNumber = start;barNumber < BarCount - pivotBars;barNumber++ )
{
if ( pivotHigh[barNumber] < pivotHigh[barNumber-1] )
{
for ( count = barNumber + 1;count < BarCount;count++ )
{
if ( High[count] < pivotLow[barNumber-1] )
count = BarCount;
else
pivotHigh[barNumber] = pivotHigh[barNumber-1];
}
}
if ( pivotLow[barNumber] > pivotLow[barNumber-1] )
{
for ( count = barNumber + 1;count < BarCount;count++ )
{
if ( Low[count] > pivotHigh[barNumber-1] )
count = BarCount;
else
pivotLow[barNumber] = pivotLow[barNumber-1];
}
}
}
pivotHigh = Ceil( Ref( pivotHigh, -1 * pivotBars - 1 ) );
pivotLow = Floor( Ref( pivotLow, -1 * pivotBars - 1 ) );
Plot( pivotHigh, "", colorTan, styleNoRescale | styleStaircase, Null, Null, Null, -2 );
Plot( pivotLow, "", colorTan, styleNoRescale | styleStaircase, Null, Null, Null, -2 );
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Cloud
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
startPeriod = Param("Period", 17, 7, 251, 1);
endPeriod = startPeriod + 4;
colorFactor = 255 / ( endPeriod - startPeriod );
a1 = KAMA( Close, startPeriod );
a2 = KAMA( Close, startPeriod + 1 );
a3 = KAMA( Close, startPeriod + 2 );
a4 = KAMA( Close, startPeriod + 3 );
a5 = KAMA( Close, startPeriod + 4 );
cloudTop = Max( a1, Max( a2, Max( a3, Max( a4, a5))));
cloudBottom = Min( a1, Min( a2, Min( a3, Min( a4, a5))));
side = IIf( a1 > a2 AND Ref( a1 > a2, 1 ), 1, 0.73 );
PlotOHLC( a1, a1, a2, a2, "", ColorHSB( colorFactor * 1, 83, 255 * side ), styleCloud | styleNoLabel | styleNoRescale, Null, Null, Null, -4 );
side = IIf( a2 > a3 AND Ref( a2 > a3, 1 ), 1, 0.73 );
PlotOHLC( a2, a2, a3, a3, "", ColorHSB( colorFactor * 2, 83, 255 * side ), styleCloud | styleNoLabel | styleNoRescale, Null, Null, Null, -4 );
side = IIf( a3 > a4 AND Ref( a3 > a4, 1 ), 1, 0.73 );
PlotOHLC( a3, a3, a4, a4, "", ColorHSB( colorFactor * 3, 83, 255 * side ), styleCloud | styleNoLabel | styleNoRescale, Null, Null, Null, -4 );
side = IIf( a4 > a5 AND Ref( a4 > a5, 1 ), 1, 0.73 );
PlotOHLC( a4, a4, a5, a5, "", ColorHSB( colorFactor * 4, 83, 255 * side ), styleCloud | styleNoLabel | styleNoRescale, Null, Null, Null, -4 );
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// The Title
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
candleColor = SelectedValue( IIf( Close < Open, colorRed, colorDarkGreen ) );
_N( Title = StrFormat( "Mittens : " + Name() + ", {{INTERVAL}}, {{DATE}}, "
+ StrExtract( "SUNDAY,MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,S ATURDAY", SelectedValue( DayOfWeek() ) )
+ EncodeColor( -1 ) + "\nOpen %g, High %g, Low %g, "
+ EncodeColor( candleColor ) + "Close %g,"
+ EncodeColor( -1 ) + "\n{{VALUES}}"
, Open, High, Low, Close
) );
_SECTION_END();