Example #1
0
        /// <summary>
        /// DEMA = 2 * EMA - EMA of EMA
        /// </summary>
        /// <see cref="http://forex-indicators.net/trend-indicators/dema"/>
        /// <returns></returns>
        public override SingleDoubleSerie Calculate()
        {
            var demaSerie = new SingleDoubleSerie();
            EMA ema       = new EMA(Period, false);

            ema.Load(OhlcList);
            var emaValues = ema.Calculate().Values;

            // assign EMA values to Close price
            for (var i = 0; i < OhlcList.Count; i++)
            {
                OhlcList[i].closePrice = emaValues[i].HasValue ? emaValues[i].Value : 0.0m;
            }

            ema.Load(OhlcList.Skip(Period - 1).ToList());
            // EMA(EMA(value))
            var emaEmaValues = ema.Calculate().Values;

            for (var i = 0; i < Period - 1; i++)
            {
                emaEmaValues.Insert(0, null);
            }

            // Calculate DEMA
            for (var i = 0; i < OhlcList.Count; i++)
            {
                if (i >= 2 * Period - 2)
                {
                    var dema = 2 * emaValues[i] - emaEmaValues[i];
                    demaSerie.Values.Add(dema);
                }
                else
                {
                    demaSerie.Values.Add(null);
                }
            }

            return(demaSerie);
        }
Example #2
0
        /// <summary>
        /// TrueHigh = Highest of high[0] or close[-1]
        /// TrueLow = Highest of low[0] or close[-1]
        /// TR = TrueHigh - TrueLow
        /// ATR = EMA(TR)
        /// </summary>
        /// <see cref="http://www.fmlabs.com/reference/default.htm?url=TR.htm"/>
        /// <see cref="http://www.fmlabs.com/reference/default.htm?url=ATR.htm"/>
        /// <returns></returns>
        public override ATRSerie Calculate()
        {
            var _atr_serie = new ATRSerie();

            _atr_serie.TrueHigh.Add(null);
            _atr_serie.TrueLow.Add(null);
            _atr_serie.TrueRange.Add(null);
            _atr_serie.ATR.Add(null);

            for (var i = 1; i < OhlcList.Count; i++)
            {
                var trueHigh = OhlcList[i].highPrice >= OhlcList[i - 1].closePrice ? OhlcList[i].highPrice : OhlcList[i - 1].closePrice;
                _atr_serie.TrueHigh.Add(trueHigh);

                var trueLow = OhlcList[i].lowPrice <= OhlcList[i - 1].closePrice ? OhlcList[i].lowPrice : OhlcList[i - 1].closePrice;
                _atr_serie.TrueLow.Add(trueLow);

                var trueRange = trueHigh - trueLow;
                _atr_serie.TrueRange.Add(trueRange);
            }

            for (var i = 1; i < OhlcList.Count; i++)
            {
                OhlcList[i].closePrice = _atr_serie.TrueRange[i].Value;
            }

            var _ema = new EMA(Period, true);

            _ema.Load(OhlcList.Skip(1).ToList());

            var atrList = _ema.Calculate().Values;

            foreach (var atr in atrList)
            {
                _atr_serie.ATR.Add(atr);
            }

            return(_atr_serie);
        }
Example #3
0
        public override ADXSerie Calculate()
        {
            var adxSerie = new ADXSerie();

            var tempOhlcList = new List <Ohlcv>();

            for (var i = 0; i < OhlcList.Count; i++)
            {
                var tempOhlc = new Ohlcv()
                {
                    closePrice = OhlcList[i].highPrice
                };
                tempOhlcList.Add(tempOhlc);
            }
            var momentum = new Momentum();

            momentum.Load(tempOhlcList);
            var highMomentums = momentum.Calculate().Values;

            tempOhlcList = new List <Ohlcv>();
            for (var i = 0; i < OhlcList.Count; i++)
            {
                var tempOhlc = new Ohlcv()
                {
                    closePrice = OhlcList[i].lowPrice
                };
                tempOhlcList.Add(tempOhlc);
            }
            momentum = new Momentum();
            momentum.Load(tempOhlcList);
            var lowMomentums = momentum.Calculate().Values;

            for (var i = 0; i < lowMomentums.Count; i++)
            {
                if (lowMomentums[i].HasValue)
                {
                    lowMomentums[i] *= -1;
                }
            }

            //DMIp <- ifelse( dH==dL | (dH< 0 & dL< 0), 0, ifelse( dH >dL, dH, 0 ) )
            var DMIPositives = new List <decimal?>()
            {
                null
            };
            // DMIn <- ifelse( dH==dL | (dH< 0 & dL< 0), 0, ifelse( dH <dL, dL, 0 ) )
            var DMINegatives = new List <decimal?>()
            {
                null
            };

            for (var i = 1; i < OhlcList.Count; i++)
            {
                if (highMomentums[i] == lowMomentums[i] || (highMomentums[i] < 0 & lowMomentums[i] < 0))
                {
                    DMIPositives.Add(0);
                }
                else
                {
                    if (highMomentums[i] > lowMomentums[i])
                    {
                        DMIPositives.Add(highMomentums[i]);
                    }
                    else
                    {
                        DMIPositives.Add(0);
                    }
                }

                if (highMomentums[i] == lowMomentums[i] || (highMomentums[i] < 0 & lowMomentums[i] < 0))
                {
                    DMINegatives.Add(0);
                }
                else
                {
                    if (highMomentums[i] < lowMomentums[i])
                    {
                        DMINegatives.Add(lowMomentums[i]);
                    }
                    else
                    {
                        DMINegatives.Add(0);
                    }
                }
            }

            ATR atr = new ATR();

            atr.Load(OhlcList);
            var trueRanges = atr.Calculate().TrueRange;

            adxSerie.TrueRange = trueRanges;

            var trSum = wilderSum(trueRanges);

            // DIp <- 100 * wilderSum(DMIp, n=n) / TRsum
            var DIPositives     = new List <decimal?>();
            var wilderSumOfDMIp = wilderSum(DMIPositives);

            for (var i = 0; i < wilderSumOfDMIp.Count; i++)
            {
                if (wilderSumOfDMIp[i].HasValue)
                {
                    DIPositives.Add(wilderSumOfDMIp[i].Value * 100 / trSum[i].Value);
                }
                else
                {
                    DIPositives.Add(null);
                }
            }
            adxSerie.DIPositive = DIPositives;

            // DIn <- 100 * wilderSum(DMIn, n=n) / TRsum
            var DINegatives     = new List <decimal?>();
            var wilderSumOfDMIn = wilderSum(DMINegatives);

            for (var i = 0; i < wilderSumOfDMIn.Count; i++)
            {
                if (wilderSumOfDMIn[i].HasValue)
                {
                    DINegatives.Add(wilderSumOfDMIn[i].Value * 100 / trSum[i].Value);
                }
                else
                {
                    DINegatives.Add(null);
                }
            }
            adxSerie.DINegative = DINegatives;

            // DX  <- 100 * ( abs(DIp - DIn) / (DIp + DIn) )
            var DX = new List <decimal?>();

            for (var i = 0; i < OhlcList.Count; i++)
            {
                if (DIPositives[i].HasValue)
                {
                    var dx = 100 * (Math.Abs(DIPositives[i].Value - DINegatives[i].Value) / (DIPositives[i].Value + DINegatives[i].Value));
                    DX.Add(dx);
                }
                else
                {
                    DX.Add(null);
                }
            }
            adxSerie.DX = DX;

            for (var i = 0; i < OhlcList.Count; i++)
            {
                if (DX[i].HasValue)
                {
                    OhlcList[i].closePrice = DX[i].Value;
                }
                else
                {
                    OhlcList[i].closePrice = 0.0m;
                }
            }

            EMA ema = new EMA(Period, true);

            ema.Load(OhlcList.Skip(Period).ToList());
            var emaValues = ema.Calculate().Values;

            for (var i = 0; i < Period; i++)
            {
                emaValues.Insert(0, null);
            }
            adxSerie.ADX = emaValues;

            return(adxSerie);
        }
Example #4
0
        /// <summary>
        /// 1 - EMA of Close prices [EMA(Close)]
        /// 2 - Double smooth [EMA(EMA(Close))]
        /// 3 - Triple smooth [EMA(EMA(EMA(Close)))]
        /// 4 - a) Calculation with percentage: [ROC(EMA(EMA(EMA(Close))))]
        /// 4 - b) Calculation with percentage: [Momentum(EMA(EMA(EMA(Close))))]
        /// </summary>
        /// <see cref="http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:trix"/>
        /// <see cref="http://www.fmlabs.com/reference/default.htm?url=TRIX.htm"/>
        /// <returns></returns>
        public override SingleDoubleSerie Calculate()
        {
            // EMA calculation
            var _ema = new EMA(Period, false);

            _ema.Load(OhlcList);

            var emaValues = _ema.Calculate().Values;

            for (var i = 0; i < OhlcList.Count; i++)
            {
                OhlcList[i].closePrice = emaValues[i].HasValue ? emaValues[i].Value : 0.0m;
            }

            // Double smooth
            _ema.Load(OhlcList.Skip(Period - 1).ToList());
            var doubleSmoothValues = _ema.Calculate().Values;

            for (var i = 0; i < Period - 1; i++)
            {
                doubleSmoothValues.Insert(0, null);
            }

            for (var i = 0; i < OhlcList.Count; i++)
            {
                OhlcList[i].closePrice = doubleSmoothValues[i].HasValue ? doubleSmoothValues[i].Value : 0.0m;
            }

            // Triple smooth
            _ema.Load(OhlcList.Skip(2 * (Period - 1)).ToList());
            var tripleSmoothValues = _ema.Calculate().Values;

            for (var i = 0; i < (2 * (Period - 1)); i++)
            {
                tripleSmoothValues.Insert(0, null);
            }

            for (var i = 0; i < OhlcList.Count; i++)
            {
                OhlcList[i].closePrice = tripleSmoothValues[i].HasValue ? tripleSmoothValues[i].Value : 0.0m;
            }

            // Last step
            var trixSerie = new SingleDoubleSerie();

            if (CalculatePercentage)
            {
                ROC roc = new ROC(1);
                roc.Load(OhlcList.Skip(3 * (Period - 1)).ToList());
                trixSerie = roc.Calculate();
            }
            else
            {
                Momentum momentum = new Momentum();
                momentum.Load(OhlcList.Skip(3 * (Period - 1)).ToList());
                trixSerie = momentum.Calculate();
            }

            for (var i = 0; i < (3 * (Period - 1)); i++)
            {
                trixSerie.Values.Insert(0, null);
            }

            return(trixSerie);
        }