Hi,
I am trying to implement a custom indicator but my C# code does not yield the right values.
You can find below my code.
I have also attached a notebook with the equivalent code. in Python,
Can someone highlight what is incorrect?
Thanks..
using System;
using QuantConnect.Data.Market;
namespace QuantConnect.Indicators
{
/// <summary>
///
/// AdaptiveFilter is defined as etcetc........
///
///
/// </summary>
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 = 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;
return _value;
}
/// <summary>
/// Resets this indicator to its initial state
/// </summary>
public override void Reset()
{
_previousInput = null;
base.Reset();
}
}
}
Alethea Lin
Hi Rob,
You just need to add one line of code in your ComputeNextValue method after you do the calculations.
_previousInput = input;
Please see the attached backtest, and the values match your Python results in the notebook.
Hope this helps!
Rob Pec
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!