Skip to content

Margin Estimations

In cTrader Windows 4.5, we have added estimated margin calculations to the Algo API. In other words, you can now use a method to check how much of your margin will be used by a new position or an order.

How Margin Estimations Work

To calculate estimated margin, call the Symbol.GetEstimatedMargin() method and pass the required arguments, namely the trade type (either buy or sell) and the order volume.

1
2
3
4
var estimatedMargin = Symbol.GetEstimatedMargin(TradeType.Buy, 10000);

if (estimatedMargin > Account.FreeMargin)
    Print("Not enough margin");

As GetEstimatedMargin() is an interface method, you can use it with any Symbol / SymbolInfo objects as shown below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class ExpectedMargin : Robot
    {
        [Parameter("Volume", DefaultValue = 10000, MinValue = -1)]
        public double Volume {get; set;}

        [Parameter("Symbol Name")]
        public string UserSymbolName {get; set;}

        protected override void OnStart()
        {
            if (string.IsNullOrWhiteSpace(UserSymbolName))
                UserSymbolName = SymbolName;

            Print($"Buy: {Symbols.GetSymbolInfo(UserSymbolName).GetEstimatedMargin(TradeType.Buy, Volume)}");
            Print($"Sell: {Symbols.GetSymbolInfo(UserSymbolName).GetEstimatedMargin(TradeType.Sell, Volume)}"); 
        }
    }
}

Note

During backtesting/optimisation, the GetEstimatedMargin() method will return the estimated margin without taking account exposure into consideration.