コード例 #1
0
ファイル: CCI.cs プロジェクト: wingcd/NetTrader.Indicator
        /// <summary>
        /// Commodity Channel Index (CCI)
        /// tp = (high + low + close) / 3
        /// cci = (tp - SMA(tp)) / (Factor * meanDeviation(tp))
        /// </summary>
        /// <see cref="http://www.fmlabs.com/reference/default.htm?url=CCI.htm"/>
        /// <see cref="http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:commodity_channel_index_cci"/>
        /// <returns></returns>
        public override SingleDoubleSerie Calculate()
        {
            SingleDoubleSerie cciSerie = new SingleDoubleSerie();

            for (int i = 0; i < OhlcList.Count; i++)
            {
                OhlcList[i].Close = (OhlcList[i].High + OhlcList[i].Low + OhlcList[i].Close) / 3;
            }

            SMA sma = new SMA(Period);

            sma.Load(OhlcList);
            List <double?> smaList = sma.Calculate().Values;

            List <double?> meanDeviationList = new List <double?>();

            for (int i = 0; i < OhlcList.Count; i++)
            {
                if (i >= Period - 1)
                {
                    double total = 0.0;
                    for (int j = i; j >= i - (Period - 1); j--)
                    {
                        total += Math.Abs(smaList[i].Value - OhlcList[j].Close);
                    }
                    meanDeviationList.Add(total / (double)Period);

                    double cci = (OhlcList[i].Close - smaList[i].Value) / (Factor * meanDeviationList[i].Value);
                    cciSerie.Values.Add(cci);
                }
                else
                {
                    meanDeviationList.Add(null);
                    cciSerie.Values.Add(null);
                }
            }

            return(cciSerie);
        }