Hi there, I am trying to write up an indicator but I am struggling at the psudo code for creating a new indicator which uses the PREVIOUS value combined with the CURRENT value:
AtrRsi = abs(RsiMa[1] - RsiMa)
MaAtrRsi = exponentialaverage[WildersPeriod](AtrRsi)
I need to do the above, but at initialization time, I do not know how to reference RsiMA[1] (the previous value)
Could someone point me to another indicator which does something similar?
Jing Wu
Hi Ian,
The rolling window of indicator could help you get the previous value and the current value of the indicator, please see this example
Ian Worthington
I think I might have just spotted that the "Delay(1).Of" is what I'm looking for, perhaps. If not I'll look at the rolling window.
Quant Trader
You have to wait till your indicator has all the past (previous) values before you can calculate the indicator value. In this post it looks like [1] means previous value.
In this sample you see that 3 data points are stored before calculating:
using MathNet.Numerics; using QuantConnect.Indicators; using System; using System.Linq; //Copied from this forum: //href https://www.quantconnect.com/forum/discussion/695/adjusted-slope--exponential-slope----annualized-slope--r-squuared--adjusted-slope/p1 namespace QuantConnect.Algorithm.CSharp.Helpers { public class AnnualizedExponentialSlopeIndicator : WindowIndicator<IndicatorDataPoint> { public AnnualizedExponentialSlopeIndicator(int period) : base("AESI(" + period + ")", period) { } public AnnualizedExponentialSlopeIndicator(string name, int period) : base(name, period) { } protected override decimal ComputeNextValue(IReadOnlyWindow<IndicatorDataPoint> window, IndicatorDataPoint input) { if (window.Count < 3) return 0m; var xVals = new double[window.Count]; var yVals = new double[window.Count]; // load input data for regression for (int i = 0; i < window.Count; i++) { xVals[i] = i; // we want the log of our y values yVals[i] = Math.Log((double)window[window.Count - i - 1].Value); } //http://numerics.mathdotnet.com/Regression.html // solves y=a + b*x via linear regression var fit = Fit.Line(xVals, yVals); var intercept = fit.Item1; var slope = fit.Item2; // compute rsquared var rsquared = GoodnessOfFit.RSquared(xVals.Select(x => intercept + slope * x), yVals); // anything this small can be viewed as flat if (double.IsNaN(slope) || Math.Abs(slope) < 1e-25) return 0m; // trading days per year for us equities const int dayCount = 252; // annualize dy/dt var annualSlope = ((Math.Pow(Math.Exp(slope), dayCount)) - 1) * 100; // scale with rsquared //annualSlope = annualSlope * Math.Pow(rsquared, 2); annualSlope = annualSlope * rsquared; if (annualSlope >= (double)decimal.MaxValue || annualSlope <= (double)decimal.MinValue) { annualSlope = -1000; //Debug("Negative slope due to arithmic overflow"); } return Convert.ToDecimal(annualSlope); } } }
Ian Worthington
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!