Actual Buy Price different from Backtest Result

Hi Everyone. Consider below Code on 1 minute Bar interval:

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.2f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

SetTradeDelays( 0,0,0,0 );
BuyPrice= Close;
SellPrice= Close;

Buy = Cross( MACD(), SIGNAL() );
Sell = Cross( Signal(), MACD() );

if (LastValue(Buy) == True)
{
	_TRACE("Buy Price: " + BuyPrice); // Print Buy Price
}

/************************ Plot Buy/Sell Arrows ******************************/
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);

PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

When i Backtest above Code in Analysis window on 1 minute Bar interval for date 6/24/2020, i get following output:

amibroker Backtest

It shows a long trade entered on 6/24/2020 at 9:18 AM at Buy Price of 138.5.

But when i run Bar Replay on same date and time (i.e 6/24/2020 at 9:18 AM) on 1 minute bar and step interval as 1 second i get following trace output:

Bar Replay

Here, I get several Buy Signals with First Buy signal at Buy Price of 138.25. I have seperate code to remove repeated Buy signals, so i actually enter only one long trade (during realtime trading) at first Buy signal i.e 138.25

As we can see, there is difference between actual Buy Price (in realtime trading) and Buy Price shown in Backtest Result. I understand that Backtest result will show Close Price of the Bar as Buy Price. But, it is not the actual Buy Price in realtime trading as evident from Bar Replay. Actual Buy Price is 138.25 whereas Buy price shown in Backtest result is 138.50. How can i get actual Buy Price in Backtest Result?

You say your backtest is one minute interval.
What log messages do you get if do a bar replay with steps of 1 minute interval
Do you want to trade seconds or minute ?

When I do Bar Replay with steps of 1-minute interval on 1 minute Candle, I get results similar to the Backtest result.

But, I want to trade as soon as the "BUY" signal is available on the BAR, even when the candle is not completed.

For Ex: For 1-minute BAR interval, if candle starts at 10:00:00 AM and I get a "Buy Signal" at 10:00:10 i.e 10 seconds after start of the candle, I will Buy it with BuyPrice = "ClosePrice" at 10:00:10. But, When I will Backtest the same it will show me "BuyPrice" equal to the "ClosePrice" at 10:00:59 i.e the Close at the end of the 1 minute Candle.

You can not look inside bar from 1-minute to 1 second if your backtest interval is 1-minute. You have just four prices within that interval O-H-L-C. So there is just single close price.
You would have to look to 1-minute from 1-second to get intra-bar prices.

You seem to look for running MACD similar to

2 Likes