public void ATR() { ATR atr = new ATR(); atr.Load(Directory.GetCurrentDirectory() + "\\table.csv"); ATRSerie serie = atr.Calculate(); Assert.IsNotNull(serie); Assert.IsTrue(serie.ATR.Count > 0); Assert.IsTrue(serie.TrueHigh.Count > 0); Assert.IsTrue(serie.TrueLow.Count > 0); Assert.IsTrue(serie.TrueRange.Count > 0); }
public void ATR() { ATR atr = new ATR(); atr.Load(OhlcList); ATRSerie serie = atr.Calculate(); Assert.IsNotNull(serie); Assert.IsTrue(serie.ATR.Count > 0); Assert.IsTrue(serie.TrueHigh.Count > 0); Assert.IsTrue(serie.TrueLow.Count > 0); Assert.IsTrue(serie.TrueRange.Count > 0); }
public double[] CalculateATR(IEnumerable <Quote> quotes, int period) { var listOhlc = quotes.Select( x => new Ohlc { Date = x.Date, Close = x.Close, AdjClose = x.Close, High = x.High, Low = x.Low, Open = x.Open, Volume = x.Volume }).ToList(); var atr = new ATR(period); atr.Load(listOhlc); var result = atr.Calculate(); return(result.ATR.Select(x => x.GetValueOrDefault()).ToArray()); }
public override ADXSerie Calculate() { ADXSerie adxSerie = new ADXSerie(); List <Ohlc> tempOhlcList = new List <Ohlc>(); for (int i = 0; i < OhlcList.Count; i++) { Ohlc tempOhlc = new Ohlc() { Close = OhlcList[i].High }; tempOhlcList.Add(tempOhlc); } Momentum momentum = new Momentum(); momentum.Load(tempOhlcList); List <double?> highMomentums = momentum.Calculate().Values; tempOhlcList = new List <Ohlc>(); for (int i = 0; i < OhlcList.Count; i++) { Ohlc tempOhlc = new Ohlc() { Close = OhlcList[i].Low }; tempOhlcList.Add(tempOhlc); } momentum = new Momentum(); momentum.Load(tempOhlcList); List <double?> lowMomentums = momentum.Calculate().Values; for (int i = 0; i < lowMomentums.Count; i++) { if (lowMomentums[i].HasValue) { lowMomentums[i] *= -1; } } //DMIp <- ifelse( dH==dL | (dH< 0 & dL< 0), 0, ifelse( dH >dL, dH, 0 ) ) List <double?> DMIPositives = new List <double?>() { null }; // DMIn <- ifelse( dH==dL | (dH< 0 & dL< 0), 0, ifelse( dH <dL, dL, 0 ) ) List <double?> DMINegatives = new List <double?>() { null }; for (int i = 1; i < OhlcList.Count; i++) { if (highMomentums[i] == lowMomentums[i] || (highMomentums[i] < 0 & lowMomentums[i] < 0)) { DMIPositives.Add(0); } else { if (highMomentums[i] > lowMomentums[i]) { DMIPositives.Add(highMomentums[i]); } else { DMIPositives.Add(0); } } if (highMomentums[i] == lowMomentums[i] || (highMomentums[i] < 0 & lowMomentums[i] < 0)) { DMINegatives.Add(0); } else { if (highMomentums[i] < lowMomentums[i]) { DMINegatives.Add(lowMomentums[i]); } else { DMINegatives.Add(0); } } } ATR atr = new ATR(); atr.Load(OhlcList); List <double?> trueRanges = atr.Calculate().TrueRange; adxSerie.TrueRange = trueRanges; List <double?> trSum = wilderSum(trueRanges); // DIp <- 100 * wilderSum(DMIp, n=n) / TRsum List <double?> DIPositives = new List <double?>(); List <double?> wilderSumOfDMIp = wilderSum(DMIPositives); for (int i = 0; i < wilderSumOfDMIp.Count; i++) { if (wilderSumOfDMIp[i].HasValue) { DIPositives.Add(wilderSumOfDMIp[i].Value * 100 / trSum[i].Value); } else { DIPositives.Add(null); } } adxSerie.DIPositive = DIPositives; // DIn <- 100 * wilderSum(DMIn, n=n) / TRsum List <double?> DINegatives = new List <double?>(); List <double?> wilderSumOfDMIn = wilderSum(DMINegatives); for (int i = 0; i < wilderSumOfDMIn.Count; i++) { if (wilderSumOfDMIn[i].HasValue) { DINegatives.Add(wilderSumOfDMIn[i].Value * 100 / trSum[i].Value); } else { DINegatives.Add(null); } } adxSerie.DINegative = DINegatives; // DX <- 100 * ( abs(DIp - DIn) / (DIp + DIn) ) List <double?> DX = new List <double?>(); for (int i = 0; i < OhlcList.Count; i++) { if (DIPositives[i].HasValue) { double?dx = 100 * (Math.Abs(DIPositives[i].Value - DINegatives[i].Value) / (DIPositives[i].Value + DINegatives[i].Value)); DX.Add(dx); } else { DX.Add(null); } } adxSerie.DX = DX; for (int i = 0; i < OhlcList.Count; i++) { if (DX[i].HasValue) { OhlcList[i].Close = DX[i].Value; } else { OhlcList[i].Close = 0.0; } } EMA ema = new EMA(Period, true); ema.Load(OhlcList.Skip(Period).ToList()); List <double?> emaValues = ema.Calculate().Values; for (int i = 0; i < Period; i++) { emaValues.Insert(0, null); } adxSerie.ADX = emaValues; return(adxSerie); }
public void ATR() { ATR atr = new ATR(); List <Ohlc> ohlcList = new List <Ohlc> { new Ohlc { High = 48.70, Low = 47.79, Close = 48.16 }, new Ohlc { High = 48.72, Low = 48.14, Close = 48.61 }, new Ohlc { High = 48.90, Low = 48.39, Close = 48.75 }, new Ohlc { High = 48.87, Low = 48.37, Close = 48.63 }, new Ohlc { High = 48.82, Low = 48.24, Close = 48.74 }, new Ohlc { High = 49.05, Low = 48.64, Close = 49.03 }, new Ohlc { High = 49.20, Low = 48.94, Close = 49.07 }, new Ohlc { High = 49.35, Low = 48.86, Close = 49.32 }, new Ohlc { High = 49.92, Low = 49.50, Close = 49.91 }, new Ohlc { High = 50.19, Low = 49.87, Close = 50.13 }, new Ohlc { High = 50.12, Low = 49.20, Close = 49.53 }, new Ohlc { High = 49.66, Low = 48.90, Close = 49.50 }, new Ohlc { High = 49.88, Low = 49.43, Close = 49.75 }, new Ohlc { High = 50.19, Low = 49.73, Close = 50.03 }, new Ohlc { High = 50.36, Low = 49.26, Close = 50.31 }, new Ohlc { High = 50.57, Low = 50.09, Close = 50.52 }, new Ohlc { High = 50.65, Low = 50.30, Close = 50.41 }, new Ohlc { High = 50.43, Low = 49.21, Close = 49.34 }, new Ohlc { High = 49.63, Low = 48.98, Close = 49.37 }, new Ohlc { High = 50.33, Low = 49.61, Close = 50.23 } }; atr.Load(ohlcList); ATRSerie serie = atr.Calculate(); Assert.NotNull(serie); Assert.True(serie.ATR.Count > 0); Assert.True(serie.TrueRange.Count > 0); Assert.True(Math.Round(serie.TrueRange[13].Value, 2) == 0.46); Assert.True(Math.Round(serie.TrueRange[14].Value, 2) == 1.10); Assert.True(Math.Round(serie.TrueRange[15].Value, 2) == 0.48); Assert.True(Math.Round(serie.TrueRange[16].Value, 2) == 0.35); Assert.True(Math.Round(serie.TrueRange[17].Value, 2) == 1.22); Assert.True(Math.Round(serie.TrueRange[18].Value, 2) == 0.65); Assert.True(Math.Round(serie.TrueRange[19].Value, 2) == 0.96); Assert.True(Math.Round(serie.ATR[13].Value, 2) == 0.55); Assert.True(Math.Round(serie.ATR[14].Value, 2) == 0.59); Assert.True(Math.Round(serie.ATR[15].Value, 2) == 0.59); Assert.True(Math.Round(serie.ATR[16].Value, 2) == 0.57); Assert.True(Math.Round(serie.ATR[17].Value, 2) == 0.61); Assert.True(Math.Round(serie.ATR[18].Value, 2) == 0.62); Assert.True(Math.Round(serie.ATR[19].Value, 2) == 0.64); }