// returns price where sMACD cross 0
        private double PsMACDzero(int bar, TimeSeries price, int periodX, int periodY)
        {
            double result = (periodX * periodY * (FastSMA.Calculate(bar, price, periodX) - FastSMA.Calculate(bar, price, periodY)) +
                             periodX * price[periodY + 1] -
                             periodY * price[periodX + 1]) /
                            (periodX - periodY);

            return(result);
        }
Example #2
0
        //This static method allows ad-hoc calculation of InstantaneousTrendLine (single calc mode)
        public static double Calculate(int bar, TimeSeries ds, int period)
        {
            if (bar < period + 2 || period > ds.Count)
            {
                return(0);
            }

            double smoothedslope = ds[bar] - ds[bar - period + 1]
                                   + ds[bar - 3] - ds[bar - period + 1 - 3] +
                                   2 * (ds[bar - 1] - ds[bar - period + 1 - 1]
                                        + ds[bar - 2] - ds[bar - period + 1 - 2]);

            return(FastSMA.Calculate(bar, ds, period) + smoothedslope / 12);
        }
Example #3
0
        //populate
        public override void Populate()
        {
            BarHistory bars   = Parameters[0].AsBarHistory;
            Int32      period = Parameters[1].AsInt;

            DateTimes = bars.DateTimes;

            if (period <= 0 || bars.Count == 0)
            {
                return;
            }

            var FirstValidValue = period;

            if (FirstValidValue > bars.Count || FirstValidValue < 0)
            {
                FirstValidValue = bars.Count;
            }

            var RelVol = new TimeSeries(DateTimes);

            if (FirstValidValue > 1)
            {
                RelVol[FirstValidValue - 1] = 0d;
            }

            for (int bar = FirstValidValue; bar < bars.Count; bar++)
            {
                double av     = FastSMA.Calculate(bar, bars.Volume, period);
                double sd     = new StdDev(bars.Volume, period)[bar];
                double relVol = (bars.Volume[bar] - av) / sd;
                RelVol[bar] = relVol;
            }

            var aMove   = ((bars.Close - (bars.Close >> 1)) / bars.Close >> 1).Abs();
            var theMin  = new Lowest(aMove, period);
            var theMax  = new Highest(aMove, period);
            var theMove = new TimeSeries(DateTimes);
            var theVol  = new TimeSeries(DateTimes);

            for (int bar = FirstValidValue; bar < bars.Count; bar++)
            {
                if ((theMax[bar] - theMin[bar]) > 0)
                {
                    theMove[bar] = 1.0 + ((aMove[bar] - theMin[bar]) * (10d - 1d)) / (theMax[bar] - theMin[bar]);
                }
            }

            var theMinV = new Lowest(RelVol, period);
            var theMaxV = new Highest(RelVol, period);

            for (int bar = FirstValidValue; bar < bars.Count; bar++)
            {
                if ((theMaxV[bar] - theMinV[bar]) > 0)
                {
                    theVol[bar] = 1.0 + ((RelVol[bar] - theMinV[bar]) * (10d - 1d)) / (theMaxV[bar] - theMinV[bar]);
                }
            }

            var vByM   = theVol / theMove;
            var avF    = new SMA(vByM, period);
            var sdF    = new StdDev(vByM, period);
            var theFoM = (vByM - avF) / sdF;

            for (int bar = 0; bar < bars.Count; bar++)
            {
                Values[bar] = theFoM[bar];
            }
        }