Example #1
0
        /// <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);
        }
Example #2
0
        static public void buildIndicators(List <BarRecord> barRecords)
        {
            SMA sma9 = new SMA(9);

            sma9.LoadOhlcList(barRecords);
            SingleDoubleSerie sma9Serie = sma9.Calculate();

            double?[] sma9Arry = sma9Serie.Values.ToArray();

            SMA sma20 = new SMA(20);

            sma20.LoadOhlcList(barRecords);
            SingleDoubleSerie sma20Serie = sma20.Calculate();

            double?[] sma20Arry = sma20Serie.Values.ToArray();

            SMA sma50 = new SMA(50);

            sma50.LoadOhlcList(barRecords);
            SingleDoubleSerie sma50Serie = sma50.Calculate();

            double?[] sma50Arry = sma50Serie.Values.ToArray();

            MACD macd = new MACD(true);

            macd.LoadOhlcList(barRecords);
            MACDSerie macdSerie = macd.Calculate();

            double?[] macdHistArry = macdSerie.MACDHistogram.ToArray();

            RSI rsi = new RSI(14);

            rsi.LoadOhlcList(barRecords);
            RSISerie rsiSerie = rsi.Calculate();

            double?[] rsiArry = rsiSerie.RSI.ToArray();

            BollingerBand bollingerBand = new BollingerBand();

            bollingerBand.LoadOhlcList(barRecords);
            BollingerBandSerie bollingerSerie = bollingerBand.Calculate();

            double?[] bollLowArry = bollingerSerie.LowerBand.ToArray();
            double?[] bollUpArry  = bollingerSerie.UpperBand.ToArray();

            CCI cci = new CCI();

            cci.LoadOhlcList(barRecords);
            SingleDoubleSerie cciSerie = cci.Calculate();

            double?[] cciArry = cciSerie.Values.ToArray();

            ATR atr = new ATR();

            atr.LoadOhlcList(barRecords);
            ATRSerie atrSerie = atr.Calculate();

            double?[] atrHighArry = atrSerie.TrueHigh.ToArray();
            double?[] atrLowArry  = atrSerie.TrueLow.ToArray();

            ADX adx = new ADX();

            adx.LoadOhlcList(barRecords);
            ADXSerie adxSerie = adx.Calculate();

            double?[] adxPositiveArry = adxSerie.DIPositive.ToArray();
            double?[] adxNegativeArry = adxSerie.DINegative.ToArray();

            Momentum mo = new Momentum();

            mo.LoadOhlcList(barRecords);
            SingleDoubleSerie moSerie = mo.Calculate();

            double?[] moArry = moSerie.Values.ToArray();

            int index = 0;

            foreach (BarRecord bar in barRecords)
            {
                bar.SMA9           = sma9Arry[index].ToString();
                bar.SMA20          = sma20Arry[index].ToString();
                bar.SMA50          = sma50Arry[index].ToString();
                bar.MACD_DIFF      = macdHistArry[index].ToString();
                bar.RSI            = rsiArry[index].ToString();
                bar.BOLL_LOW       = bollLowArry[index].ToString();
                bar.BOLL_HIGH      = bollUpArry[index].ToString();
                bar.CCI            = cciArry[index].ToString();
                bar.ADX_DIPositive = adxPositiveArry[index].ToString();
                bar.ADX_DINegative = adxNegativeArry[index].ToString();
                bar.ATR_TrueHigh   = atrHighArry[index].ToString();
                bar.ATR_TrueLow    = atrLowArry[index].ToString();
                bar.Momemtum       = moArry[index].ToString();
                index++;
            }
        }