Buy/Short prices in back tester are different than actual prices in chart

Hi, I have been trying to put the prices of Lowline and HighLine as buy and short prices respectively. But the prices in back tester differ from those in chart.
Everything is fine with the signal generations except for the prices that i'm getting in back testing. I am trying to get "Low-ATR" price for buying and "High+ATR" price for shorting.
Here's my code. -

/*
ATR Manipulation Strategy I
Conditions-
Buy = Low - ATR
Short = High + ATR

Price Point - Cutting line of L/H ATR Line.

Take positions only after 10:00 AM
Square off at 3:15
*/

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", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

_SECTION_BEGIN("ATR");
IndiATR = ATR(Param("ATR",3,1,200,1));
DoubleATR = IndiATR*2;
Plot(IndiATR,"ATR",colorGold,styleNoRescale|styleNoDraw);
Plot(DoubleATR,"DoubleATR",colorGold,styleNoRescale|styleNoDraw);

_SECTION_END();


//Strategy_Settings
newday = Day() != Ref(Day(), -1);
starttime = ValueWhen(newday,TimeNum());
tn = TimeNum();
	StartTime = 91500;
	EndTime = 151500;
	timeOK = tn >= 100000 AND tn <= 151500;

//backtesting_settings
SetPositionSize(20,spsShares);


//High/Lows
PCandleH = Ref(H, -1);
PCandleL = Ref(L, -1);
PATR = Ref(DoubleATR, -1);

//Addition/Subtraction
HighLine = (PCandleH + PATR);
LowLine = (PcandleL - PATR);
Plot(HighLine,"High+ATR",colorRed,styleStaircase);	//Red Line
lineL = Plot(LowLine,"Low-ATR",colorYellow,styleStaircase); //Yellow Line


//condition-II //Buy=Low-ATR //Short=High+ATR
Buy = Cross(lowline,Low) AND timeok;
Short = Cross(High,highline) AND timeok;
Sell =  Cross(High,highline) AND timeok OR TimeNum() >= 151459;
Cover = Cross(lowline,Low) AND timeok OR TimeNum() >= 151459;

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

//Pricepoints
BuyPrice = ValueWhen(Buy,lowline);
ShortPrice = ValueWhen(Short,Highline);
SellPrice = IIf(TimeNum() >= 151459, Close, highline);
CoverPrice = IIf(TimeNum() >= 151459, Close, lowline); 
//Plot(BuyPrice,"BuyPrice",colorWhite,styleStaircase);
//Plot(shortPrice,"ShortPrice",colorWhite,styleStaircase);

//Conditions
condH = Nz(High) > 0 AND Status("barvisible"); 	//--Cond I	High
condL = Nz(Low) > 0 AND Status("barvisible"); 	//--Cond II	Low
CondATRsub = Nz(HighLine) > 0 AND Status("barvisible"); 	//--Cond III High - ATR
CondATRAdd = Nz(LowLine) > 0 AND Status("barvisible");	//--Cond IV Low + ATR


//visualization
bkcolor = colorDarkBlue;
fontcolor = colorWhite;
PlotShapes(shapeUpArrow*Buy,colorYellow,0,Low,-40);
PlotShapes(shapeHollowdownTriangle*Sell,colorwhite,0,Low,-30);
PlotShapes(shapeDownArrow*Short,colorYellow,0,High,-40);
PlotShapes(shapeHollowupTriangle*Cover,colorWhite,0,High,-30);

Here's the result on 5 min Timeframe.

Screenshot (2)

You can see the Long price is 12314.7

Now, Look at the prices on the chart.

Screenshot (3)

As you can see in the data window, the price of "Low-ATR" is 12317.57

The same case is happening with all the long and short results.
What am I missing here? How can i get those prices in back testing results to be exactly same as those in charts. This type of differentiation has never occurred before.
Any help would be appreciated.

Hi.
One explanation can be the number of bars being used to calculate values because they can be different in Analysis setting. You have not shared the complete Analysis settings.

image

Just to get the doubt out of the way, "uncheck" QuickAFL in Analysis settings and put this code in the formula
SetBarsRequired( -2, -2 );
http://www.amibroker.com/guide/afl/setbarsrequired.html Read here for proper use.

Now can you check if LowLine has the same value in both cases ?

You can search the forum if similar case shows up and take hints from that.

1 Like

Hi @nsm51,

Thanks for the reply. Unfortunately the quick afl and setbarsrequired method did not work.

https://www.amibroker.com/guide/a_custommetrics.html

From Example 3, you can use a high-level CBT to add per trade custom metric to the BT.
Output your LowLine/HighLine variables along with the BT columns and see if the trade entry prices are the same or different to LowLine/HighLine.

With my interaction with Fxshrat for example, using ValueWhen() to assign Buy/Short prices can also cause this problem. Although you have exrem before in the code but i've understood it is better to use a custom For loop method instead.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.