Trading the Ranges

Status
Not open for further replies.


Hi to all,

Its often said that the markets trade in ranges for a longer time and as a trader or investor its opt to learn to trade these ranges.

We have MA crossover to be precise to get into trending markets. Also, a visual screening of a chart throws some light to pin point a trend. There are numerous strategies to trade trending markets such as breakouts, NR7 setups, multiple MA crossovers,Turtle Trading, trendlines etc. As already said, these are all helpful when the markets are trending. Trending markets are rare.

When the markets are in range, what are the possible strategies to deploy.
Are there ways to trade with higher probabilities to trade ranges.

A close watch on the chart will enable us to trade ranges. This is the crux of this thread is to discuss various high probability strategies to trade ranges.


If the sideways price action has lasted for about 10 bars, then a trader has to be particularly careful because the bulls and bears are in very tight balance.

Trading breakouts in this situation can be costly since every brief up move is sold aggressively by the bears, and the new bulls are quick to exit. Likewise, every sharp move down is quickly reversed.

There should be ways to trade this type of market profitably.
:)..........................................................
 

veluri1967

Well-Known Member
Many members are sending me PMs requesting for the Kama system. Instead of sending them individually, i am giving below the systems. All the best.

PS : Its not my invention. Credits to original author.

-------------------------------------------------------------------------
// AMA System by Karthikmarar
// Two adjustable parameter "Buy sensitivity" and "Buy Finetune" provided to adjust entry points.
// Two adjustable parameter "Sell sensitivity" and "Sell Finetune" provided to adjust Exit points.

_SECTION_BEGIN("KAMA System");

SetChartOptions(0,chartShowArrows|chartShowDates);
Title = ("KAMA SYSTEM - " + Name()+" " + Date() +" "+Interval(2) +" "+ EncodeColor(colorLime)+",Open "+Open +" ,High "+H+" ,Low "+L+" ,Close "+C+" "+"{{VALUES}}");


//{{VALUES}}"+ O+ H+ L+C);


//_N(Title =StrFormat("{{Name}} - {{Interval}} {{Date}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));


// Buy adjustments
bs=Param("BUY Sensitivity",3,2,20,1);
bf=Param("BUY Finetune",2,0.1,20,0.1);
// Sell Adjustments
ss=Param("SELL Sensitivity",3,2,20,1);
sf=Param("SELL Finetune",1,0.1,20,0.1);
//stock selection parameters
MyCL = Param( "CL", 10, 10, 100, 10 );
MyVK = Param( "VK", 30, 10, 100, 10 );
MyTL = Param( "TL", 300, 100, 1000, 100 );
//stock selection
//TLM = EMA(C*V/100000,100) ;
//include = C> MyCL AND V/1000> MyVK AND C*V/100000 > MyTL AND TLM > 0.333 * MyTL ;

// common
fast = 2/(2+1);
slow = 2/(30+1);
//BUY part
dirb=abs(Close-Ref(Close,-bs));
volb=Sum(abs(Close-Ref(Close,-1)),bs);
ERb=dirb/volb;
scb =( ERb*(fast-slow)+slow)^2;
xb = AMA( C, scb );
flb=bf*StDev(xb-Ref(xb,-1),20);
j=xb-Ref(xb,-3);


//SELL part
dirs=abs(Close-Ref(Close,-ss));
vols=Sum(abs(Close-Ref(Close,-1)),ss);
ERs=dirs/vols;
scs =( ERs*(fast-slow)+slow)^2;
xs = AMA( C, scs );
fls=sf*StDev(xs-Ref(xs,-1),20);
k=Ref(Xs,-3)-Xs;

Buy=Cover=Cross(j,flb) ;
Sell=Short=Cross(k,fls);
mycolor=IIf(C>xb,colorLime,colorRed);
//Plot( C, "Close", mycolor,styleNoTitle | styleBar|styleThick );
//Plot(xb,"KAMA-BUY",colorYellow,1);
//Plot(xs,"KAMA-SELL",colorOrange,1);
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);

shape = Buy * shapeUpArrow +Sell * shapeDownArrow ;

PlotShapes( shape, IIf( Buy, colorGreen, colorRed ),0, IIf( Buy, Low, High ) );

GraphXSpace = 5;
dist = 1.5*ATR(20);

//for( i = 0; i < BarCount; i++ )
//{
//if( Buy ) PlotText( "Buy" +C[ i ], i, L[ i ]-dist, colorGreen );
//if( Sell ) PlotText( "sell\n@" + C[ i ], i, L[ i ]+dist, colorRed );
//}
Filter= Buy OR Sell;
//
PositionScore=100/C;
PositionSize = - 20;
SetBarsRequired(10000, 10000);
SetFormulaName("KAMA System");

_SECTION_END();

_SECTION_BEGIN("IIR2");
// IIR2.afl
//
// Documentation to describe what the function does.
// Second order smoother
// the function statement
function IIR2( input, f0, f1, f2 )
// the function body
{
result[ 0 ] = input[ 0 ];
result[ 1 ] = input[ 1 ];
for( i = 2; i < BarCount; i++ )
{
result = f0 * input + f1 * result[i-1] + f2 * result[i-2];
}
// the function returns a single value and exits.
return result;
}
// The routine that calls the function.
SmoothedClose = IIR2(Close, 0.2, 1.4, -0.6 );
//Plot( Close, "Price", 2, styleCandle );
Plot( SmoothedClose, "function example", colorRed );
//Figure 8.1 IIR2
_SECTION_END();

_SECTION_BEGIN("GSMA");
SetBarsRequired(100000,0);
PI = 3.1415926;

function jIIR2( input, f0, f1, f2 )
{
result[ 0 ] = input[ 0 ];
result[ 1 ] = input[ 1 ];

for( i = 2; i < BarCount; i++ )
{
result[ i ] = f0 * input[ i ] +
f1 * result[ i - 1 ] +
f2 * result[ i - 2 ];
}

return result;
}

function GSMA( input, Period )
{
N = 0;
an = 2 * PI / Period;
c0 = b0 = 1;
c1 = b1 = b2 = a1 = a2 = gamma1 = 0;
beta1 = 2.415 * ( 1- cos( an ) );
alpha = -beta1 + sqrt( beta1 ^ 2 + 2 * beta1 );
alpha1 = ( cos( an ) + sin( an ) - 1 )/cos( an );
{
fo = alpha ^ 2;
f1 = 2 * ( 1- alpha ); f2 = -( 1 - alpha )*( 1 - alpha );
}


return jIIR2( input, fo,f1,f2);
}
period=Param("period",13,1,40,1);

//Plot( Close, "Price", colorBlack, styleCandle );
Plot( GSMA( C,period), "GSMA", colorLime );

// Linear Regression Line with 2 Standard Deviation Channels Plotted Above and Below
// Written by Patrick Hargus, with critical hints from Marcin Gorzynski, Amibroker.com Technical Support
// Designed for use with AB 4.63 beta and above, using drag and drop feature.
// Permits plotting a linear regression line of any price field available on the chart for a period determined by the user.
// 2 Channels, based on a standard deviation each determined by the user, are plotted above and below the linear regression line.
// A look back feature is also provided for examining how the indicator would have appeared on a chart X periods in the past.


P = ParamField("Price field",-1);
Daysback = Param("Period for Liner Regression Line",21,1,240,1);
shift = Param("Look back period",0,0,240,1);


// =============================== Math Formula =============================================================

x = Cum(1);
lastx = LastValue( x ) - shift;
aa = LastValue( Ref(LinRegIntercept( p, Daysback), -shift) );
bb = LastValue( Ref(LinRegSlope( p, Daysback ), -shift) );
y = Aa + bb * ( x - (Lastx - DaysBack +1 ) );


// ==================Plot the Linear Regression Line ==========================================================


LRColor = ParamColor("LR Color", colorCycle );
LRStyle = ParamStyle("LR Style");

LRLine = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y, Null );
Plot( LRLine , "LinReg", LRCOLOR, LRSTYLE ); // styleDots );

// ========================== Plot 1st SD Channel ===============================================================

SDP = Param("Standard Deviation", 1.5, 0, 6, 0.1);
SD = SDP/2;

width = LastValue( Ref(SD*StDev(p, Daysback),-shift) ); // THIS IS WHERE THE WIDTH OF THE CHANELS IS SET
SDU = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width , Null ) ;
SDL = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width , Null ) ;

SDColor = ParamColor("SD Color", colorCycle );
SDStyle = ParamStyle("SD Style");

Plot( SDU , "Upper Lin Reg", SDColor,SDStyle );
Plot( SDL , "Lower Lin Reg", SDColor,SDStyle );

// ========================== Plot 2d SD Channel ===============================================================

SDP2 = Param("2d Standard Deviation", 2.0, 0, 6, 0.1);
SD2 = SDP2/2;

width2 = LastValue( Ref(SD2*StDev(p, Daysback),-shift) ); // THIS IS WHERE THE WIDTH OF THE CHANELS IS SET
SDU2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y+width2 , Null ) ;
SDL2 = IIf( x > (lastx - Daysback) AND BarIndex() < Lastx, y-width2 , Null ) ;

SDColor2 = ParamColor("2 SD Color", colorCycle );
SDStyle2 = ParamStyle("2 SD Style");

Plot( SDU2 , "Upper Lin Reg", SDColor2,SDStyle2 );
Plot( SDL2 , "Lower Lin Reg", SDColor2,SDStyle2 );

// ============================ End Indicator Code ==============================================================

_SECTION_BEGIN("ATR Trailstop");
//ATR Trailstop Afl By karthikmarar ( [email protected] )
//ATR values can be changed from 1 to 3 multiplications in steps of 0.25
//ATR value or the last low pivot value can be selected. This feature can be overridden from the parameter window. It is recommended to use only ATR Value
//Choice of plots line or just dots from the parameter window is also possible
mf = Param("ATR multiplier",3,1,3,0.25);
ap=Param("ATR Period",10,5,30,1);
Lb=Param("Lookback Period",20,10,40,1);
ps=ParamToggle("Use last low pivot","Use,Dont",0);
p1=ParamToggle("plot type","line,Dots",1);

t1=HHV(C,Lb);
t2=(mf*ATR(ap));
t3=Trough(C,2,1);
t4=t1-t2;
t5=Min(t4,t3);

if(ps)
{
t6 = t1-t2;
}
else
{
t6=t5;
}

initial=t6;
stop[ 0 ] = Close[ 0 ];

for( i = 1 ; i < BarCount; i++)
{
if( Close[ i ] > stop[ i - 1])
{
temp = t6[ i ];
if( temp > stop[ i - 1 ] ) stop[ i ] = temp;
else stop[ i ] = stop[ i - 1 ];
}
else
stop[ i ] = initial[ i ];

}
Sell=Cross(stop,Close);

PlotShapes( IIf( Sell, shapeUpArrow + shapePositionAbove, shapeNone ), colorRed );
if(p1)
{
PlotShapes( shapeSmallCircle,IIf(C>stop,colorLime,colorRed) , layer=0,stop,0);
}
else
{
Plot(stop,"ATR Stop",colorBlue,8);
}
_SECTION_END();
--------------------------------------------------------------------------
 

veluri1967

Well-Known Member
Here is ATR Volatility System.

//------------------------------------------------------------------------------
//
// Formula Name: ATR Volatility System
// Author/Uploader: Marcelin
// E-mail: [email protected]
// Date/Time Added: 2006-01-14 13:08:22
// Origin:
// Keywords:
// Level: medium
// Flags: system
// Formula URL: http://www.amibroker.com/library/formula.php?id=578
// Details URL: http://www.amibroker.com/library/detail.php?id=578
//
//------------------------------------------------------------------------------
//
// ATR trading system for short terms tradings. You can use it with Absolute
// Strength Index for confirmation of signals.
//
//------------------------------------------------------------------------------

_SECTION_BEGIN("NewSystem ATR");
/*Writed & composed by Tudor Marcelin - Art Invest*/
n=Param( "perioada", 14, 5 , 20, 1 );
k=Param( "factor de multiplicare", 1.4, 0.5 , 2.5, 0.1 );

f=ATR(n);

/*R rezistenta pentru ziua curenta*/
R[0] = C[0];
/*S rezistenta pentru ziua curenta*/
S[0] = C[0];

for( i = n+1; i < BarCount; i++ )
{

R=R[i-1];
S=S[i-1];
if (( S[i-1]<=C[i-1]) AND (C[i-1] <=R[i-1] ) AND (C[i-1]+k*f[i-1])<=RV)

r = C[i-1]+k*f[i-1];

if (( S[i-1]<=C[i-1]) AND (C[i-1]<=R[i-1] ) AND (C[i-1]-k*f[i-1])>=SV)

s= C[i-1]-k*f[i-1];






if ( C[i-1] >R[i-1] )
{
r = C[i-1]+k*f[i-1];
s= C[i-1]-k*f[i-1];
RV=r;
SV=s;
}
if ( C[i-1] <S[i-1] )
{
r = C[i-1]+k*f[i-1];
s= C[i-1]-k*f[i-1];
RV=r;
SV=s;

}


Buy=Cover=Close>R;
Sell=Short=Close<S;

Cump=IIf(Close>R,1,0);
Vanz=IIf(Close<S,1,0);
}
Plot(Close,"Close",colorBlack,styleCandle);
Plot(R, "Rez:",colorGreen,styleDots|styleNoLine);
Plot(S, "Sup:",colorRed,styleDots|styleNoLine);


Buy = ExRem( Buy, Sell ); //Elimina semnalele buy consecutive
Sell = ExRem( Sell, Buy ); //Elimina semnalele sell consecutive

shape = Buy * shapeUpArrow + Sell * shapeDownArrow;

fig=Cump*shapeHollowUpArrow + Vanz*shapeHollowDownArrow;

PlotShapes( fig, IIf( Cump, colorPaleGreen , colorPink), 0, IIf( Cump, Low, High)); //Pentru a vizualiza semnalele consecutive eliminate de ExRem
PlotShapes( shape, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, Low, High));

AlertIf( Buy, "", "Volatility System", 1 );
AlertIf( Sell, "", "Volatility System",2);

Color = IIf( Vanz OR Sell, colorRed, IIf(Buy,colorGreen,colorLightGrey));

Plot( 2, "", Color, styleArea | styleOwnScale | styleNoLabel, -0.1, 50 );

GraphXSpace = 3;

Title=EncodeColor(colorBlue)+"Volatility System"+EncodeColor(colorBlack)+ " Open:"+O+" High:"+H+" Low:"+L+" Close:"+C+EncodeColor(colorGreen)+" Rez:"+R+EncodeColor(colorRed)+" Sup:"+S+EncodeColor(colorBlue)+
" \nDate: "+EncodeColor(colorRed)+Date();
_SECTION_END();
 

rajsumi121

Well-Known Member
Please check this system on daily ... not mine but only copy paste .

/*======================================================
FOREX INTRADAY HEIKIN ASHI + PIVOT POINTS
======================================================*/

_SECTION_BEGIN("The_Beast_2");
SetBarsRequired(10000,10000); /* this ensures that the charts include all bars
AND NOT just those on screen */
Prd1=Param("ATR Period 1-20",4,1,20,1);//{Default = 4 Because most traders use 5}
Prd2=Param("LookBack Period 1-20",7,1,20,1);//{Default = 11 Because most traders use 10}
//{Green} {Start Long position when Close>Green}
Green=HHV(LLV(L,Prd1)+ATR(Prd1),Prd2);
//{Red} {Stop loss when Close<Red}
RED=LLV(HHV(H,Prd1)-ATR(Prd1),Prd2);

HaClose =EMA((O+H+L+C)/4,3);
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );
Color = IIf(C>Green ,colorBrightGreen,IIf(C < RED,colorRed,colorBlue));

PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "", color, styleCandle,styleThick );

/////////////////////////////////////////////////////////////////////////////////////////////////

Odd=13;//enter Odd numbers only
CoefOdd=round(Odd/2);

Even=12;//enter Even numbers only
Coefeven=Even/2;
Coefeven2=Coefeven+1;

CongestionPercent=2.8;/*Set % above/below Moving average for congestion / sideways market*/

TriangularOdd=MA(MA(C,CoefOdd),CoefOdd);
TriangularEven=MA(MA(C,Coefeven),Coefeven2);

finalMov_avg=IIf(Odd > even,triangularOdd,TriangularEven);

Color=colorBrightGreen;//select Moving average line color
tickercolor=colorBlack;//select price color

Plot(finalMov_avg,"",IIf(C < finalmov_avg,colorRed,Color),styleLine|styleThick);

/////////////////////////////////////////////////////////////////////////////////////////////////

LB= Param("Look Back Periods",10,1,30,1);
R=ValueWhen(Cross(MA(C,LB),C),HHV(H,LB),1);
S=ValueWhen(Cross(C,MA(C,LB)),LLV(L,LB),1);
//UpClose = Close - Ref(Close,-1);
//Color = IIf(UpClose >= 0, colorBlue, colorRed);

UpClose = Close - Ref(Close,-1);
Color = IIf(UpClose > 0, colorBrightGreen, colorRed);


Plot (R,"Resz",ParamColor("R Color",colorRed),8+16);
Plot (S,"Supp",ParamColor("S Color",colorGreen),8+16);

//Plot (R,"Resz",22,8+16);
//Plot (S,"Supp",19,8+16);
// Plot (C,"Close",color,64,32);
GraphXSpace=4;

//////////////////////////////////////////////////////////////////////////////////////////////////////////

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} - "+ EncodeColor(colorYellow)+"Open = "+ EncodeColor(colorYellow) +"%g "+ EncodeColor(colorBrightGreen)+"High = "+ EncodeColor(colorBrightGreen) +"%g - "+ EncodeColor(colorRed)+"Low = "+ EncodeColor(colorRed) +"%g "+ EncodeColor(colorYellow) +"Close = "+ EncodeColor(colorYellow) +" %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));


///////////////////////////////////////////////////////////////////////////////////////// ///////////////////



////////////////////////////////////////////////////////////////////////////////////////////////////////


//Previous Days HI LO //

DayH = TimeFrameGetPrice("H", inDaily, -1); DayHI = LastValue (DayH,1);// yesterdays high
DayL = TimeFrameGetPrice("L", inDaily, -1); DayLI = LastValue (DayL,1); // yesterdays low
DayC = TimeFrameGetPrice("C", inDaily, -1); // yesterdays close
DayO = TimeFrameGetPrice("O", inDaily); // current day open
DayH2= TimeFrameGetPrice("H", inDaily, -2); DayH2I = LastValue (DayH2,1); // Two days before high
DayL2= TimeFrameGetPrice("L", inDaily, -2); DayL2I = LastValue (DayL2,1); // Two days before low
DayH3= TimeFrameGetPrice("H", inDaily, -3); DayH3I = LastValue (DayH3,1); // Three days before high
DayL3= TimeFrameGetPrice("L", inDaily, -3); DayL3I = LastValue (DayL3,1); // Three days before low

numbars = LastValue(Cum(Status("barvisible")));
hts = -33.5;

YHL = ParamToggle("Yesterday HI LO","Show|Hide",1);
if(YHL==1) {
Plot(DayL,"YL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayH,"YH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
PlotText(" YH " , LastValue(BarIndex())-(numbars/Hts), DayHI, colorTurquoise);
PlotText(" YL " , LastValue(BarIndex())-(numbars/Hts), DayLI, colorTurquoise);
}

TDBHL = ParamToggle("2/3Days before HI LO","Show|Hide",0);
if(TDBHL==1) {
Plot(DayL2,"2DBL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayH2,"2DBH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayL3,"3DBL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayH3,"3DBH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
PlotText(" 2DBH " , LastValue(BarIndex())-(numbars/Hts), DayH2I, colorTurquoise);
PlotText(" 2DBL " , LastValue(BarIndex())-(numbars/Hts), DayL2I, colorTurquoise);
PlotText(" 3DBH " , LastValue(BarIndex())-(numbars/Hts), DayH3I, colorTurquoise);
PlotText(" 3DBL " , LastValue(BarIndex())-(numbars/Hts), DayL3I, colorTurquoise);
}

// Pivot Levels //
PP = (DayL + DayH + DayC)/3; PPI = LastValue (PP,1); // Pivot
R1 = (PP * 2) - DayL; R1I = LastValue (R1,1); // Resistance 1
S1 = (PP * 2) - DayH; S1I = LastValue (S1,1); // Support 1
R2 = PP + R1 - S1; R2I = LastValue (R2,1); // Resistance 2
S2 = PP - R1 + S1; S2I = LastValue (S2,1); // Support 2
R3 = PP + R2 - S1; R3I = LastValue (R3,1); // Resistance 3
S3 = PP - R2 + S1; S3I = LastValue (S3,1); // Support 3

ppl = ParamToggle("Pivot Levels","Show|Hide",1);
if(ppl==1) {
Plot(PP, "PP",colorYellow,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(R1, "R1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(S1, "S1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(R2, "R2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(S2, "S2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(R3, "R3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(S3, "S3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);

PlotText(" Pivot ", LastValue(BarIndex())-(numbars/Hts), PPI, colorYellow);
PlotText(" R1 " , LastValue(BarIndex())-(numbars/Hts), R1I, colorViolet);
PlotText(" S1 " , LastValue(BarIndex())-(numbars/Hts), S1I, colorViolet);
PlotText(" R2 " , LastValue(BarIndex())-(numbars/Hts), R2I, colorViolet);
PlotText(" S2 " , LastValue(BarIndex())-(numbars/Hts), S2I, colorViolet);
PlotText(" R3 " , LastValue(BarIndex())-(numbars/Hts), R3I, colorViolet);
PlotText(" S3 " , LastValue(BarIndex())-(numbars/Hts), S3I, colorViolet);
}
// Camerilla Levels //

rg = (DayH - DayL);

H5=DayC+1.1*rg; H5I = LastValue (H5,1);
H4=DayC+1.1*rg/2; H4I = LastValue (H4,1);
H3=DayC+1.1*rg/4; H3I = LastValue (H3,1);
H2=DayC+1.1*rg/6; H2I = LastValue (H2,1);
H1=DayC+1.1*rg/12; H1I = LastValue (H1,1);
L1=DayC-1.1*rg/12; L1I = LastValue (L1,1);
L2=DayC-1.1*rg/6; L2I = LastValue (L2,1);
L3=DayC-1.1*rg/4; L3I = LastValue (L3,1);
L4=DayC-1.1*rg/2; L4I = LastValue (L4,1);
L5=DayC-1.1*rg; L5I = LastValue (L5,1);

pcl = ParamToggle("Camerilla Levels","Show|Hide",0);
if(pcl==1) {
Plot(H5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
PlotText(" H5 = " , LastValue(BarIndex())-(numbars/Hts), H5I +0.05, colorRose);
PlotText(" H4 = " , LastValue(BarIndex())-(numbars/Hts), H4I +0.05, colorRose);
PlotText(" H3 = " , LastValue(BarIndex())-(numbars/Hts), H3I +0.05, colorRose);
PlotText(" H2 = " , LastValue(BarIndex())-(numbars/Hts), H2I +0.05, colorRose);
PlotText(" H1 = " , LastValue(BarIndex())-(numbars/Hts), H1I +0.05, colorRose);
PlotText(" L1 = " , LastValue(BarIndex())-(numbars/Hts), L1I +0.05, colorRose);
PlotText(" L2 = " , LastValue(BarIndex())-(numbars/Hts), L2I +0.05, colorRose);
PlotText(" L3 = " , LastValue(BarIndex())-(numbars/Hts), L3I +0.05, colorRose);
PlotText(" L4 = " , LastValue(BarIndex())-(numbars/Hts), L4I +0.05, colorRose);
PlotText(" L5 = " , LastValue(BarIndex())-(numbars/Hts), L5I +0.05, colorRose);
}

// Current Days Hi Lo //
THL = ParamToggle("Todays Hi Lo","Show|Hide",1);
if(THL==1) {
isRth = TimeNum() >= 084500 & TimeNum() <= 085959;
isdRth = TimeNum() >= 084500 & TimeNum() <= 160000;
aRthL = IIf(isRth, L, 1000000);
aRthH = IIf(isdRth, H, Null);
aRthLd = IIf(isdRth, L, 1000000);
DayH = TimeFrameCompress( aRthH, inDaily, compressHigh );
DayH = TimeFrameExpand( DayH, inDaily, expandFirst );
DayL = TimeFrameCompress( aRthLd, inDaily, compressLow );
DayL = TimeFrameExpand( DayL, inDaily, expandFirst );
Bars = BarsSince(TimeNum() >= 94500 AND TimeNum() < 095959);//,BarIndex(),1); // AND DateNum()==LastValue(DateNum());
x0 = BarCount-LastValue(Bars);
x1 = BarCount-1;
DayHline=LineArray(x0,LastValue(DayH),x1,LastValue (DayH),0);
DayLline=LineArray(x0,LastValue(DayL),x1,LastValue (DayL),0);
DayHlineI = LastValue (DayHline,1);
DayLlineI = LastValue (DayLline,1);
Plot(DayHline,"DayH",colorYellow,styleBar|styleNoRescale|styleNoTitle);
Plot(DayLline,"DayL",colorYellow,styleBar|styleNoRescale|styleNoTitle);
PlotText(" Day Hi " , LastValue(BarIndex())-(numbars/Hts), DayHlineI +0.05, colorYellow);
PlotText(" Day Lo " , LastValue(BarIndex())-(numbars/Hts), DayLlineI +0.05, colorYellow);
}

/////////////////////////////////////////////////////////////////////////////////////////////////

_SECTION_BEGIN("Pivot_Finder");
/* **********************************

Code to automatically identify pivots

********************************** */

// -- what will be our lookback range for the hh and ll?
farback=Param("How Far back to go",200,0,5000,10);
nBars = Param("Number of bars", 12, 5, 40);


// -- Create 0-initialized arrays the size of barcount

aHPivs = H - H;

aLPivs = L - L;

// -- More for future use, not necessary for basic plotting

aHPivHighs = H - H;

aLPivLows = L - L;

aHPivIdxs = H - H;

aLPivIdxs = L - L;

nHPivs = 0;

nLPivs = 0;

lastHPIdx = 0;

lastLPIdx = 0;

lastHPH = 0;

lastLPL = 0;

curPivBarIdx = 0;

// -- looking back from the current bar, how many bars

// back were the hhv and llv values of the previous

// n bars, etc.?

aHHVBars = HHVBars(H, nBars);

aLLVBars = LLVBars(L, nBars);

aHHV = HHV(H, nBars);

aLLV = LLV(L, nBars);

// -- Would like to set this up so pivots are calculated back from

// last visible bar to make it easy to "go back" and see the pivots

// this code would find. However, the first instance of

// _Trace output will show a value of 0

aVisBars = Status("barvisible");

nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));

_TRACE("Last visible bar: " + nLastVisBar);

// -- Initialize value of curTrend

curBar = (BarCount-1);

curTrend = "";

if (aLLVBars[curBar] <

aHHVBars[curBar]) {

curTrend = "D";

}

else {

curTrend = "U";

}

// -- Loop through bars. Search for

// entirely array-based approach

// in future version

for (i=0; i<farback; i++) {

curBar = (BarCount - 1) - i;

// -- Have we identified a pivot? If trend is down...

if (aLLVBars[curBar] < aHHVBars[curBar]) {

// ... and had been up, this is a trend change

if (curTrend == "U") {

curTrend = "D";

// -- Capture pivot information

curPivBarIdx = curBar - aLLVBars[curBar];

aLPivs[curPivBarIdx] = 1;

aLPivLows[nLPivs] = L[curPivBarIdx];

aLPivIdxs[nLPivs] = curPivBarIdx;

nLPivs++;

}

// -- or current trend is up

} else {

if (curTrend == "D") {

curTrend = "U";

curPivBarIdx = curBar - aHHVBars[curBar];

aHPivs[curPivBarIdx] = 1;

aHPivHighs[nHPivs] = H[curPivBarIdx];

aHPivIdxs[nHPivs] = curPivBarIdx;

nHPivs++;

}

// -- If curTrend is up...else...

}

// -- loop through bars

}

// -- Basic attempt to add a pivot this logic may have missed

// -- OK, now I want to look at last two pivots. If the most

// recent low pivot is after the last high, I could

// still have a high pivot that I didn't catch

// -- Start at last bar

curBar = (BarCount-1);

candIdx = 0;

candPrc = 0;

lastLPIdx = aLPivIdxs[0];

lastLPL = aLPivLows[0];

lastHPIdx = aHPivIdxs[0];

lastHPH = aHPivHighs[0];

if (lastLPIdx > lastHPIdx) {

// -- Bar and price info for candidate pivot

candIdx = curBar - aHHVBars[curBar];

candPrc = aHHV[curBar];

if (

lastHPH < candPrc AND

candIdx > lastLPIdx AND

candIdx < curBar) {


// -- OK, we'll add this as a pivot...

aHPivs[candIdx] = 1;

// ...and then rearrange elements in the

// pivot information arrays

for (j=0; j<nHPivs; j++) {

aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-

(j+1)];

aHPivIdxs[nHPivs-j] = aHPivIdxs[nHPivs-(j+1)];

}

aHPivHighs[0] = candPrc ;

aHPivIdxs[0] = candIdx;

nHPivs++;

}

} else {


// -- Bar and price info for candidate pivot

candIdx = curBar - aLLVBars[curBar];

candPrc = aLLV[curBar];

if (

lastLPL > candPrc AND

candIdx > lastHPIdx AND

candIdx < curBar) {


// -- OK, we'll add this as a pivot...

aLPivs[candIdx] = 1;

// ...and then rearrange elements in the

// pivot information arrays

for (j=0; j<nLPivs; j++) {

aLPivLows[nLPivs-j] = aLPivLows[nLPivs-(j+1)];

aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-(j+1)];

}

aLPivLows[0] = candPrc;

aLPivIdxs[0] = candIdx;

nLPivs++;

}

}

// -- Dump inventory of high pivots for debugging



for (k=0; k<nHPivs; k++) {

_TRACE("High pivot no. " + k

+ " at barindex: " + aHPivIdxs[k] + ", "

+ WriteVal(ValueWhen(BarIndex()==aHPivIdxs[k],

DateTime(), 1), formatDateTime)

+ ", " + aHPivHighs[k]);

}

//////////////////////////////////////////////////////////////////////////////

// -- OK, let's plot the pivots using arrows

PlotShapes(

IIf(aHPivs==1, shapeStar, shapeNone), colorBrightGreen, 0, H, 25);

PlotShapes(

IIf(aLPivs==1, shapeStar , shapeNone), colorRed, 0, L, -20);

//////////////////////////////////////////////////////////////////////////////


/* **********************************

Code to automatically identify pivots

********************************** */
// -- what will be our lookback range for the hh and ll?
farback=Param("How Far back to go",200,12,30,1);
nBars = Param("Number of bars", 12, 1, 30, 1);
// -- Create 0-initialized arrays the size of barcount
aHPivs = H - H;
aLPivs = L - L;
// -- More for future use, not necessary for basic plotting
aHPivHighs = H - H;
aLPivLows = L - L;
aHPivIdxs = H - H;
aLPivIdxs = L - L;
nHPivs = 0;
nLPivs = 0;
lastHPIdx = 0;
lastLPIdx = 0;
lastHPH = 0;
lastLPL = 0;
curPivBarIdx = 0;
// -- looking back from the current bar, how many bars
// back were the hhv and llv values of the previous
// n bars, etc.?
aHHVBars = HHVBars(H, nBars);
aLLVBars = LLVBars(L, nBars);
aHHV = HHV(H, nBars);
aLLV = LLV(L, nBars);
// -- Would like to set this up so pivots are calculated back from
// last visible bar to make it easy to "go back" and see the pivots
// this code would find. However, the first instance of
// _Trace output will show a value of 0
aVisBars = Status("barvisible");
nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));
_TRACE("Last visible bar: " + nLastVisBar);
// -- Initialize value of curTrend
curBar = (BarCount-1);
curTrend = "";
if (aLLVBars[curBar] <
aHHVBars[curBar]) {
curTrend = "D";
}
else {
curTrend = "U";
}
// -- Loop through bars. Search for
// entirely array-based approach
// in future version
for (i=0; i<farback; i++) {
curBar = (BarCount - 1) - i;
// -- Have we identified a pivot? If trend is down...
if (aLLVBars[curBar] < aHHVBars[curBar]) {
// ... and had been up, this is a trend change
if (curTrend == "U") {
curTrend = "D";
// -- Capture pivot information
curPivBarIdx = curBar - aLLVBars[curBar];
aLPivs[curPivBarIdx] = 1;
aLPivLows[nLPivs] = L[curPivBarIdx];
aLPivIdxs[nLPivs] = curPivBarIdx;
nLPivs++;
}
// -- or current trend is up
} else {
if (curTrend == "D") {
curTrend = "U";
curPivBarIdx = curBar - aHHVBars[curBar];
aHPivs[curPivBarIdx] = 1;
aHPivHighs[nHPivs] = H[curPivBarIdx];
aHPivIdxs[nHPivs] = curPivBarIdx;
nHPivs++;
}
// -- If curTrend is up...else...
}
// -- loop through bars
}
// -- Basic attempt to add a pivot this logic may have missed
// -- OK, now I want to look at last two pivots. If the most
// recent low pivot is after the last high, I could
// still have a high pivot that I didn't catch
// -- Start at last bar
curBar = (BarCount-1);
candIdx = 0;
candPrc = 0;
lastLPIdx = aLPivIdxs[0];
lastLPL = aLPivLows[0];
lastHPIdx = aHPivIdxs[0];
lastHPH = aHPivHighs[0];
if (lastLPIdx > lastHPIdx) {
// -- Bar and price info for candidate pivot
candIdx = curBar - aHHVBars[curBar];
candPrc = aHHV[curBar];
if (
lastHPH < candPrc AND
candIdx > lastLPIdx AND
candIdx < curBar) {
// -- OK, we'll add this as a pivot...
aHPivs[candIdx] = 1;
// ...and then rearrange elements in the
// pivot information arrays
for (j=0; j<nHPivs; j++) {
aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-
(j+1)];
aHPivIdxs[nHPivs-j] = aHPivIdxs[nHPivs-(j+1)];
}
aHPivHighs[0] = candPrc ;
aHPivIdxs[0] = candIdx;
nHPivs++;
}
} else {
// -- Bar and price info for candidate pivot
candIdx = curBar - aLLVBars[curBar];
candPrc = aLLV[curBar];
if (
lastLPL > candPrc AND
candIdx > lastHPIdx AND
candIdx < curBar) {
// -- OK, we'll add this as a pivot...
aLPivs[candIdx] = 1;
// ...and then rearrange elements in the
// pivot information arrays
for (j=0; j<nLPivs; j++) {
aLPivLows[nLPivs-j] = aLPivLows[nLPivs-(j+1)];
aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-(j+1)];
}
aLPivLows[0] = candPrc;
aLPivIdxs[0] = candIdx;
nLPivs++;
}
}
// -- Dump inventory of high pivots for debugging
/*
for (k=0; k<nHPivs; k++) {
_TRACE("High pivot no. " + k
+ " at barindex: " + aHPivIdxs[k] + ", "
+ WriteVal(ValueWhen(BarIndex()==aHPivIdxs[k],
DateTime(), 1), formatDateTime)
+ ", " + aHPivHighs[k]);
}
*/
// -- OK, let's plot the pivots using arrows
PlotShapes(IIf(aHPivs==1, shapeSmallDownTriangle, shapeNone), colorCustom12, 0, High, Offset=-5);
PlotShapes(IIf(aLPivs==1, shapeSmallUpTriangle , shapeNone), colorCustom11, 0, Low, Offset=-5);

Sell = aHPivs == 1 ;
Buy = aLPivs == 1 ;
Filter=Buy OR Sell;
Sell=ExRem(Sell,Buy);
Buy=ExRem(Buy,Sell);

///////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////

R=3;//Marking circle radius
m=0.015*(HHV(H,159)-LLV(L,159));//scaling factor
x=Cum(1);
per = 2.5;//Sensitivity Calibration
pR = PeakBars( H, per, 1 ) == 0;//Peak condition
x01= LastValue(ValueWhen( pR, x, 1 ));
x02=LastValue(ValueWhen( pR, x, 2 ));
x03=LastValue(ValueWhen( pR, x, 3 ));
y01 = LastValue(ValueWhen( pR, H, 1 ) );
y02=LastValue(ValueWhen( pR, H, 2 ) );
y03 = LastValue( ValueWhen( pR, H, 3 ));

y0=y01;x0=x01;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y02;x0=x02;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y03;x0=x03;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
TpR = TroughBars( L, per, 1 ) == 0;//Trough condition
x01T= LastValue(ValueWhen( TpR, x, 1 ));
x02T=LastValue(ValueWhen( TpR, x, 2 ));
x03T=LastValue(ValueWhen( TpR, x, 3 ));
y01T = LastValue(ValueWhen( TpR, L, 1 ) );
y02T=LastValue(ValueWhen( TpR, L, 2 ) );
y03T = LastValue( ValueWhen( TpR, L, 3 ));
y0T=y01T;x0T=x01T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(Y,"",5,1);Plot(Y1,"",5,1);
y0T=y02T;x0T=x02T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);
y0T=y03T;x0T=x03T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);

R=2;//Marking circle radius
m=0.015*(HHV(H,159)-LLV(L,159));//scaling factor
x=Cum(1);
per = 1.5;//Sensitivity Calibration
pR = PeakBars( H, per, 1 ) == 0;//Peak condition
x01= LastValue(ValueWhen( pR, x, 1 ));
x02=LastValue(ValueWhen( pR, x, 2 ));
x03=LastValue(ValueWhen( pR, x, 3 ));
y01 = LastValue(ValueWhen( pR, H, 1 ) );
y02=LastValue(ValueWhen( pR, H, 2 ) );
y03 = LastValue( ValueWhen( pR, H, 3 ));

y0=y01;x0=x01;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y02;x0=x02;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y03;x0=x03;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
TpR = TroughBars( L, per, 1 ) == 0;//Trough condition
x01T= LastValue(ValueWhen( TpR, x, 1 ));
x02T=LastValue(ValueWhen( TpR, x, 2 ));
x03T=LastValue(ValueWhen( TpR, x, 3 ));
y01T = LastValue(ValueWhen( TpR, L, 1 ) );
y02T=LastValue(ValueWhen( TpR, L, 2 ) );
y03T = LastValue( ValueWhen( TpR, L, 3 ));
y0T=y01T;x0T=x01T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(Y,"",5,1);Plot(Y1,"",5,1);
y0T=y02T;x0T=x02T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);
y0T=y03T;x0T=x03T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);


////////////////////////////////////////////////////////////////////////////////

R=1;//Marking circle radius
m=0.015*(HHV(H,159)-LLV(L,159));//scaling factor
x=Cum(1);
per = 0.325;//Sensitivity Calibration
pR = PeakBars( H, per, 1 ) == 0;//Peak condition
x01= LastValue(ValueWhen( pR, x, 1 ));
x02=LastValue(ValueWhen( pR, x, 2 ));
x03=LastValue(ValueWhen( pR, x, 3 ));
y01 = LastValue(ValueWhen( pR, H, 1 ) );
y02=LastValue(ValueWhen( pR, H, 2 ) );
y03 = LastValue( ValueWhen( pR, H, 3 ));

y0=y01;x0=x01;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y02;x0=x02;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y03;x0=x03;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
TpR = TroughBars( L, per, 1 ) == 0;//Trough condition
x01T= LastValue(ValueWhen( TpR, x, 1 ));
x02T=LastValue(ValueWhen( TpR, x, 2 ));
x03T=LastValue(ValueWhen( TpR, x, 3 ));
y01T = LastValue(ValueWhen( TpR, L, 1 ) );
y02T=LastValue(ValueWhen( TpR, L, 2 ) );
y03T = LastValue( ValueWhen( TpR, L, 3 ));
y0T=y01T;x0T=x01T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(Y,"",5,1);Plot(Y1,"",5,1);
y0T=y02T;x0T=x02T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);
y0T=y03T;x0T=x03T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);

EMA34 = EMA(C,34);
LSMA = LinearReg(C,23);

/////////////////////////////////////////////////////////////////////////////////////////////////

_SECTION_BEGIN("Price");

SetChartOptions(0,chartShowArrows|chartShowDates);

_N(Title = EncodeColor(colorCustom12) +StrFormat(" {{NAME}} - {{INTERVAL}} {{DATE}} "+ EncodeColor(colorWhite) +" Open = "+ EncodeColor(colorWhite) +"%g "+ EncodeColor(colorBrightGreen) +" High : "+ EncodeColor(colorBrightGreen) +" %g "+ EncodeColor(colorRed) +" - Low : "+ EncodeColor(colorRed) +" %g "+ EncodeColor(colorWhite) +" Close = "+ EncodeColor(colorWhite) +"%g
{{VALUES}}",O,H,L,C ));

_SECTION_END();

/////////////////////////////////////////////////////////////////////////////////////////////////


_SECTION_BEGIN("Break Outs");
//{Buy}
x1=5; x2=4; x3=3; x4=2; x5=1; x6=.5;
//{Lowest price stock <5}
AA=IIf(C<5,Cross(C,ValueWhen(Peak(H,x1,1) !=Ref(Peak(H,x1,1),-1),H,1)),
//{Price between 5 AND 20}
IIf(C>5 AND C<20,Cross(C,ValueWhen(Peak(H,x2,1) !=Ref(Peak(H,x2,1),-1),H,1)),
//{Price between 20 AND 70}
IIf(C>20 AND C<70,Cross(C,ValueWhen(Peak(H,x3,1) !=Ref(Peak(H,x3,1),-1),H,1)),
//{Price between 70 AND 150}
IIf(C>70 AND C<150,Cross(C,ValueWhen(Peak(H,x4,1) !=Ref(Peak(H,x4,1),-1),H,1)),
//{Price between 150 AND 300}
IIf(C>150 AND C<300,Cross(C,ValueWhen(Peak(H,x5,1) !=Ref(Peak(H,x5,1),-1),H,1)),
//{Price over 300}
Cross(C,ValueWhen(Peak(H,x6,1)!=Ref(Peak(H,x6,1),-1),H,1)))))));

//{Sell }
x1=5; x2=4; x3=3; x4=2; x5=1; x6=.5;
//{Lowest price stock <5}
BB=IIf(C<5,Cross(ValueWhen(Trough(L,x1,1) !=Ref(Trough(L,x1,1),-1),L,1),C),
//{Price between 5 AND 20}
IIf(C>5 AND C<20,Cross(ValueWhen(Trough(L,x2,1) !=Ref(Trough(L,x2,1),-1),L,1),C),
//{Price between 20 AND 70}
IIf(C>20 AND C<70,Cross(ValueWhen(Trough(L,x3,1) !=Ref(Trough(L,x3,1),-1),L,1),C),
//{Price between 70 AND 150}
IIf(C>70 AND C<150,Cross(ValueWhen(Trough(L,x4,1) !=Ref(Trough(L,x4,1),-1),L,1),C),
//{Price between 150 AND 300}
IIf(C>150 AND C<300,Cross(ValueWhen(Trough(L,x5,1) !=Ref(Trough(L,x5,1),-1),L,1),C),
//{Price over 300}
Cross(ValueWhen(Trough(L,x6,1)!=Ref(Trough(L,x6,1),-1),L,1),C))))));

Color=IIf(BarsSince(AA)>BarsSince(BB),colorRed,IIf(RSI()>70,colorCustom11,colorDarkGreen));


///// Trailing Stop Module /////

P6=Param("Trailing Stop Risk",2.5,2,3.5,0.1);
P7=Param("Trailing Stop LookBack",2,5,25,1);
P8=Param("Trailing Stop PrevLow Switch",0,0,1,1);
PrevLow=IIf(P8==1, Ref(C,-TroughBars(C,3,1)) ,Null);
//Plot(PrevLow,"",colorRed);

//Position sizing//
MyTotalPort = Param("MyTotalPort",1000000,10000,10000000,100000);
AcceptableRisk = Param("AcceptableRisk",0.5,0.1,3,0.1);
BarsFromStart = BarsSince(Cross(AA,BB)AND Ref(Color,-1)==colorRed) ;
InitialStopLoss =Ref( H - P6*ATR(P7),-BarsFromStart);
PositionSizing = 0.01*AcceptableRisk*MyTotalPort/( C - InitialStopLoss );

PPP = IIf( HHV(H - P6*ATR(P7),BarsFromStart+1) <C ,HHV(H - P6*ATR(P7),BarsFromStart+1),Null);
Plot( PPP ,"",colorCustom12);

/////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////

_SECTION_BEGIN("Magnified Market Price");
//by Vidyasagar, [email protected]//
FS=Param("Font Size",30,30,100,1);
GfxSelectFont("Arial", FS, 900, italic = False, underline = False, True );
GfxSetBkMode( colorWhite );
GfxSetTextColor( ParamColor("Color",colorYellow) );
Hor=Param("Horizontal Position",1350,1350,1350,1350);
Ver=Param("Vertical Position",12,14,16,18);
GfxTextOut(""+C,Hor , Ver );
YC=TimeFrameGetPrice("C",inDaily,-1);
DD=Prec(C-YC,2);
xx=Prec((DD/YC)*100,2);
GfxSelectFont("Arial", 12, 700, italic = False, underline = False, True );
GfxSetBkMode( colorWhite );
GfxSetTextColor(ParamColor("Color",colorYellow) );
GfxTextOut(""+DD+" ("+xx+"%)", Hor+5, Ver+45 );
_SECTION_END();

/////////////////////////////////////////////////////////////////////////////////////////////////

_SECTION_BEGIN("Ribbon");
uptrend=PDI()>MDI()AND Signal()<MACD();
downtrend=MDI()>PDI()AND Signal()>MACD();
Plot( 1, /*efines the height of the ribbon in percent of pane width */"ribbon",
IIf( uptrend, colorLime, IIf( downtrend, colorRed,IIf(Signal()<MACD(), colorBlack, colorBlack ))), /* choose color */
styleOwnScale|styleArea|styleNoLabel, -01, 50 );
_SECTION_END();

//////////////////////////////////////////////////////////////////////////////////////////////////

GraphXSpace=10;

/////////////////////////////////////////////////////////////////////////////////////////////////
 

rajsumi121

Well-Known Member
Please check this system on daily ... not mine but only copy paste .

/*======================================================
FOREX INTRADAY HEIKIN ASHI + PIVOT POINTS
======================================================*/

_SECTION_BEGIN("The_Beast_2");
SetBarsRequired(10000,10000); /* this ensures that the charts include all bars
AND NOT just those on screen */
Prd1=Param("ATR Period 1-20",4,1,20,1);//{Default = 4 Because most traders use 5}
Prd2=Param("LookBack Period 1-20",7,1,20,1);//{Default = 11 Because most traders use 10}
//{Green} {Start Long position when Close>Green}
Green=HHV(LLV(L,Prd1)+ATR(Prd1),Prd2);
//{Red} {Stop loss when Close<Red}
RED=LLV(HHV(H,Prd1)-ATR(Prd1),Prd2);

HaClose =EMA((O+H+L+C)/4,3);
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );
Color = IIf(C>Green ,colorBrightGreen,IIf(C < RED,colorRed,colorBlue));

PlotOHLC( HaOpen, HaHigh, HaLow, HaClose, "", color, styleCandle,styleThick );

/////////////////////////////////////////////////////////////////////////////////////////////////

Odd=13;//enter Odd numbers only
CoefOdd=round(Odd/2);

Even=12;//enter Even numbers only
Coefeven=Even/2;
Coefeven2=Coefeven+1;

CongestionPercent=2.8;/*Set % above/below Moving average for congestion / sideways market*/

TriangularOdd=MA(MA(C,CoefOdd),CoefOdd);
TriangularEven=MA(MA(C,Coefeven),Coefeven2);

finalMov_avg=IIf(Odd > even,triangularOdd,TriangularEven);

Color=colorBrightGreen;//select Moving average line color
tickercolor=colorBlack;//select price color

Plot(finalMov_avg,"",IIf(C < finalmov_avg,colorRed,Color),styleLine|styleThick);

/////////////////////////////////////////////////////////////////////////////////////////////////

LB= Param("Look Back Periods",10,1,30,1);
R=ValueWhen(Cross(MA(C,LB),C),HHV(H,LB),1);
S=ValueWhen(Cross(C,MA(C,LB)),LLV(L,LB),1);
//UpClose = Close - Ref(Close,-1);
//Color = IIf(UpClose >= 0, colorBlue, colorRed);

UpClose = Close - Ref(Close,-1);
Color = IIf(UpClose > 0, colorBrightGreen, colorRed);


Plot (R,"Resz",ParamColor("R Color",colorRed),8+16);
Plot (S,"Supp",ParamColor("S Color",colorGreen),8+16);

//Plot (R,"Resz",22,8+16);
//Plot (S,"Supp",19,8+16);
// Plot (C,"Close",color,64,32);
GraphXSpace=4;

//////////////////////////////////////////////////////////////////////////////////////////////////////////

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} - "+ EncodeColor(colorYellow)+"Open = "+ EncodeColor(colorYellow) +"%g "+ EncodeColor(colorBrightGreen)+"High = "+ EncodeColor(colorBrightGreen) +"%g - "+ EncodeColor(colorRed)+"Low = "+ EncodeColor(colorRed) +"%g "+ EncodeColor(colorYellow) +"Close = "+ EncodeColor(colorYellow) +" %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));


///////////////////////////////////////////////////////////////////////////////////////// ///////////////////



////////////////////////////////////////////////////////////////////////////////////////////////////////


//Previous Days HI LO //

DayH = TimeFrameGetPrice("H", inDaily, -1); DayHI = LastValue (DayH,1);// yesterdays high
DayL = TimeFrameGetPrice("L", inDaily, -1); DayLI = LastValue (DayL,1); // yesterdays low
DayC = TimeFrameGetPrice("C", inDaily, -1); // yesterdays close
DayO = TimeFrameGetPrice("O", inDaily); // current day open
DayH2= TimeFrameGetPrice("H", inDaily, -2); DayH2I = LastValue (DayH2,1); // Two days before high
DayL2= TimeFrameGetPrice("L", inDaily, -2); DayL2I = LastValue (DayL2,1); // Two days before low
DayH3= TimeFrameGetPrice("H", inDaily, -3); DayH3I = LastValue (DayH3,1); // Three days before high
DayL3= TimeFrameGetPrice("L", inDaily, -3); DayL3I = LastValue (DayL3,1); // Three days before low

numbars = LastValue(Cum(Status("barvisible")));
hts = -33.5;

YHL = ParamToggle("Yesterday HI LO","Show|Hide",1);
if(YHL==1) {
Plot(DayL,"YL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayH,"YH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
PlotText(" YH " , LastValue(BarIndex())-(numbars/Hts), DayHI, colorTurquoise);
PlotText(" YL " , LastValue(BarIndex())-(numbars/Hts), DayLI, colorTurquoise);
}

TDBHL = ParamToggle("2/3Days before HI LO","Show|Hide",0);
if(TDBHL==1) {
Plot(DayL2,"2DBL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayH2,"2DBH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayL3,"3DBL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(DayH3,"3DBH",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
PlotText(" 2DBH " , LastValue(BarIndex())-(numbars/Hts), DayH2I, colorTurquoise);
PlotText(" 2DBL " , LastValue(BarIndex())-(numbars/Hts), DayL2I, colorTurquoise);
PlotText(" 3DBH " , LastValue(BarIndex())-(numbars/Hts), DayH3I, colorTurquoise);
PlotText(" 3DBL " , LastValue(BarIndex())-(numbars/Hts), DayL3I, colorTurquoise);
}

// Pivot Levels //
PP = (DayL + DayH + DayC)/3; PPI = LastValue (PP,1); // Pivot
R1 = (PP * 2) - DayL; R1I = LastValue (R1,1); // Resistance 1
S1 = (PP * 2) - DayH; S1I = LastValue (S1,1); // Support 1
R2 = PP + R1 - S1; R2I = LastValue (R2,1); // Resistance 2
S2 = PP - R1 + S1; S2I = LastValue (S2,1); // Support 2
R3 = PP + R2 - S1; R3I = LastValue (R3,1); // Resistance 3
S3 = PP - R2 + S1; S3I = LastValue (S3,1); // Support 3

ppl = ParamToggle("Pivot Levels","Show|Hide",1);
if(ppl==1) {
Plot(PP, "PP",colorYellow,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(R1, "R1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(S1, "S1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(R2, "R2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(S2, "S2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(R3, "R3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(S3, "S3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);

PlotText(" Pivot ", LastValue(BarIndex())-(numbars/Hts), PPI, colorYellow);
PlotText(" R1 " , LastValue(BarIndex())-(numbars/Hts), R1I, colorViolet);
PlotText(" S1 " , LastValue(BarIndex())-(numbars/Hts), S1I, colorViolet);
PlotText(" R2 " , LastValue(BarIndex())-(numbars/Hts), R2I, colorViolet);
PlotText(" S2 " , LastValue(BarIndex())-(numbars/Hts), S2I, colorViolet);
PlotText(" R3 " , LastValue(BarIndex())-(numbars/Hts), R3I, colorViolet);
PlotText(" S3 " , LastValue(BarIndex())-(numbars/Hts), S3I, colorViolet);
}
// Camerilla Levels //

rg = (DayH - DayL);

H5=DayC+1.1*rg; H5I = LastValue (H5,1);
H4=DayC+1.1*rg/2; H4I = LastValue (H4,1);
H3=DayC+1.1*rg/4; H3I = LastValue (H3,1);
H2=DayC+1.1*rg/6; H2I = LastValue (H2,1);
H1=DayC+1.1*rg/12; H1I = LastValue (H1,1);
L1=DayC-1.1*rg/12; L1I = LastValue (L1,1);
L2=DayC-1.1*rg/6; L2I = LastValue (L2,1);
L3=DayC-1.1*rg/4; L3I = LastValue (L3,1);
L4=DayC-1.1*rg/2; L4I = LastValue (L4,1);
L5=DayC-1.1*rg; L5I = LastValue (L5,1);

pcl = ParamToggle("Camerilla Levels","Show|Hide",0);
if(pcl==1) {
Plot(H5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(H1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L1,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L2,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L3,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L4,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
Plot(L5,"",colorRose,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
PlotText(" H5 = " , LastValue(BarIndex())-(numbars/Hts), H5I +0.05, colorRose);
PlotText(" H4 = " , LastValue(BarIndex())-(numbars/Hts), H4I +0.05, colorRose);
PlotText(" H3 = " , LastValue(BarIndex())-(numbars/Hts), H3I +0.05, colorRose);
PlotText(" H2 = " , LastValue(BarIndex())-(numbars/Hts), H2I +0.05, colorRose);
PlotText(" H1 = " , LastValue(BarIndex())-(numbars/Hts), H1I +0.05, colorRose);
PlotText(" L1 = " , LastValue(BarIndex())-(numbars/Hts), L1I +0.05, colorRose);
PlotText(" L2 = " , LastValue(BarIndex())-(numbars/Hts), L2I +0.05, colorRose);
PlotText(" L3 = " , LastValue(BarIndex())-(numbars/Hts), L3I +0.05, colorRose);
PlotText(" L4 = " , LastValue(BarIndex())-(numbars/Hts), L4I +0.05, colorRose);
PlotText(" L5 = " , LastValue(BarIndex())-(numbars/Hts), L5I +0.05, colorRose);
}

// Current Days Hi Lo //
THL = ParamToggle("Todays Hi Lo","Show|Hide",1);
if(THL==1) {
isRth = TimeNum() >= 084500 & TimeNum() <= 085959;
isdRth = TimeNum() >= 084500 & TimeNum() <= 160000;
aRthL = IIf(isRth, L, 1000000);
aRthH = IIf(isdRth, H, Null);
aRthLd = IIf(isdRth, L, 1000000);
DayH = TimeFrameCompress( aRthH, inDaily, compressHigh );
DayH = TimeFrameExpand( DayH, inDaily, expandFirst );
DayL = TimeFrameCompress( aRthLd, inDaily, compressLow );
DayL = TimeFrameExpand( DayL, inDaily, expandFirst );
Bars = BarsSince(TimeNum() >= 94500 AND TimeNum() < 095959);//,BarIndex(),1); // AND DateNum()==LastValue(DateNum());
x0 = BarCount-LastValue(Bars);
x1 = BarCount-1;
DayHline=LineArray(x0,LastValue(DayH),x1,LastValue (DayH),0);
DayLline=LineArray(x0,LastValue(DayL),x1,LastValue (DayL),0);
DayHlineI = LastValue (DayHline,1);
DayLlineI = LastValue (DayLline,1);
Plot(DayHline,"DayH",colorYellow,styleBar|styleNoRescale|styleNoTitle);
Plot(DayLline,"DayL",colorYellow,styleBar|styleNoRescale|styleNoTitle);
PlotText(" Day Hi " , LastValue(BarIndex())-(numbars/Hts), DayHlineI +0.05, colorYellow);
PlotText(" Day Lo " , LastValue(BarIndex())-(numbars/Hts), DayLlineI +0.05, colorYellow);
}

/////////////////////////////////////////////////////////////////////////////////////////////////

_SECTION_BEGIN("Pivot_Finder");
/* **********************************

Code to automatically identify pivots

********************************** */

// -- what will be our lookback range for the hh and ll?
farback=Param("How Far back to go",200,0,5000,10);
nBars = Param("Number of bars", 12, 5, 40);


// -- Create 0-initialized arrays the size of barcount

aHPivs = H - H;

aLPivs = L - L;

// -- More for future use, not necessary for basic plotting

aHPivHighs = H - H;

aLPivLows = L - L;

aHPivIdxs = H - H;

aLPivIdxs = L - L;

nHPivs = 0;

nLPivs = 0;

lastHPIdx = 0;

lastLPIdx = 0;

lastHPH = 0;

lastLPL = 0;

curPivBarIdx = 0;

// -- looking back from the current bar, how many bars

// back were the hhv and llv values of the previous

// n bars, etc.?

aHHVBars = HHVBars(H, nBars);

aLLVBars = LLVBars(L, nBars);

aHHV = HHV(H, nBars);

aLLV = LLV(L, nBars);

// -- Would like to set this up so pivots are calculated back from

// last visible bar to make it easy to "go back" and see the pivots

// this code would find. However, the first instance of

// _Trace output will show a value of 0

aVisBars = Status("barvisible");

nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));

_TRACE("Last visible bar: " + nLastVisBar);

// -- Initialize value of curTrend

curBar = (BarCount-1);

curTrend = "";

if (aLLVBars[curBar] <

aHHVBars[curBar]) {

curTrend = "D";

}

else {

curTrend = "U";

}

// -- Loop through bars. Search for

// entirely array-based approach

// in future version

for (i=0; i<farback; i++) {

curBar = (BarCount - 1) - i;

// -- Have we identified a pivot? If trend is down...

if (aLLVBars[curBar] < aHHVBars[curBar]) {

// ... and had been up, this is a trend change

if (curTrend == "U") {

curTrend = "D";

// -- Capture pivot information

curPivBarIdx = curBar - aLLVBars[curBar];

aLPivs[curPivBarIdx] = 1;

aLPivLows[nLPivs] = L[curPivBarIdx];

aLPivIdxs[nLPivs] = curPivBarIdx;

nLPivs++;

}

// -- or current trend is up

} else {

if (curTrend == "D") {

curTrend = "U";

curPivBarIdx = curBar - aHHVBars[curBar];

aHPivs[curPivBarIdx] = 1;

aHPivHighs[nHPivs] = H[curPivBarIdx];

aHPivIdxs[nHPivs] = curPivBarIdx;

nHPivs++;

}

// -- If curTrend is up...else...

}

// -- loop through bars

}

// -- Basic attempt to add a pivot this logic may have missed

// -- OK, now I want to look at last two pivots. If the most

// recent low pivot is after the last high, I could

// still have a high pivot that I didn't catch

// -- Start at last bar

curBar = (BarCount-1);

candIdx = 0;

candPrc = 0;

lastLPIdx = aLPivIdxs[0];

lastLPL = aLPivLows[0];

lastHPIdx = aHPivIdxs[0];

lastHPH = aHPivHighs[0];

if (lastLPIdx > lastHPIdx) {

// -- Bar and price info for candidate pivot

candIdx = curBar - aHHVBars[curBar];

candPrc = aHHV[curBar];

if (

lastHPH < candPrc AND

candIdx > lastLPIdx AND

candIdx < curBar) {


// -- OK, we'll add this as a pivot...

aHPivs[candIdx] = 1;

// ...and then rearrange elements in the

// pivot information arrays

for (j=0; j<nHPivs; j++) {

aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-

(j+1)];

aHPivIdxs[nHPivs-j] = aHPivIdxs[nHPivs-(j+1)];

}

aHPivHighs[0] = candPrc ;

aHPivIdxs[0] = candIdx;

nHPivs++;

}

} else {


// -- Bar and price info for candidate pivot

candIdx = curBar - aLLVBars[curBar];

candPrc = aLLV[curBar];

if (

lastLPL > candPrc AND

candIdx > lastHPIdx AND

candIdx < curBar) {


// -- OK, we'll add this as a pivot...

aLPivs[candIdx] = 1;

// ...and then rearrange elements in the

// pivot information arrays

for (j=0; j<nLPivs; j++) {

aLPivLows[nLPivs-j] = aLPivLows[nLPivs-(j+1)];

aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-(j+1)];

}

aLPivLows[0] = candPrc;

aLPivIdxs[0] = candIdx;

nLPivs++;

}

}

// -- Dump inventory of high pivots for debugging



for (k=0; k<nHPivs; k++) {

_TRACE("High pivot no. " + k

+ " at barindex: " + aHPivIdxs[k] + ", "

+ WriteVal(ValueWhen(BarIndex()==aHPivIdxs[k],

DateTime(), 1), formatDateTime)

+ ", " + aHPivHighs[k]);

}

//////////////////////////////////////////////////////////////////////////////

// -- OK, let's plot the pivots using arrows

PlotShapes(

IIf(aHPivs==1, shapeStar, shapeNone), colorBrightGreen, 0, H, 25);

PlotShapes(

IIf(aLPivs==1, shapeStar , shapeNone), colorRed, 0, L, -20);

//////////////////////////////////////////////////////////////////////////////


/* **********************************

Code to automatically identify pivots

********************************** */
// -- what will be our lookback range for the hh and ll?
farback=Param("How Far back to go",200,12,30,1);
nBars = Param("Number of bars", 12, 1, 30, 1);
// -- Create 0-initialized arrays the size of barcount
aHPivs = H - H;
aLPivs = L - L;
// -- More for future use, not necessary for basic plotting
aHPivHighs = H - H;
aLPivLows = L - L;
aHPivIdxs = H - H;
aLPivIdxs = L - L;
nHPivs = 0;
nLPivs = 0;
lastHPIdx = 0;
lastLPIdx = 0;
lastHPH = 0;
lastLPL = 0;
curPivBarIdx = 0;
// -- looking back from the current bar, how many bars
// back were the hhv and llv values of the previous
// n bars, etc.?
aHHVBars = HHVBars(H, nBars);
aLLVBars = LLVBars(L, nBars);
aHHV = HHV(H, nBars);
aLLV = LLV(L, nBars);
// -- Would like to set this up so pivots are calculated back from
// last visible bar to make it easy to "go back" and see the pivots
// this code would find. However, the first instance of
// _Trace output will show a value of 0
aVisBars = Status("barvisible");
nLastVisBar = LastValue(Highest(IIf(aVisBars, BarIndex(), 0)));
_TRACE("Last visible bar: " + nLastVisBar);
// -- Initialize value of curTrend
curBar = (BarCount-1);
curTrend = "";
if (aLLVBars[curBar] <
aHHVBars[curBar]) {
curTrend = "D";
}
else {
curTrend = "U";
}
// -- Loop through bars. Search for
// entirely array-based approach
// in future version
for (i=0; i<farback; i++) {
curBar = (BarCount - 1) - i;
// -- Have we identified a pivot? If trend is down...
if (aLLVBars[curBar] < aHHVBars[curBar]) {
// ... and had been up, this is a trend change
if (curTrend == "U") {
curTrend = "D";
// -- Capture pivot information
curPivBarIdx = curBar - aLLVBars[curBar];
aLPivs[curPivBarIdx] = 1;
aLPivLows[nLPivs] = L[curPivBarIdx];
aLPivIdxs[nLPivs] = curPivBarIdx;
nLPivs++;
}
// -- or current trend is up
} else {
if (curTrend == "D") {
curTrend = "U";
curPivBarIdx = curBar - aHHVBars[curBar];
aHPivs[curPivBarIdx] = 1;
aHPivHighs[nHPivs] = H[curPivBarIdx];
aHPivIdxs[nHPivs] = curPivBarIdx;
nHPivs++;
}
// -- If curTrend is up...else...
}
// -- loop through bars
}
// -- Basic attempt to add a pivot this logic may have missed
// -- OK, now I want to look at last two pivots. If the most
// recent low pivot is after the last high, I could
// still have a high pivot that I didn't catch
// -- Start at last bar
curBar = (BarCount-1);
candIdx = 0;
candPrc = 0;
lastLPIdx = aLPivIdxs[0];
lastLPL = aLPivLows[0];
lastHPIdx = aHPivIdxs[0];
lastHPH = aHPivHighs[0];
if (lastLPIdx > lastHPIdx) {
// -- Bar and price info for candidate pivot
candIdx = curBar - aHHVBars[curBar];
candPrc = aHHV[curBar];
if (
lastHPH < candPrc AND
candIdx > lastLPIdx AND
candIdx < curBar) {
// -- OK, we'll add this as a pivot...
aHPivs[candIdx] = 1;
// ...and then rearrange elements in the
// pivot information arrays
for (j=0; j<nHPivs; j++) {
aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-
(j+1)];
aHPivIdxs[nHPivs-j] = aHPivIdxs[nHPivs-(j+1)];
}
aHPivHighs[0] = candPrc ;
aHPivIdxs[0] = candIdx;
nHPivs++;
}
} else {
// -- Bar and price info for candidate pivot
candIdx = curBar - aLLVBars[curBar];
candPrc = aLLV[curBar];
if (
lastLPL > candPrc AND
candIdx > lastHPIdx AND
candIdx < curBar) {
// -- OK, we'll add this as a pivot...
aLPivs[candIdx] = 1;
// ...and then rearrange elements in the
// pivot information arrays
for (j=0; j<nLPivs; j++) {
aLPivLows[nLPivs-j] = aLPivLows[nLPivs-(j+1)];
aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-(j+1)];
}
aLPivLows[0] = candPrc;
aLPivIdxs[0] = candIdx;
nLPivs++;
}
}
// -- Dump inventory of high pivots for debugging
/*
for (k=0; k<nHPivs; k++) {
_TRACE("High pivot no. " + k
+ " at barindex: " + aHPivIdxs[k] + ", "
+ WriteVal(ValueWhen(BarIndex()==aHPivIdxs[k],
DateTime(), 1), formatDateTime)
+ ", " + aHPivHighs[k]);
}
*/
// -- OK, let's plot the pivots using arrows
PlotShapes(IIf(aHPivs==1, shapeSmallDownTriangle, shapeNone), colorCustom12, 0, High, Offset=-5);
PlotShapes(IIf(aLPivs==1, shapeSmallUpTriangle , shapeNone), colorCustom11, 0, Low, Offset=-5);

Sell = aHPivs == 1 ;
Buy = aLPivs == 1 ;
Filter=Buy OR Sell;
Sell=ExRem(Sell,Buy);
Buy=ExRem(Buy,Sell);

///////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////

R=3;//Marking circle radius
m=0.015*(HHV(H,159)-LLV(L,159));//scaling factor
x=Cum(1);
per = 2.5;//Sensitivity Calibration
pR = PeakBars( H, per, 1 ) == 0;//Peak condition
x01= LastValue(ValueWhen( pR, x, 1 ));
x02=LastValue(ValueWhen( pR, x, 2 ));
x03=LastValue(ValueWhen( pR, x, 3 ));
y01 = LastValue(ValueWhen( pR, H, 1 ) );
y02=LastValue(ValueWhen( pR, H, 2 ) );
y03 = LastValue( ValueWhen( pR, H, 3 ));

y0=y01;x0=x01;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y02;x0=x02;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y03;x0=x03;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
TpR = TroughBars( L, per, 1 ) == 0;//Trough condition
x01T= LastValue(ValueWhen( TpR, x, 1 ));
x02T=LastValue(ValueWhen( TpR, x, 2 ));
x03T=LastValue(ValueWhen( TpR, x, 3 ));
y01T = LastValue(ValueWhen( TpR, L, 1 ) );
y02T=LastValue(ValueWhen( TpR, L, 2 ) );
y03T = LastValue( ValueWhen( TpR, L, 3 ));
y0T=y01T;x0T=x01T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(Y,"",5,1);Plot(Y1,"",5,1);
y0T=y02T;x0T=x02T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);
y0T=y03T;x0T=x03T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);

R=2;//Marking circle radius
m=0.015*(HHV(H,159)-LLV(L,159));//scaling factor
x=Cum(1);
per = 1.5;//Sensitivity Calibration
pR = PeakBars( H, per, 1 ) == 0;//Peak condition
x01= LastValue(ValueWhen( pR, x, 1 ));
x02=LastValue(ValueWhen( pR, x, 2 ));
x03=LastValue(ValueWhen( pR, x, 3 ));
y01 = LastValue(ValueWhen( pR, H, 1 ) );
y02=LastValue(ValueWhen( pR, H, 2 ) );
y03 = LastValue( ValueWhen( pR, H, 3 ));

y0=y01;x0=x01;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y02;x0=x02;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y03;x0=x03;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
TpR = TroughBars( L, per, 1 ) == 0;//Trough condition
x01T= LastValue(ValueWhen( TpR, x, 1 ));
x02T=LastValue(ValueWhen( TpR, x, 2 ));
x03T=LastValue(ValueWhen( TpR, x, 3 ));
y01T = LastValue(ValueWhen( TpR, L, 1 ) );
y02T=LastValue(ValueWhen( TpR, L, 2 ) );
y03T = LastValue( ValueWhen( TpR, L, 3 ));
y0T=y01T;x0T=x01T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(Y,"",5,1);Plot(Y1,"",5,1);
y0T=y02T;x0T=x02T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);
y0T=y03T;x0T=x03T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);


////////////////////////////////////////////////////////////////////////////////

R=1;//Marking circle radius
m=0.015*(HHV(H,159)-LLV(L,159));//scaling factor
x=Cum(1);
per = 0.325;//Sensitivity Calibration
pR = PeakBars( H, per, 1 ) == 0;//Peak condition
x01= LastValue(ValueWhen( pR, x, 1 ));
x02=LastValue(ValueWhen( pR, x, 2 ));
x03=LastValue(ValueWhen( pR, x, 3 ));
y01 = LastValue(ValueWhen( pR, H, 1 ) );
y02=LastValue(ValueWhen( pR, H, 2 ) );
y03 = LastValue( ValueWhen( pR, H, 3 ));

y0=y01;x0=x01;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y02;x0=x02;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
y0=y03;x0=x03;
y=y0+m*sqrt(R^2-(x-x0)^2);
y1=y0-m*sqrt(R^2-(x-x0)^2);
Plot(y,"",4,1);Plot(y1,"",4,1);
TpR = TroughBars( L, per, 1 ) == 0;//Trough condition
x01T= LastValue(ValueWhen( TpR, x, 1 ));
x02T=LastValue(ValueWhen( TpR, x, 2 ));
x03T=LastValue(ValueWhen( TpR, x, 3 ));
y01T = LastValue(ValueWhen( TpR, L, 1 ) );
y02T=LastValue(ValueWhen( TpR, L, 2 ) );
y03T = LastValue( ValueWhen( TpR, L, 3 ));
y0T=y01T;x0T=x01T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(Y,"",5,1);Plot(Y1,"",5,1);
y0T=y02T;x0T=x02T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);
y0T=y03T;x0T=x03T;
y=y0T+m*sqrt(R^2-(x-x0T)^2);
y1=y0T-m*sqrt(R^2-(x-x0T)^2);
Plot(y,"",5,1);Plot(y1,"",5,1);

EMA34 = EMA(C,34);
LSMA = LinearReg(C,23);

/////////////////////////////////////////////////////////////////////////////////////////////////

_SECTION_BEGIN("Price");

SetChartOptions(0,chartShowArrows|chartShowDates);

_N(Title = EncodeColor(colorCustom12) +StrFormat(" {{NAME}} - {{INTERVAL}} {{DATE}} "+ EncodeColor(colorWhite) +" Open = "+ EncodeColor(colorWhite) +"%g "+ EncodeColor(colorBrightGreen) +" High : "+ EncodeColor(colorBrightGreen) +" %g "+ EncodeColor(colorRed) +" - Low : "+ EncodeColor(colorRed) +" %g "+ EncodeColor(colorWhite) +" Close = "+ EncodeColor(colorWhite) +"%g
{{VALUES}}",O,H,L,C ));

_SECTION_END();

/////////////////////////////////////////////////////////////////////////////////////////////////


_SECTION_BEGIN("Break Outs");
//{Buy}
x1=5; x2=4; x3=3; x4=2; x5=1; x6=.5;
//{Lowest price stock <5}
AA=IIf(C<5,Cross(C,ValueWhen(Peak(H,x1,1) !=Ref(Peak(H,x1,1),-1),H,1)),
//{Price between 5 AND 20}
IIf(C>5 AND C<20,Cross(C,ValueWhen(Peak(H,x2,1) !=Ref(Peak(H,x2,1),-1),H,1)),
//{Price between 20 AND 70}
IIf(C>20 AND C<70,Cross(C,ValueWhen(Peak(H,x3,1) !=Ref(Peak(H,x3,1),-1),H,1)),
//{Price between 70 AND 150}
IIf(C>70 AND C<150,Cross(C,ValueWhen(Peak(H,x4,1) !=Ref(Peak(H,x4,1),-1),H,1)),
//{Price between 150 AND 300}
IIf(C>150 AND C<300,Cross(C,ValueWhen(Peak(H,x5,1) !=Ref(Peak(H,x5,1),-1),H,1)),
//{Price over 300}
Cross(C,ValueWhen(Peak(H,x6,1)!=Ref(Peak(H,x6,1),-1),H,1)))))));

//{Sell }
x1=5; x2=4; x3=3; x4=2; x5=1; x6=.5;
//{Lowest price stock <5}
BB=IIf(C<5,Cross(ValueWhen(Trough(L,x1,1) !=Ref(Trough(L,x1,1),-1),L,1),C),
//{Price between 5 AND 20}
IIf(C>5 AND C<20,Cross(ValueWhen(Trough(L,x2,1) !=Ref(Trough(L,x2,1),-1),L,1),C),
//{Price between 20 AND 70}
IIf(C>20 AND C<70,Cross(ValueWhen(Trough(L,x3,1) !=Ref(Trough(L,x3,1),-1),L,1),C),
//{Price between 70 AND 150}
IIf(C>70 AND C<150,Cross(ValueWhen(Trough(L,x4,1) !=Ref(Trough(L,x4,1),-1),L,1),C),
//{Price between 150 AND 300}
IIf(C>150 AND C<300,Cross(ValueWhen(Trough(L,x5,1) !=Ref(Trough(L,x5,1),-1),L,1),C),
//{Price over 300}
Cross(ValueWhen(Trough(L,x6,1)!=Ref(Trough(L,x6,1),-1),L,1),C))))));

Color=IIf(BarsSince(AA)>BarsSince(BB),colorRed,IIf(RSI()>70,colorCustom11,colorDarkGreen));


///// Trailing Stop Module /////

P6=Param("Trailing Stop Risk",2.5,2,3.5,0.1);
P7=Param("Trailing Stop LookBack",2,5,25,1);
P8=Param("Trailing Stop PrevLow Switch",0,0,1,1);
PrevLow=IIf(P8==1, Ref(C,-TroughBars(C,3,1)) ,Null);
//Plot(PrevLow,"",colorRed);

//Position sizing//
MyTotalPort = Param("MyTotalPort",1000000,10000,10000000,100000);
AcceptableRisk = Param("AcceptableRisk",0.5,0.1,3,0.1);
BarsFromStart = BarsSince(Cross(AA,BB)AND Ref(Color,-1)==colorRed) ;
InitialStopLoss =Ref( H - P6*ATR(P7),-BarsFromStart);
PositionSizing = 0.01*AcceptableRisk*MyTotalPort/( C - InitialStopLoss );

PPP = IIf( HHV(H - P6*ATR(P7),BarsFromStart+1) <C ,HHV(H - P6*ATR(P7),BarsFromStart+1),Null);
Plot( PPP ,"",colorCustom12);

/////////////////////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////////////////////

_SECTION_BEGIN("Magnified Market Price");
//by Vidyasagar, [email protected]//
FS=Param("Font Size",30,30,100,1);
GfxSelectFont("Arial", FS, 900, italic = False, underline = False, True );
GfxSetBkMode( colorWhite );
GfxSetTextColor( ParamColor("Color",colorYellow) );
Hor=Param("Horizontal Position",1350,1350,1350,1350);
Ver=Param("Vertical Position",12,14,16,18);
GfxTextOut(""+C,Hor , Ver );
YC=TimeFrameGetPrice("C",inDaily,-1);
DD=Prec(C-YC,2);
xx=Prec((DD/YC)*100,2);
GfxSelectFont("Arial", 12, 700, italic = False, underline = False, True );
GfxSetBkMode( colorWhite );
GfxSetTextColor(ParamColor("Color",colorYellow) );
GfxTextOut(""+DD+" ("+xx+"%)", Hor+5, Ver+45 );
_SECTION_END();

/////////////////////////////////////////////////////////////////////////////////////////////////

_SECTION_BEGIN("Ribbon");
uptrend=PDI()>MDI()AND Signal()<MACD();
downtrend=MDI()>PDI()AND Signal()>MACD();
Plot( 1, /*efines the height of the ribbon in percent of pane width */"ribbon",
IIf( uptrend, colorLime, IIf( downtrend, colorRed,IIf(Signal()<MACD(), colorBlack, colorBlack ))), /* choose color */
styleOwnScale|styleArea|styleNoLabel, -01, 50 );
_SECTION_END();

//////////////////////////////////////////////////////////////////////////////////////////////////

GraphXSpace=10;

/////////////////////////////////////////////////////////////////////////////////////////////////




Only trade on * :clap::clap::clapping:
 
Hi Veluri,

first let me thank you for such a good informative postings. can you please email me your personal contact details so i have some questions on amibroker etc..my id is nsoundar-at-gmail-dot-com

thanks
soundaran
 
Status
Not open for further replies.

Similar threads