#### Happy_Singh

##### Happy
rk,
you mean, we need to track the day's high when we are long and day's low when we are short? am not sure how do i get these value from amibroker.. i will figure it out
Amibroker gives that in backtest results . . .
look for the columns called MAE, MFE

thats what exactly RKK wanted . . .

MFE - max favorable excursion

Happy

EDIT:

and putting in profit stops is very easy . . . use

ApplyStop(1,2,R);

where R is the opening range you are getting

Last edited:

#### amitrandive

##### Well-Known Member
Seniors

Please verify . I have posted entire code with the modifications suggested in this thread.

Amit

function ParamOptimize( pname, defaultval, minv, maxv, step )
{
return Optimize( pname,
Param( pname, defaultval, minv, maxv, step ),
minv, maxv, step );
}

_SECTION_BEGIN("Augubhai's ORB System v1.1");

TimeFrameSet(in5Minute); //If reseting, check formula for TimeFrameInMinutes
TimeFrameInMinutes = 5;

//--Define all params
EntryBufferPct = ParamOptimize("Entry Buffer %", 0, 0, 2, 0.1);
SLPct = ParamOptimize("SL %", 1.4, 0.1, 10, 0.1);
TargetPct = ParamOptimize("Target %", 0, 0, 20, 0.5);
MaxTarget = 100;
TargetPct = IIf(TargetPct == 0, MaxTarget, TargetPct);
EntryTimeStart = ParamOptimize("Entry Time Start (Minutes)", 5, 5, 120, 5);
EntryBarStart = floor(EntryTimeStart/TimeFrameInMinutes) - 1;
EntryTimeEnd = ParamOptimize("Entry Time End (Minutes)", 25, 10, 180, 5);
EntryBarEnd = floor(EntryTimeEnd/TimeFrameInMinutes) - 1;
EntryBarEnd = IIf(EntryBarEnd < EntryBarStart, EntryBarStart, EntryBarEnd);

//--Plot Price Candle Chart
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", colorBlack, styleNoTitle | GetPriceStyle() );

//--New Day & Time. End Day & Time . End Day & Time is null till end of day 1
NewDay = (Day()!= Ref(Day(), -1)) OR BarIndex() == 0;
printf("\n NewDay : " + NewDay );
EndDay = (Day()!= Ref(Day(), 1));
printf("\n EndDay : " + EndDay );
SquareOffTime = EndTime;

//--Calculate ORB, and SL
HighestOfDay = HighestSince(NewDay,H,1);
LowestOfDay = LowestSince(NewDay,L,1);
ORBH = ValueWhen(BarsSinceNewDay<=EntryBarStart,HighestOfDay ,1) * (1 + (EntryBufferPct/100));
ORBL = ValueWhen(BarsSinceNewDay<=EntryBarStart,LowestOfDay ,1) * (1 - (EntryBufferPct/100));
ORBHSL = ORBH * (1-(SLPct/100));
//ORBHSL = ORBL;
ORBLSL = ORBL * (1+(SLPct/100));
//ORBLSL = ORBH;
ORBHTarget = ORBH * (1+(TargetPct/100));
ORBLTarget = ORBL * (1-(TargetPct/100));

//--Find Buy, Sell, Short & Cover Signals
ShortSignal = (L <= ORBL) AND (BarsSinceNewDay > EntryBarStart) ;
printf("\nShortSignal : " + ShortSignal );
Cover = (H >= ORBLSL) OR (L <= ORBLTarget) OR (TimeNum() > SquareOffTime-1) AND (BarsSinceNewDay > BarsSinceLastShortSignal);
printf("\nSell : " + Sell );
printf("\nShort : " + Short );
printf("\nCover : " + Cover );

//--Handle if ORB broken both sides on same bar
//--And remove duplicate Sell & Cover signals, since ExRem did not work as needed when Buy & Sell on same bar
orbBothSides = IIf(Buy AND Short, 1, 0);
Short = IIf(orbBothSides AND C > O, 0, Short);
Sell = IIf(orbBothSides AND C > O AND (L <= ORBHSL), 1, Sell);
Cover = IIf(orbBothSides AND C <= O AND (H >= ORBLSL), 1, Cover);
printf("\n\norbBothSides : " + orbBothSides);
printf("\nSell : " + Sell );
printf("\nShort : " + Short );
printf("\nCover : " + Cover );

//--Special Condition for 18 & 19 May 2009 for the Indian Market
Sell =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Sell );
Short =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Short );
Cover =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Cover );

//--Set prices
SellPrice = IIf(Sell, IIf(H >= ORBHTarget, ORBHTarget, Max(ORBHSL, L)), Null);
ShortPrice = IIf(Short, ORBL, Null);
CoverPrice = IIf(Cover, IIf(L <= ORBLTarget, ORBLTarget, Min(ORBLSL, H)), Null);

//--Plot ORB, and SL
Plot(ORBHSL,"",colorRed,styleDashed);
Plot(ORBLSL,"",colorRed,styleDashed);
Plot(IIf(TargetPct == MaxTarget, Null, ORBHTarget),"",colorGreen,styleDashed);
Plot(IIf(TargetPct == MaxTarget, Null, ORBLTarget),"",colorGreen,styleDashed);
PlotOHLC( ORBL, ORBH, ORBL, ORBH, "", colorYellow, styleCloud);

//--Plot Signals
shape1 = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shape1, IIf( Buy, colorGreen, colorGreen), 0, IIf( Buy, Low, High ) );
shape2 = Cover * shapeUpArrow + Short * shapeDownArrow;
PlotShapes( shape2, IIf( Cover, colorRed, colorRed), 0, IIf( Cover, Low, High ) );
GraphXSpace = 5;

//--Restore time frame
TimeFrameRestore();
_SECTION_END();

Last edited:

#### Cubt

##### Well-Known Member
Amit,

i tested it again but still am getting the same results. No change, can you check ur code again, pls.

#### amitrandive

##### Well-Known Member
Cubt

I have just pasted the lines suggested by HappySingh in "Augubhai's ORB System v1.1" AFL for the benefit of all.

I do not know AFL coding.

Thanks
Amit

#### Happy_Singh

##### Happy
Not checked the above code but the exact statement you need to add to it would be

Code:
``````R = ORBH-ORBL;
ApplyStop(1,2,R);``````

Happy

#### Cubt

##### Well-Known Member
Happy,

It would be great if you could make the following changes in the code.

1. After breakout of 5 Mins bar, the position needs to be exited once the Target of 10 points is reached.

2. Stop loss is when 5 mins bar closes above/below the opening range. (i.e. above/below 1st 5 mins bar)

3. If neither Target or Stop Loss is hit, the position should be exited @ 03:20 PM @ market price.

4. When backtesting, it should be considered as Intraday trade. i.e no positions should be carried over.

I guess the 3rd point will not happen as the target is very less, either the target or stop loss will be triggered before 3:20PM. If you feel the same, you can skip this part in the code.

I tried making the changes in the code, but its not showing the correct results. I think am writing a wrong code in the

stop loss part. Pls help. Thanks.

ORBHSL = ORBH;
ORBLSL = ORBL;
ORBHTarget = ORBH + 10;
ORBLTarget = ORBL - 10;

actual AFL
Code:
``````function ParamOptimize( pname, defaultval, minv, maxv, step )
{
return Optimize( pname,
Param( pname, defaultval, minv, maxv, step ),
minv, maxv, step );
}

_SECTION_BEGIN("Augubhai's ORB System v1.1");

TimeFrameSet(in5Minute); //If reseting, check formula for TimeFrameInMinutes
TimeFrameInMinutes = 5;

//--Define all params
EntryBufferPct = ParamOptimize("Entry Buffer %", 0, 0, 2, 0.1);
SLPct = ParamOptimize("SL %", 1.4, 0.1, 10, 0.1);
TargetPct = ParamOptimize("Target %", 0, 0, 20, 0.5);
MaxTarget = 100;
TargetPct = IIf(TargetPct == 0, MaxTarget, TargetPct);
EntryTimeStart = ParamOptimize("Entry Time Start (Minutes)", 5, 5, 120, 5);
EntryBarStart = floor(EntryTimeStart/TimeFrameInMinutes) - 1;
EntryTimeEnd = ParamOptimize("Entry Time End (Minutes)", 25, 10, 180, 5);
EntryBarEnd = floor(EntryTimeEnd/TimeFrameInMinutes) - 1;
EntryBarEnd = IIf(EntryBarEnd < EntryBarStart, EntryBarStart, EntryBarEnd);

//--Plot Price Candle Chart
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", colorBlack, styleNoTitle | GetPriceStyle() );

//--New Day & Time. End Day & Time . End Day & Time is null till end of day 1
NewDay = (Day()!= Ref(Day(), -1)) OR BarIndex() == 0;
printf("\n NewDay  : " + NewDay );
EndDay = (Day()!= Ref(Day(), 1));
printf("\n EndDay  : " + EndDay );
SquareOffTime = EndTime;

//--Calculate ORB, and SL
HighestOfDay = HighestSince(NewDay,H,1);
LowestOfDay = LowestSince(NewDay,L,1);
ORBH = ValueWhen(BarsSinceNewDay<=EntryBarStart,HighestOfDay ,1) * (1 + (EntryBufferPct/100));
ORBL = ValueWhen(BarsSinceNewDay<=EntryBarStart,LowestOfDay ,1) * (1 - (EntryBufferPct/100));
ORBHSL = ORBH * (1-(SLPct/100));
//ORBHSL = ORBL;
ORBLSL = ORBL * (1+(SLPct/100));
//ORBLSL = ORBH;
ORBHTarget = ORBH * (1+(TargetPct/100));
ORBLTarget = ORBL * (1-(TargetPct/100));

//--Find Buy, Sell, Short & Cover Signals
ShortSignal = (L <= ORBL) AND (BarsSinceNewDay  > EntryBarStart) ;
printf("\nShortSignal  : " + ShortSignal );
Cover = (H >= ORBLSL) OR (L <= ORBLTarget) OR (TimeNum() > SquareOffTime-1) AND (BarsSinceNewDay > BarsSinceLastShortSignal);
printf("\nSell : " + Sell );
printf("\nShort : " + Short );
printf("\nCover : " + Cover );

//--Handle if ORB broken both sides on same bar
//--And remove duplicate Sell & Cover signals, since ExRem did not work as needed when Buy & Sell on same bar
orbBothSides = IIf(Buy AND Short, 1, 0);
Short = IIf(orbBothSides AND C > O, 0, Short);
Sell = IIf(orbBothSides AND C > O AND (L <= ORBHSL), 1, Sell);
Cover = IIf(orbBothSides AND C <= O AND (H >= ORBLSL), 1, Cover);
printf("\n\norbBothSides : " + orbBothSides);
printf("\nSell : " + Sell );
printf("\nShort : " + Short );
printf("\nCover : " + Cover );

//--Special Condition for 18 & 19 May 2009 for the Indian Market
Sell =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Sell );
Short =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Short );
Cover =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Cover );

//--Set prices
SellPrice = IIf(Sell, IIf(H >= ORBHTarget, ORBHTarget, Max(ORBHSL, L)), Null);
ShortPrice = IIf(Short, ORBL, Null);
CoverPrice = IIf(Cover, IIf(L <= ORBLTarget, ORBLTarget, Min(ORBLSL, H)), Null);

//--Plot ORB, and SL
Plot(ORBHSL,"",colorRed,styleDashed);
Plot(ORBLSL,"",colorRed,styleDashed);
Plot(IIf(TargetPct == MaxTarget, Null, ORBHTarget),"",colorGreen,styleDashed);
Plot(IIf(TargetPct == MaxTarget, Null, ORBLTarget),"",colorGreen,styleDashed);
PlotOHLC( ORBL, ORBH, ORBL, ORBH, "", colorYellow, styleCloud);

//--Plot Signals
shape1 = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shape1, IIf( Buy, colorGreen, colorGreen), 0, IIf( Buy, Low, High ) );
shape2 = Cover * shapeUpArrow + Short * shapeDownArrow;
PlotShapes( shape2, IIf( Cover, colorRed, colorRed), 0, IIf( Cover, Low, High ) );
GraphXSpace = 5;

//--Restore time frame
TimeFrameRestore();
_SECTION_END();``````

#### Happy_Singh

##### Happy
Happy,

It would be great if you could make the following changes in the code.

1. After breakout of 5 Mins bar, the position needs to be exited once the Target of 10 points is reached.

2. Stop loss is when 5 mins bar closes above/below the opening range. (i.e. above/below 1st 5 mins bar)

3. If neither Target or Stop Loss is hit, the position should be exited @ 03:20 PM @ market price.

4. When backtesting, it should be considered as Intraday trade. i.e no positions should be carried over.

I guess the 3rd point will not happen as the target is very less, either the target or stop loss will be triggered before 3:20PM. If you feel the same, you can skip this part in the code.

I tried making the changes in the code, but its not showing the correct results. I think am writing a wrong code in the

stop loss part. Pls help. Thanks.

ORBHSL = ORBH;
ORBLSL = ORBL;
ORBHTarget = ORBH + 10;
ORBLTarget = ORBL - 10;

actual AFL
Code:
``````function ParamOptimize( pname, defaultval, minv, maxv, step )
{
return Optimize( pname,
Param( pname, defaultval, minv, maxv, step ),
minv, maxv, step );
}

_SECTION_BEGIN("Augubhai's ORB System v1.1");

TimeFrameSet(in5Minute); //If reseting, check formula for TimeFrameInMinutes
TimeFrameInMinutes = 5;

//--Define all params
EntryBufferPct = ParamOptimize("Entry Buffer %", 0, 0, 2, 0.1);
SLPct = ParamOptimize("SL %", 1.4, 0.1, 10, 0.1);
TargetPct = ParamOptimize("Target %", 0, 0, 20, 0.5);
MaxTarget = 100;
TargetPct = IIf(TargetPct == 0, MaxTarget, TargetPct);
EntryTimeStart = ParamOptimize("Entry Time Start (Minutes)", 5, 5, 120, 5);
EntryBarStart = floor(EntryTimeStart/TimeFrameInMinutes) - 1;
EntryTimeEnd = ParamOptimize("Entry Time End (Minutes)", 25, 10, 180, 5);
EntryBarEnd = floor(EntryTimeEnd/TimeFrameInMinutes) - 1;
EntryBarEnd = IIf(EntryBarEnd < EntryBarStart, EntryBarStart, EntryBarEnd);

//--Plot Price Candle Chart
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", colorBlack, styleNoTitle | GetPriceStyle() );

//--New Day & Time. End Day & Time . End Day & Time is null till end of day 1
NewDay = (Day()!= Ref(Day(), -1)) OR BarIndex() == 0;
printf("\n NewDay  : " + NewDay );
EndDay = (Day()!= Ref(Day(), 1));
printf("\n EndDay  : " + EndDay );
SquareOffTime = EndTime;

//--Calculate ORB, and SL
HighestOfDay = HighestSince(NewDay,H,1);
LowestOfDay = LowestSince(NewDay,L,1);
ORBH = ValueWhen(BarsSinceNewDay<=EntryBarStart,HighestOfDay ,1) * (1 + (EntryBufferPct/100));
ORBL = ValueWhen(BarsSinceNewDay<=EntryBarStart,LowestOfDay ,1) * (1 - (EntryBufferPct/100));
ORBHSL = ORBH * (1-(SLPct/100));
//ORBHSL = ORBL;
ORBLSL = ORBL * (1+(SLPct/100));
//ORBLSL = ORBH;
ORBHTarget = ORBH * (1+(TargetPct/100));
ORBLTarget = ORBL * (1-(TargetPct/100));

//--Find Buy, Sell, Short & Cover Signals
ShortSignal = (L <= ORBL) AND (BarsSinceNewDay  > EntryBarStart) ;
printf("\nShortSignal  : " + ShortSignal );
Cover = (H >= ORBLSL) OR (L <= ORBLTarget) OR (TimeNum() > SquareOffTime-1) AND (BarsSinceNewDay > BarsSinceLastShortSignal);
printf("\nSell : " + Sell );
printf("\nShort : " + Short );
printf("\nCover : " + Cover );

//--Handle if ORB broken both sides on same bar
//--And remove duplicate Sell & Cover signals, since ExRem did not work as needed when Buy & Sell on same bar
orbBothSides = IIf(Buy AND Short, 1, 0);
Short = IIf(orbBothSides AND C > O, 0, Short);
Sell = IIf(orbBothSides AND C > O AND (L <= ORBHSL), 1, Sell);
Cover = IIf(orbBothSides AND C <= O AND (H >= ORBLSL), 1, Cover);
printf("\n\norbBothSides : " + orbBothSides);
printf("\nSell : " + Sell );
printf("\nShort : " + Short );
printf("\nCover : " + Cover );

//--Special Condition for 18 & 19 May 2009 for the Indian Market
Sell =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Sell );
Short =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Short );
Cover =IIf(DateNum()==1090518 OR (DateNum()==1090519 AND NewDay),0,Cover );

//--Set prices
SellPrice = IIf(Sell, IIf(H >= ORBHTarget, ORBHTarget, Max(ORBHSL, L)), Null);
ShortPrice = IIf(Short, ORBL, Null);
CoverPrice = IIf(Cover, IIf(L <= ORBLTarget, ORBLTarget, Min(ORBLSL, H)), Null);

//--Plot ORB, and SL
Plot(ORBHSL,"",colorRed,styleDashed);
Plot(ORBLSL,"",colorRed,styleDashed);
Plot(IIf(TargetPct == MaxTarget, Null, ORBHTarget),"",colorGreen,styleDashed);
Plot(IIf(TargetPct == MaxTarget, Null, ORBLTarget),"",colorGreen,styleDashed);
PlotOHLC( ORBL, ORBH, ORBL, ORBH, "", colorYellow, styleCloud);

//--Plot Signals
shape1 = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shape1, IIf( Buy, colorGreen, colorGreen), 0, IIf( Buy, Low, High ) );
shape2 = Cover * shapeUpArrow + Short * shapeDownArrow;
PlotShapes( shape2, IIf( Cover, colorRed, colorRed), 0, IIf( Cover, Low, High ) );
GraphXSpace = 5;

//--Restore time frame
TimeFrameRestore();
_SECTION_END();``````
Hi

Before we change any code try setting a profit stop of 10 points . . .
can set it in Backtest Settings >> Stops >> Profit > Points > 10 > exit intraday at stop

Without the SL if you are getting enough number of winners then only one should try introducing a SL.

Happy

#### Cubt

##### Well-Known Member
Thanks Happy.

I checked it and i could see 80% of total trades achieved the target. With the present AFL, the entry and stop loss condition varies completely.

1. After breakout of 5 Mins bar, the position needs to be exited once the Target of 10 points is reached.

2. Stop loss is when 5 mins bar closes above/below the opening range. (i.e. above/below 1st 5 mins bar)

#### anup

##### Well-Known Member
Thanks Happy.

I checked it and i could see 80% of total trades achieved the target. With the present AFL, the entry and stop loss condition varies completely.