Source » Oscillator Logic - source code

        /// <summary>
        /// Calculates the logic of an 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="fLevelLong">The Level value for a Long position.</param>
        /// <param name="fLevelShort">The Level value for a Short position.</param>
        /// <param name="indCompLong">Indicator component for Long position.</param>
        /// <param name="indCompShort">Indicator component for Short position.</param>
        /// <param name="indLogic">The chosen logic.</param>
        /// <returns>True if everyting is ok.</returns>
        protected static bool OscillatorLogic(int iFirstBar, int iPrvs, float[] afIndValue, float fLevelLong, float fLevelShort,
                                              ref IndicatorComp indCompLong, ref IndicatorComp indCompShort, IndicatorLogic indLogic)
        {
            switch (indLogic)
            {
                case IndicatorLogic.The_indicator_rises:
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        indCompLong.Value [iBar] = afIndValue[iBar - iPrvs - 1] < afIndValue[iBar - iPrvs] - fMicron ? 1 : 0;
                        indCompShort.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++)
                    {
                        indCompLong.Value [iBar] = afIndValue[iBar - iPrvs - 1] > afIndValue[iBar - iPrvs] + fMicron ? 1 : 0;
                        indCompShort.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++)
                    {
                        indCompLong.Value [iBar] = afIndValue[iBar - iPrvs] > fLevelLong  + fMicron ? 1 : 0;
                        indCompShort.Value[iBar] = afIndValue[iBar - iPrvs] < fLevelShort - fMicron ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_is_lower_than_the_level_line:
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        indCompLong.Value [iBar] = afIndValue[iBar - iPrvs] < fLevelLong  - fMicron ? 1 : 0;
                        indCompShort.Value[iBar] = afIndValue[iBar - iPrvs] > fLevelShort + 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] - fLevelLong) < fMicron && iBaseBar > iFirstBar) { iBaseBar--; }

                        indCompLong.Value [iBar] = (afIndValue[iBaseBar] < fLevelLong  - fMicron && afIndValue[iBar - iPrvs] > fLevelLong  + fMicron) ? 1 : 0;
                        indCompShort.Value[iBar] = (afIndValue[iBaseBar] > fLevelShort + fMicron && afIndValue[iBar - iPrvs] < fLevelShort - 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] - fLevelLong) < fMicron && iBaseBar > iFirstBar) { iBaseBar--; }

                        indCompLong.Value [iBar] = (afIndValue[iBaseBar] > fLevelLong  + fMicron && afIndValue[iBar - iPrvs] < fLevelLong  - fMicron) ? 1 : 0;
                        indCompShort.Value[iBar] = (afIndValue[iBaseBar] < fLevelShort - fMicron && afIndValue[iBar - iPrvs] > fLevelShort + 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--;
                        }

                        indCompLong.Value [iBar] = (afIndValue[iBar2] > afIndValue[iBar1] && afIndValue[iBar1] < afIndValue[iBar0] && iBar1 == iBar0 - 1) ? 1 : 0;
                        indCompShort.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--;
                        }

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

                default:
                    return false;
            }

            return true;
        }

Top