Wrong Backtesting Result  [SOLVED]

Questions about MultiCharts and user contributed studies.
McGeorge
Posts: 26
Joined: 22 Jun 2018
Has thanked: 8 times
Been thanked: 1 time

Wrong Backtesting Result

Postby McGeorge » 26 Feb 2019

Hi there,

I wrote below codes to backtest how MC treats Limit order on historical data (60 Secs bar). I chose a bar (Date = 1180810, Time = 16:25) that was 28225 on Close and arbitrarily set a Buy Limit price of Close - 4 (28225 - 4 = 28221) , because the first 3 ticks of next bar (Date = 1180810, Time 16:26) was 28225, 28223, 28218. As I had turned on IntraBarOrderGeneration, and Bar Magnifier, I assumed that the filled price should be 28218 because 28218 was the first best buy price for Buy Limit price of 28221. However, the backtesting result shows that the Filled Buy Price was 28221 instead. As there was not even a tick of 28221 within that bar, I appears to me that MC did not really assess each tick in the bar, but just arbitrarily took 28221 as the Buy price.

Code: Select all

Inputs:
StartDate (1180810),
StartTime (1624),
EndDate (1180810),
EndTime (1625);


Variables:
StartDateTime (0),
EndDateTime (0),
IntraBarPersist Price_Current (0),
IntraBarPersist Price_Planned (0),
IntraBarPersist Price_Actual (0);


StartDateTime = ELDateToDateTime (StartDate) + ELTimeToDateTime (StartTime);
EndDateTime = ELDateToDateTime (EndDate) + ELTimeToDateTime (EndTime);

If DateTime [0] >= StartDateTime And DateTime [0] <= EndDateTime Then Begin
Print ( "DateTime = ", DateTimeToString (DateTime), ", Current Price = ", Close [0], ", Time = ", NumToStr (Time, 0) );
If Time = 1624 Then Begin
Price_Current = Close [0];
Price_Planned = Price_Current - 4;
Print ( "DateTime = ", DateTimeToString (DateTime), ", Planned Price = ", Price_Planned, ", Time = ", NumToStr (Time, 0) );
End;
If Time = 1625 Then Buy Next Bar At Price_Planned Limit;

If MarketPosition <> 0 Then Begin
Price_Actual = EntryPrice;
Print ( "DateTime = ", DateTimeToString (DateTime), ", Actual Price = ", Price_Actual, ", MarketPosition = ", MarketPosition );
End;
Print ("============================================================================================================================");
End;

I attached the screenshots showing the Close price at 16:25, the buy price at 16:26 on the chart, and the print result of various prices (Tick price, planned Buy Limit Price, Actual Filled Price) in the Output window of the PL editor. I also attached a text file containing the historical data, and the screenshots showing my strategy settings.
Chart 1180810 1625.PNG
Chart 1180810 1625.PNG (195.65 KiB) Viewed 912 times
Chart 1180810 1626.PNG
Chart 1180810 1626.PNG (199.81 KiB) Viewed 912 times
PL Editor Output.PNG
PL Editor Output.PNG (94.19 KiB) Viewed 912 times
IOG Setting.PNG
IOG Setting.PNG (56.02 KiB) Viewed 912 times
Bar Magnifier.PNG
Bar Magnifier.PNG (91.38 KiB) Viewed 912 times

Please help investigate and explain why caused the error, and how it can be corrected.


Thanks,
McGeorge

McGeorge
Posts: 26
Joined: 22 Jun 2018
Has thanked: 8 times
Been thanked: 1 time

Re: Wrong Backtesting Result

Postby McGeorge » 26 Feb 2019

Not sure why I was not allowed to attach the historical data file. Please see attached.
HSI 1 Tick 1808-Tick-Trade - b.txt
(98 KiB) Downloaded 115 times

User avatar
TJ
Posts: 7188
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 990 times
Been thanked: 2049 times

Re: Wrong Backtesting Result

Postby TJ » 26 Feb 2019

What MC version and build number?

What is your chart resolution?

McGeorge
Posts: 26
Joined: 22 Jun 2018
Has thanked: 8 times
Been thanked: 1 time

Re: Wrong Backtesting Result

Postby McGeorge » 26 Feb 2019

Hi TJ,

Version: MultiCharts64 Version 12.0 Release (Build 17586)

It's a 60 Second chart.

Thanks,
McGeorge

McGeorge
Posts: 26
Joined: 22 Jun 2018
Has thanked: 8 times
Been thanked: 1 time

Re: Wrong Backtesting Result

Postby McGeorge » 27 Feb 2019

Dear Multicharts Support team,

Any feedback?

Thanks,
McGeorge

User avatar
Anna MultiCharts
Posts: 434
Joined: 14 Jul 2017
Has thanked: 30 times
Been thanked: 108 times

Re: Wrong Backtesting Result

Postby Anna MultiCharts » 28 Feb 2019

Hello, McGeorge!

The behavior is correct.
Our backtesting is designed to simulate the real trading conditions. In real trading when you place an order it gets into the order book (DOM). So if you placed a limit order for 28221, this price would get into the order book and your order would be executed at this price level exactly. Maybe there would never be any 28218 price on that bar if there were your price, and it would be the first most suitable price for your order execution.
In respect of the market and stop orders the execution price would be one of the OHLC prices within the bar.

McGeorge
Posts: 26
Joined: 22 Jun 2018
Has thanked: 8 times
Been thanked: 1 time

Re: Wrong Backtesting Result

Postby McGeorge » 28 Feb 2019

Hi Anna,

It is reasonable for MC to assume that some market orders absorb my buy limit order for 28221 in order for the trade price to fall from 28223 to 28218. However, when I ran the same signal using playback mode (have the time added by 1 in my codes to match the non-intra-bar timestamp first), the buy limit order was filled at 28218 not 28221 (see attached screenshot). Why is the logic different between backtesting and playback mode? They should apply the same logic! Please explain.
PL Editor Output - Playback.PNG
PL Editor Output - Playback.PNG (57.32 KiB) Viewed 854 times
Thanks,
McGeorge

User avatar
Anna MultiCharts
Posts: 434
Joined: 14 Jul 2017
Has thanked: 30 times
Been thanked: 108 times

Re: Wrong Backtesting Result  [SOLVED]

Postby Anna MultiCharts » 01 Mar 2019

McGeorge,

Thanks for pointing this out. Our engineers revealed a bug with the limit order assumptions in the Playback mode. It should be fixed in one of the nearest releases. Please visit our blog every once in a while to see when a new release is launched and if it contains this fix:
https://www.multicharts.com/traders-blog/


Return to “MultiCharts”