Forex_Strategy_Builder.Stochastics.Calculate C# (CSharp) Метод

Calculate() публичный Метод

Calculates the indicator's components
public Calculate ( SlotTypes slotType ) : void
slotType SlotTypes
Результат void
        public override void Calculate(SlotTypes slotType)
        {
            // Reading the parameters
            MAMethod maMethod = (MAMethod)IndParam.ListParam[1].Index;
            int iK     = (int)IndParam.NumParam[0].Value;
            int iDFast = (int)IndParam.NumParam[1].Value;
            int iDSlow = (int)IndParam.NumParam[2].Value;
            int iLevel = (int)IndParam.NumParam[3].Value;
            int iPrvs  = IndParam.CheckParam[0].Checked ? 1 : 0;

            // Calculation
            int iFirstBar = iK + iDFast + iDSlow + 3;

            double[] adHighs = new double[Bars];
            double[] adLows  = new double[Bars];
            for (int iBar = 0; iBar < iK; iBar++)
            {
                double dMin = double.MaxValue;
                double dMax = double.MinValue;
                for (int i = 0; i < iBar; i++)
                {
                    if (High[iBar - i] > dMax) dMax = High[iBar - i];
                    if (Low[iBar  - i] < dMin) dMin = Low[iBar  - i];
                }
                adHighs[iBar] = dMax;
                adLows[iBar]  = dMin;
            }
            adHighs[0] = High[0];
            adLows[0]  = Low[0];

            for (int iBar = iK; iBar < Bars; iBar++)
            {
                double dMin = double.MaxValue;
                double dMax = double.MinValue;
                for (int i = 0; i < iK; i++)
                {
                    if (High[iBar - i] > dMax) dMax = High[iBar - i];
                    if (Low[iBar  - i] < dMin) dMin = Low[iBar  - i];
                }
                adHighs[iBar] = dMax;
                adLows[iBar]  = dMin;
            }

            double[] adK = new double[Bars];
            for (int iBar = iK; iBar < Bars; iBar++)
            {
                if (adHighs[iBar] == adLows[iBar])
                    adK[iBar] = 50;
                else
                    adK[iBar] = 100 * (Close[iBar] - adLows[iBar]) / (adHighs[iBar] - adLows[iBar]);
            }

            double[] adDFast = new double[Bars];
            for (int iBar = iDFast; iBar < Bars; iBar++)
            {
                double dSumHigh = 0;
                double dSumLow  = 0;
                for (int i = 0; i < iDFast; i++)
                {
                    dSumLow  += Close[iBar - i]   - adLows[iBar - i];
                    dSumHigh += adHighs[iBar - i] - adLows[iBar - i];
                }
                if (dSumHigh == 0)
                    adDFast[iBar] = 100;
                else
                    adDFast[iBar] = 100 * dSumLow / dSumHigh;
            }

            double[] adDSlow = MovingAverage(iDSlow, 0, maMethod, adDFast);

            // Saving the components
            Component = new IndicatorComp[5];

            Component[0] = new IndicatorComp();
            Component[0].CompName   = "%K";
            Component[0].DataType   = IndComponentType.IndicatorValue;
            Component[0].ChartType  = IndChartType.Line;
            Component[0].ChartColor = Color.Brown;
            Component[0].FirstBar   = iFirstBar;
            Component[0].Value      = adK;

            Component[1] = new IndicatorComp();
            Component[1].CompName   = "Fast %D";
            Component[1].DataType   = IndComponentType.IndicatorValue;
            Component[1].ChartType  = IndChartType.Line;
            Component[1].ChartColor = Color.Gold;
            Component[1].FirstBar   = iFirstBar;
            Component[1].Value      = adDFast;

            Component[2] = new IndicatorComp();
            Component[2].CompName   = "Slow %D";
            Component[2].DataType   = IndComponentType.IndicatorValue;
            Component[2].ChartType  = IndChartType.Line;
            Component[2].ChartColor = Color.Blue;
            Component[2].FirstBar   = iFirstBar;
            Component[2].Value      = adDSlow;

            Component[3] = new IndicatorComp();
            Component[3].ChartType = IndChartType.NoChart;
            Component[3].FirstBar  = iFirstBar;
            Component[3].Value     = new double[Bars];

            Component[4] = new IndicatorComp();
            Component[4].ChartType = IndChartType.NoChart;
            Component[4].FirstBar  = iFirstBar;
            Component[4].Value     = new double[Bars];

            // Sets the Component's type
            if (slotType == SlotTypes.OpenFilter)
            {
                Component[3].DataType = IndComponentType.AllowOpenLong;
                Component[3].CompName = "Is long entry allowed";
                Component[4].DataType = IndComponentType.AllowOpenShort;
                Component[4].CompName = "Is short entry allowed";
            }
            else if (slotType == SlotTypes.CloseFilter)
            {
                Component[3].DataType = IndComponentType.ForceCloseLong;
                Component[3].CompName = "Close out long position";
                Component[4].DataType = IndComponentType.ForceCloseShort;
                Component[4].CompName = "Close out short position";
            }

            // Calculation of the logic
            IndicatorLogic indLogic = IndicatorLogic.It_does_not_act_as_a_filter;

            if (IndParam.ListParam[0].Text == "The %K crosses the Slow %D upward")
            {
                SpecialValues = new double[1] { 50 };
                IndicatorCrossesAnotherIndicatorUpwardLogic(iFirstBar, iPrvs,adK, adDSlow, ref Component[3], ref Component[4]);
                return;
            }
            else if (IndParam.ListParam[0].Text == "The %K crosses the Slow %D downward")
            {
                SpecialValues = new double[1] { 50 };
                IndicatorCrossesAnotherIndicatorDownwardLogic(iFirstBar, iPrvs, adK, adDSlow, ref Component[3], ref Component[4]);
                return;
            }
            else if (IndParam.ListParam[0].Text == "The %K is higher than the Slow %D")
            {
                SpecialValues = new double[1] { 50 };
                IndicatorIsHigherThanAnotherIndicatorLogic(iFirstBar, iPrvs, adK, adDSlow, ref Component[3], ref Component[4]);
                return;
            }
            else if (IndParam.ListParam[0].Text == "The %K is lower than the Slow %D")
            {
                SpecialValues = new double[1] { 50 };
                IndicatorIsLowerThanAnotherIndicatorLogic(iFirstBar, iPrvs, adK, adDSlow, ref Component[3], ref Component[4]);
                return;
            }
            else
            {
                switch (IndParam.ListParam[0].Text)
                {
                    case "The Slow %D rises":
                        indLogic = IndicatorLogic.The_indicator_rises;
                        SpecialValues = new double[1] { 50 };
                        break;

                    case "The Slow %D falls":
                        indLogic = IndicatorLogic.The_indicator_falls;
                        SpecialValues = new double[1] { 50 };
                        break;

                    case "The Slow %D is higher than the Level line":
                        indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line;
                        SpecialValues = new double[2] { iLevel, 100 - iLevel };
                        break;

                    case "The Slow %D is lower than the Level line":
                        indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line;
                        SpecialValues = new double[2] { iLevel, 100 - iLevel };
                        break;

                    case "The Slow %D crosses the Level line upward":
                        indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward;
                        SpecialValues = new double[2] { iLevel, 100 - iLevel };
                        break;

                    case "The Slow %D crosses the Level line downward":
                        indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward;
                        SpecialValues = new double[2] { iLevel, 100 - iLevel };
                        break;

                    case "The Slow %D changes its direction upward":
                        indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward;
                        SpecialValues = new double[1] { 50 };
                        break;

                    case "The Slow %D changes its direction downward":
                        indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward;
                        SpecialValues = new double[1] { 50 };
                        break;

                    default:
                        break;
                }

                OscillatorLogic(iFirstBar, iPrvs, adDSlow, iLevel, 100 - iLevel, ref Component[3], ref Component[4], indLogic);
            }

            return;
        }