Ejemplo n.º 1
0
        protected Indicator()
        {
            indicatorName          = string.Empty;
            PossibleSlots          = SlotTypes.NotDefined;
            SeparatedChart         = false;
            SeparatedChartMinValue = double.MaxValue;
            SeparatedChartMaxValue = double.MinValue;
            IsDiscreteValues       = false;
            CustomIndicator        = false;
            LoaddedFromDll         = false;
            WarningMessage         = string.Empty;
            AllowClosingFilters    = false;

            SpecialValues = new double[] {};
            IndParam      = new IndicatorParam();
            Component     = new IndicatorComp[] {};

            IsBacktester  = true;
            IsGeneratable = true;

            ExitFilterShortDescription  = "Not defined";
            EntryFilterShortDescription = "Not defined";
            ExitFilterLongDescription   = "Not defined";
            EntryFilterLongDescription  = "Not defined";
            ExitPointShortDescription   = "Not defined";
            ExitPointLongDescription    = "Not defined";
            EntryPointShortDescription  = "Not defined";
            EntryPointLongDescription   = "Not defined";
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Returns signals for the logic rule "The Indicator crosses AnotherIndicator upward"
        /// </summary>
        protected void IndicatorCrossesAnotherIndicatorUpwardLogic(int firstBar, int prvs, double[] adIndValue,
                                                                   double[] adAnotherIndValue,
                                                                   ref IndicatorComp indCompLong,
                                                                   ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();

            firstBar = Math.Max(firstBar, 2);

            for (int bar = firstBar; bar < Bars; bar++)
            {
                int currentBar = bar - prvs;
                int baseBar    = currentBar - 1;
                while (Math.Abs(adIndValue[baseBar] - adAnotherIndValue[baseBar]) < sigma && baseBar > firstBar)
                {
                    baseBar--;
                }

                indCompLong.Value[bar] = adIndValue[currentBar] > adAnotherIndValue[currentBar] + sigma &&
                                         adIndValue[baseBar] < adAnotherIndValue[baseBar] - sigma
                                             ? 1
                                             : 0;
                indCompShort.Value[bar] = adIndValue[currentBar] < adAnotherIndValue[currentBar] - sigma &&
                                          adIndValue[baseBar] > adAnotherIndValue[baseBar] + sigma
                                              ? 1
                                              : 0;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// The default constructor
        /// </summary>
        public Indicator()
        {
            indicatorName = string.Empty;
            PossibleSlots = SlotTypes.NotDefined;
            SeparatedChart = false;
            SeparatedChartMinValue = double.MaxValue;
            SeparatedChartMaxValue = double.MinValue;
            IsDiscreteValues = false;
            CustomIndicator = false;
            LoaddedFromDll = false;
            WarningMessage = string.Empty;
            AllowClosingFilters = false;

            SpecialValues = new double[] { };
            IndParam = new IndicatorParam();
            Component = new IndicatorComp[] { };

            IsBacktester = false;
            IsGeneratable = true;

            ExitFilterShortDescription = "Not defined";
            EntryFilterShortDescription = "Not defined";
            ExitFilterLongDescription = "Not defined";
            EntryFilterLongDescription = "Not defined";
            ExitPointShortDescription = "Not defined";
            ExitPointLongDescription = "Not defined";
            EntryPointShortDescription = "Not defined";
            EntryPointLongDescription = "Not defined";
        }
Ejemplo n.º 4
0
        /// <summary>
        ///     Returns signals for the logic rule "Indicator falls"
        /// </summary>
        protected void IndicatorFallsLogic(int firstBar, int prvs, double[] adIndValue, ref IndicatorComp indCompLong,
                                           ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();

            firstBar = Math.Max(firstBar, 2);

            for (int bar = firstBar; bar < Bars; bar++)
            {
                int  currentBar = bar - prvs;
                int  baseBar    = currentBar - 1;
                bool isNoChange = true;
                bool isLower    = adIndValue[currentBar] < adIndValue[baseBar];

                while (Math.Abs(adIndValue[currentBar] - adIndValue[baseBar]) < sigma && isNoChange &&
                       baseBar > firstBar)
                {
                    isNoChange = (isLower == (adIndValue[baseBar + 1] < adIndValue[baseBar]));
                    baseBar--;
                }

                indCompLong.Value[bar]  = adIndValue[currentBar] < adIndValue[baseBar] - sigma ? 1 : 0;
                indCompShort.Value[bar] = adIndValue[currentBar] > adIndValue[baseBar] + sigma ? 1 : 0;
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        ///     Returns signals for the logic rule "The bar opens below the Indicator after opening above it"
        /// </summary>
        protected void BarOpensBelowIndicatorAfterOpeningAboveLogic(int firstBar, int prvs, double[] adIndValue,
                                                                    ref IndicatorComp indCompLong,
                                                                    ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();

            firstBar = Math.Max(firstBar, 2);

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

                indCompLong.Value[bar] = Open[bar] < adIndValue[bar - prvs] - sigma &&
                                         Open[baseBar] > adIndValue[baseBar - prvs] + sigma
                                             ? 1
                                             : 0;
                indCompShort.Value[bar] = Open[bar] > adIndValue[bar - prvs] + sigma &&
                                          Open[baseBar] < adIndValue[baseBar - prvs] - sigma
                                              ? 1
                                              : 0;
            }
        }
Ejemplo n.º 6
0
        public Indicator()
        {
            IndParam = new IndicatorParam();

            IndicatorName = string.Empty;
            PossibleSlots = SlotTypes.NotDefined;
            SlotType      = SlotTypes.NotDefined;

            SeparatedChart         = false;
            SeparatedChartMinValue = double.MaxValue;
            SeparatedChartMaxValue = double.MinValue;
            SpecialValues          = new double[0];

            IsDiscreteValues    = false;
            CustomIndicator     = false;
            IsBacktester        = true;
            IsGeneratable       = true;
            WarningMessage      = string.Empty;
            AllowClosingFilters = false;

            IndicatorAuthor      = "Forex Software Ltd";
            IndicatorVersion     = "1.0";
            IndicatorDescription = "Bundled in FSB distribution.";

            ExitFilterShortDescription  = "Not defined";
            EntryFilterShortDescription = "Not defined";
            ExitFilterLongDescription   = "Not defined";
            EntryFilterLongDescription  = "Not defined";
            ExitPointShortDescription   = "Not defined";
            ExitPointLongDescription    = "Not defined";
            EntryPointShortDescription  = "Not defined";
            EntryPointLongDescription   = "Not defined";

            Component = new IndicatorComp[] {};
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Returns signals for the logic rule "The bar opens above the Indicator"
        /// </summary>
        protected void BarOpensAboveIndicatorLogic(int firstBar, int previous, double[] adIndValue,
                                                   ref IndicatorComp indCompLong, ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();

            for (int bar = firstBar; bar < Bars; bar++)
            {
                indCompLong.Value[bar]  = Open[bar] > adIndValue[bar - previous] + sigma ? 1 : 0;
                indCompShort.Value[bar] = Open[bar] < adIndValue[bar - previous] - sigma ? 1 : 0;
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        ///     Returns signals for the logic rule "The bar closes below the Indicator"
        /// </summary>
        protected void BarClosesBelowIndicatorLogic(int firstBar, int prvs, double[] adIndValue,
                                                    ref IndicatorComp indCompLong, ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();

            for (int bar = firstBar; bar < Bars; bar++)
            {
                indCompLong.Value[bar]  = Close[bar] < adIndValue[bar - prvs] - sigma ? 1 : 0;
                indCompShort.Value[bar] = Close[bar] > adIndValue[bar - prvs] + sigma ? 1 : 0;
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        ///     Returns signals for the logic rule "The Indicator is lower than the AnotherIndicator"
        /// </summary>
        protected void IndicatorIsLowerThanAnotherIndicatorLogic(int firstBar, int previous, double[] adIndValue,
                                                                 double[] adAnotherIndValue,
                                                                 ref IndicatorComp indCompLong, ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();

            for (int bar = firstBar; bar < Bars; bar++)
            {
                int currentBar = bar - previous;
                indCompLong.Value[bar]  = adIndValue[currentBar] < adAnotherIndValue[currentBar] - sigma ? 1 : 0;
                indCompShort.Value[bar] = adIndValue[currentBar] > adAnotherIndValue[currentBar] + sigma ? 1 : 0;
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        ///     Returns a copy.
        /// </summary>
        public IndicatorComp Clone()
        {
            var indicatorComp = new IndicatorComp
            {
                CompName           = CompName,
                DataType           = DataType,
                ChartType          = ChartType,
                ChartColor         = ChartColor,
                FirstBar           = FirstBar,
                UsePreviousBar     = UsePreviousBar,
                ShowInDynInfo      = ShowInDynInfo,
                PosPriceDependence = PosPriceDependence
            };


            if (Value != null)
            {
                indicatorComp.Value = new double[Value.Length];
                Value.CopyTo(indicatorComp.Value, 0);
            }

            return(indicatorComp);
        }
Ejemplo n.º 11
0
        /// <summary>
        ///     Calculates the logic of a band indicator.
        /// </summary>
        /// <param name="firstBar">The first bar number.</param>
        /// <param name="prvs">To use the previous bar or not.</param>
        /// <param name="adUpperBand">The Upper band values.</param>
        /// <param name="adLowerBand">The Lower band values.</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>
        protected void BandIndicatorLogic(int firstBar, int prvs, double[] adUpperBand, double[] adLowerBand,
                                          ref IndicatorComp indCompLong, ref IndicatorComp indCompShort,
                                          BandIndLogic indLogic)
        {
            double sigma = Sigma();

            switch (indLogic)
            {
            case BandIndLogic.The_bar_opens_below_the_Upper_Band:
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    indCompLong.Value[bar]  = Open[bar] < adUpperBand[bar - prvs] - sigma ? 1 : 0;
                    indCompShort.Value[bar] = Open[bar] > adLowerBand[bar - prvs] + sigma ? 1 : 0;
                }
                break;

            case BandIndLogic.The_bar_opens_above_the_Upper_Band:
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    indCompLong.Value[bar]  = Open[bar] > adUpperBand[bar - prvs] + sigma ? 1 : 0;
                    indCompShort.Value[bar] = Open[bar] < adLowerBand[bar - prvs] - sigma ? 1 : 0;
                }
                break;

            case BandIndLogic.The_bar_opens_below_the_Lower_Band:
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    indCompLong.Value[bar]  = Open[bar] < adLowerBand[bar - prvs] - sigma ? 1 : 0;
                    indCompShort.Value[bar] = Open[bar] > adUpperBand[bar - prvs] + sigma ? 1 : 0;
                }
                break;

            case BandIndLogic.The_bar_opens_above_the_Lower_Band:
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    indCompLong.Value[bar]  = Open[bar] > adLowerBand[bar - prvs] + sigma ? 1 : 0;
                    indCompShort.Value[bar] = Open[bar] < adUpperBand[bar - prvs] - sigma ? 1 : 0;
                }
                break;

            case BandIndLogic.The_bar_opens_below_the_Upper_Band_after_opening_above_it:
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    int baseBar = bar - 1;
                    while (Math.Abs(Open[baseBar] - adUpperBand[baseBar - prvs]) < sigma && baseBar > firstBar)
                    {
                        baseBar--;
                    }

                    indCompLong.Value[bar] = Open[bar] < adUpperBand[bar - prvs] - sigma &&
                                             Open[baseBar] > adUpperBand[baseBar - prvs] + sigma
                                                     ? 1
                                                     : 0;

                    baseBar = bar - 1;
                    while (Math.Abs(Open[baseBar] - adLowerBand[baseBar - prvs]) < sigma && baseBar > firstBar)
                    {
                        baseBar--;
                    }

                    indCompShort.Value[bar] = Open[bar] > adLowerBand[bar - prvs] + sigma &&
                                              Open[baseBar] < adLowerBand[baseBar - prvs] - sigma
                                                      ? 1
                                                      : 0;
                }
                break;

            case BandIndLogic.The_bar_opens_above_the_Upper_Band_after_opening_below_it:
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    int baseBar = bar - 1;
                    while (Math.Abs(Open[baseBar] - adUpperBand[baseBar - prvs]) < sigma && baseBar > firstBar)
                    {
                        baseBar--;
                    }

                    indCompLong.Value[bar] = Open[bar] > adUpperBand[bar - prvs] + sigma &&
                                             Open[baseBar] < adUpperBand[baseBar - prvs] - sigma
                                                     ? 1
                                                     : 0;

                    baseBar = bar - 1;
                    while (Math.Abs(Open[baseBar] - adLowerBand[baseBar - prvs]) < sigma && baseBar > firstBar)
                    {
                        baseBar--;
                    }

                    indCompShort.Value[bar] = Open[bar] < adLowerBand[bar - prvs] - sigma &&
                                              Open[baseBar] > adLowerBand[baseBar - prvs] + sigma
                                                      ? 1
                                                      : 0;
                }
                break;

            case BandIndLogic.The_bar_opens_below_the_Lower_Band_after_opening_above_it:
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    int baseBar = bar - 1;
                    while (Math.Abs(Open[baseBar] - adLowerBand[baseBar - prvs]) < sigma && baseBar > firstBar)
                    {
                        baseBar--;
                    }

                    indCompLong.Value[bar] = Open[bar] < adLowerBand[bar - prvs] - sigma &&
                                             Open[baseBar] > adLowerBand[baseBar - prvs] + sigma
                                                     ? 1
                                                     : 0;

                    baseBar = bar - 1;
                    while (Math.Abs(Open[baseBar] - adUpperBand[baseBar - prvs]) < sigma && baseBar > firstBar)
                    {
                        baseBar--;
                    }

                    indCompShort.Value[bar] = Open[bar] > adUpperBand[bar - prvs] + sigma &&
                                              Open[baseBar] < adUpperBand[baseBar - prvs] - sigma
                                                      ? 1
                                                      : 0;
                }
                break;

            case BandIndLogic.The_bar_opens_above_the_Lower_Band_after_opening_below_it:
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    int baseBar = bar - 1;
                    while (Math.Abs(Open[baseBar] - adLowerBand[baseBar - prvs]) < sigma && baseBar > firstBar)
                    {
                        baseBar--;
                    }

                    indCompLong.Value[bar] = Open[bar] > adLowerBand[bar - prvs] + sigma &&
                                             Open[baseBar] < adLowerBand[baseBar - prvs] - sigma
                                                     ? 1
                                                     : 0;

                    baseBar = bar - 1;
                    while (Math.Abs(Open[baseBar] - adUpperBand[baseBar - prvs]) < sigma && baseBar > firstBar)
                    {
                        baseBar--;
                    }

                    indCompShort.Value[bar] = Open[bar] < adUpperBand[bar - prvs] - sigma &&
                                              Open[baseBar] > adUpperBand[baseBar - prvs] + sigma
                                                      ? 1
                                                      : 0;
                }
                break;

            case BandIndLogic.The_bar_closes_below_the_Upper_Band:
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    indCompLong.Value[bar]  = Close[bar] < adUpperBand[bar - prvs] - sigma ? 1 : 0;
                    indCompShort.Value[bar] = Close[bar] > adLowerBand[bar - prvs] + sigma ? 1 : 0;
                }
                break;

            case BandIndLogic.The_bar_closes_above_the_Upper_Band:
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    indCompLong.Value[bar]  = Close[bar] > adUpperBand[bar - prvs] + sigma ? 1 : 0;
                    indCompShort.Value[bar] = Close[bar] < adLowerBand[bar - prvs] - sigma ? 1 : 0;
                }
                break;

            case BandIndLogic.The_bar_closes_below_the_Lower_Band:
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    indCompLong.Value[bar]  = Close[bar] < adLowerBand[bar - prvs] - sigma ? 1 : 0;
                    indCompShort.Value[bar] = Close[bar] > adUpperBand[bar - prvs] + sigma ? 1 : 0;
                }
                break;

            case BandIndLogic.The_bar_closes_above_the_Lower_Band:
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    indCompLong.Value[bar]  = Close[bar] > adLowerBand[bar - prvs] + sigma ? 1 : 0;
                    indCompShort.Value[bar] = Close[bar] < adUpperBand[bar - prvs] - sigma ? 1 : 0;
                }
                break;

            default:
                return;
            }
        }
        /// <summary>
        /// Calculates the logic of a band indicator.
        /// </summary>
        /// <param name="firstBar">The first bar number.</param>
        /// <param name="previous">To use the previous bar or not.</param>
        /// <param name="adUpperBand">The Upper band values.</param>
        /// <param name="adLowerBand">The Lower band values.</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 bool BandIndicatorLogic(int firstBar, int previous, double[] adUpperBand, double[] adLowerBand,
            ref IndicatorComp indCompLong, ref IndicatorComp indCompShort, BandIndLogic indLogic)
        {
            double sigma = Sigma();

            switch (indLogic)
            {
                case BandIndLogic.The_bar_opens_below_the_Upper_Band:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar]  = Open[bar] < adUpperBand[bar - previous] - sigma ? 1 : 0;
                        indCompShort.Value[bar] = Open[bar] > adLowerBand[bar - previous] + sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_opens_above_the_Upper_Band:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar]  = Open[bar] > adUpperBand[bar - previous] + sigma ? 1 : 0;
                        indCompShort.Value[bar] = Open[bar] < adLowerBand[bar - previous] - sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_opens_below_the_Lower_Band:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar]  = Open[bar] < adLowerBand[bar - previous] - sigma ? 1 : 0;
                        indCompShort.Value[bar] = Open[bar] > adUpperBand[bar - previous] + sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_opens_above_the_Lower_Band:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar]  = Open[bar] > adLowerBand[bar - previous] + sigma ? 1 : 0;
                        indCompShort.Value[bar] = Open[bar] < adUpperBand[bar - previous] - sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_opens_below_the_Upper_Band_after_opening_above_it:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int baseBar = bar - 1;
                        while (Math.Abs(Open[baseBar] - adUpperBand[baseBar - previous]) < sigma && baseBar > firstBar)
                        { baseBar--; }

                        indCompLong.Value[bar]  = Open[bar] < adUpperBand[bar - previous] - sigma && Open[baseBar] > adUpperBand[baseBar - previous] + sigma ? 1 : 0;

                        baseBar = bar - 1;
                        while (Math.Abs(Open[baseBar] - adLowerBand[baseBar - previous]) < sigma && baseBar > firstBar)
                        { baseBar--; }

                        indCompShort.Value[bar] = Open[bar] > adLowerBand[bar - previous] + sigma && Open[baseBar] < adLowerBand[baseBar - previous] - sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_opens_above_the_Upper_Band_after_opening_below_it:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int baseBar = bar - 1;
                        while (Math.Abs(Open[baseBar] - adUpperBand[baseBar - previous]) < sigma && baseBar > firstBar)
                        { baseBar--; }

                        indCompLong.Value[bar]  = Open[bar] > adUpperBand[bar - previous] + sigma && Open[baseBar] < adUpperBand[baseBar - previous] - sigma ? 1 : 0;

                        baseBar = bar - 1;
                        while (Math.Abs(Open[baseBar] - adLowerBand[baseBar - previous]) < sigma && baseBar > firstBar)
                        { baseBar--; }

                        indCompShort.Value[bar] = Open[bar] < adLowerBand[bar - previous] - sigma && Open[baseBar] > adLowerBand[baseBar - previous] + sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_opens_below_the_Lower_Band_after_opening_above_it:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int baseBar = bar - 1;
                        while (Math.Abs(Open[baseBar] - adLowerBand[baseBar - previous]) < sigma && baseBar > firstBar)
                        { baseBar--; }

                        indCompLong.Value[bar]  = Open[bar] < adLowerBand[bar - previous] - sigma && Open[baseBar] > adLowerBand[baseBar - previous] + sigma ? 1 : 0;

                        baseBar = bar - 1;
                        while (Math.Abs(Open[baseBar] - adUpperBand[baseBar - previous]) < sigma && baseBar > firstBar)
                        { baseBar--; }

                        indCompShort.Value[bar] = Open[bar] > adUpperBand[bar - previous] + sigma && Open[baseBar] < adUpperBand[baseBar - previous] - sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_opens_above_the_Lower_Band_after_opening_below_it:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        int baseBar = bar - 1;
                        while (Math.Abs(Open[baseBar] - adLowerBand[baseBar - previous]) < sigma && baseBar > firstBar)
                        { baseBar--; }

                        indCompLong.Value[bar]  = Open[bar] > adLowerBand[bar - previous] + sigma && Open[baseBar] < adLowerBand[baseBar - previous] - sigma ? 1 : 0;

                        baseBar = bar - 1;
                        while (Math.Abs(Open[baseBar] - adUpperBand[baseBar - previous]) < sigma && baseBar > firstBar)
                        { baseBar--; }

                        indCompShort.Value[bar] = Open[bar] < adUpperBand[bar - previous] - sigma && Open[baseBar] > adUpperBand[baseBar - previous] + sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_closes_below_the_Upper_Band:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar]  = Close[bar] < adUpperBand[bar - previous] - sigma ? 1 : 0;
                        indCompShort.Value[bar] = Close[bar] > adLowerBand[bar - previous] + sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_closes_above_the_Upper_Band:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar]  = Close[bar] > adUpperBand[bar - previous] + sigma ? 1 : 0;
                        indCompShort.Value[bar] = Close[bar] < adLowerBand[bar - previous] - sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_closes_below_the_Lower_Band:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar]  = Close[bar] < adLowerBand[bar - previous] - sigma ? 1 : 0;
                        indCompShort.Value[bar] = Close[bar] > adUpperBand[bar - previous] + sigma ? 1 : 0;
                    }
                    break;

                case BandIndLogic.The_bar_closes_above_the_Lower_Band:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indCompLong.Value[bar]  = Close[bar] > adLowerBand[bar - previous] + sigma ? 1 : 0;
                        indCompShort.Value[bar] = Close[bar] < adUpperBand[bar - previous] - sigma ? 1 : 0;
                    }
                    break;

                default:
                    return false;
            }

            return true;
        }
Ejemplo n.º 13
0
        /// <summary>
        ///     Calculates the logic of an Oscillator.
        /// </summary>
        /// <param name="firstBar">The first bar number.</param>
        /// <param name="prvs">To use the previous bar or not.</param>
        /// <param name="adIndValue">The indicator values.</param>
        /// <param name="levelLong">The Level value for a Long position.</param>
        /// <param name="levelShort">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 everything is ok.</returns>
        protected void 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 (!IsDiscreteValues)     // Aroon oscillator uses IsDiscreteValues = 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 (!IsDiscreteValues)     // Aroon oscillator uses IsDiscreteValues = 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;
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        ///     Calculates the logic of a No Direction Oscillator.
        /// </summary>
        /// <param name="firstBar">The first bar number.</param>
        /// <param name="prvs">To use the previous bar or not.</param>
        /// <param name="adIndValue">The indicator values.</param>
        /// <param name="dLevel">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 everything is ok.</returns>
        protected void NoDirectionOscillatorLogic(int firstBar, int prvs, double[] adIndValue, double dLevel,
                                                  ref IndicatorComp indComp, 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];
                    bool isNoChange = true;

                    while (Math.Abs(adIndValue[currBar] - adIndValue[baseBar]) < sigma && isNoChange &&
                           baseBar > firstBar)
                    {
                        isNoChange = (isHigher == (adIndValue[baseBar + 1] > adIndValue[baseBar]));
                        baseBar--;
                    }

                    indComp.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];
                    bool isNoChange = true;

                    while (Math.Abs(adIndValue[currBar] - adIndValue[baseBar]) < sigma && isNoChange &&
                           baseBar > firstBar)
                    {
                        isNoChange = (isHigher == (adIndValue[baseBar + 1] > adIndValue[baseBar]));
                        baseBar--;
                    }

                    indComp.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++)
                {
                    indComp.Value[bar] = adIndValue[bar - prvs] > dLevel + sigma ? 1 : 0;
                }
                break;

            case IndicatorLogic.The_indicator_is_lower_than_the_level_line:
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    indComp.Value[bar] = adIndValue[bar - prvs] < dLevel - 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] - dLevel) < sigma && baseBar > firstBar)
                    {
                        baseBar--;
                    }

                    indComp.Value[bar] = (adIndValue[baseBar] < dLevel - sigma &&
                                          adIndValue[bar - prvs] > dLevel + 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] - dLevel) < sigma && baseBar > firstBar)
                    {
                        baseBar--;
                    }

                    indComp.Value[bar] = (adIndValue[baseBar] > dLevel + sigma &&
                                          adIndValue[bar - prvs] < dLevel - 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 bar2 = bar1 - 1 > firstBar ? bar1 - 1 : firstBar;
                    while (Math.Abs(adIndValue[bar1] - adIndValue[bar2]) < sigma && bar2 > firstBar)
                    {
                        bar2--;
                    }

                    indComp.Value[bar] = (adIndValue[bar2] > 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 bar2 = bar1 - 1 > firstBar ? bar1 - 1 : firstBar;
                    while (Math.Abs(adIndValue[bar1] - adIndValue[bar2]) < sigma && bar2 > firstBar)
                    {
                        bar2--;
                    }

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

            default:
                return;
            }
        }
        /// <summary>
        /// Calculates the logic of an Oscillator.
        /// </summary>
        /// <param name="firstBar">The first bar number.</param>
        /// <param name="previous">To use the previous bar or not.</param>
        /// <param name="adIndValue">The indicator values.</param>
        /// <param name="levelLong">The Level value for a Long position.</param>
        /// <param name="levelShort">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 everything is ok.</returns>
        protected bool OscillatorLogic(int firstBar, int previous, double[] indValue, 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 - previous;
                        int  baseBar  = currBar - 1;
                        bool isHigher = indValue[currBar] > indValue[baseBar];

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

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

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

                        if (!IsDiscreteValues)
                        {
                            bool isNoChange = true;
                            while (Math.Abs(indValue[currBar] - indValue[baseBar]) < sigma && isNoChange && baseBar > firstBar)
                            {
                                isNoChange = (isHigher == (indValue[baseBar + 1] > indValue[baseBar]));
                                baseBar--;
                            }
                        }

                        indCompLong.Value[bar]  = indValue[baseBar] > indValue[currBar] + sigma ? 1 : 0;
                        indCompShort.Value[bar] = indValue[baseBar] < indValue[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]  = indValue[bar - previous] > levelLong  + sigma ? 1 : 0;
                        indCompShort.Value[bar] = indValue[bar - previous] < 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]  = indValue[bar - previous] < levelLong  - sigma ? 1 : 0;
                        indCompShort.Value[bar] = indValue[bar - previous] > levelShort + sigma ? 1 : 0;
                    }
                    break;

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

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

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

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

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

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

                        indCompLong.Value[bar]  = (indValue[bar2] > indValue[bar1] && indValue[bar1] < indValue[bar0] && bar1 == bar0 - 1) ? 1 : 0;
                        indCompShort.Value[bar] = (indValue[bar2] < indValue[bar1] && indValue[bar1] > indValue[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  - previous;
                        int bar1 = bar0 - 1;
                        while (Math.Abs(indValue[bar0] - indValue[bar1]) < sigma && bar1 > firstBar)
                        { bar1--; }

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

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

                default:
                    return false;
            }

            return true;
        }
Ejemplo n.º 16
0
        protected void IndicatorChangesItsDirectionDownward(int firstBar, int prvs, double[] adIndValue, ref IndicatorComp indCompLong, ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();

            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 bar2 = bar1 - 1 > firstBar ? bar1 - 1 : firstBar;
                while (Math.Abs(adIndValue[bar1] - adIndValue[bar2]) < sigma && bar2 > firstBar)
                {
                    bar2--;
                }

                indCompLong.Value[bar]  = (adIndValue[bar2] < adIndValue[bar1] && adIndValue[bar1] > adIndValue[bar0] && bar1 == bar0 - 1) ? 1 : 0;
                indCompShort.Value[bar] = (adIndValue[bar2] > adIndValue[bar1] && adIndValue[bar1] < adIndValue[bar0] && bar1 == bar0 - 1) ? 1 : 0;
            }
        }
        /// <summary>
        /// Returns signals for the logic rule "Indicator rises".
        /// </summary>
        protected void IndicatorRisesLogic(int firstBar, int previous, double[] adIndValue, ref IndicatorComp indCompLong, ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();

            for (int bar = firstBar; bar < Bars; bar++)
            {
                int  currentBar = bar - previous;
                int  baseBar    = currentBar - 1;
                bool isNoChange = true;
                bool isHigher   = adIndValue[currentBar] > adIndValue[baseBar];

                while (Math.Abs(adIndValue[currentBar] - adIndValue[baseBar]) < sigma && isNoChange && baseBar > firstBar)
                {
                    isNoChange = (isHigher == (adIndValue[baseBar + 1] > adIndValue[baseBar]));
                    baseBar--;
                }

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

            return;
        }
        /// <summary>
        /// Calculates the logic of a No Direction Oscillator.
        /// </summary>
        /// <param name="firstBar">The first bar number.</param>
        /// <param name="previous">To use the previous bar or not.</param>
        /// <param name="adIndValue">The indicator values.</param>
        /// <param name="level">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 everything is ok.</returns>
        protected bool NoDirectionOscillatorLogic(int firstBar, int previous, double[] adIndValue, double level, ref IndicatorComp indComp, IndicatorLogic indLogic)
        {
            double sigma = Sigma();

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

                        while (Math.Abs(adIndValue[currentBar] - adIndValue[baseBar]) < sigma && isNoChange && baseBar > firstBar)
                        {
                            isNoChange = (isHigher == (adIndValue[baseBar + 1] > adIndValue[baseBar]));
                            baseBar--;
                        }

                        indComp.Value[bar] = adIndValue[baseBar] < adIndValue[currentBar] - sigma ? 1 : 0;
                    }
                    break;

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

                        while (Math.Abs(adIndValue[currentBar] - adIndValue[baseBar]) < sigma && isNoChange && baseBar > firstBar)
                        {
                            isNoChange = (isHigher == (adIndValue[baseBar + 1] > adIndValue[baseBar]));
                            baseBar--;
                        }

                        indComp.Value[bar] = adIndValue[baseBar] > adIndValue[currentBar] + sigma ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_is_higher_than_the_level_line:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indComp.Value[bar] = adIndValue[bar - previous] > level + sigma ? 1 : 0;
                    }
                    break;

                case IndicatorLogic.The_indicator_is_lower_than_the_level_line:
                    for (int bar = firstBar; bar < Bars; bar++)
                    {
                        indComp.Value[bar] = adIndValue[bar - previous] < level - sigma ? 1 : 0;
                    }
                    break;

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

                        indComp.Value[bar] = (adIndValue[baseBar] < level - sigma && adIndValue[bar - previous] > level + sigma) ? 1 : 0;
                    }
                    break;

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

                        indComp.Value[bar] = (adIndValue[baseBar] > level + sigma && adIndValue[bar - previous] < level - sigma) ? 1 : 0;
                    }
                    break;

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

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

                        indComp.Value[bar] = (adIndValue[bar2] > 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  - previous;
                        int bar1 = bar0 - 1;
                        while (Math.Abs(adIndValue[bar0] - adIndValue[bar1]) < sigma && bar1 > firstBar)
                        { bar1--; }

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

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

                default:
                    return false;
            }

            return true;
        }
        /// <summary>
        /// Returns signals for the logic rule "The Indicator crosses AnotherIndicator upward"
        /// </summary>
        protected void IndicatorCrossesAnotherIndicatorUpwardLogic(int firstBar, int previous, double[] adIndValue, double[] adAnotherIndValue,
            ref IndicatorComp indCompLong, ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();

            for (int bar = firstBar; bar < Bars; bar++)
            {
                int currentBar = bar - previous;
                int baseBar = currentBar - 1;
                while (Math.Abs(adIndValue[baseBar] - adAnotherIndValue[baseBar]) < sigma && baseBar > firstBar)
                { baseBar--; }

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

            return;
        }
        /// <summary>
        /// Returns signals for the logic rule "The Indicator is lower than the AnotherIndicator"
        /// </summary>
        protected void IndicatorIsLowerThanAnotherIndicatorLogic(int firstBar, int previous, double[] adIndValue, double[] adAnotherIndValue,
            ref IndicatorComp indCompLong, ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();

            for (int bar = firstBar; bar < Bars; bar++)
            {
                int currentBar = bar - previous;
                indCompLong.Value[bar]  = adIndValue[currentBar] < adAnotherIndValue[currentBar] - sigma ? 1 : 0;
                indCompShort.Value[bar] = adIndValue[currentBar] > adAnotherIndValue[currentBar] + sigma ? 1 : 0;
            }

            return;
        }
        /// <summary>
        /// Returns signals for the logic rule "The bar opens below the Indicator"
        /// </summary>
        protected void BarOpensBelowIndicatorLogic(int firstBar, int previous, double[] adIndValue, ref IndicatorComp indCompLong, ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();

            for (int bar = firstBar; bar < Bars; bar++)
            {
                indCompLong.Value[bar]  = Open[bar] < adIndValue[bar - previous] - sigma ? 1 : 0;
                indCompShort.Value[bar] = Open[bar] > adIndValue[bar - previous] + sigma ? 1 : 0;
            }

            return;
        }
        /// <summary>
        /// Returns signals for the logic rule "The bar opens below the Indicator after opening above it"
        /// </summary>
        protected void BarOpensBelowIndicatorAfterOpeningAboveLogic(int firstBar, int previous, double[] adIndValue, ref IndicatorComp indCompLong, ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();

            for (int bar = firstBar; bar < Bars; bar++)
            {
                int baseBar = bar - 1;
                while (Math.Abs(Open[baseBar] - adIndValue[baseBar - previous]) < sigma && baseBar > firstBar)
                { baseBar--; }

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

            return;
        }
        /// <summary>
        ///     Returns signals for the logic rule "The Indicator is higher than the AnotherIndicator"
        /// </summary>
        protected void IndicatorIsHigherThanAnotherIndicatorLogic(int firstBar, int prvs, double[] adIndValue,
                                                                  double[] adAnotherIndValue,
                                                                  ref IndicatorComp indCompLong,
                                                                  ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();
            firstBar = Math.Max(firstBar, 2);

            for (int bar = firstBar; bar < Bars; bar++)
            {
                int currBar = bar - prvs;
                indCompLong.Value[bar] = adIndValue[currBar] > adAnotherIndValue[currBar] + sigma ? 1 : 0;
                indCompShort.Value[bar] = adIndValue[currBar] < adAnotherIndValue[currBar] - sigma ? 1 : 0;
            }
        }
        /// <summary>
        ///     Returns a copy.
        /// </summary>
        public IndicatorComp Clone()
        {
            var indicatorComp = new IndicatorComp
                {
                    CompName = CompName,
                    DataType = DataType,
                    ChartType = ChartType,
                    ChartColor = ChartColor,
                    FirstBar = FirstBar,
                    UsePreviousBar = UsePreviousBar,
                    ShowInDynInfo = ShowInDynInfo,
                    PosPriceDependence = PosPriceDependence
                };


            if (Value != null)
            {
                indicatorComp.Value = new double[Value.Length];
                Value.CopyTo(indicatorComp.Value, 0);
            }

            return indicatorComp;
        }
        /// <summary>
        ///     Returns signals for the logic rule "The bar closes below the Indicator"
        /// </summary>
        protected void BarClosesBelowIndicatorLogic(int firstBar, int prvs, double[] adIndValue,
                                                    ref IndicatorComp indCompLong, ref IndicatorComp indCompShort)
        {
            double sigma = Sigma();
            firstBar = Math.Max(firstBar, 2);

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