27 private readonly decimal _maintenanceMarginRate;
28 private readonly decimal _maintenanceAmount;
36 public CryptoFutureMarginModel(decimal leverage = 25, decimal maintenanceMarginRate = 0.05m, decimal maintenanceAmount = 0)
39 _maintenanceAmount = maintenanceAmount;
40 _maintenanceMarginRate = maintenanceMarginRate;
52 if (security?.GetLastData() ==
null || quantity == 0m)
57 var positionValue = security.Holdings.GetQuantityValue(quantity, security.Price);
58 var marginRequirementInCollateral = Math.Abs(positionValue.Amount) * _maintenanceMarginRate - _maintenanceAmount;
60 return new MaintenanceMargin(marginRequirementInCollateral * positionValue.Cash.ConversionRate);
72 if (security?.GetLastData() ==
null || quantity == 0m)
77 var positionValue = security.Holdings.GetQuantityValue(quantity, security.Price);
78 var marginRequirementInCollateral = Math.Abs(positionValue.Amount) /
GetLeverage(security);
80 return new InitialMargin(marginRequirementInCollateral * positionValue.Cash.ConversionRate);
93 var collateralCurrency = GetCollateralCash(security);
94 var totalCollateralCurrency = collateralCurrency.Amount;
95 var result = totalCollateralCurrency;
97 foreach (var kvp
in portfolio.Where(holdings => holdings.Value.Invested && holdings.Value.Type ==
SecurityType.CryptoFuture && holdings.Value.Symbol != security.Symbol))
99 var otherCryptoFuture = portfolio.Securities[kvp.Key];
101 if (collateralCurrency == GetCollateralCash(otherCryptoFuture))
110 var holdings = security.Holdings;
126 else if (holdings.IsShort)
143 result *= collateralCurrency.ConversionRate;
144 return result < 0 ? 0 : result;
150 private static Cash GetCollateralCash(
Security security)
154 var collateralCurrency = cryptoFuture.BaseCurrency;
155 if (!cryptoFuture.IsCryptoCoinFuture())
157 collateralCurrency = cryptoFuture.QuoteCurrency;
160 return collateralCurrency;