Exemplo n.º 1
0
        public void bollingerBandsLowerUsingSMAAndStandardDeviation()
        {
            BollingerBandsMiddleIndicator bbmSMA            = new BollingerBandsMiddleIndicator(sma);
            StandardDeviationIndicator    standardDeviation = new StandardDeviationIndicator(closePrice, timeFrame);
            BollingerBandsLowerIndicator  bblSMA            = new BollingerBandsLowerIndicator(bbmSMA, standardDeviation);

            Assert.AreEqual(bblSMA.getK(), 2);

            Assert.AreEqual(bblSMA.GetValue(0), 1);
            Assert.AreEqual(bblSMA.GetValue(1), 0.5M);
            Assert.AreEqual(bblSMA.GetValue(2), 0.3670068381445479345351439502M);
            Assert.AreEqual(bblSMA.GetValue(3), 1.3670068381445479345351439502M);
            Assert.AreEqual(bblSMA.GetValue(4), 2.3905242917512699674655408505M);
            Assert.AreEqual(bblSMA.GetValue(5), 2.7238576250846033007988741839M);
            Assert.AreEqual(bblSMA.GetValue(6), 2.3670068381445479345351439502M);

            BollingerBandsLowerIndicator bblSMAwithK = new BollingerBandsLowerIndicator(bbmSMA, standardDeviation, 1.5M);

            Assert.AreEqual(bblSMAwithK.getK(), 1.5M);

            Assert.AreEqual(bblSMAwithK.GetValue(0), 1);
            Assert.AreEqual(bblSMAwithK.GetValue(1), 0.75M);
            Assert.AreEqual(bblSMAwithK.GetValue(2), 0.7752551286084109509013579626M);
            Assert.AreEqual(bblSMAwithK.GetValue(3), 1.7752551286084109509013579626M);
            Assert.AreEqual(bblSMAwithK.GetValue(4), 2.6262265521467858089324889712M);
            Assert.AreEqual(bblSMAwithK.GetValue(5), 2.9595598854801191422658223046M);
            Assert.AreEqual(bblSMAwithK.GetValue(6), 2.7752551286084109509013579626M);
        }
        public void StandardDeviationShouldBeZeroWhenTimeFrameIs1()
        {
            var sdv = new StandardDeviationIndicator(new ClosePriceIndicator(_data), 1);

            TaTestsUtils.AssertDecimalEquals(sdv.GetValue(3), 0);
            TaTestsUtils.AssertDecimalEquals(sdv.GetValue(8), 0);
        }
Exemplo n.º 3
0
        public void BollingerBandsUpperUsingSmaAndStandardDeviation()
        {
            var bbmSma            = new BollingerBandsMiddleIndicator(_sma);
            var standardDeviation = new StandardDeviationIndicator(_closePrice, _timeFrame);
            var bbuSma            = new BollingerBandsUpperIndicator(bbmSma, standardDeviation);

            TaTestsUtils.AssertDecimalEquals(bbuSma.K, 2);

            TaTestsUtils.AssertDecimalEquals(bbuSma.GetValue(0), 1);
            TaTestsUtils.AssertDecimalEquals(bbuSma.GetValue(1), 2.5);
            TaTestsUtils.AssertDecimalEquals(bbuSma.GetValue(2), 3.633);
            TaTestsUtils.AssertDecimalEquals(bbuSma.GetValue(3), 4.633);
            TaTestsUtils.AssertDecimalEquals(bbuSma.GetValue(4), 4.2761);
            TaTestsUtils.AssertDecimalEquals(bbuSma.GetValue(5), 4.6094);
            TaTestsUtils.AssertDecimalEquals(bbuSma.GetValue(6), 5.633);
            TaTestsUtils.AssertDecimalEquals(bbuSma.GetValue(7), 5.2761);
            TaTestsUtils.AssertDecimalEquals(bbuSma.GetValue(8), 5.633);
            TaTestsUtils.AssertDecimalEquals(bbuSma.GetValue(9), 4.2761);

            var bbuSmAwithK = new BollingerBandsUpperIndicator(bbmSma, standardDeviation, Decimal.ValueOf("1.5"));

            TaTestsUtils.AssertDecimalEquals(bbuSmAwithK.K, 1.5);

            TaTestsUtils.AssertDecimalEquals(bbuSmAwithK.GetValue(0), 1);
            TaTestsUtils.AssertDecimalEquals(bbuSmAwithK.GetValue(1), 2.25);
            TaTestsUtils.AssertDecimalEquals(bbuSmAwithK.GetValue(2), 3.2247);
            TaTestsUtils.AssertDecimalEquals(bbuSmAwithK.GetValue(3), 4.2247);
            TaTestsUtils.AssertDecimalEquals(bbuSmAwithK.GetValue(4), 4.0404);
            TaTestsUtils.AssertDecimalEquals(bbuSmAwithK.GetValue(5), 4.3737);
            TaTestsUtils.AssertDecimalEquals(bbuSmAwithK.GetValue(6), 5.2247);
            TaTestsUtils.AssertDecimalEquals(bbuSmAwithK.GetValue(7), 5.0404);
            TaTestsUtils.AssertDecimalEquals(bbuSmAwithK.GetValue(8), 5.2247);
            TaTestsUtils.AssertDecimalEquals(bbuSmAwithK.GetValue(9), 4.0404);
        }
Exemplo n.º 4
0
        public void standardDeviationShouldBeZeroWhenTimeFrameIs1()
        {
            StandardDeviationIndicator sdv = new StandardDeviationIndicator(new ClosePriceIndicator(data), 1);

            Assert.AreEqual(sdv.GetValue(3), 0);
            Assert.AreEqual(sdv.GetValue(8), 0);
        }
Exemplo n.º 5
0
        public void BollingerBandsUpperUsingSMAAndStandardDeviation()
        {
            BollingerBandsMiddleIndicator bbmSMA            = new BollingerBandsMiddleIndicator(sma);
            StandardDeviationIndicator    standardDeviation = new StandardDeviationIndicator(closePrice, timeFrame);
            BollingerBandsUpperIndicator  bbuSMA            = new BollingerBandsUpperIndicator(bbmSMA, standardDeviation);

            Assert.AreEqual(bbuSMA.getK(), 2);

            Assert.AreEqual(bbuSMA.GetValue(0), 1);
            Assert.AreEqual(bbuSMA.GetValue(1), 2.5M);
            Assert.AreEqual(bbuSMA.GetValue(2), 3.6329931618554520654648560498M);
            Assert.AreEqual(bbuSMA.GetValue(3), 4.6329931618554520654648560498M);
            Assert.AreEqual(bbuSMA.GetValue(4), 4.2761423749153966992011258161M);
            Assert.AreEqual(bbuSMA.GetValue(5), 4.6094757082487300325344591495M);
            Assert.AreEqual(bbuSMA.GetValue(6), 5.6329931618554520654648560498M);
            Assert.AreEqual(bbuSMA.GetValue(7), 5.2761423749153966992011258161M);
            Assert.AreEqual(bbuSMA.GetValue(8), 5.6329931618554520654648560498M);
            Assert.AreEqual(bbuSMA.GetValue(9), 4.2761423749153966992011258161M);

            BollingerBandsUpperIndicator bbuSMAwithK = new BollingerBandsUpperIndicator(bbmSMA, standardDeviation, 1.5M);

            Assert.AreEqual(bbuSMAwithK.getK(), 1.5M);

            Assert.AreEqual(bbuSMAwithK.GetValue(0), 1);
            Assert.AreEqual(bbuSMAwithK.GetValue(1), 2.25M);
            Assert.AreEqual(bbuSMAwithK.GetValue(2), 3.2247448713915890490986420374M);
            Assert.AreEqual(bbuSMAwithK.GetValue(3), 4.2247448713915890490986420374M);
            Assert.AreEqual(bbuSMAwithK.GetValue(4), 4.0404401145198808577341776954M);
            Assert.AreEqual(bbuSMAwithK.GetValue(5), 4.3737734478532141910675110288M);
            Assert.AreEqual(bbuSMAwithK.GetValue(6), 5.2247448713915890490986420374M);
            Assert.AreEqual(bbuSMAwithK.GetValue(7), 5.0404401145198808577341776954M);
            Assert.AreEqual(bbuSMAwithK.GetValue(8), 5.2247448713915890490986420374M);
            Assert.AreEqual(bbuSMAwithK.GetValue(9), 4.0404401145198808577341776954M);
        }
        public void BollingerBandsLowerUsingSmaAndStandardDeviation()
        {
            var bbmSma            = new BollingerBandsMiddleIndicator(_sma);
            var standardDeviation = new StandardDeviationIndicator(_closePrice, _timeFrame);
            var bblSma            = new BollingerBandsLowerIndicator(bbmSma, standardDeviation);

            TaTestsUtils.AssertDecimalEquals(bblSma.K, 2);

            TaTestsUtils.AssertDecimalEquals(bblSma.GetValue(0), 1);
            TaTestsUtils.AssertDecimalEquals(bblSma.GetValue(1), 0.5);
            TaTestsUtils.AssertDecimalEquals(bblSma.GetValue(2), 0.367);
            TaTestsUtils.AssertDecimalEquals(bblSma.GetValue(3), 1.367);
            TaTestsUtils.AssertDecimalEquals(bblSma.GetValue(4), 2.3905);
            TaTestsUtils.AssertDecimalEquals(bblSma.GetValue(5), 2.7239);
            TaTestsUtils.AssertDecimalEquals(bblSma.GetValue(6), 2.367);

            var bblSmAwithK = new BollingerBandsLowerIndicator(bbmSma, standardDeviation, Decimal.ValueOf("1.5"));

            TaTestsUtils.AssertDecimalEquals(bblSmAwithK.K, 1.5);

            TaTestsUtils.AssertDecimalEquals(bblSmAwithK.GetValue(0), 1);
            TaTestsUtils.AssertDecimalEquals(bblSmAwithK.GetValue(1), 0.75);
            TaTestsUtils.AssertDecimalEquals(bblSmAwithK.GetValue(2), 0.7752);
            TaTestsUtils.AssertDecimalEquals(bblSmAwithK.GetValue(3), 1.7752);
            TaTestsUtils.AssertDecimalEquals(bblSmAwithK.GetValue(4), 2.6262);
            TaTestsUtils.AssertDecimalEquals(bblSmAwithK.GetValue(5), 2.9595);
            TaTestsUtils.AssertDecimalEquals(bblSmAwithK.GetValue(6), 2.7752);
        }
        public void BollingerBandWidthUsingSMAAndStandardDeviation()
        {
            SMAIndicator sma = new SMAIndicator(closePrice, 5);
            StandardDeviationIndicator standardDeviation = new StandardDeviationIndicator(closePrice, 5);

            BollingerBandsMiddleIndicator bbmSMA = new BollingerBandsMiddleIndicator(sma);
            BollingerBandsUpperIndicator  bbuSMA = new BollingerBandsUpperIndicator(bbmSMA, standardDeviation);
            BollingerBandsLowerIndicator  bblSMA = new BollingerBandsLowerIndicator(bbmSMA, standardDeviation);

            BollingerBandWidthIndicator bandwidth = new BollingerBandWidthIndicator(bbuSMA, bbmSMA, bblSMA);

            Assert.AreEqual(bandwidth.GetValue(0), 0.0M);
            Assert.AreEqual(bandwidth.GetValue(1), 36.363636363636363636363636360M);
            Assert.AreEqual(bandwidth.GetValue(2), 66.642313545610556218920998730M);
            Assert.AreEqual(bandwidth.GetValue(3), 60.244280375440064123683819780M);
            Assert.AreEqual(bandwidth.GetValue(4), 71.076741021144541578134348630M);
            Assert.AreEqual(bandwidth.GetValue(5), 69.939393317876184065428705400M);
            Assert.AreEqual(bandwidth.GetValue(6), 62.704283664302785713832632430M);
            Assert.AreEqual(bandwidth.GetValue(7), 56.017820550978806498453596730M);
            Assert.AreEqual(bandwidth.GetValue(8), 27.682979522960278118630842150M);
            Assert.AreEqual(bandwidth.GetValue(9), 12.649110640673517327995574180M);
            Assert.AreEqual(bandwidth.GetValue(10), 12.649110640673517327995574180M);
            Assert.AreEqual(bandwidth.GetValue(11), 24.295632895188751961975636990M);
            Assert.AreEqual(bandwidth.GetValue(12), 68.333165356240492140358775280M);
            Assert.AreEqual(bandwidth.GetValue(13), 85.14693182963200583066660912M);
            Assert.AreEqual(bandwidth.GetValue(14), 112.84810090360856581393406310M);
            Assert.AreEqual(bandwidth.GetValue(15), 108.16818718178015127023901017M);
            Assert.AreEqual(bandwidth.GetValue(16), 66.932802122726043838253762060M);
            Assert.AreEqual(bandwidth.GetValue(17), 56.519416526043901158871302720M);
            Assert.AreEqual(bandwidth.GetValue(18), 28.109134757052260728879053730M);
            Assert.AreEqual(bandwidth.GetValue(19), 32.536151189338620744206510720M);
        }
Exemplo n.º 8
0
        public void BollingerBandWidthUsingSmaAndStandardDeviation()
        {
            var sma = new SmaIndicator(_closePrice, 5);
            var standardDeviation = new StandardDeviationIndicator(_closePrice, 5);

            var bbmSma = new BollingerBandsMiddleIndicator(sma);
            var bbuSma = new BollingerBandsUpperIndicator(bbmSma, standardDeviation);
            var bblSma = new BollingerBandsLowerIndicator(bbmSma, standardDeviation);

            var bandwidth = new BollingerBandWidthIndicator(bbuSma, bbmSma, bblSma);

            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(0), 0.0);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(1), 36.3636);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(2), 66.6423);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(3), 60.2443);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(4), 71.0767);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(5), 69.9394);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(6), 62.7043);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(7), 56.0178);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(8), 27.683);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(9), 12.6491);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(10), 12.6491);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(11), 24.2956);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(12), 68.3332);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(13), 85.1469);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(14), 112.8481);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(15), 108.1682);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(16), 66.9328);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(17), 56.5194);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(18), 28.1091);
            TaTestsUtils.AssertDecimalEquals(bandwidth.GetValue(19), 32.5362);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Constructor. </summary>
        /// <param name="indicator"> an indicator (usually close price) </param>
        /// <param name="timeFrame"> the time frame </param>
        /// <param name="k"> the K multiplier (usually 2.0) </param>
        public PercentBIndicator(IIndicator <Decimal> indicator, int timeFrame, Decimal k) : base(indicator)
        {
            _indicator = indicator;
            _bbm       = new BollingerBandsMiddleIndicator(new SmaIndicator(indicator, timeFrame));
            var sd = new StandardDeviationIndicator(indicator, timeFrame);

            _bbu = new BollingerBandsUpperIndicator(_bbm, sd, k);
            _bbl = new BollingerBandsLowerIndicator(_bbm, sd, k);
        }
        public void StandardDeviationUsingTimeFrame4UsingClosePrice()
        {
            var sdv = new StandardDeviationIndicator(new ClosePriceIndicator(_data), 4);

            TaTestsUtils.AssertDecimalEquals(sdv.GetValue(0), 0);
            TaTestsUtils.AssertDecimalEquals(sdv.GetValue(1), Math.Sqrt(0.25));
            TaTestsUtils.AssertDecimalEquals(sdv.GetValue(2), Math.Sqrt(2.0 / 3));
            TaTestsUtils.AssertDecimalEquals(sdv.GetValue(3), Math.Sqrt(1.25));
            TaTestsUtils.AssertDecimalEquals(sdv.GetValue(4), Math.Sqrt(0.5));
            TaTestsUtils.AssertDecimalEquals(sdv.GetValue(5), Math.Sqrt(0.25));
            TaTestsUtils.AssertDecimalEquals(sdv.GetValue(6), Math.Sqrt(0.5));
            TaTestsUtils.AssertDecimalEquals(sdv.GetValue(7), Math.Sqrt(0.5));
            TaTestsUtils.AssertDecimalEquals(sdv.GetValue(8), Math.Sqrt(0.5));
            TaTestsUtils.AssertDecimalEquals(sdv.GetValue(9), Math.Sqrt(3.5));
            TaTestsUtils.AssertDecimalEquals(sdv.GetValue(10), Math.Sqrt(10.5));
        }
Exemplo n.º 11
0
        public void standardDeviationUsingTimeFrame4UsingClosePrice()
        {
            StandardDeviationIndicator sdv = new StandardDeviationIndicator(new ClosePriceIndicator(data), 4);

            Assert.AreEqual(sdv.GetValue(0), 0M);
            Assert.AreEqual(sdv.GetValue(1), 0.5M);                             // Math.Sqrt(0.25));
            Assert.AreEqual(sdv.GetValue(2), 0.8164965809277260327324280249M);  // Math.Sqrt(2.0 / 3));
            Assert.AreEqual(sdv.GetValue(3), 1.1180339887498948482045868344M);  // Math.Sqrt(1.25));
            Assert.AreEqual(sdv.GetValue(4), 0.7071067811865475244008443621M);  // Math.Sqrt(0.5));
            Assert.AreEqual(sdv.GetValue(5), 0.5M);                             // Math.Sqrt(0.25));
            Assert.AreEqual(sdv.GetValue(6), 0.7071067811865475244008443621M);  // Math.Sqrt(0.5));
            Assert.AreEqual(sdv.GetValue(7), 0.7071067811865475244008443621M);  // Math.Sqrt(0.5));
            Assert.AreEqual(sdv.GetValue(8), 0.7071067811865475244008443621M);  // Math.Sqrt(0.5));
            Assert.AreEqual(sdv.GetValue(9), 1.8708286933869706927918743662M);  // Math.Sqrt(3.5));
            Assert.AreEqual(sdv.GetValue(10), 3.2403703492039301154829837180M); // Math.Sqrt(10.5));
        }
Exemplo n.º 12
0
        public static void Main(string[] args)
        {
            /// <summary>
            /// Getting time series
            /// </summary>
            TimeSeries series = CsvTradesLoader.loadBitstampSeries();

            /// <summary>
            /// Creating indicators
            /// </summary>
            // Close price
            ClosePriceIndicator closePrice = new ClosePriceIndicator(series);
            // Typical price
            TypicalPriceIndicator typicalPrice = new TypicalPriceIndicator(series);
            // Price variation
            PriceVariationIndicator priceVariation = new PriceVariationIndicator(series);
            // Simple moving averages
            SMAIndicator shortSma = new SMAIndicator(closePrice, 8);
            SMAIndicator longSma  = new SMAIndicator(closePrice, 20);
            // Exponential moving averages
            EMAIndicator shortEma = new EMAIndicator(closePrice, 8);
            EMAIndicator longEma  = new EMAIndicator(closePrice, 20);
            // Percentage price oscillator
            PPOIndicator ppo = new PPOIndicator(closePrice, 12, 26);
            // Rate of change
            ROCIndicator roc = new ROCIndicator(closePrice, 100);
            // Relative strength index
            RSIIndicator rsi = new RSIIndicator(closePrice, 14);
            // Williams %R
            WilliamsRIndicator williamsR = new WilliamsRIndicator(series, 20);
            // Average true range
            AverageTrueRangeIndicator atr = new AverageTrueRangeIndicator(series, 20);
            // Standard deviation
            StandardDeviationIndicator sd = new StandardDeviationIndicator(closePrice, 14);

            /// <summary>
            /// Building header
            /// </summary>
            StringBuilder sb = new StringBuilder("timestamp,close,typical,variation,sma8,sma20,ema8,ema20,ppo,roc,rsi,williamsr,atr,sd\n");

            /// <summary>
            /// Adding indicators values
            /// </summary>
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final int nbTicks = series.getTickCount();
            int nbTicks = series.TickCount;

            for (int i = 0; i < nbTicks; i++)
            {
                sb.Append(series.getTick(i).EndTime.Millis / 1000d).Append(',').Append(closePrice.getValue(i)).Append(',').Append(typicalPrice.getValue(i)).Append(',').Append(priceVariation.getValue(i)).Append(',').Append(shortSma.getValue(i)).Append(',').Append(longSma.getValue(i)).Append(',').Append(shortEma.getValue(i)).Append(',').Append(longEma.getValue(i)).Append(',').Append(ppo.getValue(i)).Append(',').Append(roc.getValue(i)).Append(',').Append(rsi.getValue(i)).Append(',').Append(williamsR.getValue(i)).Append(',').Append(atr.getValue(i)).Append(',').Append(sd.getValue(i)).Append('\n');
            }

            /// <summary>
            /// Writing CSV file
            /// </summary>
            System.IO.StreamWriter writer = null;
            try
            {
                writer = new System.IO.StreamWriter("indicators.csv");
                writer.Write(sb.ToString());
            } catch (IOException ioe)
            {
//JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method:
                Logger.getLogger(typeof(IndicatorsToCsv).FullName).log(Level.SEVERE, "Unable to write CSV file", ioe);
            } finally
            {
                try
                {
                    if (writer != null)
                    {
                        writer.Close();
                    }
                } catch (IOException)
                {
                }
            }
        }