cTrader backtests executes take profits often to optimistic. Seems like a bug

Created at 03 Jun 2023, 18:34
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
WO

wolters

Joined 25.03.2022

cTrader backtests executes take profits often to optimistic. Seems like a bug
03 Jun 2023, 18:34


Hi.

It looks as if there is a bug in the backtesting module if using sampled/combined data like 15-min data from server.

In my example I'm using a simple position with a take profit. If the take profit is reached by the tick-data, it executes as expected.
If the same take profit is reached by the 15-min-bar the take profit is executed with totally different price. It somehow seems as if it often uses the close price of the bar that reached the take profit.

Let's get into an example. Here is the Event-Log of the position:


1 - Position created 01/05/2023 12:15:00.000 0.32 Lots - Sell - 0.83888
1-1Position changed (TP) 01/05/2023 12:15:00.000 0.32 Lots - Sell - 0.83888 TP: 0.83803
2-1Take-Profit-Executed 01/05/2023 16:15:00.000 0.32 Lots - Sell - 0.83888 TP: 0.83803  Execution price: 0.837154 = 17 Pips! This is a positive difference of 8.5 Pips to the real take profit point.

In the chart below, you can see the entry and exit of the position and I added the Take Profit level as horizontal line.
As you can see the execution of the position is way too optimistic. In real life, it will be closed at the TP and not at the time where some candles start/end.

This behavior of the backtest simulator makes the non-tick-tests more or less useless because the execution price is randomly placed at some candles in profit. There are no executions below the TP if it was hit, but a lot executions at way better prices than in real life.

Could you check, why this happens and make the backtest simulator more robust and execute the position at the correct target when leaving the tick-data timeframe?


@wolters
Replies

roul.charts
20 Apr 2024, 00:15

I'm also wondering when a stored TP is executed.

I'm in Tick-Data Backtester / Optimizer Mode
I had my own TP logic:
tpHit = position.TradeType == TradeType.Buy ? currentPrice >= targetTpLevel : currentPrice <= targetTpLevel;

And also set the same TP at Trade Execution direct with the SL in the Trade ID.

My code is always executed faster at a TP Level then the built in TP in Backtesting. I can't imagine why. Even if I set a threshold that my code is executed later - after the TP is reached. 
My code is executed earlier then the built in TP.

My aim was to let the built in TP close the trade if i only buy 1 lot instead of running through my code…


@roul.charts

PanagiotisCharalampous
20 Apr 2024, 06:18

RE: cTrader backtests executes take profits often to optimistic. Seems like a bug

roul.charts said: 

I'm also wondering when a stored TP is executed.

I'm in Tick-Data Backtester / Optimizer Mode
I had my own TP logic:
tpHit = position.TradeType == TradeType.Buy ? currentPrice >= targetTpLevel : currentPrice <= targetTpLevel;

And also set the same TP at Trade Execution direct with the SL in the Trade ID.

My code is always executed faster at a TP Level then the built in TP in Backtesting. I can't imagine why. Even if I set a threshold that my code is executed later - after the TP is reached. 
My code is executed earlier then the built in TP.

My aim was to let the built in TP close the trade if i only buy 1 lot instead of running through my code…

Hi there,

It is hard to understand your problem from an abstract description. Feel free to share source code and exact steps to reproduce what you are looking at and we will have a look.

Best regards,

Panagiotis


@PanagiotisCharalampous

roul.charts
20 Apr 2024, 09:23

RE: RE: cTrader backtests executes take profits often to optimistic. Seems like a bug

PanagiotisCharalampous said: 

roul.charts said: 

I'm also wondering when a stored TP is executed.

I'm in Tick-Data Backtester / Optimizer Mode
I had my own TP logic:
tpHit = position.TradeType == TradeType.Buy ? currentPrice >= targetTpLevel : currentPrice <= targetTpLevel;

And also set the same TP at Trade Execution direct with the SL in the Trade ID.

My code is always executed faster at a TP Level then the built in TP in Backtesting. I can't imagine why. Even if I set a threshold that my code is executed later - after the TP is reached. 
My code is executed earlier then the built in TP.

My aim was to let the built in TP close the trade if i only buy 1 lot instead of running through my code…

Hi there,

It is hard to understand your problem from an abstract description. Feel free to share source code and exact steps to reproduce what you are looking at and we will have a look.

Best regards,

Panagiotis

Ok, i can't share the whole bot. It's 3000 lines of code already. But i can make an example bot to show the difference / timing of TP execution. Is the Trade TP executed OnTick or OnBar if i'm in the backtester mode?

 


@roul.charts

PanagiotisCharalampous
22 Apr 2024, 06:07

RE: RE: RE: cTrader backtests executes take profits often to optimistic. Seems like a bug

roul.charts said: 

PanagiotisCharalampous said: 

roul.charts said: 

I'm also wondering when a stored TP is executed.

I'm in Tick-Data Backtester / Optimizer Mode
I had my own TP logic:
tpHit = position.TradeType == TradeType.Buy ? currentPrice >= targetTpLevel : currentPrice <= targetTpLevel;

And also set the same TP at Trade Execution direct with the SL in the Trade ID.

My code is always executed faster at a TP Level then the built in TP in Backtesting. I can't imagine why. Even if I set a threshold that my code is executed later - after the TP is reached. 
My code is executed earlier then the built in TP.

My aim was to let the built in TP close the trade if i only buy 1 lot instead of running through my code…

Hi there,

It is hard to understand your problem from an abstract description. Feel free to share source code and exact steps to reproduce what you are looking at and we will have a look.

Best regards,

Panagiotis

Ok, i can't share the whole bot. It's 3000 lines of code already. But i can make an example bot to show the difference / timing of TP execution. Is the Trade TP executed OnTick or OnBar if i'm in the backtester mode?

 

If you use TP, SL or pending orders which are executed at a specific level, then you should use tick data for your backtesting. If you use bar data the results will be wrong.


@PanagiotisCharalampous