/// <summary> /// <see cref = "AlgorithmPythonWrapper"/> constructor. /// Creates and wraps the algorithm written in python. /// </summary> /// <param name="module">Python module with the algorithm written in Python</param> public AlgorithmPythonWrapper(PyObject module) { _algorithm = null; try { using (Py.GIL()) { if (!module.HasAttr("QCAlgorithm")) { return; } var baseClass = module.GetAttr("QCAlgorithm"); // Load module with util methods _util = ImportUtil(); var moduleName = module.Repr().Split('\'')[1]; foreach (var name in module.Dir()) { var attr = module.GetAttr(name.ToString()); if (attr.IsSubclass(baseClass) && attr.Repr().Contains(moduleName)) { attr.SetAttr("OnPythonData", _util.GetAttr("OnPythonData")); _algorithm = attr.Invoke(); // QCAlgorithm reference for LEAN internal C# calls (without going from C# to Python and back) _baseAlgorithm = (QCAlgorithm)_algorithm; // write events such that when the base handles an event it // will also invoke event handlers defined on this instance _baseAlgorithm.AlphasGenerated += AlphasGenerated; // Set pandas _baseAlgorithm.SetPandasConverter(); return; } } } } catch (Exception e) { Logging.Log.Error(e); } }
/// <summary> /// <see cref = "AlgorithmPythonWrapper"/> constructor. /// Creates and wraps the algorithm written in python. /// </summary> /// <param name="module">Python module with the algorithm written in Python</param> public AlgorithmPythonWrapper(PyObject module) { _algorithm = null; try { using (Py.GIL()) { if (!module.HasAttr("QCAlgorithm")) { return; } var baseClass = module.GetAttr("QCAlgorithm"); // Load module with util methods var onPythonData = Py.Import("AlgorithmPythonUtil").GetAttr("OnPythonData"); var moduleName = module.Repr().Split('\'')[1]; foreach (var name in module.Dir()) { var attr = module.GetAttr(name.ToString()); if (attr.IsSubclass(baseClass) && attr.Repr().Contains(moduleName)) { attr.SetAttr("OnPythonData", onPythonData); _pyAlgorithm = attr.Invoke(); _algorithm = Impromptu.ActLike <IAlgorithm>(_pyAlgorithm); // QCAlgorithm reference for LEAN internal C# calls (without going from C# to Python and back) _baseAlgorithm = _algorithm.UndoActLike(); return; } } } } catch (Exception e) { Logging.Log.Error(e); } }