From b60df268522de9b7c3579f4edf99aa7e35ed1e50 Mon Sep 17 00:00:00 2001 From: Martin Molinero Date: Wed, 9 Jan 2019 18:46:38 -0300 Subject: [PATCH] Keep OnData and OnOrderEvent python reference - `AlgorithmPythonWrapper` will now keep reference to the `OnData` and `OnOrderEvent` `PyObject` method, giving a performance improvement, since it does not have to resolve it in each loop. --- .../Python/Wrappers/AlgorithmPythonWrapper.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs b/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs index 13beb5946d62..48571828885a 100644 --- a/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs +++ b/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs @@ -42,6 +42,8 @@ namespace QuantConnect.AlgorithmFactory.Python.Wrappers public class AlgorithmPythonWrapper : IAlgorithm { private readonly dynamic _algorithm = null; + private readonly dynamic _onData; + private readonly dynamic _onOrderEvent; private readonly IAlgorithm _baseAlgorithm; private readonly bool _isOnDataDefined = false; @@ -82,8 +84,11 @@ public AlgorithmPythonWrapper(string moduleName) // determines whether OnData method was defined or inherits from QCAlgorithm // If it is not, OnData from the base class will not be called - var pythonType = (_algorithm as PyObject).GetAttr("OnData").GetPythonType(); + _onData = (_algorithm as PyObject).GetAttr("OnData"); + var pythonType = _onData.GetPythonType(); _isOnDataDefined = pythonType.Repr().Equals(""); + + _onOrderEvent = (_algorithm as PyObject).GetAttr("OnOrderEvent"); } } @@ -543,7 +548,7 @@ public void OnData(Slice slice) { using (Py.GIL()) { - _algorithm.OnData(SubscriptionManager.HasCustomData ? new PythonSlice(slice) : slice); + _onData(SubscriptionManager.HasCustomData ? new PythonSlice(slice) : slice); } } } @@ -688,7 +693,7 @@ public void OnOrderEvent(OrderEvent newEvent) { using (Py.GIL()) { - _algorithm.OnOrderEvent(newEvent); + _onOrderEvent(newEvent); } }