Exemple #1
0
        public void TestBollingerBand()
        {
            double[] inData = new double[] {
                86.16, 89.09, 88.78, 90.32, 89.07,
                91.15, 89.44, 89.18, 86.93, 87.68,
                86.96, 89.43, 89.32, 88.72, 87.45,
                87.26, 89.50, 87.90, 89.13, 90.70,
                92.90, 92.98, 91.80, 92.66, 92.68,
                92.30, 92.77, 92.54, 92.95, 93.20,
                91.07, 89.83, 89.74, 90.40, 90.74,
                88.02, 88.09, 88.84, 90.78, 90.54,
                91.39, 90.65
            };

            int len    = inData.Length;
            int period = 20;

            double?[] m = new double?[len];
            double?[] h = new double?[len];
            double?[] l = new double?[len];

            BollingerBand.Calculate(inData, period, 2.0, m, h, l);

            Console.WriteLine("middle: " + ObjectHelper.ToJson(m) + "\n");
            Console.WriteLine("high: " + ObjectHelper.ToJson(h) + "\n");
            Console.WriteLine("low: " + ObjectHelper.ToJson(l) + "\n");
        }
        public async Task<IndBBEntity[]> GetBB(string code, int start = 0, int end = 0, double factor = 2.0, int period = 20, string type = "day")
        {
            Console.WriteLine($"{code} {start} {end} {type}");
            TickerEntity[] tickers = await base.getTickerEntityArray(code, start, end, type);
            List<IndBBEntity> outList = new List<IndBBEntity>();

            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?[] outHigh = new double?[len];
            double?[] outMid = new double?[len];
            double?[] outLow = new double?[len];

            BollingerBand.Calculate(close, period, factor, outMid, outHigh, outLow);

            for (int i = 0; i < len; i++)
            {
                outList.Add(new IndBBEntity
                {
                    T = tickers[i].T,
                    P = tickers[i].P,
                    High = outHigh[i],
                    Mid = outMid[i],
                    Low = outLow[i]
                });
            }

            return outList.Where(r => (start == 0 || r.P >= start) && (end == 0 || r.P <= end)).ToArray();
        }
Exemple #3
0
        public void BollingerBand()
        {
            BollingerBand bollingerBand = new BollingerBand();

            bollingerBand.Load(Directory.GetCurrentDirectory() + "\\table.csv");
            BollingerBandSerie serie = bollingerBand.Calculate();

            Assert.IsNotNull(serie);
            Assert.IsTrue(serie.BandWidth.Count > 0);
            Assert.IsTrue(serie.BPercent.Count > 0);
            Assert.IsTrue(serie.LowerBand.Count > 0);
            Assert.IsTrue(serie.MidBand.Count > 0);
            Assert.IsTrue(serie.UpperBand.Count > 0);
        }
        public void BollingerBand()
        {
            BollingerBand bollingerBand = new BollingerBand();

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

            Assert.IsNotNull(serie);
            Assert.IsTrue(serie.BandWidth.Count > 0);
            Assert.IsTrue(serie.BPercent.Count > 0);
            Assert.IsTrue(serie.LowerBand.Count > 0);
            Assert.IsTrue(serie.MidBand.Count > 0);
            Assert.IsTrue(serie.UpperBand.Count > 0);
        }
Exemple #5
0
        public void TestCalculateRealData()
        {
            TickerBLL tbll = new TickerBLL(_unit);

            List <Ticker> tList = tbll.GetTickerListByShareDB(1585, null, 21100000);

            double[]  inputData = new double[tList.Count];
            double?[] m         = new double?[tList.Count];
            double?[] h         = new double?[tList.Count];
            double?[] l         = new double?[tList.Count];

            var i = 0;

            foreach (var t in tList)
            {
                inputData[i] = t.Close;
                i++;
            }


            Result res = BollingerBand.Calculate(inputData, 20, 2.5, m, h, l);
        }
Exemple #6
0
        public void TestCalculate()
        {
            double[] inData = new double[] {
                86.16, 89.09, 88.78, 90.32, 89.07,
                91.15, 89.44, 89.18, 86.93, 87.68,
                86.96, 89.43, 89.32, 88.72, 87.45,
                87.26, 89.50, 87.90, 89.13, 90.70,
                92.90, 92.98, 91.80, 92.66, 92.68,
                92.30, 92.77, 92.54, 92.95, 93.20,
                91.07, 89.83, 89.74, 90.40, 90.74,
                88.02, 88.09, 88.84, 90.78, 90.54,
                91.39, 90.65
            };

            int len    = inData.Length;
            int period = 20;

            double?[] m = new double?[len];
            double?[] h = new double?[len];
            double?[] l = new double?[len];

            BollingerBand.Calculate(inData, period, 2.0, m, h, l);
        }
Exemple #7
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]);
                }
            }
        }