Skip to content

Commit a2eea30

Browse files
committed
Filter marginCall orders
1 parent acf262c commit a2eea30

3 files changed

Lines changed: 17 additions & 20 deletions

File tree

Common/Securities/Security.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,9 @@ public SecurityExchange Exchange
206206
set
207207
{
208208
_exchange = value;
209-
if (_localTimeKeeper != null && !_exchange.HasTimeProvider)
209+
if (_localTimeKeeper != null)
210210
{
211-
var hasDifferentTimeZones = _localTimeKeeper.TimeZone != _exchange.TimeZone;
212-
var newLocalTimeKeeper = hasDifferentTimeZones
213-
? new LocalTimeKeeper(_localTimeKeeper.LocalTime.ConvertToUtc(_exchange.TimeZone), _exchange.TimeZone)
214-
: _localTimeKeeper;
215-
216-
_exchange.SetLocalDateTimeFrontierProvider(newLocalTimeKeeper);
211+
_exchange.SetLocalDateTimeFrontierProvider(_localTimeKeeper);
217212
}
218213
}
219214
}

Common/Securities/SecurityExchange.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,7 @@ public class SecurityExchange
5757
/// <summary>
5858
/// Boolean property for quickly testing if the exchange is 10 minutes away from closing.
5959
/// </summary>
60-
public bool ClosingSoon => IsClosingSoon(minutesToClose: 10);
61-
62-
/// <summary>
63-
/// Indicates whether a time provider has been set for this exchange.
64-
/// </summary>
65-
internal bool HasTimeProvider => _timeProvider != null;
60+
public bool ClosingSoon => IsClosingSoon(minutesToClose:10);
6661

6762
/// <summary>
6863
/// Initializes a new instance of the <see cref="SecurityExchange"/> class using the specified

Engine/AlgorithmManager.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -329,22 +329,29 @@ public void Run(AlgorithmNodePacket job, IAlgorithm algorithm, ISynchronizer syn
329329
// perform margin calls, in live mode we can also use realtime to emit these
330330
if (time >= nextMarginCallTime || (_liveMode && nextMarginCallTime > DateTime.UtcNow))
331331
{
332-
bool allMarketsOpen = algorithm.Securities.Values.All(security => security.Exchange.ExchangeOpen);
333332
// determine if there are possible margin call orders to be executed
334333
bool issueMarginCallWarning;
335334
var marginCallOrders = algorithm.Portfolio.MarginCallModel.GetMarginCallOrders(out issueMarginCallWarning);
336-
if (marginCallOrders.Count != 0 && allMarketsOpen)
335+
var executedTicketsCount = 0;
336+
if (marginCallOrders.Count != 0)
337337
{
338338
var executingMarginCall = false;
339339
try
340340
{
341-
// tell the algorithm we're about to issue the margin call
342-
algorithm.OnMarginCall(marginCallOrders);
341+
var filteredMarginCallOrders = marginCallOrders
342+
.Where(order => algorithm.Portfolio.Securities[order.Symbol].Exchange.ExchangeOpen)
343+
.ToList();
343344

344-
executingMarginCall = true;
345+
// tell the algorithm we're about to issue the margin call
346+
if (filteredMarginCallOrders.Count > 0)
347+
{
348+
algorithm.OnMarginCall(filteredMarginCallOrders);
349+
}
345350

346351
// execute the margin call orders
347-
var executedTickets = algorithm.Portfolio.MarginCallModel.ExecuteMarginCall(marginCallOrders);
352+
var executedTickets = algorithm.Portfolio.MarginCallModel.ExecuteMarginCall(filteredMarginCallOrders);
353+
executedTicketsCount = executedTickets.Count;
354+
348355
foreach (var ticket in executedTickets)
349356
{
350357
algorithm.Error($"{algorithm.Time.ToStringInvariant()} - Executed MarginCallOrder: {ticket.Symbol} - " +
@@ -359,7 +366,7 @@ public void Run(AlgorithmNodePacket job, IAlgorithm algorithm, ISynchronizer syn
359366
}
360367
}
361368
// we didn't perform a margin call, but got the warning flag back, so issue the warning to the algorithm
362-
else if (issueMarginCallWarning)
369+
if (executedTicketsCount == 0 && issueMarginCallWarning)
363370
{
364371
try
365372
{

0 commit comments

Comments
 (0)