跳转至

保证金估算

在 cTrader Windows 4.5 中,我们为 Algo API 添加了估算保证金计算功能。 换句话说,您现在可以使用一种方法来检查新头寸或订单将使用多少保证金。

保证金估算如何工作

要计算估算保证金,请调用 Symbol.GetEstimatedMargin() 方法并传入所需参数,即交易类型(买入或卖出)和交易量。

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

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

由于 GetEstimatedMargin() 是一个接口方法,您可以将其用于任何 SymbolSymbolInfo 对象,如下所示。

 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)}"); 
        }
    }
}

注意

在回测或优化期间,GetEstimatedMargin() 方法将返回估算保证金,而不考虑敞口。