Example #1
0
 /// <summary>
 /// Constructor. </summary>
 /// <param name="series"> a time series </param>
 /// <param name="timeFrame"> the number of ticks used to calculate the average lower shadow </param>
 /// <param name="factor"> the factor used when checking if a candle has a very short lower shadow </param>
 public ThreeBlackCrowsIndicator(TimeSeries series, int timeFrame, Decimal factor) : base(series)
 {
     _series                = series;
     _lowerShadowInd        = new LowerShadowIndicator(series);
     _averageLowerShadowInd = new SmaIndicator(_lowerShadowInd, timeFrame);
     _factor                = factor;
 }
 /// <summary>
 /// Constructor. </summary>
 /// <param name="series"> a time series </param>
 /// <param name="timeFrame"> the number of ticks used to calculate the average upper shadow </param>
 /// <param name="factor"> the factor used when checking if a candle has a very short upper shadow </param>
 public ThreeWhiteSoldiersIndicator(TimeSeries series, int timeFrame, Decimal factor) : base(series)
 {
     _series                = series;
     _upperShadowInd        = new UpperShadowIndicator(series);
     _averageUpperShadowInd = new SmaIndicator(_upperShadowInd, timeFrame);
     _factor                = factor;
 }
Example #3
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);
        }
Example #4
0
 /// <summary>
 /// Constructor. </summary>
 /// <param name="series"> the time series </param>
 /// <param name="timeFrame"> the time frame </param>
 public CciIndicator(TimeSeries series, int timeFrame) : base(series)
 {
     _typicalPriceInd  = new TypicalPriceIndicator(series);
     _smaInd           = new SmaIndicator(_typicalPriceInd, timeFrame);
     _meanDeviationInd = new MeanDeviationIndicator(_typicalPriceInd, timeFrame);
     _timeFrame        = timeFrame;
 }
Example #5
0
 public void SetUp()
 {
     _data       = GenerateTimeSeries.From(1, 2, 3, 4, 3, 4, 5, 4, 3, 3, 4, 3, 2);
     _timeFrame  = 3;
     _closePrice = new ClosePriceIndicator(_data);
     _sma        = new SmaIndicator(_closePrice, _timeFrame);
 }
Example #6
0
        public List <SmaIndicator> GetSma(string ticker, int period, string interval)
        {
            string  url      = AlphaVantageApi.GetSMAUrl(ticker, period, interval);
            JObject result   = JObject.Parse(client.GetStringAsync(url).Result);
            JObject analysis = result[$"Technical Analysis: SMA"].Value <JObject>();

            return(SmaIndicator.ParseSmaArray(analysis, 50));
        }
 /// <summary>
 /// Constructor. </summary>
 /// <param name="indicator1"> the first indicator </param>
 /// <param name="indicator2"> the second indicator </param>
 /// <param name="timeFrame"> the time frame </param>
 public CovarianceIndicator(IIndicator <Decimal> indicator1, IIndicator <Decimal> indicator2, int timeFrame) : base(indicator1)
 {
     _indicator1 = indicator1;
     _indicator2 = indicator2;
     _timeFrame  = timeFrame;
     _sma1       = new SmaIndicator(indicator1, timeFrame);
     _sma2       = new SmaIndicator(indicator2, timeFrame);
 }
        public void IfCacheWorks()
        {
            var sma        = new SmaIndicator(new ClosePriceIndicator(_series), 3);
            var firstTime  = sma.GetValue(4);
            var secondTime = sma.GetValue(4);

            Assert.AreEqual(firstTime, secondTime);
        }
        public void GetTimeSeries()
        {
            var cp = new ClosePriceIndicator(_series);

            Assert.AreEqual(_series, cp.TimeSeries);
            var sma = new SmaIndicator(cp, 3);

            Assert.AreEqual(_series, sma.TimeSeries);
        }
Example #10
0
        public void SmaWhenTimeFrameIs1ResultShouldBeIndicatorValue()
        {
            var quoteSma = new SmaIndicator(new ClosePriceIndicator(_data), 1);

            for (var i = 0; i < _data.TickCount; i++)
            {
                Assert.AreEqual(_data.GetTick(i).ClosePrice, quoteSma.GetValue(i));
            }
        }
Example #11
0
        public void GetValueWithCacheLengthIncrease()
        {
            var data = new double[200];

            TaTestsUtils.ArraysFill(data, 10);
            var sma = new SmaIndicator(new ClosePriceIndicator(GenerateTimeSeries.From(data)), 100);

            TaTestsUtils.AssertDecimalEquals(sma.GetValue(105), 10);
        }
        public void BollingerBandsMiddleUsingSma()
        {
            var sma    = new SmaIndicator(new ClosePriceIndicator(_data), 3);
            var bbmSma = new BollingerBandsMiddleIndicator(sma);

            for (var i = 0; i < _data.TickCount; i++)
            {
                Assert.AreEqual(sma.GetValue(i), bbmSma.GetValue(i));
            }
        }
Example #13
0
        public void ValuesLessThanTimeFrameMustBeEqualsToSmaValues()
        {
            var zlema = new ZlemaIndicator(new ClosePriceIndicator(_data), 10);
            var sma   = new SmaIndicator(new ClosePriceIndicator(_data), 10);

            for (var i = 0; i < 9; i++)
            {
                Assert.AreEqual(sma.GetValue(i), zlema.GetValue(i));
            }
        }
        public void StochasticOscilatorDParam14UsingSma3AndGenericConstructer()
        {
            var sof = new StochasticOscillatorKIndicator(_data, 14);
            var sma = new SmaIndicator(sof, 3);
            var sos = new StochasticOscillatorDIndicator(sma);

            Assert.AreEqual(sma.GetValue(0), sos.GetValue(0));
            Assert.AreEqual(sma.GetValue(1), sos.GetValue(1));
            Assert.AreEqual(sma.GetValue(2), sos.GetValue(2));
        }
Example #15
0
        public void GetValueWithOldResultsRemoval()
        {
            var data = new double[20];

            TaTestsUtils.ArraysFill(data, 1);
            TimeSeries timeSeries = GenerateTimeSeries.From(data);
            var        sma        = new SmaIndicator(new ClosePriceIndicator(timeSeries), 10);

            TaTestsUtils.AssertDecimalEquals(sma.GetValue(5), 1);
            TaTestsUtils.AssertDecimalEquals(sma.GetValue(10), 1);
            timeSeries.MaximumTickCount = 12;
            TaTestsUtils.AssertDecimalEquals(sma.GetValue(19), 1);
        }
Example #16
0
        public void GetValueWithNullTimeSeries()
        {
            var constant = new ConstantIndicator <Decimal>(Decimal.Ten);

            Assert.AreEqual(Decimal.Ten, constant.GetValue(0));
            Assert.AreEqual(Decimal.Ten, constant.GetValue(100));
            Assert.IsNull(constant.TimeSeries);

            var sma = new SmaIndicator(constant, 10);

            Assert.AreEqual(Decimal.Ten, sma.GetValue(0));
            Assert.AreEqual(Decimal.Ten, sma.GetValue(100));
            Assert.IsNull(sma.TimeSeries);
        }
Example #17
0
        public void GetValueOnResultsCalculatedFromRemovedTicksShouldReturnFirstRemainingResult()
        {
            TimeSeries timeSeries = GenerateTimeSeries.From(1, 1, 1, 1, 1);

            timeSeries.MaximumTickCount = 3;
            Assert.AreEqual(2, timeSeries.RemovedTicksCount);

            var sma = new SmaIndicator(new ClosePriceIndicator(timeSeries), 2);

            for (var i = 0; i < 5; i++)
            {
                TaTestsUtils.AssertDecimalEquals(sma.GetValue(i), 1);
            }
        }
Example #18
0
        public void Dpo()
        {
            var dpo       = new DpoIndicator(_series, 9);
            var cp        = new ClosePriceIndicator(_series);
            var sma       = new SmaIndicator(cp, 9);
            int timeShift = 9 / 2 + 1;

            for (int i = _series.Begin; i <= _series.End; i++)
            {
                TaTestsUtils.AssertDecimalEquals(dpo.GetValue(i), cp.GetValue(i).Minus(sma.GetValue(i - timeShift)));
            }

            TaTestsUtils.AssertDecimalEquals(dpo.GetValue(9), 0.111999);
            TaTestsUtils.AssertDecimalEquals(dpo.GetValue(10), -0.02);
            TaTestsUtils.AssertDecimalEquals(dpo.GetValue(11), 0.21142857142);
            TaTestsUtils.AssertDecimalEquals(dpo.GetValue(12), 0.169999999999999);
        }
Example #19
0
        public void SmaUsingTimeFrame3UsingClosePrice()
        {
            var sma = new SmaIndicator(new ClosePriceIndicator(_data), 3);

            TaTestsUtils.AssertDecimalEquals(sma.GetValue(0), 1);
            TaTestsUtils.AssertDecimalEquals(sma.GetValue(1), 1.5);
            TaTestsUtils.AssertDecimalEquals(sma.GetValue(2), 2);
            TaTestsUtils.AssertDecimalEquals(sma.GetValue(3), 3);
            TaTestsUtils.AssertDecimalEquals(sma.GetValue(4), 10d / 3);
            TaTestsUtils.AssertDecimalEquals(sma.GetValue(5), 11d / 3);
            TaTestsUtils.AssertDecimalEquals(sma.GetValue(6), 4);
            TaTestsUtils.AssertDecimalEquals(sma.GetValue(7), 13d / 3);
            TaTestsUtils.AssertDecimalEquals(sma.GetValue(8), 4);
            TaTestsUtils.AssertDecimalEquals(sma.GetValue(9), 10d / 3);
            TaTestsUtils.AssertDecimalEquals(sma.GetValue(10), 10d / 3);
            TaTestsUtils.AssertDecimalEquals(sma.GetValue(11), 10d / 3);
            TaTestsUtils.AssertDecimalEquals(sma.GetValue(12), 3);
        }
Example #20
0
        public void StrategyExecutionOnCachedIndicatorAndLimitedTimeSeries()
        {
            TimeSeries timeSeries = GenerateTimeSeries.From(0, 1, 2, 3, 4, 5, 6, 7);
            var        sma        = new SmaIndicator(new ClosePriceIndicator(timeSeries), 2);

            // Theoretical values for SMA(2) cache: 0, 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5
            timeSeries.MaximumTickCount = 6;
            // Theoretical values for SMA(2) cache: null, null, 2, 2.5, 3.5, 4.5, 5.5, 6.5

            var strategy = new Strategy(new OverIndicatorRule(sma, Decimal.Three), new UnderIndicatorRule(sma, Decimal.Three)
                                        );

            // Theoretical shouldEnter results: false, false, false, false, true, true, true, true
            // Theoretical shouldExit results: false, false, true, true, false, false, false, false

            // As we return the first tick/result found for the removed ticks:
            // -> Approximated values for ClosePrice cache: 2, 2, 2, 3, 4, 5, 6, 7
            // -> Approximated values for SMA(2) cache: 2, 2, 2, 2.5, 3.5, 4.5, 5.5, 6.5

            // Then enters/exits are also approximated:
            // -> shouldEnter results: false, false, false, false, true, true, true, true
            // -> shouldExit results: true, true, true, true, false, false, false, false

            Assert.IsFalse(strategy.ShouldEnter(0));
            Assert.IsTrue(strategy.ShouldExit(0));
            Assert.IsFalse(strategy.ShouldEnter(1));
            Assert.IsTrue(strategy.ShouldExit(1));
            Assert.IsFalse(strategy.ShouldEnter(2));
            Assert.IsTrue(strategy.ShouldExit(2));
            Assert.IsFalse(strategy.ShouldEnter(3));
            Assert.IsTrue(strategy.ShouldExit(3));
            Assert.IsTrue(strategy.ShouldEnter(4));
            Assert.IsFalse(strategy.ShouldExit(4));
            Assert.IsTrue(strategy.ShouldEnter(5));
            Assert.IsFalse(strategy.ShouldExit(5));
            Assert.IsTrue(strategy.ShouldEnter(6));
            Assert.IsFalse(strategy.ShouldExit(6));
            Assert.IsTrue(strategy.ShouldEnter(7));
            Assert.IsFalse(strategy.ShouldExit(7));
        }
Example #21
0
        public void QuickStart()
        {
            // Getting a time series (from any provider: CSV, web service, etc.)
            var series = CsvTradesLoader.LoadBitstampSeries();

            // Getting the close price of the ticks
            var firstClosePrice = series.GetTick(0).ClosePrice;

            Console.WriteLine("First close price: " + firstClosePrice.ToDouble());
            // Or within an indicator:
            var closePrice = new ClosePriceIndicator(series);

            // Here is the same close price:
            Console.WriteLine(firstClosePrice.IsEqual(closePrice.GetValue(0))); // equal to firstClosePrice

            // Getting the simple moving average (SMA) of the close price over the last 5 ticks
            var shortSma = new SmaIndicator(closePrice, 5);

            // Here is the 5-ticks-SMA value at the 42nd index
            Console.WriteLine("5-ticks-SMA value at the 42nd index: " + shortSma.GetValue(42).ToDouble());

            // Getting a longer SMA (e.g. over the 30 last ticks)
            var longSma = new SmaIndicator(closePrice, 30);


            // Ok, now let's building our trading rules!

            // Buying rules
            // We want to buy:
            //  - if the 5-ticks SMA crosses over 30-ticks SMA
            //  - or if the price goes below a defined price (e.g $800.00)
            var buyingRule = (new CrossedUpIndicatorRule(shortSma, longSma))
                             .Or(new CrossedDownIndicatorRule(closePrice, Decimal.ValueOf("800")));

            // Selling rules
            // We want to sell:
            //  - if the 5-ticks SMA crosses under 30-ticks SMA
            //  - or if if the price looses more than 3%
            //  - or if the price earns more than 2%
            var sellingRule = (new CrossedDownIndicatorRule(shortSma, longSma))
                              //.Or(new CrossedDownIndicatorRule(new TrailingStopLossIndicator(closePrice, Decimal.ValueOf("30")), closePrice ))
                              .Or(new StopLossRule(closePrice, Decimal.ValueOf("3")))
                              .Or(new StopGainRule(closePrice, Decimal.ValueOf("2")));

            // Running our juicy trading strategy...
            var tradingRecord = series.Run(new Strategy(buyingRule, sellingRule));

            Console.WriteLine("Number of trades for our strategy: " + tradingRecord.TradeCount);

            // Analysis

            // Getting the cash flow of the resulting trades
            var cashFlow = new CashFlow(series, tradingRecord);

            // Getting the profitable trades ratio
            IAnalysisCriterion profitTradesRatio = new AverageProfitableTradesCriterion();

            Console.WriteLine("Profitable trades ratio: " + profitTradesRatio.Calculate(series, tradingRecord));
            // Getting the reward-risk ratio
            IAnalysisCriterion rewardRiskRatio = new RewardRiskRatioCriterion();

            Console.WriteLine("Reward-risk ratio: " + rewardRiskRatio.Calculate(series, tradingRecord));

            // Total profit of our strategy
            // vs total profit of a buy-and-hold strategy
            IAnalysisCriterion vsBuyAndHold = new VersusBuyAndHoldCriterion(new TotalProfitCriterion());

            Console.WriteLine("Our profit vs buy-and-hold profit: " + vsBuyAndHold.Calculate(series, tradingRecord));

            // Your turn!
        }
Example #22
0
 /// <summary>
 /// Constructor. </summary>
 /// <param name="series"> a time series </param>
 /// <param name="timeFrame"> the number of ticks used to calculate the average body height </param>
 /// <param name="bodyFactor"> the factor used when checking if a candle is Doji </param>
 public DojiIndicator(TimeSeries series, int timeFrame, Decimal bodyFactor) : base(series)
 {
     _bodyHeightInd        = new AbsoluteIndicator(new RealBodyIndicator(series));
     _averageBodyHeightInd = new SmaIndicator(_bodyHeightInd, timeFrame);
     _factor = bodyFactor;
 }
Example #23
0
 public AwesomeOscillatorIndicator(IIndicator <Decimal> indicator, int timeFrameSma1 = 5, int timeFrameSma2 = 34) : base(indicator)
 {
     _sma5  = new SmaIndicator(indicator, timeFrameSma1);
     _sma34 = new SmaIndicator(indicator, timeFrameSma2);
 }
Example #24
0
 /// <summary>
 /// Constructor. </summary>
 /// <param name="indicator"> the indicator </param>
 /// <param name="timeFrame"> the time frame </param>
 public VarianceIndicator(IIndicator <Decimal> indicator, int timeFrame) : base(indicator)
 {
     _indicator = indicator;
     _timeFrame = timeFrame;
     _sma       = new SmaIndicator(indicator, timeFrame);
 }
Example #25
0
 /// <summary>
 /// Constructor. </summary>
 /// <param name="price"> the price </param>
 /// <param name="timeFrame"> the time frame </param>
 internal DpoIndicator(IIndicator <Decimal> price, int timeFrame) : base(price)
 {
     _timeShift = timeFrame / 2 + 1;
     _price     = price;
     _sma       = new SmaIndicator(price, timeFrame);
 }
Example #26
0
 /// <summary>
 /// Constructor. </summary>
 /// <param name="indicator"> the indicator </param>
 /// <param name="timeFrame"> the time frame </param>
 public MeanDeviationIndicator(IIndicator <Decimal> indicator, int timeFrame) : base(indicator)
 {
     _indicator = indicator;
     _timeFrame = timeFrame;
     _sma       = new SmaIndicator(indicator, timeFrame);
 }
Example #27
0
 /// <summary>
 /// Constructor. </summary>
 /// <param name="price"> the price </param>
 /// <param name="shortSmaTimeFrame"> the time frame for the short SMA (usually 7) </param>
 /// <param name="longSmaTimeFrame"> the time frame for the long SMA (usually 65) </param>
 public RaviIndicator(IIndicator <Decimal> price, int shortSmaTimeFrame, int longSmaTimeFrame) : base(price)
 {
     _shortSma = new SmaIndicator(price, shortSmaTimeFrame);
     _longSma  = new SmaIndicator(price, longSmaTimeFrame);
 }