18 using System.Collections.Generic;
39 private readonly decimal _marginBuffer;
64 _marginBuffer = marginBuffer;
75 issueMarginCallWarning =
false;
80 if (totalMarginUsed <= 0)
82 return new List<SubmitOrderRequest>();
89 if (marginRemaining <= totalPortfolioValue * 0.05m)
91 issueMarginCallWarning =
true;
95 var marginCallOrders =
new List<SubmitOrderRequest>();
98 if (marginRemaining <= 0)
100 if (totalMarginUsed > totalPortfolioValue * (1 + _marginBuffer))
106 if (positionMarginCallOrders.Count > 0 && positionMarginCallOrders.All(x => x.Quantity != 0))
108 marginCallOrders.AddRange(positionMarginCallOrders);
113 issueMarginCallWarning = marginCallOrders.Count > 0;
116 return marginCallOrders;
128 if (positionGroup.Positions.Any(position =>
Portfolio.
Securities[position.Symbol].QuoteCurrency.ConversionRate == 0))
137 var currentlyUsedBuyingPower = positionGroup.BuyingPowerModel.GetReservedBuyingPowerForPositionGroup(
Portfolio, positionGroup);
140 var buyingPowerToKeep = Math.Max(0, currentlyUsedBuyingPower - deltaAccountCurrency);
143 var deltaBuyingPower = (currentlyUsedBuyingPower - buyingPowerToKeep) * -Math.Sign(positionGroup.Quantity);
146 Portfolio, positionGroup, deltaBuyingPower,
148 minimumOrderMarginPortfolioPercentage: 0
151 var absQuantity = Math.Abs(result.NumberOfLots);
161 return positionGroup.Positions.Select(position =>
165 var legQuantity = absQuantity * position.UnitQuantity * -Math.Sign(position.Quantity);
171 legQuantity.GetOrderLegGroupQuantity(groupOrderManager),
174 security.LocalTime.ConvertToUtc(security.Exchange.TimeZone),
187 public virtual List<OrderTicket>
ExecuteMarginCall(IEnumerable<SubmitOrderRequest> generatedMarginCallOrders)
192 return new List<OrderTicket>();
196 var executedOrders =
new List<OrderTicket>();
197 var ordersWithSecurities = generatedMarginCallOrders.ToDictionary(x => x, x =>
Portfolio[x.Symbol]);
198 var groupManagerTemporalIds = -ordersWithSecurities.Count;
199 var orderedByLosers = ordersWithSecurities
201 .GroupBy(x => x.Key.GroupOrderManager?.Id ?? groupManagerTemporalIds++)
202 .OrderBy(x => x.Sum(kvp => kvp.Value.UnrealizedProfit))
203 .Select(x => x.Select(kvp => kvp.Key));
204 foreach (var requests
in orderedByLosers)
206 var tickets =
new List<OrderTicket>();
207 foreach (var request
in requests)
212 foreach (var ticket
in tickets)
214 if (ticket.Status.IsOpen())
218 executedOrders.Add(ticket);
227 return executedOrders;