public void IndicatorsShouldNotChangeTheSourceOhlcList()
        {
            // read ohlc list from csv file
            List <Ohlc> ohlcList = ReadCsvFile(csvPath);
            // create a deep clone of the list
            List <Ohlc> ohlcListDeepClone = ohlcList.DeepClone();
            // run MACD on the ohlcList
            MACD macd = new MACD(true);

            macd.Load(ohlcList);
            MACDSerie serie = macd.Calculate();
            // check if the ohlcList is still the same as its deep copy
            int i = 0;

            foreach (var item in ohlcList)
            {
                Assert.Equal(item.AdjClose, ohlcListDeepClone[i].AdjClose);
                Assert.Equal(item.Close, ohlcListDeepClone[i].Close);
                Assert.Equal(item.Date, ohlcListDeepClone[i].Date);
                Assert.Equal(item.High, ohlcListDeepClone[i].High);
                Assert.Equal(item.Low, ohlcListDeepClone[i].Low);
                Assert.Equal(item.Open, ohlcListDeepClone[i].Open);
                Assert.Equal(item.Volume, ohlcListDeepClone[i].Volume);
                i++;
            }
        }
Example #2
0
        public static void WriteMacdhistogramDataToExcel(MACDSerie mACDSerie, RSISerie rsiSeries, SingleDoubleSerieV2 shorttermSingleDoubleSerieV2, SingleDoubleSerieV2 longtermSingleDoubleSerieV2, string sheetName)
        {
            var subset = mACDSerie.MACDHistogramDataList.GetRange(mACDSerie.MACDHistogramDataList.Count - 60, 60);
            //Search For the template file

            XSSFWorkbook workbook;
            string       excelFile = sheetName + "Data.xlsx";

            File.Delete(excelFile);
            // File.Copy(@"MacdHDataTemplate.xlsx", excelFile);

            using (FileStream file = new FileStream(@"MacdHDataTemplate.xlsx", FileMode.Open, FileAccess.Read))
            {
                workbook = new XSSFWorkbook(file);
                ISheet sheet = workbook.GetSheet("template");

                //sheet = sheet.CopySheet(sheetName);
                //                sheet = workbook.CloneSheet(workbook.IndexOf(sheet));


                int i = 1;
                foreach (var item in subset)
                {
                    List <BuySellSignal> signals = new List <BuySellSignal>();
                    signals = MACD.SetSignalType(item, signals);
                    var row     = sheet.CreateRow(i);
                    var rsiData = rsiSeries.rsiDataPoints.Where(x => x.Date == item.DataDate).FirstOrDefault();
                    var shortTermSingleDoubleSeriesData = shorttermSingleDoubleSerieV2.Values.Where(x => x.date == item.DataDate).FirstOrDefault();
                    var longTermSingleDoubleSeriesData  = longtermSingleDoubleSerieV2.Values.Where(x => x.date == item.DataDate).FirstOrDefault();
                    shortTermSingleDoubleSeriesData.findSignals(longTermSingleDoubleSeriesData, signals, item.ClosingValue);

                    row.CreateCell(0).SetCellValue(item.DataDate.Date.ToString("dd/MM/yyyy"));
                    row.CreateCell(1).SetCellValue(item.isConvergingOrDiverging.ToString());
                    row.CreateCell(2).SetCellValue(item.EmaLineDifference.Value);
                    row.CreateCell(3).SetCellValue(item.changeInDivergenceMomentum.Value);
                    row.CreateCell(4).SetCellValue(item.isDiffereneAmountDecreasing);
                    //if (rsiData != null)
                    //{
                    //    row.CreateCell(5).SetCellValue(rsiData.RS.Value);
                    //    row.CreateCell(6).SetCellValue(rsiData.RSI.Value);
                    //}
                    if (longTermSingleDoubleSeriesData != null && shortTermSingleDoubleSeriesData != null)
                    {
                        row.CreateCell(7).SetCellValue(shortTermSingleDoubleSeriesData.data.Value);
                        row.CreateCell(8).SetCellValue(longTermSingleDoubleSeriesData.data.Value);
                        row.CreateCell(9).SetCellValue(shortTermSingleDoubleSeriesData.data.Value < longTermSingleDoubleSeriesData.data.Value);
                    }
                    row.CreateCell(10).SetCellValue(item.ClosingValue);
                    row.CreateCell(11).SetCellValue(string.Join(Environment.NewLine, signals.ToArray()));
                    i++;
                }
            }
            using (FileStream file = new FileStream(excelFile, FileMode.CreateNew, FileAccess.Write))
            {
                workbook.Write(file);
                file.Close();
            }
            //Write to specific Rows in the excel file
        }
Example #3
0
        private static MACDSerie GetMacdSeries(MACD macd, string stockName)
        {
            string downloadFolderName = ConfigurationManager.AppSettings["downloadLocation"];

            macd.Load(downloadFolderName + stockName + ".ax.csv");
            MACDSerie serie = macd.Calculate();

            //ExcelUtilities.WriteMacdhistogramDataToExcel(serie, stockName);
            return(serie);
        }
Example #4
0
        public void MACD()
        {
            //MACD macd = new MACD();
            MACD macd = new MACD(true);

            macd.Load(OhlcList);
            MACDSerie serie = macd.Calculate();

            Assert.IsNotNull(serie);
            Assert.IsTrue(serie.Signal.Count > 0);
            Assert.IsTrue(serie.MACDLine.Count > 0);
            Assert.IsTrue(serie.MACDHistogram.Count > 0);
        }
        public void MACD()
        {
            //MACD macd = new MACD();
            MACD macd = new MACD(true);

            macd.Load(Directory.GetCurrentDirectory() + "\\table.csv");
            MACDSerie serie = macd.Calculate();

            Assert.NotNull(serie);
            Assert.True(serie.Signal.Count > 0);
            Assert.True(serie.MACDLine.Count > 0);
            Assert.True(serie.MACDHistogram.Count > 0);
        }
Example #6
0
        static void Main(string[] args)
        {
            DownloadAllStockFiles();

            string[] stockNames = ConfigurationManager.AppSettings["StockNames"].Split(',');
            foreach (var stockName in stockNames)
            {
                MACD macd = new MACD(false);

                MACDSerie           macdSeries   = GetMacdSeries(macd, stockName);
                RSI                 rsi          = new RSI(14);
                RSISerie            rsiSeries    = GetRsiSeries(rsi, stockName);
                SMAV2               shortTermSma = new SMAV2(14);
                SingleDoubleSerieV2 shorttermSingleDoubleSerieV2 = GetSingleDoubleSeriesV2(shortTermSma, stockName);
                SMAV2               longTermSma = new SMAV2(60);
                SingleDoubleSerieV2 longtermSingleDoubleSerieV2 = GetSingleDoubleSeriesV2(longTermSma, stockName);

                ExcelUtilities.WriteMacdhistogramDataToExcel(macdSeries, rsiSeries, shorttermSingleDoubleSerieV2, longtermSingleDoubleSerieV2, stockName);
            }
        }
Example #7
0
        public void MACD()
        {
            //File.Delete(@"MacdHData.xlsx");
            //MACD macd = new MACD();
            MACD macd = new MACD(false);

            //https://au.finance.yahoo.com/quote/VAS.AX/
            //https://au.finance.yahoo.com/quote/STW.AX/
            //https://au.finance.yahoo.com/quote/APT.AX
            //MACDSerie serie = GenerateMacdHistogramData(macd, "vas"); q q
            //macd = new MACD(false);
            //serie = GenerateMacdHistogramData(macd, "stw");
            //macd = new MACD(false);
            //serie = GenerateMacdHistogramData(macd, "apt");
            macd.Load(Directory.GetCurrentDirectory() + "\\" + "table" + ".csv");
            MACDSerie serie = macd.Calculate();

            Assert.IsNotNull(serie);
            Assert.IsTrue(serie.Signal.Count > 0);
            Assert.IsTrue(serie.MACDLine.Count > 0);
            Assert.IsTrue(serie.MACDHistogramDataList.Count > 0);
            //serie.MACDHistogram.Select((x,i) => new {i, value = x.GetValueOrDefault(),Consider = x != null &&  x.Value < 0 && x.Value >-1 }).Where(x => x.Consider)
            //serie.MACDHistogram.ElementAt(i-1) if its negative and > current negative value between 0 and -1 , good time to buy
        }
Example #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]);
                }
            }
        }