Beispiel #1
0
        /**
         * Calculates the regression line.
         * @param startIndex the start index (inclusive) in the time series
         * @param endIndex the end index (inclusive) in the time series
         */
        private void CalculateRegressionLine(int startIndex, int endIndex)
        {
            // First pass: compute xBar and yBar
            decimal sumX = Decimals.Zero;
            decimal sumY = Decimals.Zero;

            for (int i = startIndex; i <= endIndex; i++)
            {
                sumX = sumX.Plus(i);
                sumY = sumY.Plus(_indicator.GetValue(i));
            }
            decimal nbObservations = endIndex - startIndex + 1;
            decimal xBar           = sumX.DividedBy(nbObservations);
            decimal yBar           = sumY.DividedBy(nbObservations);

            // Second pass: compute slope and intercept
            decimal xxBar = Decimals.Zero;
            decimal xyBar = Decimals.Zero;

            for (int i = startIndex; i <= endIndex; i++)
            {
                decimal dX = -xBar;
                decimal dY = _indicator.GetValue(i).Minus(yBar);
                xxBar = xxBar.Plus(dX.MultipliedBy(dX));
                xyBar = xyBar.Plus(dX.MultipliedBy(dY));
            }

            _slope     = xyBar.DividedBy(xxBar);
            _intercept = yBar.Minus(_slope.MultipliedBy(xBar));
        }
Beispiel #2
0
        public void zeroIfNoGain()
        { // throws exception
            IIndicator <decimal> indicator = getIndicator(new ClosePriceIndicator(data), 1);

            Assert.AreEqual(Decimals.ZERO, indicator.GetValue(1));
            Assert.AreEqual(Decimals.ZERO, indicator.GetValue(2));
        }
Beispiel #3
0
        /// <summary>
        /// Calculates the regression line. </summary>
        /// <param name="startIndex"> the start index (inclusive) in the time series </param>
        /// <param name="endIndex"> the end index (inclusive) in the time series </param>
        private void CalculateRegressionLine(int startIndex, int endIndex)
        {
            // First pass: compute xBar and yBar
            var sumX = Decimal.Zero;
            var sumY = Decimal.Zero;

            for (var i = startIndex; i <= endIndex; i++)
            {
                sumX = sumX.Plus(Decimal.ValueOf(i));
                sumY = sumY.Plus(_indicator.GetValue(i));
            }
            var nbObservations = Decimal.ValueOf(endIndex - startIndex + 1);
            var xBar           = sumX.DividedBy(nbObservations);
            var yBar           = sumY.DividedBy(nbObservations);

            // Second pass: compute slope and intercept
            var xxBar = Decimal.Zero;
            var xyBar = Decimal.Zero;

            for (var i = startIndex; i <= endIndex; i++)
            {
                var dX = Decimal.ValueOf(i).Minus(xBar);
                var dY = _indicator.GetValue(i).Minus(yBar);
                xxBar = xxBar.Plus(dX.MultipliedBy(dX));
                xyBar = xyBar.Plus(dX.MultipliedBy(dY));
            }

            _slope     = xyBar.DividedBy(xxBar);
            _intercept = yBar.Minus(_slope.MultipliedBy(xBar));
        }
Beispiel #4
0
        public void hundredIfNoLoss()
        { // throws exception
            IIndicator <decimal> indicator = getIndicator(new ClosePriceIndicator(data), 1);

            Assert.AreEqual(Decimals.HUNDRED, indicator.GetValue(14));
            Assert.AreEqual(Decimals.HUNDRED, indicator.GetValue(15));
        }
Beispiel #5
0
        protected override Decimal Calculate(int index)
        {
            var currentValue = _indicator.GetValue(index);

            var helpPartialTimeframe = index % _timeFrame;
            var helpFullTimeframes   = Math.Floor(_indicator.TimeSeries.TickCount / (double)_timeFrame);
            var helpIndexTimeframes  = index / (double)_timeFrame;

            var helpPartialTimeframeHeld = helpPartialTimeframe / (double)_timeFrame;
            var partialTimeframeHeld     = (Math.Abs(helpPartialTimeframeHeld) < double.Epsilon) ? 1.0 : helpPartialTimeframeHeld;

            // Avoid calculations of returns:
            // a.) if index number is below timeframe
            // e.g. timeframe = 365, index = 5 => no calculation
            // b.) if at the end of a series incomplete timeframes would remain
            var timeframedReturn = Decimal.NaNRenamed;

            if ((index >= _timeFrame) && (helpIndexTimeframes < helpFullTimeframes))
            {
                var movingValue        = _indicator.GetValue(index - _timeFrame);
                var movingSimpleReturn = (currentValue.Minus(movingValue)).DividedBy(movingValue);

                var timeframedReturnDouble = Math.Pow((1 + movingSimpleReturn.ToDouble()), (1 / partialTimeframeHeld)) - 1;
                timeframedReturn = Decimal.ValueOf(timeframedReturnDouble);
            }

            return(timeframedReturn);
        }
        protected override decimal Calculate(int index)
        {
            decimal currentValue = _indicator.GetValue(index);

            int     helpPartialTimeframe = index % _timeFrame;
            decimal helpFullTimeframes   = ((decimal)_indicator.TimeSeries.GetBarCount() / _timeFrame).Floor();
            decimal helpIndexTimeframes  = (decimal)index / _timeFrame;

            decimal helpPartialTimeframeHeld = (decimal)helpPartialTimeframe / _timeFrame;
            decimal partialTimeframeHeld     = (helpPartialTimeframeHeld == 0) ? 1.0M : helpPartialTimeframeHeld;

            // Avoid calculations of returns:
            // a.) if index number is below timeframe
            // e.g. timeframe = 365, index = 5 => no calculation
            // b.) if at the end of a series incomplete timeframes would remain
            decimal timeframedReturn = Decimals.NaN;

            if ((index >= _timeFrame) /*(a)*/ && (helpIndexTimeframes < helpFullTimeframes) /*(b)*/)
            {
                decimal movingValue        = _indicator.GetValue(index - _timeFrame);
                decimal movingSimpleReturn = (currentValue.Minus(movingValue)).DividedBy(movingValue);

                decimal timeframedReturn_double = (1 + movingSimpleReturn).Pow(1M / partialTimeframeHeld) - 1;
                timeframedReturn = timeframedReturn_double;
            }

            return(timeframedReturn);
        }
Beispiel #7
0
        protected override Decimal Calculate(int index)
        {
            if (index == 0)
            {
                return(_indicator.GetValue(0));
            }
            var value = Decimal.Zero;

            if (index - _timeFrame < 0)
            {
                for (var i = index + 1; i > 0; i--)
                {
                    value = value.Plus(Decimal.ValueOf(i).MultipliedBy(_indicator.GetValue(i - 1)));
                }
                return(value.DividedBy(Decimal.ValueOf(((index + 1) * (index + 2)) / 2)));
            }

            var actualIndex = index;

            for (var i = _timeFrame; i > 0; i--)
            {
                value = value.Plus(Decimal.ValueOf(i).MultipliedBy(_indicator.GetValue(actualIndex)));
                actualIndex--;
            }
            return(value.DividedBy(Decimal.ValueOf((_timeFrame * (_timeFrame + 1)) / 2)));
        }
Beispiel #8
0
        protected override decimal Calculate(int index)
        {
            if (index == 0)
            {
                return(_indicator.GetValue(0));
            }
            decimal value = Decimals.ZERO;

            if (index - _timeFrame < 0)
            {
                for (int i = index + 1; i > 0; i--)
                {
                    value = value.Plus(i.MultipliedBy(_indicator.GetValue(i - 1)));
                }
                return(value.DividedBy(((index + 1) * (index + 2)) / 2));
            }

            int actualIndex = index;

            for (int i = _timeFrame; i > 0; i--)
            {
                value = value.Plus(i.MultipliedBy(_indicator.GetValue(actualIndex)));
                actualIndex--;
            }
            return(value.DividedBy((_timeFrame * (_timeFrame + 1)) / 2));
        }
Beispiel #9
0
        public void mmaUsingTimeFrame10UsingClosePrice()
        { // throws exception
            IIndicator <decimal> actualIndicator = getIndicator(new ClosePriceIndicator(data), 10);

            Assert.AreEqual(63.99833548204M, actualIndicator.GetValue(9));
            Assert.AreEqual(63.731501933836M, actualIndicator.GetValue(10));
            Assert.AreEqual(63.5093517404524M, actualIndicator.GetValue(11));
        }
Beispiel #10
0
        protected override Decimal Calculate(int index)
        {
            var nIndex           = Math.Max(index - _timeFrame, 0);
            var nPeriodsAgoValue = _indicator.GetValue(nIndex);
            var currentValue     = _indicator.GetValue(index);

            return(currentValue.Minus(nPeriodsAgoValue).DividedBy(nPeriodsAgoValue).MultipliedBy(Decimal.Hundred));
        }
Beispiel #11
0
        public override bool IsSatisfied(int index, ITradingRecord tradingRecord)
        {
            bool satisfied = _indicator.GetValue(index).IsLessThanOrEqual(_upper.GetValue(index)) &&
                             _indicator.GetValue(index).IsGreaterThanOrEqual(_lower.GetValue(index));

            traceIsSatisfied(index, satisfied);
            return(satisfied);
        }
Beispiel #12
0
        public void usingTimeFrame10UsingClosePrice()
        { // throws exception
            IIndicator <decimal> indicator = getIndicator(new ClosePriceIndicator(data), 10);

            Assert.AreEqual(indicator.GetValue(9), 63.694826748355546959417260457M);
            Assert.AreEqual(indicator.GetValue(10), 63.264858248654538421341394919M);
            Assert.AreEqual(indicator.GetValue(11), 62.945793112535531435642959479M);
        }
Beispiel #13
0
        protected override decimal Calculate(int index)
        {
            if (index == 0)
            {
                return(_indicator.GetValue(0));
            }
            decimal prevValue = GetValue(index - 1);

            return(_indicator.GetValue(index).Minus(prevValue).MultipliedBy(_multiplier).Plus(prevValue));
        }
Beispiel #14
0
        protected override decimal Calculate(int index)
        {
            int     nIndex           = Math.Max(index - _timeFrame, 0);
            decimal nPeriodsAgoValue = _indicator.GetValue(nIndex);
            decimal currentValue     = _indicator.GetValue(index);

            return(currentValue.Minus(nPeriodsAgoValue)
                   .DividedBy(nPeriodsAgoValue)
                   .MultipliedBy(Decimals.HUNDRED));
        }
Beispiel #15
0
        protected override bool Calculate(int index)
        {
            if (index < 1)
            {
                return(_bodyHeightInd.GetValue(index).IsZero());
            }

            decimal averageBodyHeight = _averageBodyHeightInd.GetValue(index - 1);
            decimal currentBodyHeight = _bodyHeightInd.GetValue(index);

            return(currentBodyHeight.IsLessThan(averageBodyHeight.MultipliedBy(_factor)));
        }
Beispiel #16
0
        protected override Decimal Calculate(int index)
        {
            var sumOfLosses = Decimal.Zero;

            for (var i = Math.Max(1, index - _timeFrame + 1); i <= index; i++)
            {
                if (_indicator.GetValue(i).IsLessThan(_indicator.GetValue(i - 1)))
                {
                    sumOfLosses = sumOfLosses.Plus(_indicator.GetValue(i - 1).Minus(_indicator.GetValue(i)));
                }
            }
            return(sumOfLosses);
        }
        protected override decimal Calculate(int index)
        {
            decimal sumOfGains = Decimals.Zero;

            for (int i = Math.Max(1, index - _timeFrame + 1); i <= index; i++)
            {
                if (_indicator.GetValue(i).IsGreaterThan(_indicator.GetValue(i - 1)))
                {
                    sumOfGains = sumOfGains.Plus(_indicator.GetValue(i).Minus(_indicator.GetValue(i - 1)));
                }
            }
            return(sumOfGains);
        }
Beispiel #18
0
        protected override Decimal Calculate(int index)
        {
            var start   = Math.Max(0, index - _timeFrame + 1);
            var highest = _indicator.GetValue(start);

            for (var i = start + 1; i <= index; i++)
            {
                if (highest.IsLessThan(_indicator.GetValue(i)))
                {
                    highest = _indicator.GetValue(i);
                }
            }
            return(highest);
        }
Beispiel #19
0
        public void onlineExampleTest()
        { // throws exception
          // from http://cns.bu.edu/~gsc/CN710/fincast/Technical%20_indicators/Relative%20Strength%20Index%20(RSI).htm
          // which uses a different calculation of RSI than ta4j
            ITimeSeries series = new MockTimeSeries(
                46.1250M,
                47.1250M, 46.4375M, 46.9375M, 44.9375M, 44.2500M, 44.6250M, 45.7500M,
                47.8125M, 47.5625M, 47.0000M, 44.5625M, 46.3125M, 47.6875M, 46.6875M,
                45.6875M, 43.0625M, 43.5625M, 44.8750M, 43.6875M);
            // ta4j RSI uses MMA for average gain and loss
            // then uses simple division of the two for RS
            IIndicator <decimal> indicator = getIndicator(new ClosePriceIndicator(
                                                              series), 14);
            IIndicator <decimal> close = new ClosePriceIndicator(series);
            IIndicator <decimal> gain  = new GainIndicator(close);
            IIndicator <decimal> loss  = new LossIndicator(close);
            // this site uses SMA for average gain and loss
            // then uses ratio of MMAs for RS (except for first calculation)
            IIndicator <decimal> avgGain = new SMAIndicator(gain, 14);
            IIndicator <decimal> avgLoss = new SMAIndicator(loss, 14);

            // first online calculation is simple division
            decimal onlineRs = avgGain.GetValue(14).DividedBy(avgLoss.GetValue(14));

            Assert.AreEqual(0.5848214285714285714285714286M, avgGain.GetValue(14));
            Assert.AreEqual(0.5446428571428571428571428571M, avgLoss.GetValue(14));
            Assert.AreEqual(1.0737704918032786885245901641M, onlineRs);
            decimal onlineRsi = 100M - (100M / (1M + onlineRs));

            // difference in RSI values:
            Assert.AreEqual(51.778656126482213438735177869M, onlineRsi);
            Assert.AreEqual(52.130477585417047385335308781M, indicator.GetValue(14));

            // strange, online average gain and loss is not a simple moving average!
            // but they only use them for the first RS calculation
            // Assert.AreEqual(0.5430, avgGain.getValue(15));
            // Assert.AreEqual(0.5772, avgLoss.getValue(15));
            // second online calculation uses MMAs
            // MMA of average gain
            decimal dividend = avgGain.GetValue(14).MultipliedBy(13M).Plus(gain.GetValue(15)).DividedBy(14M);
            // MMA of average loss
            decimal divisor = avgLoss.GetValue(14).MultipliedBy(13M).Plus(loss.GetValue(15)).DividedBy(14M);

            onlineRs = dividend / divisor;
            Assert.AreEqual(0.940883977900552486187845304M, onlineRs);
            onlineRsi = 100M - (100M / (1M + onlineRs));
            // difference in RSI values:
            Assert.AreEqual(48.477085112439510389980074014M, onlineRsi);
            Assert.AreEqual(47.37103140045740279363506511M, indicator.GetValue(15));
        }
Beispiel #20
0
 protected override decimal Calculate(int index)
 {
     if (index == 0)
     {
         return(Decimals.Zero);
     }
     if (_indicator.GetValue(index).IsLessThan(_indicator.GetValue(index - 1)))
     {
         return(_indicator.GetValue(index - 1).Minus(_indicator.GetValue(index)));
     }
     else
     {
         return(Decimals.Zero);
     }
 }
        public override bool IsSatisfied(int index, ITradingRecord tradingRecord)
        {
            bool satisfied = _indicator.GetValue(index);

            traceIsSatisfied(index, satisfied);
            return(satisfied);
        }
        protected override decimal Calculate(int index)
        {
            decimal n = _timeFrame;

            decimal Sx  = Decimals.Zero;
            decimal Sy  = Decimals.Zero;
            decimal Sxx = Decimals.Zero;
            decimal Syy = Decimals.Zero;
            decimal Sxy = Decimals.Zero;

            for (int i = Math.Max(TimeSeries.GetBeginIndex(), index - _timeFrame + 1); i <= index; i++)
            {
                decimal x = _indicator1.GetValue(i);
                decimal y = _indicator2.GetValue(i);

                Sx  = Sx.Plus(x);
                Sy  = Sy.Plus(y);
                Sxy = Sxy.Plus(x.MultipliedBy(y));
                Sxx = Sxx.Plus(x.MultipliedBy(x));
                Syy = Syy.Plus(y.MultipliedBy(y));
            }

            // (n * Sxx - Sx * Sx) * (n * Syy - Sy * Sy)
            decimal toSqrt = (n.MultipliedBy(Sxx).Minus(Sx.MultipliedBy(Sx)))
                             .MultipliedBy(n.MultipliedBy(Syy).Minus(Sy.MultipliedBy(Sy)));

            if (toSqrt.IsGreaterThan(Decimals.Zero))
            {
                // pearson = (n * Sxy - Sx * Sy) / sqrt((n * Sxx - Sx * Sx) * (n * Syy - Sy * Sy))
                return((n.MultipliedBy(Sxy).Minus(Sx.MultipliedBy(Sy))).DividedBy(toSqrt.Sqrt()));
            }

            return(Decimals.NaN);
        }
Beispiel #23
0
        public override bool IsSatisfied(int index, TradingRecord tradingRecord)
        {
            var satisfied = _ref.GetValue(index).IsLessThanOrEqual(_upper.GetValue(index)) && _ref.GetValue(index).IsGreaterThanOrEqual(_lower.GetValue(index));

            TraceIsSatisfied(index, satisfied);
            return(satisfied);
        }
Beispiel #24
0
        public override bool IsSatisfied(int index, ITradingRecord tradingRecord)
        {
            bool satisfied = _first.GetValue(index).Equals(_second.GetValue(index));

            traceIsSatisfied(index, satisfied);
            return(satisfied);
        }
        /// <summary>
        /// Simple implementation of the trailing stop-loss concept.
        /// Logic:
        /// IF CurrentPrice - StopLossDistance > StopLossLimit THEN StopLossLimit = CurrentPrice - StopLossDistance </summary>
        /// <param name="index"> </param>
        /// <returns> Decimal </returns>
        protected override Decimal Calculate(int index)
        {
            if (_stopLossLimit.NaN)
            {
                // Case without initial stop-loss limit value
                _stopLossLimit = _indicator.GetValue(0).Minus(_stopLossDistance);
            }
            var currentValue   = _indicator.GetValue(index);
            var referenceValue = _stopLossLimit.Plus(_stopLossDistance);

            if (currentValue.IsGreaterThan(referenceValue))
            {
                _stopLossLimit = currentValue.Minus(_stopLossDistance);
            }
            return(_stopLossLimit);
        }
Beispiel #26
0
        protected override Decimal Calculate(int index)
        {
            if (index + 1 < _timeFrame)
            {
                // Starting point of the EMA
                return((new SmaIndicator(_indicator, _timeFrame)).GetValue(index));
            }
            if (index == 0)
            {
                // If the timeframe is bigger than the indicator's value count
                return(_indicator.GetValue(0));
            }
            var emaPrev = GetValue(index - 1);

            return(_indicator.GetValue(index).Minus(emaPrev).MultipliedBy(_multiplier).Plus(emaPrev));
        }
Beispiel #27
0
        public override bool IsSatisfied(int index, TradingRecord tradingRecord)
        {
            var satisfied = _first.GetValue(index).IsLessThan(_second.GetValue(index));

            TraceIsSatisfied(index, satisfied);
            return(satisfied);
        }
Beispiel #28
0
        public void testDummy() // throws exception
        {
            List <IBar> bars = new List <IBar>();

            bars.Add(new MockBar(0, 12, 15, 8));
            bars.Add(new MockBar(0, 8, 11, 6));
            bars.Add(new MockBar(0, 15, 17, 14));
            bars.Add(new MockBar(0, 15, 17, 14));
            bars.Add(new MockBar(0, 0, 0, 2));
            IIndicator <decimal> indicator = getIndicator(new MockTimeSeries(bars), 3);

            Assert.AreEqual(7M, indicator.GetValue(0));
            Assert.AreEqual(6.6666666666666666666666666667M, indicator.GetValue(1)); // 6M / 3 + (1 - 1M / 3) * indicator.getValue(0)
            Assert.AreEqual(7.4444444444444444444444444444M, indicator.GetValue(2)); // 9M / 3 + (1 - 1M / 3) * indicator.getValue(1)
            Assert.AreEqual(5.9629629629629629629629629631M, indicator.GetValue(3)); // 3M / 3 + (1 - 1M / 3) * indicator.getValue(2)
            Assert.AreEqual(8.975308641975308641975308642M, indicator.GetValue(4));  // 15M / 3 + (1 - 1M / 3) * indicator.getValue(3)
        }
Beispiel #29
0
        protected override Decimal Calculate(int index)
        {
            var value    = _indicator.GetValue(index);
            var upValue  = _bbu.GetValue(index);
            var lowValue = _bbl.GetValue(index);

            return(value.Minus(lowValue).DividedBy(upValue.Minus(lowValue)));
        }
Beispiel #30
0
        protected override decimal Calculate(int index)
        {
            if (index + 1 < _timeFrame)
            {
                // Starting point of the ZLEMA
                return(new SMAIndicator(_indicator, _timeFrame).GetValue(index));
            }
            if (index == 0)
            {
                // If the timeframe is bigger than the indicator's value count
                return(_indicator.GetValue(0));
            }
            decimal zlemaPrev = GetValue(index - 1);

            return(_k.MultipliedBy(Decimals.TWO.MultipliedBy(_indicator.GetValue(index)).Minus(_indicator.GetValue(index - _lag)))
                   .Plus(Decimals.ONE.Minus(_k).MultipliedBy(zlemaPrev)));
        }