Source » No Direction Oscillator Logic - source code

        /// <summary>
        /// Calculates the logic of a No Direction Oscillator.
        /// </summary>
        /// <param name="iFirstBar">The first bar number.</param>
        /// <param name="iPrvs">To use the previous bar or not.</param>
        /// <param name="afIndValue">The indicator values.</param>
        /// <param name="fLevel">The Level value.</param>
        /// <param name="indComp">Indicator component where to save the results.</param>
        /// <param name="indLogic">The chosen logic.</param>
        /// <returns>True if everyting is ok.</returns>
        protected static bool NoDirectionOscillatorLogic(int iFirstBar, int iPrvs, float[] afIndValue, float fLevel, 
                                                         ref IndicatorComp indComp, IndicatorLogic indLogic)
        {
            switch (indLogic)
            {
                case IndicatorLogic.The_indicator_rises:
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        indComp.Value[iBar] = afIndValue[iBar - iPrvs - 1] < afIndValue[iBar - iPrvs] - fMicron ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_falls:
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        indComp.Value[iBar] = afIndValue[iBar - iPrvs - 1] > afIndValue[iBar - iPrvs] + fMicron ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_is_higher_than_the_level_line:
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        indComp.Value[iBar] = afIndValue[iBar - iPrvs] > fLevel + fMicron ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_is_lower_than_the_level_line:
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        indComp.Value[iBar] = afIndValue[iBar - iPrvs] < fLevel - fMicron ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_crosses_the_level_line_upward:
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        int iBaseBar = iBar - iPrvs - 1;
                        while (Math.Abs(afIndValue[iBaseBar] - fLevel) < fMicron && iBaseBar > iFirstBar) { iBaseBar--; }

                        indComp.Value[iBar] = (afIndValue[iBaseBar] < fLevel - fMicron && afIndValue[iBar - iPrvs] > fLevel + fMicron) ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_crosses_the_level_line_downward:
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        int iBaseBar = iBar - iPrvs - 1;
                        while (Math.Abs(afIndValue[iBaseBar] - fLevel) < fMicron && iBaseBar > iFirstBar) { iBaseBar--; }

                        indComp.Value[iBar] = (afIndValue[iBaseBar] > fLevel + fMicron && afIndValue[iBar - iPrvs] < fLevel - fMicron) ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_changes_its_direction_upward:
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        int iBar0 = iBar - iPrvs;
                        int iBar1 = iBar0 - 1;
                        while (Math.Abs(afIndValue[iBar0] - afIndValue[iBar1]) < fMicron && iBar1 > iFirstBar)
                        {
                            iBar1--;
                        }

                        int iBar2 = iBar1 - 1;
                        while (Math.Abs(afIndValue[iBar1] - afIndValue[iBar2]) < fMicron && iBar2 > iFirstBar)
                        {
                            iBar2--;
                        }

                        indComp.Value[iBar] = (afIndValue[iBar2] > afIndValue[iBar1] && afIndValue[iBar1] < afIndValue[iBar0] && iBar1 == iBar0 - 1) ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_changes_its_direction_downward:
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        int iBar0 = iBar - iPrvs;
                        int iBar1 = iBar0 - 1;
                        while (Math.Abs(afIndValue[iBar0] - afIndValue[iBar1]) < fMicron && iBar1 > iFirstBar)
                        {
                            iBar1--;
                        }

                        int iBar2 = iBar1 - 1;
                        while (Math.Abs(afIndValue[iBar1] - afIndValue[iBar2]) < fMicron && iBar2 > iFirstBar)
                        {
                            iBar2--;
                        }

                        indComp.Value[iBar] = (afIndValue[iBar2] < afIndValue[iBar1] && afIndValue[iBar1] > afIndValue[iBar0] && iBar1 == iBar0 - 1) ? 1 : 0;
                    }
                    break;

                default:
                    return false;
            }

            return true;
        }

Top