Ejemplo n.º 1
0
        public override MACDSerie Calculate()
        {
            MACDSerie macdSerie = new MACDSerie();

            EMA ema = new EMA(Fast, false);

            ema.Load(OhlcList);
            List <double?> fastEmaValues = ema.Calculate().Values;

            ema = new EMA(Slow, false);
            ema.Load(OhlcList);
            List <double?> slowEmaValues = ema.Calculate().Values;

            for (int i = 0; i < OhlcList.Count; i++)
            {
                // MACD Line
                if (fastEmaValues[i].HasValue && slowEmaValues[i].HasValue)
                {
                    if (!Percent)
                    {
                        macdSerie.MACDLine.Add(fastEmaValues[i].Value - slowEmaValues[i].Value);
                    }
                    else
                    {
                        // macd <- 100 * ( mavg.fast / mavg.slow - 1 )
                        macdSerie.MACDLine.Add(100 * ((fastEmaValues[i].Value / slowEmaValues[i].Value) - 1));
                    }
                    OhlcList[i].Close = macdSerie.MACDLine[i].Value;
                }
                else
                {
                    macdSerie.MACDLine.Add(null);
                    OhlcList[i].Close = 0.0;
                }
            }

            int zeroCount = macdSerie.MACDLine.Where(x => x == null).Count();

            ema = new EMA(Signal, false);
            ema.Load(OhlcList.Skip(zeroCount).ToList());
            List <double?> signalEmaValues = ema.Calculate().Values;

            for (int i = 0; i < zeroCount; i++)
            {
                signalEmaValues.Insert(0, null);
            }

            // Fill Signal and MACD Histogram lists
            for (int i = 0; i < signalEmaValues.Count; i++)
            {
                macdSerie.Signal.Add(signalEmaValues[i]);

                macdSerie.MACDHistogram.Add(macdSerie.MACDLine[i] - macdSerie.Signal[i]);
            }

            return(macdSerie);
        }
Ejemplo n.º 2
0
        public void EMA()
        {
            EMA ema = new EMA(10, true);

            ema.Load(Directory.GetCurrentDirectory() + "\\table.csv");
            SingleDoubleSerie serie = ema.Calculate();

            Assert.IsNotNull(serie);
            Assert.IsTrue(serie.Values.Count > 0);
        }
Ejemplo n.º 3
0
        public void EMA()
        {
            EMA ema = new EMA(10, true);

            ema.Load(OhlcList);
            SingleDoubleSerie serie = ema.Calculate();

            Assert.IsNotNull(serie);
            Assert.IsTrue(serie.Values.Count > 0);
        }
Ejemplo n.º 4
0
        public void EMA()
        {
            EMA ema = new EMA(10, true);

            ema.Load(csvPath);
            SingleDoubleSerie serie = ema.Calculate();

            Assert.NotNull(serie);
            Assert.True(serie.Values.Count > 0);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// DEMA = 2 * EMA - EMA of EMA
        /// </summary>
        /// <see cref="http://forex-indicators.net/trend-indicators/dema"/>
        /// <returns></returns>
        public override SingleDoubleSerie Calculate()
        {
            SingleDoubleSerie demaSerie = new SingleDoubleSerie();
            EMA ema = new EMA(Period, false);

            ema.Load(OhlcList);
            List <double?> emaValues = ema.Calculate().Values;

            // assign EMA values to Close price
            for (int i = 0; i < OhlcList.Count; i++)
            {
                OhlcList[i].Close = emaValues[i].HasValue ? emaValues[i].Value : 0.0;
            }

            ema.Load(OhlcList.Skip(Period - 1).ToList());
            // EMA(EMA(value))
            List <double?> emaEmaValues = ema.Calculate().Values;

            for (int i = 0; i < Period - 1; i++)
            {
                emaEmaValues.Insert(0, null);
            }

            // Calculate DEMA
            for (int i = 0; i < OhlcList.Count; i++)
            {
                if (i >= 2 * Period - 2)
                {
                    var dema = 2 * emaValues[i] - emaEmaValues[i];
                    demaSerie.Values.Add(dema);
                }
                else
                {
                    demaSerie.Values.Add(null);
                }
            }

            return(demaSerie);
        }
Ejemplo n.º 6
0
        public double[] CalculateEMA(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 ema = new EMA(period, true);

            ema.Load(listOhlc);
            var result = ema.Calculate();

            return(result.Values.Select(x => x.GetValueOrDefault()).ToArray());
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 1 - EMA of Close prices [EMA(Close)]
        /// 2 - Double smooth [EMA(EMA(Close))]
        /// 3 - Triple smooth [EMA(EMA(EMA(Close)))]
        /// 4 - a) Calculation with percentage: [ROC(EMA(EMA(EMA(Close))))]
        /// 4 - b) Calculation with percentage: [Momentum(EMA(EMA(EMA(Close))))]
        /// </summary>
        /// <see cref="http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:trix"/>
        /// <see cref="http://www.fmlabs.com/reference/default.htm?url=TRIX.htm"/>
        /// <returns></returns>
        public override SingleDoubleSerie Calculate()
        {
            // EMA calculation
            EMA ema = new EMA(Period, false);

            ema.Load(OhlcList);
            List <double?> emaValues = ema.Calculate().Values;

            for (int i = 0; i < OhlcList.Count; i++)
            {
                OhlcList[i].Close = emaValues[i].HasValue ? emaValues[i].Value : 0.0;
            }

            // Double smooth
            ema.Load(OhlcList.Skip(Period - 1).ToList());
            List <double?> doubleSmoothValues = ema.Calculate().Values;

            for (int i = 0; i < Period - 1; i++)
            {
                doubleSmoothValues.Insert(0, null);
            }
            for (int i = 0; i < OhlcList.Count; i++)
            {
                OhlcList[i].Close = doubleSmoothValues[i].HasValue ? doubleSmoothValues[i].Value : 0.0;
            }

            // Triple smooth
            ema.Load(OhlcList.Skip(2 * (Period - 1)).ToList());
            List <double?> tripleSmoothValues = ema.Calculate().Values;

            for (int i = 0; i < (2 * (Period - 1)); i++)
            {
                tripleSmoothValues.Insert(0, null);
            }
            for (int i = 0; i < OhlcList.Count; i++)
            {
                OhlcList[i].Close = tripleSmoothValues[i].HasValue ? tripleSmoothValues[i].Value : 0.0;
            }

            // Last step
            SingleDoubleSerie trixSerie = new SingleDoubleSerie();

            if (CalculatePercentage)
            {
                ROC roc = new ROC(1);
                roc.Load(OhlcList.Skip(3 * (Period - 1)).ToList());
                trixSerie = roc.Calculate();
            }
            else
            {
                Momentum momentum = new Momentum();
                momentum.Load(OhlcList.Skip(3 * (Period - 1)).ToList());
                trixSerie = momentum.Calculate();
            }

            for (int i = 0; i < (3 * (Period - 1)); i++)
            {
                trixSerie.Values.Insert(0, null);
            }

            return(trixSerie);
        }
Ejemplo n.º 8
0
        public Form1()
        {
            InitializeComponent();
            chart1.Series.Clear();
            Int32 timeStampStart = (Int32)(DateTime.UtcNow.AddMonths(-12).Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
            Int32 timeStampNow   = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

            String yahooAPI = $"https://query1.finance.yahoo.com/v7/finance/download/BTC-EUR?period1={timeStampStart}&period2={timeStampNow}&interval=1d&events=history";

            using (var client = new WebClient())
            {
                client.DownloadFile(yahooAPI, "fileLOG");
            }

            String       csvFile  = @"fileLOG";
            List <Ohlc>  ohlcList = new List <Ohlc>();
            StreamReader r        = new StreamReader(csvFile);
            String       file     = r.ReadToEnd();

            String[] pricesClose = file.Split(new String[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
            chart1.Series.Add("Price").ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastPoint;
            // chart1.Series["Price"].Color = Color.Black;
            int             j     = 0;
            List <DateTime> dates = new List <DateTime>();

            foreach (var s in pricesClose)
            {
                String[] pricesOscillation = s.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                try
                {
                    int      index    = 0;
                    DateTime date     = DateTime.Parse(pricesOscillation[index++]);
                    double   open     = double.Parse(pricesOscillation[index++]);
                    double   high     = double.Parse(pricesOscillation[index++]);
                    double   low      = double.Parse(pricesOscillation[index++]);
                    double   close    = double.Parse(pricesOscillation[index++]);
                    double   adjClose = double.Parse(pricesOscillation[index++]);
                    double   volume   = double.Parse(pricesOscillation[index++]);

                    Ohlc newOhlc = new Ohlc();
                    newOhlc.Open     = open;
                    newOhlc.High     = high;
                    newOhlc.Low      = low;
                    newOhlc.Close    = close;
                    newOhlc.AdjClose = adjClose;
                    newOhlc.Volume   = volume;

                    ohlcList.Add(newOhlc);

                    dates.Add(date);
                    chart1.Series["Price"].Points.AddXY(date, close);
                }
                catch (Exception err)
                {
                }
            }


            BollingerBand bollingerBand = new BollingerBand();

            bollingerBand.Load(ohlcList);
            BollingerBandSerie serie = bollingerBand.Calculate();

            MACD macd = new MACD();

            macd.Load(ohlcList);
            MACDSerie macdserie = macd.Calculate();

            EMA ema = new EMA();

            ema.Load(ohlcList);
            SingleDoubleSerie singleDoubleSerie = ema.Calculate();


            RSI rsi = new RSI(14);

            rsi.Load(ohlcList);
            RSISerie serieRSI = rsi.Calculate();

            chart1.Series.Add("bUP").ChartType           = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            chart1.Series.Add("bDOWN").ChartType         = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            chart1.Series.Add("bMIDDLE").ChartType       = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            chart1.Series.Add("RSI").ChartType           = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            chart1.Series.Add("EMA").ChartType           = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            chart1.Series.Add("MACD").ChartType          = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
            chart1.Series.Add("MACDHistogram").ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;

            for (int i = 0; i < serie.BandWidth.Count; i++)
            {
                if (macdserie.MACDLine[i] != null)
                {
                    chart1.Series["MACD"].Points.AddXY(dates[i], macdserie.MACDLine[i]);
                }

                if (macdserie.MACDHistogram[i] != null)
                {
                    chart1.Series["MACDHistogram"].Points.AddXY(dates[i], macdserie.MACDHistogram[i]);
                }

                if (singleDoubleSerie.Values[i] != null)
                {
                    chart1.Series["EMA"].Points.AddXY(dates[i], singleDoubleSerie.Values[i]);
                }

                if (serie.UpperBand[i] != null)
                {
                    chart1.Series["bUP"].Points.AddXY(dates[i], serie.UpperBand[i]);
                }

                if (serie.MidBand[i] != null)
                {
                    chart1.Series["bMIDDLE"].Points.AddXY(dates[i], serie.MidBand[i]);
                }

                if (serie.LowerBand[i] != null)
                {
                    chart1.Series["bDOWN"].Points.AddXY(dates[i], serie.LowerBand[i]);
                }

                if (serieRSI.RSI[i] != null)
                {
                    chart1.Series["RSI"].Points.AddXY(dates[i], serieRSI.RSI[i]);
                }
            }
        }
Ejemplo n.º 9
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);
        }