예제 #1
0
        public async Task<IndSingleValueEntity[]> GetATR(string code, int period, int? start = 0, int? end = 0, string type = "day")
        {
            TickerEntity[] tickers = await base.getTickerEntityArray(code, start, end, type);
            List<IndSingleValueEntity> outList = new List<IndSingleValueEntity>();

            int len = tickers.Length;

            double[] close = tickers.Select(t => (double)t.C).ToArray();
            double[] high = tickers.Select(t => (double)t.H).ToArray();
            double[] low = tickers.Select(t => (double)t.L).ToArray();
            double?[] outATR = new double?[len];

            ATR.Calculate(high, low, close, period, outATR);

            for (int i = 0; i < len; i++)
            {
                outList.Add(new IndSingleValueEntity
                {
                    T = tickers[i].T,
                    P = tickers[i].P,
                    V = outATR[i]
                });
            }

            return outList.Where(r => (start == 0 || r.P >= start) && (end == 0 || r.P <= end)).ToArray();
        }
예제 #2
0
        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);
        }
예제 #3
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);
        }
예제 #4
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());
        }
예제 #5
0
        public void TestATR()
        {
            double[] high = new double[] {
                48.70, 48.72, 48.90, 48.87, 48.82,
                49.05, 49.20, 49.35, 49.92, 50.19,
                50.12, 49.66, 49.88, 50.19, 50.36,
                50.57, 50.65, 50.43, 49.63, 50.33,
                50.29, 50.17, 49.32, 48.50, 48.32,
                46.80, 47.80, 48.39, 48.66, 48.79
            };

            double[] low = new double[] {
                47.79, 48.14, 48.39, 48.37, 48.24,
                48.64, 48.94, 48.86, 49.50, 49.87,
                49.20, 48.90, 49.43, 49.73, 49.26,
                50.09, 50.30, 49.21, 48.98, 49.61,
                49.20, 49.43, 48.08, 47.64, 41.55,
                44.28, 47.31, 47.20, 47.90, 47.73
            };


            double[] close = new double[] {
                48.16, 48.61, 48.75, 48.63, 48.74,
                49.03, 49.07, 49.32, 49.91, 50.13,
                49.53, 49.50, 49.75, 50.03, 50.31,
                50.52, 50.41, 49.34, 49.37, 50.23,
                49.24, 49.93, 48.43, 48.18, 46.57,
                45.41, 47.77, 47.72, 48.62, 47.85
            };

            int len = close.Length;

            double?[] outATR = new double?[len];


            ATR.Calculate(high, low, close, 14, outATR);

            Console.WriteLine("di plus: " + ObjectHelper.ToJson(outATR));
        }
예제 #6
0
 public void Calculate__CalculatesCorrectly(float[] dataL, float[] dataC, float[] dataH, float[] expected)
 {
     _testObj.Calculate(dataH, dataL, dataC, 4).ShouldBe(expected);
 }
예제 #7
0
        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);
        }
예제 #8
0
        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);
        }