/// <summary> /// Computes the next value of this indicator from the given state /// </summary> /// <param name="window"></param> /// <param name="input">The input given to the indicator</param> /// <returns> /// A new value for this indicator /// </returns> protected override decimal ComputeNextValue(IReadOnlyWindow<IndicatorDataPoint> window, IndicatorDataPoint input) { // Until the window is ready, the indicator returns the input value. if (window.Samples <= window.Size) return input; // Sort the window by time, convert the observations to double and transform it to an array var series = window .OrderBy(i => i.Time) .Select(i => Convert.ToDouble(i.Value)) .ToArray(); // Fit OLS var ols = Fit.Line(x: t, y: series); Intercept.Update(input.Time, (decimal)ols.Item1); Slope.Update(input.Time, (decimal)ols.Item2); // Calculate the fitted value corresponding to the input return Intercept + Slope * Period; }
/// <summary> /// Computes the next value of this indicator from the given state /// </summary> /// <param name="window"></param> /// <param name="input">The input given to the indicator</param> /// <returns> /// A new value for this indicator /// </returns> protected override decimal ComputeNextValue(IReadOnlyWindow <IndicatorDataPoint> window, IndicatorDataPoint input) { // Until the windows is ready, the indicator returns the input value. decimal output = input; if (IsReady) { // Sort the windows by time, convert the observations ton double and transform it to a double array double[] series = window .OrderBy(i => i.Time) .Select(i => Convert.ToDouble(i.Value)) .ToArray <double>(); // Fit OLS Tuple <double, double> ols = Fit.Line(x: t, y: series); var alfa = (decimal)ols.Item1; var beta = (decimal)ols.Item2; // Make the projection. output = alfa + beta * (Period); } return(output); }