Hi.
Could some good soul explain to me why the code below returns NaN?
P.S.: TONYRadar is another indicator that I created and that is working correctly returning a non-negative value.
Could some good soul explain to me why the code below returns NaN?
CODE:
using System; using WealthLab.Core; using WealthLab.Indicators; namespace _GabrielEmilio.Indicators { public class TRadarStoch : IndicatorBase { // Parâmetros private const int RadarTriggerTopoMedia = 44; private const double RadarTriggerTopoDesvio = 2; private const int RadarTriggerFundoMedia = 44; private const double RadarTriggerFundoDesvio = 2; private const int PeriodoRadar = 6; private const int PeriodoEstocastico = 20; private const int MediaLenta = 3; private const double LimiteSuperior = 80; private const double LimiteInferior = 20; public override string Name => "Radar Estocástico"; public override string Abbreviation => "TRadarStoch"; public override string HelpDescription => "This indicator implements a custom radar and stochastic calculation."; public override string PaneTag => "TRadarStoch"; public override WLColor DefaultColor => WLColor.Blue; public TRadarStoch() { } public TRadarStoch(BarHistory bh) { base.Parameters[0].Value = bh; Populate(); } protected override void GenerateParameters() { AddParameter("Source", ParameterType.BarHistory, null); } public override void Populate() { BarHistory bh = base.Parameters[0].AsBarHistory; DateTimes = bh.DateTimes; TimeSeries estocastico = new TimeSeries(bh.DateTimes); TimeSeries radar = new TimeSeries(bh.DateTimes); TimeSeries RadarTriggerTopo = new TimeSeries(bh.DateTimes); TimeSeries RadarTriggerFundo = new TimeSeries(bh.DateTimes); TimeSeries fpClose = new TimeSeries(bh.DateTimes); TimeSeries fpHigh = new TimeSeries(bh.DateTimes); TimeSeries fpLow = new TimeSeries(bh.DateTimes); TimeSeries HPc = new TimeSeries(bh.DateTimes); TimeSeries HPh = new TimeSeries(bh.DateTimes); TimeSeries HPl = new TimeSeries(bh.DateTimes); int lp = (int)(PeriodoEstocastico / 2); if (lp <= 0) lp = 1; double alpha1 = (Math.Cos(360 / PeriodoEstocastico) + Math.Sin(360 / PeriodoEstocastico) - 1) / Math.Cos(360 / PeriodoEstocastico); double a1 = Math.Exp(-1.414 * Math.PI / lp); double b1 = 2 * a1 * Math.Cos(1.414 * 180 / lp); double c2 = b1; double c3 = -1 * a1 * a1; double c1 = 1 - c2 - c3; int xSelect; // Inicialização xSelect = 0; for (int i = 1; i < bh.Count; i++) { // Roofing Filter HPc[i] = (1 - alpha1 / 2) * (bh.Close[i] - bh.Close[i - 1]) + (1 - alpha1) * HPc[i - 1]; fpClose[i] = c1 * (HPc[i] + HPc[i - 1]) / 2 + c2 * fpClose[i - 1] + c3 * fpClose[i - 2]; HPh[i] = (1 - alpha1 / 2) * (bh.High[i] - bh.High[i - 1]) + (1 - alpha1) * HPh[i - 1]; fpHigh[i] = c1 * (HPh[i] + HPh[i - 1]) / 2 + c2 * fpHigh[i - 1] + c3 * fpHigh[i - 2]; HPl[i] = (1 - alpha1 / 2) * (bh.Low[i] - bh.Low[i - 1]) + (1 - alpha1) * HPl[i - 1]; fpLow[i] = c1 * (HPl[i] + HPl[i - 1]) / 2 + c2 * fpLow[i - 1] + c3 * fpLow[i - 2]; // NoNoise Estocastico double lowest = Lowest.Series(fpLow, PeriodoEstocastico)[i]; double highest = Highest.Series(fpHigh, PeriodoEstocastico)[i]; if (highest != lowest) { estocastico[i] = ((fpClose[i] - lowest) / (highest - lowest)) * 100; } else { estocastico[i] = 0; } estocastico = SMA.Series(estocastico, MediaLenta); radar = TONYRadar.Series(bh, PeriodoRadar); RadarTriggerTopo = SMA.Series(radar, RadarTriggerTopoMedia) * RadarTriggerTopoDesvio; RadarTriggerFundo = SMA.Series(radar, RadarTriggerFundoMedia) / RadarTriggerFundoDesvio; // Topos e Fundos if (estocastico[i] > LimiteSuperior && radar[i] > RadarTriggerTopo[i] && (radar[i] < radar[i - 1] || estocastico[i] < estocastico[i - 1])) xSelect = 1; else if (estocastico[i] < LimiteInferior && radar[i] > RadarTriggerTopo[i] && (radar[i] < radar[i - 1] || estocastico[i] > estocastico[i - 1])) xSelect = -1; else xSelect = 0; base.Values[i] = xSelect; } } } }
P.S.: TONYRadar is another indicator that I created and that is working correctly returning a non-negative value.
Rename
I think it is these lines
Since i starts at 1, all of the values at index 0 will have NaNs.
Then you are adding Hpc[i-1]. On the first iteration of the loop this will process index 1. and i-1 is index zero, which contains a NaN. So the NaN gets assigned, and just propagates forward.
CODE:
for (int i = 1; i < bh.Count; i++) { // Roofing Filter HPc[i] = (1 - alpha1 / 2) * (bh.Close[i] - bh.Close[i - 1]) + (1 - alpha1) * HPc[i - 1]; fpClose[i] = c1 * (HPc[i] + HPc[i - 1]) / 2 + c2 * fpClose[i - 1] + c3 * fpClose[i - 2];
Since i starts at 1, all of the values at index 0 will have NaNs.
Then you are adding Hpc[i-1]. On the first iteration of the loop this will process index 1. and i-1 is index zero, which contains a NaN. So the NaN gets assigned, and just propagates forward.
Yes, this solves the problem of NaN. Now it's returning zero all the way.
At least it's another issue, the first one went away rs
I will investigate further, thanks!
At least it's another issue, the first one went away rs
I will investigate further, thanks!
Your Response
Post
Edit Post
Login is required