Bug in "Used Margin" computation in backtesting cTrader V 5.0.29

Created at 31 Jul 2024, 12:58
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!
QU

Quantrosoft

Joined 02.02.2024

Bug in "Used Margin" computation in backtesting cTrader V 5.0.29
31 Jul 2024, 12:58


I think there is a bug in the "Used Margin" computation when using ModifyVolume(volume).
To prove the bug I did the following (see code below):

  1. Open several positions of EURUSD with 1 Lot one after each other
    - The sum of the quantity is 3 Lots ==> OK
    - Each position “costs” 200,- € Margin, so the sum of “Used Margin” is 600,- ==> OK


     
  2. Open one position EURUSD with 1 Lot 
    - Than do a 1st ModifyVolume(2 Lot)
    - Than do a 2nd ModifyVolume(3 Lot)
    - The sum of the quantity is 3 Lots ==> OK
    - HOWEVER, the sum of “Used Margin” is 1200,- ==> NOT OK



    The row of highering the Used Margin is multiplying the lotsize by the single margin value and adding it to the Used Margin:
    200, 600, 1200, 2000, 3000, 4200, …
    THIS IS WRONG!!!

    3. Doing the same thing on an demo or live account gives the same correct result as in 1.
    1st Trade: 200,- Used Margin
    1st ModifyVolume(2Lot): 400,- Used Margin
    2nd ModifyVolume(2Lot): 600,- Used Margin 
    3rd ModifyVolume(2Lot): 800,- Used Margin 
    and so on

    Here is the code to test and reproduce this:
using cAlgo.API;
using System.Diagnostics;
namespace cAlgo.Robots
{
  [Robot(AccessRights = AccessRights.FullAccess, AddIndicators = true)]
  public class MarginBug : Robot
  {
     int mVolume = 100000;
     protected override void OnStart()
     {
        Debugger.Launch();
     }
     protected override void OnTick()
     {
        if (mVolume <= 1e6)
        {
           if (0 == Positions.Count)
              ExecuteMarketOrder(TradeType.Buy, "EURUSD", mVolume);
           else
              Positions[0].ModifyVolume(mVolume);
           mVolume += 100000;
        }
     }
     protected override void OnStop()
     {
     }
  }
}

@Quantrosoft
Replies

PanagiotisCharalampous
01 Aug 2024, 07:19

Hi there,

It is a known issue and it will be fixed in an upcoming update.

Best regards,

Panagiotis


@PanagiotisCharalampous