fc6ddc2120
Build & Test Lean / build (push) Has been cancelled
Regression Tests / build (push) Has been cancelled
* Implement IIndicatorWarmUpPeriodProvider - Implement IIndicatorWarmUpPeriodProvider in PythonIndicator.cs - Make a unit test to check whether the WarmUpPeriod is working as expected - Make a regression test to check the new feature at a system level * Nit change * Change Period parameter for WarmUpPeriod parameter - Change regression test to check if the new parameter keep backwards compatibility with indicators that do not set WarmUpPeriod * Documentation change * Fix tests bugs - In CommonIndicatorTests.cs before finish the test it checks the period.value with the number of samples but for default the period.value was set to -1 * Change names * Change WarmUp and RegisterIndicator methods - Lean WarmUp indicator skip custom python indicators that don't define WarmUpPeriod parameter * Call WarmUpIndicator manually - Add a new "bridge" method called WarmUpIndicator in QCAlgorithm.Python.cs to set up everything to call WarmUpIndicator in QCAlgorithm.Indicators.cs - Change the regression algorithm to warm up the indicators manually * Remove unnecessary code and add more tests * Nit change * Revert "Nit change" This reverts commit da411f59c9e4295d75a11c6c581f615a938dd2c6. * Fix bugs * Try fix bugs * Add C# regression test - More nit changes - Fix bugs * Requested changes * Remove unnecessary code * Requested changes * Nit changes - Add new Python class to check a custom indicator, which doesn't inherits from PythonIndicator, warms up properly * Reduce redundant code * Fix bug and add more unit and regression tests * - Add more unit tests * Nit change * Test cleanup Co-authored-by: Martin-Molinero <martin@quantconnect.com>
149 lines
5.6 KiB
C#
149 lines
5.6 KiB
C#
/*
|
|
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
|
|
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
using Python.Runtime;
|
|
using QuantConnect.Data;
|
|
using System;
|
|
|
|
namespace QuantConnect.Indicators
|
|
{
|
|
/// <summary>
|
|
/// Provides a wrapper for <see cref="IndicatorBase{IBaseData}"/> implementations written in python
|
|
/// </summary>
|
|
public class PythonIndicator : IndicatorBase<IBaseData>, IIndicatorWarmUpPeriodProvider
|
|
{
|
|
private bool _isReady;
|
|
private dynamic _indicator;
|
|
|
|
/// <summary>
|
|
/// Get the indicator Name. If not defined, use the class name
|
|
/// </summary>
|
|
/// <param name="indicator">The python implementation of <see cref="IndicatorBase{IBaseDataBar}"/></param>
|
|
/// <returns>The indicator Name.</returns>
|
|
private static string GetIndicatorName(PyObject indicator)
|
|
{
|
|
using (Py.GIL())
|
|
{
|
|
var name = indicator.HasAttr("Name")
|
|
? indicator.GetAttr("Name")
|
|
: indicator.GetAttr("__class__").GetAttr("__name__");
|
|
|
|
return name.GetAndDispose<string>();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get the indicator WarmUpPeriod parameter. If not defined, use 0
|
|
/// </summary>
|
|
/// <param name="indicator">The python implementation of <see cref="IndicatorBase{IBaseDataBar}"/></param>
|
|
/// <returns>The WarmUpPeriod of the indicator.</returns>
|
|
private static int GetIndicatorWarmUpPeriod(PyObject indicator)
|
|
{
|
|
using (Py.GIL())
|
|
{
|
|
var warmUpPeriod = indicator.HasAttr("WarmUpPeriod")
|
|
? indicator.GetAttr("WarmUpPeriod")
|
|
: 0.ToPython();
|
|
|
|
return warmUpPeriod.GetAndDispose<int>();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the PythonIndicator class using the specified name.
|
|
/// </summary>
|
|
/// <remarks>This overload allows inheritance for python classes with no arguments</remarks>
|
|
public PythonIndicator()
|
|
: base("")
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the PythonIndicator class using the specified name.
|
|
/// </summary>
|
|
/// <remarks>This overload allows inheritance for python classes with multiple arguments</remarks>
|
|
public PythonIndicator(params PyObject[] args)
|
|
: base (GetIndicatorName(args[0]))
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the PythonIndicator class using the specified name.
|
|
/// </summary>
|
|
/// <param name="indicator">The python implementation of <see cref="IndicatorBase{IBaseDataBar}"/></param>
|
|
public PythonIndicator(PyObject indicator)
|
|
: base (GetIndicatorName(indicator))
|
|
{
|
|
SetIndicator(indicator);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the python implementation of the indicator
|
|
/// </summary>
|
|
/// <param name="indicator">The python implementation of <see cref="IndicatorBase{IBaseDataBar}"/></param>
|
|
public void SetIndicator(PyObject indicator)
|
|
{
|
|
using (Py.GIL())
|
|
{
|
|
foreach (var attributeName in new[] {"IsReady", "Update", "Value"})
|
|
{
|
|
if (!indicator.HasAttr(attributeName))
|
|
{
|
|
var name = indicator.GetAttr("__class__").GetAttr("__name__");
|
|
|
|
var message = $"Indicator.{attributeName} must be implemented. " +
|
|
$"Please implement this missing method in {name}";
|
|
|
|
if (attributeName == "IsReady")
|
|
{
|
|
message += " or use PythonIndicator as base:" +
|
|
$"{Environment.NewLine}class {name}(PythonIndicator):";
|
|
}
|
|
|
|
throw new NotImplementedException(message);
|
|
}
|
|
}
|
|
}
|
|
|
|
WarmUpPeriod = GetIndicatorWarmUpPeriod(indicator);
|
|
_indicator = indicator;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a flag indicating when this indicator is ready and fully initialized
|
|
/// </summary>
|
|
public override bool IsReady => _isReady;
|
|
|
|
/// <summary>
|
|
/// Required period, in data points, for the indicator to be ready and fully initialized
|
|
/// </summary>
|
|
public int WarmUpPeriod { get; protected set; }
|
|
|
|
/// <summary>
|
|
/// Computes the next value of this indicator from the given state
|
|
/// </summary>
|
|
/// <param name="input">The input given to the indicator</param>
|
|
/// <returns>A new value for this indicator</returns>
|
|
protected override decimal ComputeNextValue(IBaseData input)
|
|
{
|
|
using (Py.GIL())
|
|
{
|
|
_isReady = _indicator.Update(input) ?? _indicator.IsReady;
|
|
return _indicator.Value;
|
|
}
|
|
}
|
|
}
|
|
}
|