CFD Backtesting Margin Calculations Bug

Created at 06 Dec 2023, 14:48
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!
IN

Intetics

Joined 29.11.2023

CFD Backtesting Margin Calculations Bug
06 Dec 2023, 14:48


Hi,

It looks like there is a bug in the way cTrader calculates margin (and free margin) during the backtest on CFD.

Here is the initial data:

I'm buying 109 shares of Apple with the share price of 148.92$ on PepperStone. I'm using the following command:

ExecuteMarketOrder(TradeType.Buy, "Apple_Inc_(AAPL.O)", 109, "MyBot", 40, 20, "Test", false);

 The leverage is 5. So I'd expect that the required margin would be (according to https://help.ctrader.com/trading-with-ctrader/dynamic-leverage/):

 109*148.92/5 = 3246.45$ 

However, cTrader calculates the margin as 30.1$. Interesting that I'm getting the same margin value independently of the qty supplied (10, 100, etc). It looks like cTrader ignores the qunatity in general

Can somebody from cTrader comment on this, please?

 


@Intetics
Replies

PanagiotisCharalampous
07 Dec 2023, 06:41

Hi there,

Can you please share the complete cBot code and your broker so that we can reproduce this behavior?

Best regards,

Panagiotis


@PanagiotisCharalampous

Intetics
08 Dec 2023, 11:38 ( Updated at: 11 Dec 2023, 09:36 )

RE: CFD Backtesting Margin Calculations Bug

PanagiotisCharalampous said: 

Hi there,

Can you please share the complete cBot code and your broker so that we can reproduce this behavior?

Best regards,

Panagiotis

Hi, 

sure, that's the whole code:

using cAlgo.API;
using cAlgo.API.Internals;
using System;
using System.Linq;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.EasternStandardTime, AccessRights = AccessRights.FullAccess)]
    public class FreeMarginBug : Robot
    {
        [Parameter(DefaultValue = "Hello world!")]
        public string Message { get; set; }

        protected override void OnStart()
        {
            Print(Message);
        }

        protected override void OnBar()
        {
            if (Time == new DateTime(2023, 03, 20, 12, 0, 0))
            {
                double qty = 109;
                Print($"Account Balance: {Account.Balance}");
                Print($"Margin Before: {Account.Margin}");
                Print($"Free Margin Before: {Account.FreeMargin}");
                Print($"Price: {Symbol.Ask}");
                ExecuteMarketOrder(TradeType.Buy, SymbolName, qty, "MyBot", 40, 20, "Test", false);
                Print($"Expected Margin: {qty * Symbol.Ask / Symbol.DynamicLeverage.First().Leverage}");
                Print($"Actual Margin: {Account.Margin}");
                Print($"Free Margin After: {Account.FreeMargin}");
            }
        }
    }
}

Steps to reproduce:

  1. Create a new bot “FreeMarginBug”
  2. Select Apple as a financial instrument
  3. Set the first date of the backtesting to 19/03/2023
  4. Run the backtesting and check the log window 

Additional data:

  • Broker: PepperStone
  • Account Type: Test
  • Account Balance: 10,000

The same happens with any stock CFD on any date and any quantity. Specific stock and date was choosen just to simplify the debugging process.

 


@Intetics

PanagiotisCharalampous
11 Dec 2023, 09:30

Hi again,

Thank you. We managed to reproduce the problem and we will fix it in an upcoming release.

Best regards,

Panagiotis


@PanagiotisCharalampous

Intetics
11 Dec 2023, 11:25

RE: CFD Backtesting Margin Calculations Bug

PanagiotisCharalampous said: 

Hi again,

Thank you. We managed to reproduce the problem and we will fix it in an upcoming release.

Best regards,

Panagiotis

Thanks @PanagiotisCharalampous . Does cTrader autoupdate on a new version? I can't seem to find this option in the settings.


@Intetics

PanagiotisCharalampous
11 Dec 2023, 12:52

RE: RE: CFD Backtesting Margin Calculations Bug

Intetics said: 

PanagiotisCharalampous said: 

Hi again,

Thank you. We managed to reproduce the problem and we will fix it in an upcoming release.

Best regards,

Panagiotis

Thanks @PanagiotisCharalampous . Does cTrader autoupdate on a new version? I can't seem to find this option in the settings.

Hi,

Yes it does.

Best regards,

Panagiotis


@PanagiotisCharalampous

PanagiotisCharalampous
11 Dec 2023, 12:52

RE: RE: CFD Backtesting Margin Calculations Bug

Intetics said: 

PanagiotisCharalampous said: 

Hi again,

Thank you. We managed to reproduce the problem and we will fix it in an upcoming release.

Best regards,

Panagiotis

Thanks @PanagiotisCharalampous . Does cTrader autoupdate on a new version? I can't seem to find this option in the settings.

Hi,

Yes it does.

Best regards,

Panagiotis


@PanagiotisCharalampous

ncel01
22 Dec 2023, 14:14 ( Updated at: 23 Dec 2023, 08:45 )

Panagiotis,

For your reference, I believe that similar issues have been reported here:
https://ctrader.com/forum/calgo-support/41606
https://ctrader.com/forum/calgo-support/42331


@ncel01

PanagiotisCharalampous
23 Dec 2023, 08:49

RE: CFD Backtesting Margin Calculations Bug

ncel01 said: 

Panagiotis,

For your reference, I believe that similar issues have been reported here:
https://ctrader.com/forum/calgo-support/41606
https://ctrader.com/forum/calgo-support/42331

Hi ncel01,

Thanks, the issue was reproduced and will be fixed in an upcoming version.

Panagiotis


@PanagiotisCharalampous