Forex_Strategy_Builder.Indicator.OscillatorLogic C# (CSharp) Метод

OscillatorLogic() защищенный Метод

Calculates the logic of an Oscillator.
protected OscillatorLogic ( int firstBar, int prvs, double adIndValue, double levelLong, double levelShort, IndicatorComp &indCompLong, IndicatorComp &indCompShort, IndicatorLogic indLogic ) : bool
firstBar int The first bar number.
prvs int To use the previous bar or not.
adIndValue double The indicator values.
levelLong double The Level value for a Long position.
levelShort double The Level value for a Short position.
indCompLong IndicatorComp Indicator component for Long position.
indCompShort IndicatorComp Indicator component for Short position.
indLogic IndicatorLogic The chosen logic.
Результат bool
        protected bool OscillatorLogic(int firstBar, int prvs, double[] adIndValue, double levelLong, double levelShort, ref IndicatorComp indCompLong, ref IndicatorComp indCompShort, IndicatorLogic indLogic)
        {
            double sigma = Sigma();

            switch (indLogic)
            {
                case IndicatorLogic.The_indicator_rises:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int  currBar  = bar - prvs;
                        int  baseBar  = currBar - 1;
                        bool isHigher = adIndValue[currBar] > adIndValue[baseBar];

                        if (!isDescreteValues)  // Aroon oscillator uses isDescreteValues = true
                        {
                            bool isNoChange = true;
                            while (Math.Abs(adIndValue[currBar] - adIndValue[baseBar]) < sigma && isNoChange && baseBar > firstBar)
                            {
                                isNoChange = (isHigher == (adIndValue[baseBar + 1] > adIndValue[baseBar]));
                                baseBar--;
                            }
                        }

                        indCompLong.Value[bar]  = adIndValue[baseBar] < adIndValue[currBar] - sigma ? 1 : 0;
                        indCompShort.Value[bar] = adIndValue[baseBar] > adIndValue[currBar] + sigma ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_falls:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int  currBar  = bar - prvs;
                        int  baseBar  = currBar - 1;
                        bool isHigher = adIndValue[currBar] > adIndValue[baseBar];

                        if (!isDescreteValues)  // Aroon oscillator uses isDescreteValues = true
                        {
                            bool isNoChange = true;
                            while (Math.Abs(adIndValue[currBar] - adIndValue[baseBar]) < sigma && isNoChange && baseBar > firstBar)
                            {
                                isNoChange = (isHigher == (adIndValue[baseBar + 1] > adIndValue[baseBar]));
                                baseBar--;
                            }
                        }

                        indCompLong.Value[bar]  = adIndValue[baseBar] > adIndValue[currBar] + sigma ? 1 : 0;
                        indCompShort.Value[bar] = adIndValue[baseBar] < adIndValue[currBar] - sigma ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_is_higher_than_the_level_line:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar]  = adIndValue[bar - prvs] > levelLong  + sigma ? 1 : 0;
                        indCompShort.Value[bar] = adIndValue[bar - prvs] < levelShort - sigma ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_is_lower_than_the_level_line:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar]  = adIndValue[bar - prvs] < levelLong  - sigma ? 1 : 0;
                        indCompShort.Value[bar] = adIndValue[bar - prvs] > levelShort + sigma ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_crosses_the_level_line_upward:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int baseBar = bar - prvs - 1;
                        while (Math.Abs(adIndValue[baseBar] - levelLong) < sigma && baseBar > firstBar)
                        { baseBar--; }

                        indCompLong.Value[bar]  = (adIndValue[baseBar] < levelLong  - sigma && adIndValue[bar - prvs] > levelLong  + sigma) ? 1 : 0;
                        indCompShort.Value[bar] = (adIndValue[baseBar] > levelShort + sigma && adIndValue[bar - prvs] < levelShort - sigma) ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_crosses_the_level_line_downward:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int baseBar = bar - prvs - 1;
                        while (Math.Abs(adIndValue[baseBar] - levelLong) < sigma && baseBar > firstBar)
                        { baseBar--; }

                        indCompLong.Value[bar]  = (adIndValue[baseBar] > levelLong  + sigma && adIndValue[bar - prvs] < levelLong  - sigma) ? 1 : 0;
                        indCompShort.Value[bar] = (adIndValue[baseBar] < levelShort - sigma && adIndValue[bar - prvs] > levelShort + sigma) ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_changes_its_direction_upward:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int bar0 = bar  - prvs;
                        int bar1 = bar0 - 1;
                        while (Math.Abs(adIndValue[bar0] - adIndValue[bar1]) < sigma && bar1 > firstBar)
                        { bar1--; }

                        int iBar2 = bar1 - 1 > firstBar ? bar1 - 1 : firstBar;
                        while (Math.Abs(adIndValue[bar1] - adIndValue[iBar2]) < sigma && iBar2 > firstBar)
                        { iBar2--; }

                        indCompLong.Value[bar]  = (adIndValue[iBar2] > adIndValue[bar1] && adIndValue[bar1] < adIndValue[bar0] && bar1 == bar0 - 1) ? 1 : 0;
                        indCompShort.Value[bar] = (adIndValue[iBar2] < adIndValue[bar1] && adIndValue[bar1] > adIndValue[bar0] && bar1 == bar0 - 1) ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_changes_its_direction_downward:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int bar0 = bar  - prvs;
                        int bar1 = bar0 - 1;
                        while (Math.Abs(adIndValue[bar0] - adIndValue[bar1]) < sigma && bar1 > firstBar)
                        { bar1--; }

                        int iBar2 = bar1 - 1 > firstBar ? bar1 - 1 : firstBar;
                        while (Math.Abs(adIndValue[bar1] - adIndValue[iBar2]) < sigma && iBar2 > firstBar)
                        { iBar2--; }

                        indCompLong.Value[bar]  = (adIndValue[iBar2] < adIndValue[bar1] && adIndValue[bar1] > adIndValue[bar0] && bar1 == bar0 - 1) ? 1 : 0;
                        indCompShort.Value[bar] = (adIndValue[iBar2] > adIndValue[bar1] && adIndValue[bar1] < adIndValue[bar0] && bar1 == bar0 - 1) ? 1 : 0;
                    }
                    break;

                default:
                    return false;
            }

            return true;
        }