Back

Port C# Custom Indicator to Python

Hi all,

 

I want to port this custom indicator , which I have coded in C# , to Python.

Is anyone able to quickly convert it as I have not fully understood the Python API yet.?

 

Thanks in advance.

R

 

public class AdaptiveFilter : BarIndicator, IIndicatorWarmUpPeriodProvider
{

private readonly double _k;
private readonly int _period;

/// <summary>
/// Required period, in data points, for the indicator to be ready and fully initialized.
/// </summary>
public int WarmUpPeriod => _period;

private IBaseDataBar _previousInput;



//+------------------------------------------------------------------+
//| Custom indicator default constructor. DO NOT REMOVE |
//+------------------------------------------------------------------+



/// <summary>
///Initializes a new instance of the Adaptive Filter indicator using the specified name, period and smoothing factor
/// </summary>
/// <param name="name">The name of this indicator</param>
/// <param name="period">Number of bars</param>
/// <param name="smoothingFactor">NThe smoothing factor /param>
public AdaptiveFilter(string name, int period, decimal smoothingFactor)
: base("name")
{

_k = (double)smoothingFactor;
_period = period;

}

/// <summary>
///Initializes a new instance of the Adaptive Filter indicator using the specified name and period
/// </summary>
/// <param name="name">The name of this indicator</param>
/// <param name="period">Number of bars</param>
public AdaptiveFilter(string name, int period)
: base("name")
{

_k = 1;
}

/// <summary>
/// Initializes a new instance of the Adaptive Filter indicator using the default name and specified period
/// </summary>
/// <param name="period">The smoothing period used to smooth the true range values</param>
public AdaptiveFilter(int period)
: this($"ADF({period})", period)
{
}


/// <summary>
/// Gets a flag indicating when this indicator is ready and fully initialized
/// </summary>
public override bool IsReady => _previousInput != null;



/// <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(IBaseDataBar input)
{
if (!IsReady)
{
_previousInput = input;
return 0m;
}

var _speed = 0m;
var iValue = (input.High + input.Low + 2 * input.Close) / 4;


var _value = (_previousInput.High + _previousInput.Low + 2 * _previousInput.Close) / 4;

var _delta = iValue -_value;
var _error = _value + (_delta * (decimal)Math.Sqrt(_k / 100));
_speed += _delta * (decimal)_k / 100;
_value = _error + _speed;

_previousInput = input;

return _value;
}
/// <summary>
/// Resets this indicator to its initial state
/// </summary>
public override void Reset()
{
_previousInput = null;
base.Reset();
}
}

 

Update Backtest







0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hi Rob Pec ,

Please check out the CustomIndicatorAlgorithm.py. This is how we code a simple moving average:

# Python implementation of SimpleMovingAverage.
# Represents the traditional simple moving average indicator (SMA).
class CustomSimpleMovingAverage(PythonIndicator):
def __init__(self, name, period):
self.Name = name
self.Value = 0
self.queue = deque(maxlen=period)

# Update method is mandatory
def Update(self, input):
self.queue.appendleft(input.Value)
count = len(self.queue)
self.Value = sum(self.queue) / count
return count == self.queue.maxlen

Basically, you need to logic from ComputeNextValue to Update. The value that you return in ComputeNextValue should be assigned to self.Value in Update which should return True since the C# algorithm is ready after the first ComputeNextValue call.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed