Entry price while backtesting

Am getting confused with the issue of entry price and will need guidance from the seniors in this forum:

Problem:

  • While backtesting a simple MACD cross over strategy, the entry price of the trade will be as per what we define the buyprice / sellprice / shortprice / coverprice variables to Open / High / Low / Close

  • Assuming a long candle on a 10 minute timeframe, the cross over condition would have occurred somewhere in between the candle

  • But while backtesting, the buyprice will be the close price (while defining buyprice = close) of the candle and not the exact price when the cross over happened

  • This may not be ideal and the backtest may produce good results which may not be the actual case while trading live (The scenario could be the other way too, that the backtest gives poor results)

Query that I have:

  • While backtesting, is there a way to ensure that the buyprice is exactly at the point where the condition is met and not at the close of the candle?

Work around that I could think of:

  • Setting Trade Delays to 1:
    SetTradeDelays(1, 1, 1, 1);

  • Buying at the subsequent candle:
    Buy = Ref(MACD_Up_CO, -1);

Would request the seniors to throw some light on how we could overcome this issue in the best possible way.

Reference image explaining my query:
AB_Query1

SetTradeDelays(0, 0, 0, 0);
RequestTimedRefresh(1, False);

SetOption("AllowPositionShrinking", True);
SetOption("PriceBoundChecking", False);

MACD_Up_CO = Cross(MACD(), Signal());
MACD_Down_CO = Cross(Signal(), MACD());

Buy = MACD_Up_CO;
Sell = MACD_Down_CO;
//Buy = Ref(MACD_Up_CO, -1);

BuyPrice = SellPrice = Close;
BuyPrice = ValueWhen(MACD_Up_CO, Close);
SellPrice = ValueWhen(MACD_Down_CO, Close);
//EntryPrice = ValueWhen(Buy, BuyPrice);

Filter = 1;

AddColumn(Open, "Open");
AddColumn(High, "High");
AddColumn(Low, "Low");
AddColumn(Close, "Close");
AddColumn(MACD_Up_CO, "MACH Up CO");
AddColumn(MACD_Down_CO, "MACD Down CO");
AddColumn(Buy, "Buy");
AddColumn(Sell, "Sell");
AddColumn(BuyPrice, "Buy Price");
AddColumn(SellPrice, "Sell Price");
//AddColumn(EntryPrice, "Entry Price");

Why is that a problem?

All calculations are based off 'End of Bar'.

If you want more responsive signals consider trading a shorter timeframe.

2 Likes

As @TrendSurfer wrote you the solution is to act (trade) on completed bars.

You should not trade on uncompleted candles because the prices may reverse to the point where no cross occurs and that would invalidate your signal.

3 Likes

I would add there can always be alternates you can look at. You could certainly use the open print in the MA calculations. Sometimes I like to do this because the MA is fixed for the whole day/interval, as apposed to evolving. That said, MACD would still wait for the bar to be completed to calculate it.

If your signals are of a type "Long on strength", it usually means signals for longs are triggered when prices go up. In this case you can also use the "High" bar field for this type of MA calculation for long entries (long signals), because the high for the day/bar can only get larger or stay the same during the day/bar.

For both the above (open price print or high) options, you can reverse engineer the "Price at which" an MACD would cross for order an placement. As @Tomasz stated, you cannot do this with the close/last price because it is transitory during the day/interval.

I'd always keep track of the "fake" signals and the potential total loss (intrabar entries that reverse). There are some technical set-ups that produce few of the fakes (iow, they would be within acceptable probability calcs). My 2-cents (worth every penny).

1 Like

This was the confusion I had and have got this clarified now, thank you @TrendSurfer

1 Like

Noted the point Dr. Tomasz, thank you

@Sean , if you could explain briefly on what you actually mean by 'open print'. (Sorry if I am sounding dumb)

Some instruments like futures have been known to massage the price they call the open price for the start of a session. In that case, an opening print means just take the very first price (tick) of the day/interval/session on your screen as your open price (which may be different than the official open), that you then use for your calculations. More of an old relic of the pits. In the day of open out cry, traders would square trades before the bell and then at the bell and the huge flurry, there was no way to tell the time stamps/order of the executions, so the exchange would publish the "Official open" which often times used to be different than what we would see viewing an intra-day chart off floor. Same sort of thing at NYSE, Amex and regional exchanges (Boston etc) and CBOE.

Aside from getting into the weeds above, simply just view it as the open price field. Just another name for the open price of whichever bar you're looking at. These days time stamps and ticks and execution orders are for the most part accurate and ordered properly because they are electronic.

2 Likes

@Sean thank you for this note, have got a hang of it!

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