Two Days Breakout System

Two Days Breakout System
 

//www.aflcode.com
_SECTION_BEGIN("2daybrkOut");

SetChartOptions(0,chartShowArrows|chartShowDates);


LClose = Close;

// Calculations for Buy and Sell Signals

DC1=TimeFrameGetPrice( "C", inDaily, -1);
DC2=TimeFrameGetPrice( "C", inDaily, -2);
DH1=TimeFrameGetPrice( "H", inDaily, -1);
DH2=TimeFrameGetPrice( "H", inDaily, -2);
DL1=TimeFrameGetPrice( "L", inDaily, -1);
DL2=TimeFrameGetPrice( "L", inDaily, -2);
DO1=TimeFrameGetPrice( "O", inDaily, -1);
DO2=TimeFrameGetPrice( "O", inDaily, -2);
MaxH=Max(DH1,DH2);
MinL=Min(DL1,DL2);

Plot(MaxH,"2 Day High",colorGreen,styleLine);
Plot(MinL,"2 Day Low",colorRed,styleLine);

Plot_Range1 = (TimeNum() >= 091500 AND TimeNum()<= 153000) AND (DateNum()==LastValue(DateNum()));

mf = Param("Percentage",0.5,0.1,5,0.1);

wDay = Param("Scan Day",LastValue(Day()),1,31,1);

ORhigh = MaxH*(1-mf/100);
ORlow = MinL*(1+mf/100);

BreakOut = IIf(LClose>=ORhigh, LClose, 0);
breakDown = IIf(LClose<=ORlow, LClose, 0);

COL=IIf(BreakOut,colorGreen,IIf(breakDOWN,colorRed,Null));
Plot(C,"",COL,64);

//_SECTION_BEGIN("Volume");
Plot( Volume, _DEFAULT_NAME(), ParamColor("Color", colorLavender ), styleNoTitle | ParamStyle( "Style", styleHistogram | styleOwnScale | styleThick | styleNoLabel, maskHistogram  ), 2 );
//_SECTION_END();


//vr=ParamToggle("Plot Daily High/Low","Yes|No" ,1);
//if(VR  == 0)
//{
//Plot(FourDayHigh,"High Breakout", ParamColor("Hi Color",colorBlue));
//Plot(FourDayLow,"Low Breakout", ParamColor("Low Color",colorRed));
//Plot(ORHigh,"ORH", ParamColor("ORHi Color",colorBlue),32);
//Plot(ORLow,"ORL", ParamColor("ORLow Color",colorRed),32);
//Plot(P,"PIVOT", ParamColor("P Color",colorTan),8);
//Plot(R1,"", ParamColor("R1 Color",colorGreen),1);
//Plot(R2,"", ParamColor("R2 Color",colorGreen),1);
//Plot(S1,"", ParamColor("S1 Color",colorLightOrange),1);
//Plot(S2,"", ParamColor("S2 Color",colorLightOrange),1);
//}

//Bprice=IIf(Buy,LClose,0); //Buying Price
//Sprice=IIf(Sell,LClose,0); //Selling Price

// Output of Exploration results and Alert of Buy and Sell Signals and Plotting of breakout and breakdown levels
//AddColumn(Bprice,"Buy price");
//AddColumn(BTarget1,"BT 1");
//AddColumn(BTarget2,"BT 2");
//AddColumn(sprice,"Sell price");
//AddColumn(STarget1,"ST 1");
//AddColumn(STarget2,"ST 2");

/*AddColumn(LClose,"CMP");
AddColumn(TwoDayHigh,"New High",1.2,colorDefault,colorDefault,90);
AddColumn(TwoDayLow,"New Low",1.2,colorDefault,colorDefault,90);
AddColumn(TodayHigh,"Today High",1.2,colorDefault,colorDefault,90);
AddColumn(TodayLow,"Today Low",1.2,colorDefault,colorDefault,90);
AddColumn(P,"Pivot",1.2,colorDefault,colorDefault,90);
AddColumn(ORHigh,"OR High",1.2,colorDefault,colorDefault,90);
AddColumn(ORLow,"OR Low",1.2,colorDefault,colorDefault,90);
AddColumn(Now(format=4),"Time Now",1.2,colorDefault,colorDefault,90);
AddColumn(TimeNum(),"Time Then",1.2,colorDefault,colorDefault,90);
AddColumn(Now(format=4)-TimeNum(),"Diff",1.2,colorDefault,colorDefault,90);*/

//AlertIf(Buy,"","Buy " + Name() + " above " + FourDayHigh + ", CMP: " + C + ", SL " + Prec(P,2) + ", Targets " + Prec(BTarget1,2) +","+Prec(BTarget2,2) + "," +Prec(BTarget3,2), 1);
//AlertIf(Sell,"","Sell " + Name() + " below " + FourDayLow + ", CMP: " + C + ", SL " + Prec(P,2) + ", Targets " + Prec(STarget1,2) +","+Prec(STarget2,2) + "," +Prec(STarget3,2), 2);

//Title = EncodeColor(colorBlue)+ "Two Day BreakOut( "+mf+")%" + " - " +  Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorBlue) +
// "  - " + Date() +" - "+"\n" +EncodeColor(colorGreen) +"Op-"+O+"  "+"Hi-"+H+"  "+"Lo-"+L+"  "+
//"Cl-"+C+"  "+ "Vol= "+ WriteVal(V)+"\n"+EncodeColor(colorDarkRed) +"-----------------------------system says----------------------------------------"+"\n";
//EncodeColor(colorDarkGreen)+
//WriteIf (Buy , "GO LONG / cover  at ="+C+"\n"+EncodeColor(colorRed) +"Stop- toadys low ="+DL+"\n"+
//EncodeColor(colorDarkRed));
//WriteIf (Short ,"EXIT LONG / short  at "+C+"\n"+EncodeColor(colorRed) +"Stop-todays high "+DH+"\n");
_SECTION_END();


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

_SECTION_BEGIN("ATR Intraday Swing Trade");

period = Param("Period", 20, 1, 240, 1);
mult = Param("Multiplier",3.3 , 1, 240, 0.1);


// ATR Trading for 5 Minutes //

TimeFrameSet(in5Minute);

f=ATR(period);

VS[0] = Close[0];
trend[0] = 0;
HighC[0]=0;
Lowc[0]=0;

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

vs[i] = vs[i-1];
trend[i] = trend[i-1];
highC[i] = HighC[i-1];
lowc[i] = lowc[i-1];

if ((trend[i]>=0) && ( C[i] <VS[i] ))
{
trend[i] =-1;
HighC[i] = C[i];
lowc[i] = C[i];
}

if ((trend[i]<=0) && (C[i] >VS[i]))
{
trend[i]=1;
HighC[i] = C[i];
lowc[i] = C[i];
}

if (trend[i]==-1)
{
if (C[i]<lowc[i]) lowc[i] = C[i];
VS[i]= lowc[i]+ (mult*f[i]);
}


if (trend[i]==1)
{
if (C[i]>HighC[i]) HighC[i] = C[i];
VS[i]= HighC[i]-(mult*f[i]);
}

}

TimeFrameRestore();


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

_SECTION_BEGIN("default");
TimeFrameSet(inHourly);
HaClose =EMA((O+H+L+C)/4,6); 
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 ); 
HaHigh = Max( H, Max( HaClose, HaOpen ) ); 
HaLow = Min( L, Min( HaClose, HaOpen ) );
TimeFrameRestore();
HAopenf=TimeFrameExpand( Haopen, inHourly);  
Haclosef=TimeFrameExpand( Haclose, inHourly);
HaHighf=TimeFrameExpand( Hahigh, inHourly);
HaLowf=TimeFrameExpand( Halow, inHourly);
Color = IIf( Haopenf > Haclosef,4, IIf( Haopenf == Haclosef,colorYellow, 6));
_SECTION_END();

_SECTION_BEGIN("4");
Compress4= Param("Compression4",6,1,12,1); 
TimeFrameSet(Compress4* Interval());
HaClose4 =EMA((O+H+L+C)/4,6); 
HaOpen4 = AMA( Ref( HaClose4, -1 ), 0.5 ); 
HaHigh4 = Max( H, Max( HaClose4, HaOpen4 ) ); 
HaLow4 = Min( L, Min( HaClose4, HaOpen4 ) ); 
TimeFrameRestore();
HAopen4f=TimeFrameExpand( Haopen4, Compress4* Interval());  
Haclose4f=TimeFrameExpand( Haclose4, Compress4* Interval());
HaHigh4f=TimeFrameExpand( Hahigh4, Compress4* Interval());
HaLow4f=TimeFrameExpand( Halow4, Compress4* Interval());
Color4 = IIf( Haopen4f > Haclose4f,4, IIf( Haopen4f == Haclose4f ,colorYellow, 6));
_SECTION_END();

_SECTION_BEGIN("5minute");
TimeFrameSet(in5Minute);
HaClose5 =EMA((O+H+L+C)/4,6); 
HaOpen5 = AMA( Ref( HaClose5, -1 ), 0.5 ); 
HaHigh5 = Max( H, Max( HaClose5, HaOpen5 ) ); 
HaLow5 = Min( L, Min( HaClose5, HaOpen5 ) );
TimeFrameRestore();
HAopen5f=TimeFrameExpand( Haopen5, in5Minute);  
Haclose5f=TimeFrameExpand( Haclose5, in5Minute);
HaHigh5f=TimeFrameExpand( Hahigh5, in5Minute);
HaLow5f=TimeFrameExpand( Halow5, in5Minute);
Color5 = IIf( Haopen5f > Haclose5f,colorRed, IIf( Haopen5f == Haclose5f,colorYellow, colorGreen));
_SECTION_END();

Plot(VS, "Stop Loss",IIf(trend==1,10,11 ),styleThick);

mkol = IIf( Trend==1, colorGreen, colorRed);

Plot(2,"", Color5, styleOwnScale|styleArea|styleNoLabel, -0.5, 100);
Plot(2,"", Color4, styleHistogram+styleThick|styleOwnScale|styleNoLabel, -2, 100 );
Plot(2,"", Color,  styleOwnScale|styleArea|styleNoLabel, -4, 100 );

//Title = EncodeColor(colorBlue)+"Shiv Intraday ATR Trading System :-"+"  "+ Name() + "  "+ EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorBlue) + 
// " - " + Date() + "    MACD : "+ MACD() + ",   Signal : " + Signal() + "Current : " + LongExit +"," + ShortExit +"\n" +
//"Buy : "+ValueWhen(Buy,Close,1)+"   Sell : "+ValueWhen(Short,Close,1) +"  Signals : " + LongPrice + "  " + ShrtPrice + 
//EncodeColor(colorRed)+
//WriteIf (Buy , " GO LONG at "+C+"  ","")+
//WriteIf (Sell , " EXIT LONG / Reverse Signal at "+C+"  ","")+
//WriteIf (Short , " GO SHORT at "+C+"  ","")+
//WriteIf (Cover , " EXIT SHORT / Reverse Signal at "+C+"  ","")+"\n"+EncodeColor(colorBlue)+
//WriteIf(Buy , "Total Profit/Loss for the Last Trade Rs."+(C-BuyPrice)+"","")+
//WriteIf(Short  , "Total Profit/Loss for the Last trade Rs."+(ShortPrice-C)+"","")+
//WriteIf(Long AND NOT Buy, "Trade : Long - Entry price Rs."+(BuyPrice),"")+
//WriteIf(shrt AND NOT Short, "Trade : Short - Entry price Rs."+(ShortPrice),"")+"\n"+ 
//WriteIf(Long AND NOT Buy, "Current Profit/Loss Rs."+(C-BuyPrice)+"","")+
//WriteIf(Shrt AND NOT Short, "Current Profit/Loss Rs."+(ShortPrice-C)+"","");
_SECTION_END();

/////////////////////////////////           Trend Indicator on Background  ////////////////////////////////////////

function ZeroLagTEMA( array, period )
{
 TMA1 = TEMA( array, period );
 TMA2 = TEMA( TMA1, period );
 Diff = TMA1 - TMA2;
 return TMA1 + Diff ;
}
 
/////////////////////
// Heikin-Ashi code
HaClose = (O+H+L+C)/4;
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
 
avp = Param("Up TEMA avg", 34, 1, 100 );
avpdn = Param("Dn TEMA avg", 34, 1, 100 );
 
// Velvoort is using not original, but modified Heikin-Ashi close
HaClose = ( HaClose + HaOpen + Max( H, HaOpen ) + Min( L, HaOpen ) )/4;
 
// up average
ZlHa = ZeroLagTEMA( HaClose, avp );
ZlCl = ZeroLagTEMA( ( H + L ) / 2, avp );
ZlDif = ZlCl - ZlHa;
 
keep1 = Hold( HaClose >= HaOpen, 2 );
keep2 = ZlDif >= 0;
keeping = keep1 OR keep2;
keepall = keeping OR ( Ref( keeping, -1 ) AND ( C > O ) OR C >= Ref( C, -1 ) );
keep3 = abs( C - O ) < ( H - L ) * 0.35 AND H >= Ref( L, -1 );
utr = keepall OR ( Ref( keepall, -1 ) AND keep3 );
 
// dn average
ZlHa = ZeroLagTEMA( HaClose, avpdn );
ZlCl = ZeroLagTEMA( ( H + L ) / 2, avpdn );
ZlDif = ZlCl - ZlHa;
 
keep1 = Hold( HaClose < HaOpen, 2 );
keep2 = ZlDif < 0;
keeping = keep1 OR keep2;
keepall = keeping OR ( Ref( keeping, -1 ) AND ( C < O ) OR C < Ref( C, -1 ) );
keep3 = abs( C - O ) < ( H - L ) * 0.35 AND L <= Ref( H, -1 );
dtr = keepall OR ( Ref( keepall, -1 ) AND keep3 );
 
upw = dtr == 0 AND Ref( dtr, -1 ) AND utr;
dnw = utr == 0 AND Ref( utr, -1 ) AND dtr;
 
Haco = Flip( upw, dnw );

if( ParamToggle("Trend Indicator in Background ?", "Yes / No")) 
{ 
if( ParamToggle("Chart Type", "Price with color back|HACO wave" ) )
{
 Plot( Haco, "Haco", colorRed );
}
else
{
 //Plot( C, "Close", colorBlack,
   //    ParamStyle( "Style", styleCandle, maskPrice ) );
 Plot( 1, "", IIf( Haco , colorPaleGreen, colorRose ),styleArea | styleOwnScale, 0, 1 );
}
}
else
{
Plot( C, "Close", colorBlack,ParamStyle( "Style", styleCandle, maskPrice ) );
}

// ****************************************************       Buy & Sell           *************************************

Buy = (LClose >= MaxH) AND (LClose > VS) AND LastValue(Day())==wDay; 
Short =(LClose <= MinL) AND (LClose < VS) AND LastValue(Day())==wDay; 

// Calculation of Stop Loss and Exit points
BuyPrice=ValueWhen(Buy,C,1);
ShortPrice=ValueWhen(Short,C,1);
BuyExit=BuyPrice*(1-1/100);
ShortExit=ShortPrice*(1+1/100);

Sell = (LClose < VS);
Cover = (LClose > VS);

Buy=ExRem(Buy, Sell);
Sell=ExRem(Sell, Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);

PlotShapes(Buy*shapeUpArrow,5,0,L,-15);
PlotShapes(Sell*shapeHollowDownArrow,5,0,H,-15);
PlotShapes(Short*shapeDownArrow,4,0,H,-15);
PlotShapes(Cover*shapeHollowUpArrow,4,0,L,-15);

Filter=Buy OR Short OR Sell OR Cover;
AddColumn(IIf(Buy,66,43),"New Trade Bar", formatChar, colorWhite, bkcolor =IIf(Buy, colorGreen,2));
AddColumn(IIf(Short,83,43),"New Trade Bar", formatChar, colorWhite, bkcolor =IIf(Short, colorRed,2));

Leave a Reply

Your email address will not be published. Required fields are marked *