@@ -47,7 +47,6 @@ namespace QuantConnect.AlgorithmFactory.Python.Wrappers
4747 /// </summary>
4848 public class AlgorithmPythonWrapper : BasePythonWrapper < IAlgorithm > , IAlgorithm
4949 {
50- private readonly PyObject _algorithm ;
5150 private readonly dynamic _onData ;
5251 private readonly dynamic _onMarginCall ;
5352 private readonly IAlgorithm _baseAlgorithm ;
@@ -110,9 +109,8 @@ public AlgorithmPythonWrapper(string moduleName)
110109 {
111110 Logging . Log . Trace ( "AlgorithmPythonWrapper(): Creating IAlgorithm instance." ) ;
112111
113- _algorithm = attr . Invoke ( ) ;
114- SetPythonInstance ( _algorithm ) ;
115- var dynAlgorithm = _algorithm as dynamic ;
112+ SetPythonInstance ( attr . Invoke ( ) ) ;
113+ var dynAlgorithm = Instance as dynamic ;
116114
117115 // Set pandas
118116 dynAlgorithm . SetPandasConverter ( ) ;
@@ -122,11 +120,11 @@ public AlgorithmPythonWrapper(string moduleName)
122120
123121 // determines whether OnData method was defined or inherits from QCAlgorithm
124122 // If it is not, OnData from the base class will not be called
125- _onData = _algorithm . GetPythonMethod ( "OnData" ) ;
123+ _onData = Instance . GetPythonMethod ( "OnData" ) ;
126124
127- _onMarginCall = _algorithm . GetPythonMethod ( "OnMarginCall" ) ;
125+ _onMarginCall = Instance . GetPythonMethod ( "OnMarginCall" ) ;
128126
129- PyObject endOfDayMethod = _algorithm . GetPythonMethod ( "OnEndOfDay" ) ;
127+ using PyObject endOfDayMethod = Instance . GetPythonMethod ( "OnEndOfDay" ) ;
130128 if ( endOfDayMethod != null )
131129 {
132130 // Since we have a EOD method implemented
@@ -148,27 +146,27 @@ public AlgorithmPythonWrapper(string moduleName)
148146 }
149147
150148 // Initialize the python methods
151- _onBrokerageDisconnect = _algorithm . GetMethod ( "OnBrokerageDisconnect" ) ;
152- _onBrokerageMessage = _algorithm . GetMethod ( "OnBrokerageMessage" ) ;
153- _onBrokerageReconnect = _algorithm . GetMethod ( "OnBrokerageReconnect" ) ;
154- _onSplits = _algorithm . GetMethod ( "OnSplits" ) ;
155- _onDividends = _algorithm . GetMethod ( "OnDividends" ) ;
156- _onDelistings = _algorithm . GetMethod ( "OnDelistings" ) ;
157- _onSymbolChangedEvents = _algorithm . GetMethod ( "OnSymbolChangedEvents" ) ;
158- _onEndOfDay = _algorithm . GetMethod ( "OnEndOfDay" ) ;
159- _onCommand = _algorithm . GetMethod ( "OnCommand" ) ;
160- _onMarginCallWarning = _algorithm . GetMethod ( "OnMarginCallWarning" ) ;
161- _onOrderEvent = _algorithm . GetMethod ( "OnOrderEvent" ) ;
162- _onAssignmentOrderEvent = _algorithm . GetMethod ( "OnAssignmentOrderEvent" ) ;
163- _onSecuritiesChanged = _algorithm . GetMethod ( "OnSecuritiesChanged" ) ;
164- _onFrameworkSecuritiesChanged = _algorithm . GetMethod ( "OnFrameworkSecuritiesChanged" ) ;
149+ _onBrokerageDisconnect = Instance . GetMethod ( "OnBrokerageDisconnect" ) ;
150+ _onBrokerageMessage = Instance . GetMethod ( "OnBrokerageMessage" ) ;
151+ _onBrokerageReconnect = Instance . GetMethod ( "OnBrokerageReconnect" ) ;
152+ _onSplits = Instance . GetMethod ( "OnSplits" ) ;
153+ _onDividends = Instance . GetMethod ( "OnDividends" ) ;
154+ _onDelistings = Instance . GetMethod ( "OnDelistings" ) ;
155+ _onSymbolChangedEvents = Instance . GetMethod ( "OnSymbolChangedEvents" ) ;
156+ _onEndOfDay = Instance . GetMethod ( "OnEndOfDay" ) ;
157+ _onCommand = Instance . GetMethod ( "OnCommand" ) ;
158+ _onMarginCallWarning = Instance . GetMethod ( "OnMarginCallWarning" ) ;
159+ _onOrderEvent = Instance . GetMethod ( "OnOrderEvent" ) ;
160+ _onAssignmentOrderEvent = Instance . GetMethod ( "OnAssignmentOrderEvent" ) ;
161+ _onSecuritiesChanged = Instance . GetMethod ( "OnSecuritiesChanged" ) ;
162+ _onFrameworkSecuritiesChanged = Instance . GetMethod ( "OnFrameworkSecuritiesChanged" ) ;
165163 }
166164 attr . Dispose ( ) ;
167165 }
168166 module . Dispose ( ) ;
169167 pyList . Dispose ( ) ;
170168 // If _algorithm could not be set, throw exception
171- if ( _algorithm == null )
169+ if ( Instance == null )
172170 {
173171 throw new Exception ( "Please ensure that one class inherits from QCAlgorithm." ) ;
174172 }
@@ -1163,13 +1161,13 @@ private bool TryConvert<T>(PyObject pyObject, out T result)
11631161 /// <returns></returns>
11641162 public override string ToString ( )
11651163 {
1166- if ( _algorithm == null )
1164+ if ( Instance == null )
11671165 {
11681166 return base . ToString ( ) ;
11691167 }
11701168 using ( Py . GIL ( ) )
11711169 {
1172- return _algorithm . Repr ( ) ;
1170+ return Instance . Repr ( ) ;
11731171 }
11741172 }
11751173
@@ -1270,5 +1268,30 @@ public void SetTags(HashSet<string> tags)
12701268 /// <returns>The command result</returns>
12711269 public CommandResultPacket RunCommand ( CallbackCommand command ) => _baseAlgorithm . RunCommand ( command ) ;
12721270
1271+ /// <summary>
1272+ /// Dispose of this instance
1273+ /// </summary>
1274+ public override void Dispose ( )
1275+ {
1276+ using var _ = Py . GIL ( ) ;
1277+ _onBrokerageDisconnect ? . Dispose ( ) ;
1278+ _onBrokerageMessage ? . Dispose ( ) ;
1279+ _onBrokerageReconnect ? . Dispose ( ) ;
1280+ _onSplits ? . Dispose ( ) ;
1281+ _onDividends ? . Dispose ( ) ;
1282+ _onDelistings ? . Dispose ( ) ;
1283+ _onSymbolChangedEvents ? . Dispose ( ) ;
1284+ _onEndOfDay ? . Dispose ( ) ;
1285+ _onMarginCallWarning ? . Dispose ( ) ;
1286+ _onOrderEvent ? . Dispose ( ) ;
1287+ _onCommand ? . Dispose ( ) ;
1288+ _onAssignmentOrderEvent ? . Dispose ( ) ;
1289+ _onSecuritiesChanged ? . Dispose ( ) ;
1290+ _onFrameworkSecuritiesChanged ? . Dispose ( ) ;
1291+
1292+ _onData ? . Dispose ( ) ;
1293+ _onMarginCall ? . Dispose ( ) ;
1294+ base . Dispose ( ) ;
1295+ }
12731296 }
12741297}
0 commit comments