Exemple #1
0
        public Choppiness(Bars bars, int period, string description)
            : base(bars, description)
        {
            base.FirstValidValue = period;

            double     true_high, true_low, true_rng, sum;
            DataSeries n_high = bars.High - bars.High;
            DataSeries n_low  = bars.Low - bars.Low;

            for (int bar = bars.FirstActualBar + period; bar < bars.Count; bar++)
            {
                true_high   = Math.Max(bars.High[bar], bars.Close[bar - 1]);
                true_low    = Math.Min(bars.Low[bar], bars.Close[bar - 1]);
                true_rng    = TrueRange.Series(bars)[bar];
                n_high[bar] = true_high;
                n_low[bar]  = true_low;
            }

            DataSeries trueHigh = Highest2.Series(bars.High, bars.Close >> 1, period);
            DataSeries trueLow  = Lowest2.Series(bars.Low, bars.Close >> 1, period);

            double nHigh, nLow, nRange, ratio, log_ratio, log_n;

            for (int bar = bars.FirstActualBar + period; bar < bars.Count; bar++)
            {
                // OLD:

                /* nHigh = Highest.Series( n_high, period )[bar];
                 * nLow = Lowest.Series( n_low, period )[bar]; */

                // NEW:
                nHigh = trueHigh[bar];
                nLow  = trueLow[bar];

                nRange    = nHigh - nLow;
                sum       = Sum.Series(TrueRange.Series(bars), period)[bar];
                ratio     = sum / nRange;
                log_ratio = Math.Log(ratio);
                log_n     = Math.Log(period);

                if (bar <= period)
                {
                    base[bar] = 50;
                }
                else
                {
                    base[bar] = 100 * log_ratio / log_n;
                }
            }
        }
        public static Highest2 Series(DataSeries ds1, DataSeries ds2, int period)
        {
            /*Highest2 _Highest2 = new Highest2( ds1, ds2, period );
             * return _Highest2;*/

            string description = string.Concat(new object[] { "_Highest2(", ds1.Description, ",", ds2.Description, ",", period, ")" });

            if (ds1.Cache.ContainsKey(description))
            {
                return((Highest2)ds1.Cache[description]);
            }

            Highest2 _Highest2 = new Highest2(ds1, ds2, period);

            ds1.Cache[description] = _Highest2;
            return(_Highest2);
        }