Backtesting net profit
Backtesting net profit
05 Aug 2024, 14:59
Dear team,
I noticed big differences in the Position.NetProfit during backtesting between the History Tab and what is resulting in coding.
I'm attaching a simple BOT that opens some positions with 10 pips take profit and 10 pips stop loss.
The sample BOT is either writing to console the NetProfit when the position is closed and OnStop.
My questions are:
- Am I doing something wrong? Do you have a workaround? Fix?
- Is this problem only happening during backtesting and not in real trading on either demo account and live account?
- Is there a reason Why the Position ID is mismatched? (for example in the History the position ID=1 is EURJPY and in the code is EURAUD)
Thanks a lot for your help, ciao!
Claudio
Account, leverage, broker and backtesting settings.
Results in the History TAB
Results in the console (totally different)
01/08/2024 00:06:00.000 | 01 | OnPositionClosed | This is BUY for EURAUD | Commission -0.3 | Swap 0 | Net 5.19 | Pips 9.6 | Gross 5.79
01/08/2024 04:13:00.000 | 01 | OnStop | This is BUY for EURAUD | Commission -0.3 | Swap 0 | Net 36.85 | Pips 62.1 | Gross 37.45
01/08/2024 00:06:00.000 | 02 | OnPositionClosed | This is SELL for EURAUD | Commission -0.3 | Swap 0 | Net -7.6 | Pips -11.6 | Gross -7
01/08/2024 04:13:00.000 | 02 | OnStop | This is SELL for EURAUD | Commission -0.3 | Swap 0 | Net -39.25 | Pips -64.1 | Gross -38.65
01/08/2024 01:06:00.000 | 03 | OnPositionClosed | This is BUY for EURCAD | Commission -0.3 | Swap 0 | Net 6.29 | Pips 10.3 | Gross 6.89
01/08/2024 04:13:00.000 | 03 | OnStop | This is BUY for EURCAD | Commission -0.3 | Swap 0 | Net 7.5 | Pips 12.1 | Gross 8.1
01/08/2024 01:00:00.000 | 04 | OnPositionClosed | This is SELL for EURCAD | Commission -0.3 | Swap 0 | Net -6.15 | Pips -8.3 | Gross -5.55
01/08/2024 04:13:00.000 | 04 | OnStop | This is SELL for EURCAD | Commission -0.3 | Swap 0 | Net -10.03 | Pips -14.1 | Gross -9.43
01/08/2024 00:01:00.000 | 05 | OnPositionClosed | This is BUY for EURJPY | Commission -0.3 | Swap 0 | Net -12.38 | Pips -19.1 | Gross -11.78
01/08/2024 04:13:00.000 | 05 | OnStop | This is BUY for EURJPY | Commission -0.3 | Swap 0 | Net -14.42 | Pips -22.4 | Gross -13.82
01/08/2024 00:01:00.000 | 06 | OnPositionClosed | This is SELL for EURJPY | Commission -0.3 | Swap 0 | Net 9.95 | Pips 17.1 | Gross 10.55
01/08/2024 04:13:00.000 | 06 | OnStop | This is SELL for EURJPY | Commission -0.3 | Swap 0 | Net 11.98 | Pips 20.4 | Gross 12.58
01/08/2024 00:25:00.000 | 07 | OnPositionClosed | This is BUY for EURNZD | Commission -0.3 | Swap 0 | Net -6.48 | Pips -10.7 | Gross -5.88
01/08/2024 04:13:00.000 | 07 | OnStop | This is BUY for EURNZD | Commission -0.3 | Swap 0 | Net 4.51 | Pips 9.3 | Gross 5.11
01/08/2024 00:26:00.000 | 08 | OnPositionClosed | This is SELL for EURNZD | Commission -0.3 | Swap 0 | Net 5.28 | Pips 10.7 | Gross 5.88
01/08/2024 04:13:00.000 | 08 | OnStop | This is SELL for EURNZD | Commission -0.3 | Swap 0 | Net -6.81 | Pips -11.3 | Gross -6.21
01/08/2024 04:13:00.000 | 09 | OnPositionClosed | This is BUY for EURUSD | Commission -0.3 | Swap 0 | Net 8.73 | Pips 10.1 | Gross 9.33
01/08/2024 04:13:00.000 | 09 | OnStop | This is BUY for EURUSD | Commission -0.3 | Swap 0 | Net 8.73 | Pips 10.1 | Gross 9.33
01/08/2024 01:36:00.000 | 10 | OnPositionClosed | This is SELL for EURUSD | Commission -0.3 | Swap 0 | Net -9.84 | Pips -10 | Gross -9.24
01/08/2024 04:13:00.000 | 10 | OnStop | This is SELL for EURUSD | Commission -0.3 | Swap 0 | Net -11.78 | Pips -12.1 | Gross -11.18
The sample CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.FullAccess)]
public class TestNetProfit : Robot
{
protected override void OnStart()
{
_MyPositions = new();
Positions.Opened += Positions_Opened;
Positions.Closed += Positions_Closed;
CreateSomePositions();
}
private void Positions_Opened(PositionOpenedEventArgs obj)
{
_MyPositions.Add(obj.Position);
}
private void Positions_Closed(PositionClosedEventArgs obj)
{
LogPostion("OnPositionClosed", obj.Position);
if (Positions.Count == 0)
{
Print("Stopping the bot ...");
foreach (var position in _MyPositions)
{
LogPostion("OnStop", position);
}
Stop();
}
}
private void LogPostion(string msg, Position position)
{
string positionIdPadded = position.Id.ToString();
positionIdPadded = positionIdPadded.PadLeft(2, '0');
Print(positionIdPadded, " | ", msg, " | ", position.Label, " | Commission ", position.Commissions, " | Swap ", position.Swap, " | Net ", position.NetProfit, " | Pips ", position.Pips, " | Gross ", position.GrossProfit);
}
private List<Position> _MyPositions;
private void CreateSomePositions()
{
Create2Positions("EURAUD");
Create2Positions("EURCAD");
Create2Positions("EURJPY");
Create2Positions("EURJPN");
Create2Positions("EURNZD");
Create2Positions("EURUSD");
}
private void Create2Positions(string symbolName)
{
ExecuteMarketOrder(TradeType.Buy, symbolName, 10000, "This is BUY for " + symbolName, 10, 10);
ExecuteMarketOrder(TradeType.Sell, symbolName, 10000, "This is SELL for " + symbolName, 10, 10);
}
protected override void OnBar()
{
}
protected override void OnStop()
{
}
}
}
Replies
PanagiotisCharalampous
06 Aug 2024, 06:54
Hi there,
Can you please let us know which version of cTrader you use?
Best regards,
Panagiotis
@PanagiotisCharalampous
claudiorubbiani
06 Aug 2024, 07:28
( Updated at: 06 Aug 2024, 10:48 )
RE: Backtesting net profit
PanagiotisCharalampous said:
Hi there,
Can you please let us know which version of cTrader you use?
Best regards,
Panagiotis
Hi Panagiotis and thanks for your quick reply,
I use cTrader Desktop for Windows ver 4.9.2.26009 branded for ICMarkets
Kind regards,
Claudio
@claudiorubbiani
PanagiotisCharalampous
06 Aug 2024, 10:49
RE: RE: Backtesting net profit
claudiorubbiani said:
PanagiotisCharalampous said:
Hi there,
Can you please let us know which version of cTrader you use?
Best regards,
Panagiotis
Hi Panagiotis and thanks for your quick reply,
I use cTrader Desktop for Windows ver 4.9.2.26009 branded for ICMarkets
Kind regards,
Claudio
Hi Claudio,
This issue should have been fixed in v5.0.29. Please use the latest version instead.
Best regards,
Panagiotis
@PanagiotisCharalampous
claudiorubbiani
06 Aug 2024, 11:54
RE: RE: RE: Backtesting net profit
PanagiotisCharalampous said:
claudiorubbiani said:
PanagiotisCharalampous said:
Hi there,
Can you please let us know which version of cTrader you use?
Best regards,
Panagiotis
Hi Panagiotis and thanks for your quick reply,
I use cTrader Desktop for Windows ver 4.9.2.26009 branded for ICMarkets
Kind regards,
Claudio
Hi Claudio,
This issue should have been fixed in v5.0.29. Please use the latest version instead.
Best regards,
Panagiotis
Hi Panagiotis,
I can confirm that v.5.0.29 solved both the 2 issues:
- Positions ID are not mismatched
- NetProfit is correctly returned and fully matching the values in the history tab
Thanks a lot, ciao!
Claudio
@claudiorubbiani
PanagiotisCharalampous
06 Aug 2024, 06:54
Hi there,
Can you please let us know which version of cTrader you use?
Best regards,
Panagiotis
@PanagiotisCharalampous