2222using System ;
2323using System . Linq ;
2424using QuantConnect . Util ;
25- using QLNet ;
2625
2726namespace QuantConnect . Algorithm . Framework . Portfolio . SignalExports
2827{
@@ -35,7 +34,7 @@ public class SignalExportManager
3534 /// <summary>
3635 /// Records the time of the first order event of a group of events
3736 /// </summary>
38- private DateTime _initialOrderEventTimeUtc = Time . EndOfTime ;
37+ private ReferenceWrapper < DateTime > _initialOrderEventTimeUtc = new ( Time . EndOfTime ) ;
3938
4039 /// <summary>
4140 /// List of signal export providers
@@ -85,19 +84,37 @@ public void AddSignalExportProvider(ISignalExportTarget signalExport)
8584 /// <param name="signalExport">Signal export provider</param>
8685 public void AddSignalExportProvider ( PyObject signalExport )
8786 {
88- AddSignalExportProvider ( new SignalExportTargetPythonWrapper ( signalExport ) ) ;
87+ if ( ! signalExport . TryConvert < ISignalExportTarget > ( out var managedSignalExport ) )
88+ {
89+ managedSignalExport = new SignalExportTargetPythonWrapper ( signalExport ) ;
90+ }
91+ AddSignalExportProvider ( managedSignalExport ) ;
8992 }
9093
9194 /// <summary>
9295 /// Adds one or more new signal exports providers
9396 /// </summary>
9497 /// <param name="signalExports">One or more signal export provider</param>
95- [ Obsolete ( "This method is deprecated. Please use AddSignalExportProvider(ISignalExportTarget)." ) ]
9698 public void AddSignalExportProviders ( params ISignalExportTarget [ ] signalExports )
9799 {
98100 signalExports . DoForEach ( AddSignalExportProvider ) ;
99101 }
100102
103+ /// <summary>
104+ /// Adds one or more new signal exports providers
105+ /// </summary>
106+ /// <param name="signalExports">One or more signal export provider</param>
107+ public void AddSignalExportProviders ( PyObject signalExports )
108+ {
109+ using var _ = Py . GIL ( ) ;
110+ if ( ! signalExports . IsIterable ( ) )
111+ {
112+ AddSignalExportProvider ( signalExports ) ;
113+ return ;
114+ }
115+ PyList . AsList ( signalExports ) . DoForEach ( AddSignalExportProvider ) ;
116+ }
117+
101118 /// <summary>
102119 /// Sets the portfolio targets from the algorihtm's Portfolio and sends them with the
103120 /// algorithm being ran to the signal exports providers already set
@@ -215,9 +232,9 @@ private IEnumerable<PortfolioTarget> GetPortfolioTargets(decimal totalPortfolioV
215232 /// <param name="orderEvent">Event information</param>
216233 public void OnOrderEvent ( OrderEvent orderEvent )
217234 {
218- if ( _initialOrderEventTimeUtc == Time . EndOfTime && orderEvent . Status . IsFill ( ) )
235+ if ( _initialOrderEventTimeUtc . Value == Time . EndOfTime && orderEvent . Status . IsFill ( ) )
219236 {
220- _initialOrderEventTimeUtc = DateTime . UtcNow ;
237+ _initialOrderEventTimeUtc = new ( orderEvent . UtcTime ) ;
221238 }
222239 }
223240
@@ -227,19 +244,28 @@ public void OnOrderEvent(OrderEvent orderEvent)
227244 /// <param name="currentTimeUtc">The current time of synchronous events</param>
228245 public void Flush ( DateTime currentTimeUtc )
229246 {
230- if ( _initialOrderEventTimeUtc == Time . EndOfTime || ! AutomaticExportTimeSpan . HasValue )
247+ var initialOrderEventTimeUtc = _initialOrderEventTimeUtc . Value ;
248+ if ( initialOrderEventTimeUtc == Time . EndOfTime || ! AutomaticExportTimeSpan . HasValue )
231249 {
232250 return ;
233251 }
234252
235- if ( currentTimeUtc - _initialOrderEventTimeUtc < AutomaticExportTimeSpan )
253+ if ( currentTimeUtc - initialOrderEventTimeUtc < AutomaticExportTimeSpan )
236254 {
237255 return ;
238256 }
239-
240- var success = SetTargetPortfolioFromPortfolio ( ) ;
241- Logging . Log . Trace ( $ "SignalExportManager.Flush({ currentTimeUtc : T} ) :: Success: { success } . Initial OrderEvent Time: { _initialOrderEventTimeUtc : T} ") ;
242- _initialOrderEventTimeUtc = Time . EndOfTime ;
257+
258+ try
259+ {
260+ SetTargetPortfolioFromPortfolio ( ) ;
261+ }
262+ catch ( Exception exception )
263+ {
264+ // SetTargetPortfolioFromPortfolio logs all known error on LEAN side.
265+ // Exceptions occurs in the ISignalExportTarget.Send method (user-defined).
266+ _algorithm . Error ( $ "Failed to send portfolio target(s). Reason: { exception . Message } .{ Environment . NewLine } { exception . StackTrace } ") ;
267+ }
268+ _initialOrderEventTimeUtc = new ( Time . EndOfTime ) ;
243269 }
244270 }
245271}
0 commit comments