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");
        }
Exemple #2
0
 public Benchmark(QREBridgeBase bridge, Symbol symbol) : base(bridge, symbol)
 {
     bbLower = new BollingerBand(bars.close, parameter <int>("LengthDn"), -parameter <int>("NumDevsDn"));
     bbUpper = new BollingerBand(bars.close, parameter <int>("LengthUp"), parameter <int>("NumDevsUp"));
     addToPlot(bbLower, "bbLower", Color.Blue);
     addToPlot(bbUpper, "bbUpper", Color.Blue);
 }
        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 #4
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 #6
0
        protected TrendingMABase(QREBridgeBase bridge, Symbol symbol, Converter <BarSpud, Spud <double> > signalSeries) : base(bridge, symbol)
        {
            maDays      = parameter <int>("MADays");
            riskDollars = parameter <double>("RiskDollars");
            atr         = new AverageTrueRange(bars, parameter <int>("ATRLen"));
            signal      = signalSeries(bars);
            ma          = new Average(signal, maDays);
            shortSum    = new Sum(signal, maDays - 1);
            var numDeviations = parameter <double>("BollingerBandDeviations");
            var barsBack      = parameter <int>("BollingerBandBarsBack");

            upperBand = new BollingerBand(ma, barsBack, numDeviations);
            lowerBand = new BollingerBand(ma, barsBack, -numDeviations);
        }
 private BollingerBand[] getBollingerBand(double[] data, int duration)
 {
     BollingerBand[] boll = new BollingerBand[data.Length];
     for (int i = duration; i < data.Length; i++)
     {
         List <double> list = new List <double>();
         for (int j = 0; j < duration; j++)
         {
             list.Add(data[i - duration + j]);
         }
         boll[i]      = new BollingerBand();
         boll[i].mean = list.Average();
         boll[i].std  = MathUtility.std(list);
     }
     return(boll);
 }
        /// <summary>
        /// Add Bollinger Bands and Volume data to list
        /// </summary>
        /// <param name="candlesticks">Array of Candlesticks</param>
        private void AddBollingerBands(ref BotStick[] candlesticks)
        {
            int     period        = candlestickCount;
            int     factor        = 2;
            decimal total_average = 0;
            decimal total_squares = 0;
            decimal prev_vol      = 0;

            for (int i = 0; i < candlesticks.Length; i++)
            {
                total_average += candlesticks[i].close;
                total_squares += (decimal)Math.Pow((double)candlesticks[i].close, 2);
                prev_vol       = prev_vol == 0 ? candlesticks[i].volume : prev_vol;

                var volData = CalculateVolumeChanges(candlesticks[i].volume, prev_vol);

                if (volData != null)
                {
                    if (volData.ContainsKey("volumeDifference"))
                    {
                        candlesticks[i].volumeChange = volData["volumeDifference"];
                    }

                    if (volData.ContainsKey("volumePercentChange"))
                    {
                        candlesticks[i].volumePercentChange = volData["volumePercentChange"];
                    }
                }

                if (i >= period - 1)
                {
                    var     bollingerBand = new BollingerBand();
                    decimal average       = total_average / period;

                    decimal stdev = (decimal)Math.Sqrt((double)(total_squares - (decimal)Math.Pow((double)total_average, 2) / period) / period);
                    bollingerBand.movingAvg  = average;
                    bollingerBand.topBand    = average + factor * stdev;
                    bollingerBand.bottomBand = average - factor * stdev;

                    candlesticks[i].bollingerBand = bollingerBand;
                    total_average -= candlesticks[i - period + 1].close;
                    total_squares -= (decimal)Math.Pow((double)candlesticks[i - period + 1].close, 2);
                }

                prev_vol = candlesticks[i].volume;
            }
        }
Exemple #9
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 #10
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 #11
0
 public FXCarryV2(QREBridgeBase bridge, Symbol symbol) : base(bridge, symbol)
 {
     payoutRatioLong  = payoutRatioSymbol(symbol, "Long").doubles(bars).allowStaleTicks();
     payoutRatioShort = payoutRatioSymbol(symbol, "Short").doubles(bars).allowStaleTicks();
     nATR             = parameter <int>("nATR");
     atrLen           = parameter <int>("ATRLen");
     atr                   = new AverageTrueRange(bars, atrLen);
     riskDollars           = parameter <double>("RiskDollars");
     bollingerBandBarsBack = parameter <int>("BollingerBandBarsBack");
     ma = new Average(bars.close, bollingerBandBarsBack);
     bollingerBandDeviations = parameter <double>("BollingerBandDeviations");
     trigger        = parameter <double>("Trigger");
     maxTrigger     = parameter <double>("MaxTrigger");
     triggerCushion = parameter <double>("TriggerCushion");
     addToPlot(payoutRatioLong, "payoutRatioLong", Color.Red, "payoutRatioLong");
     addToPlot(payoutRatioShort, "payoutRatioShort", Color.Red, "payoutRatioShort");
     upperBand = new BollingerBand(bars.close, bollingerBandBarsBack, bollingerBandDeviations);
     lowerBand = new BollingerBand(bars.close, bollingerBandBarsBack, -bollingerBandDeviations);
     addToPlot(upperBand, "upperBand", Color.Blue);
     addToPlot(lowerBand, "lowerBand", Color.Blue);
     bars.close.prepare();
     stoppedOut = true;
 }
Exemple #12
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]);
                }
            }
        }