HELP REQUIRED FOR EDITING AFL Previous Day Range Breakout

#1
Dear Seniors,
I am a working professional and want to have a code wherein i can place buy and sell orders.

Strategy name is “Previous day range breakout”
Buy above yesterdays high and Sell below yesterdays low.
If gapup then Sell below yesterdays high
If gapdown then Buy above yesterdays low.
i Have an AFL but it is not giving proper signals.
Prob 1. If Buy signal is generated then if i am on 5 Min TF every 5 Min , after candle closes, Iam getting a buy signal, same for sell signal.
Prob 2. The scanner is skipping some scripts.
Prob3. How to meet Gap up and Gap Down condition.

Thanks in advance for your help.

_SECTION_BEGIN( "PDHL" );
SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorBrightGreen ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
_SECTION_END();

_SECTION_BEGIN("Previous Days High & Low");
SetPositionSize(1,spsShares);
function CDL( array )
{
doy = DayOfYear();
Lastdoy = doy == LastValue( doy );
Dayline = array * Lastdoy;

return IIf( Dayline, Dayline, Null );
}

H1 = TimeFrameGetPrice( "H", inDaily, -1 );
L1 = TimeFrameGetPrice( "L", inDaily, -1 );



Buy = C > H1;
Sell = C < L1;
Short=Sell;
Cover=Buy;
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

_SECTION_END();
PlotShapes(Buy*shapeUpArrow,colorWhite,0,H,Offset=-40);
PlotShapes(Sell*shapeDownArrow,colorYellow,0,L,Offset=-40);
 

ajeetsingh

Well-Known Member
#2
I am not good at coding but
try to replace buy = c>h1 with Buy= Cross(C,H1)
this is the first thing thats getting in my mind
 

doss186

Well-Known Member
#3
Dear Seniors,
I am a working professional and want to have a code wherein i can place buy and sell orders.

Strategy name is “Previous day range breakout”
Buy above yesterdays high and Sell below yesterdays low.
If gapup then Sell below yesterdays high
If gapdown then Buy above yesterdays low.
i Have an AFL but it is not giving proper signals.
Prob 1. If Buy signal is generated then if i am on 5 Min TF every 5 Min , after candle closes, Iam getting a buy signal, same for sell signal.
Prob 2. The scanner is skipping some scripts.
Prob3. How to meet Gap up and Gap Down condition.

Thanks in advance for your help.

_SECTION_BEGIN( "PDHL" );
SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorBrightGreen ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
_SECTION_END();

_SECTION_BEGIN("Previous Days High & Low");
SetPositionSize(1,spsShares);
function CDL( array )
{
doy = DayOfYear();
Lastdoy = doy == LastValue( doy );
Dayline = array * Lastdoy;

return IIf( Dayline, Dayline, Null );
}

H1 = TimeFrameGetPrice( "H", inDaily, -1 );
L1 = TimeFrameGetPrice( "L", inDaily, -1 );



Buy = C > H1;
Sell = C < L1;
Short=Sell;
Cover=Buy;
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );

_SECTION_END();
PlotShapes(Buy*shapeUpArrow,colorWhite,0,H,Offset=-40);
PlotShapes(Sell*shapeDownArrow,colorYellow,0,L,Offset=-40);
Previous day range breakout only works When Nifty is also trading breaking the previous ranges when in choppy market this may lead to huge losses. Try to reduce the timeframe to previous hour. Chances of winning is more than previous Day. Here is the code.

_SECTION_BEGIN( "PDHL" );
SetChartOptions( 0, chartShowArrows | chartShowDates );
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ) );
Plot( C, "Close", ParamColor( "Color", colorBrightGreen ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
_SECTION_END();

_SECTION_BEGIN("Previous Days High & Low");
SetPositionSize(1,spsShares);
Hi1=TimeFrameGetPrice("H", inHourly,-1);
Lo1=TimeFrameGetPrice("L", inHourly,-1);
Plot( Lo1, "Prev_Low", colorTurquoise );
Plot( Hi1, "Prev_High", colorOrange );

TL = IIf( H>TimeFrameGetPrice("H", inHourly,-1), 1,
IIf( L<TimeFrameGetPrice("L", inHourly,-1), -1 , 0 ));
TL = ValueWhen( TL!= 0, TL);
Plot( TL, "", IIf(TL>0,colorTurquoise,colorRed), styleOwnScale|styleDashed|styleNoTitle);
Buy = Cross(TL,0);
Sell = Cross(0,TL);
Short=Sell;
Cover=Buy;
Buy = ExRem( Buy, Sell );
Sell = ExRem( Sell, Buy );
PlotShapes(Buy*shapeUpArrow,colorWhite,0,H,Offset=-40);
PlotShapes(Sell*shapeDownArrow,colorYellow,0,L,Offset=-40);

dist = 1.5*ATR(20);
for( i = 0; i < BarCount; i++ )
{
if(Buy ) PlotText( "" + C[ i ], i-1, C-dist, colorBrightGreen );
if( Sell ) PlotText( "" + C[ i ], i-1, C[ i ]+dist, colorYellow);
}
SetOption("NoDefaultColumns", True);
Filter = Buy OR Sell;
AddTextColumn(Name() ,"Symbol",1.0,colorDefault,colorDefault,120);
AddColumn( DateTime(), "Date/Time", formatDateTime, colorDefault,colorDefault, 160 );
AddColumn(IIf(Buy ,ValueWhen(Buy ,C),Null),"Buy@", 1.2,1,colorPaleGreen,75);
AddColumn(IIf(Sell ,ValueWhen(Sell ,C),Null),"Short@", 1.2,1,colorOrange,75);

_SECTION_END();
 
#5
I am not good at coding but
try to replace buy = c>h1 with Buy= Cross(C,H1)
this is the first thing thats getting in my mind
Sir,

I have incorporated the suggestion and the code is working fine...
How can I add
If gapup then Sell below yesterdays high
If gapdown then Buy above yesterdays low.
Please guide..
 
#6
Sir,

I have incorporated the suggestion and the code is working fine...
How can I add
If gapup then Sell below yesterdays high
If gapdown then Buy above yesterdays low.
Please guide..
 
#8
Here's the AFL for the requirements.


_SECTION_BEGIN( "Gapup_Gapdown" );
BKswitch = ParamToggle( "Background Color", "On,Off" );

OUTcolor = ParamColor( "Outer Panel Color", colorLightBlue );
INUPcolor = ParamColor( "Inner Panel Upper", colorBlack );
INDNcolor = ParamColor( "Inner Panel Lower", colorBlack );
TitleColor = ParamColor( "Title Color ", ColorRGB( 245, 245, 245 ) );

if( NOT BKswitch )
{
SetChartBkColor( OUTcolor ); // color of outer border
SetChartBkGradientFill( INUPcolor, INDNcolor, TitleColor ); // color of inner panel
}

Gup = L > Ref( H, -1 );
Gdn = H < Ref( L, -1 );
Gap = Gup OR Gdn;
LinLen = Param( "Gap Line length", 10, 1, 100, 1 );
ShowLine = BarsSince( Gup OR Gdn ) < LinLen;
TextPos = Showline < Ref( ShowLine, -1 );
GapUpHi = IIf( ShowLine, ValueWhen( Gup, Ref( H, -1 ) ), Null );
GapUpLo = IIf( ShowLine, ValueWhen( Gup, L ), Null ); // OK
GapDnHi = IIf( ShowLine, ValueWhen( GDn, Ref( L, -1 ) ), Null ); // OK
GapDnLo = IIf( ShowLine, ValueWhen( GDn, H ), Null );
GapLineHi = IIf( ShowLine, ValueWhen( Gap, IIf( Gup, L, IIf( GDn, Ref( L, -1 ), Null ) ) ), Null );
GapLineLo = IIf( ShowLine, ValueWhen( Gap, IIf( Gdn, H, IIf( Gup, Ref( H, -1 ), Null ) ) ), Null );
Plot( C, "", colorBlack, styleBar );
Plot( GapLineHi, "", 5, styleStaircase );
Plot( GapLineLo, "", 4, styleStaircase );
FirstVisibleBar = Status( "FirstVisibleBar" );
Lastvisiblebar = Status( "LastVisibleBar" );

for( b = Max( LinLen, Firstvisiblebar ); b <= Lastvisiblebar AND b < BarCount; b++ )
{
if( Gap[b - LinLen + 1] )
{
PlotText( NumToStr( GapLineHi[b], 1.2 ), b, GapLineHi[b], 5 );
PlotText( "\n" + NumToStr( GapLineLo[b], 1.2 ), b, GapLineLo[b], 4 );
}
}

t=TimeNum();tt=int(t/100);
Buy=Short=Sell=Cover=0;
activesell=0;
activebuy=0;
activeshort=0;
activecover=0;
pDayH = TimeFrameGetPrice("H", inDaily,-1);
pDayL = TimeFrameGetPrice("L", inDaily,-1);
pDayC = TimeFrameGetPrice("C", inDaily,-1);
DayO = TimeFrameGetPrice("O", inDaily,0);
DayH = TimeFrameGetPrice("H", inDaily,0);
DayL = TimeFrameGetPrice("L", inDaily,0);

for (i = 0; i < BarCount ; i++) {
if ( tt[i] >= 930 AND tt[i] < 1515 ) {
if(DayO[i] > pDayC[i] AND C[i] < pDayH[i]AND activesell == 0)
{
Buy[i] = 1;
activesell = 1;
activebuy = 0;
activeshort = 0;
activecover = 0;
continue;
}
if(DayO[i] < pDayC[i] AND C[i] > pDayL[i] AND activebuy == 0)
{
Sell[i] = 1;
activesell = 0;
activebuy = 1;
activeshort = 0;
activecover = 0;
continue;
}
}
if ( tt[i] > 1515) {
if(activebuy==1)
{Sell[i] = 1; activebuy = 0; }
if(activeshort==1)
{Cover[i] = 1; activeshort = 0; }
}
}

PlotShapes( Buy * shapeUpArrow + Sell * shapeDownArrow, IIf( Buy, colorGreen, colorRed ) );
_SECTION_END();
 

Similar threads