Vai al contenuto

Stime del margine

In cTrader Windows 4.5, abbiamo aggiunto i calcoli delle stime del margine all'Algo API. In altre parole, ora puoi utilizzare un metodo per verificare quanto del tuo margine verrà utilizzato da una nuova posizione o da un ordine.

Come funzionano le stime del margine

Per calcolare il margine stimato, chiama il metodo Symbol.GetEstimatedMargin() e passa gli argomenti richiesti, ovvero il tipo di operazione (acquisto o vendita) e il volume dell'ordine.

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

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

Poiché GetEstimatedMargin() è un metodo di interfaccia, puoi utilizzarlo con qualsiasi oggetto Symbol o SymbolInfo come mostrato di seguito.

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

Nota

Durante il backtesting o l'ottimizzazione, il metodo GetEstimatedMargin() restituirà il margine stimato senza tenere conto dell'esposizione.