コード例 #1
0
ファイル: Indicators.cs プロジェクト: jwr1995/Objective-TA
        /// <summary>
        /// Relatives the index of the strength.
        /// </summary>
        /// <returns>The strength index.</returns>
        /// <param name="candleSticks">Candle sticks.</param>
        /// <param name="period">Period.</param>
        public static RelativeStrengthIndex RelativeStrengthIndex(CandleStickCollection candleSticks, int period = 14,
                                                                  PriceSource priceSource = PriceSource.Close)
        {
            int count = candleSticks.Count;

            double[] priceArray = priceSource.GetArrayFromCandleStickCollection(candleSticks);
            double[] rsi        = new double[count];

            double[] up   = new double[count];
            double[] down = new double[count];
            double[] rs   = new double[count];

            for (int i = 1; i < count; i++)
            {
                if (priceArray[i] > priceArray[i - 1])
                {
                    up[i] = priceArray[i] - priceArray[i - 1];
                }
                else
                {
                    down[i] = priceArray[i - 1] - priceArray[i];
                }
            }

            for (int i = period; i < count; i++)
            {
                rs[i] = MovingAverage.SMMA(up, period, i - period, period).MovingAverage[i]
                        / MovingAverage.SMMA(down, period, i - period, period).MovingAverage[i];
                rsi[i] = 100 - 1 / (1 + rs[i]);
            }

            return(new RelativeStrengthIndex(rsi));
        }