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(); } }

 

Author