@@ -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