Esempio n. 1
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Reading the parameters
            var maMethod  = (MAMethod)IndParam.ListParam[2].Index;
            var periodCci = (int)IndParam.NumParam[0].Value;
            var periodMa  = (int)IndParam.NumParam[1].Value;
            var level     = (int)IndParam.NumParam[2].Value;
            int previous  = IndParam.CheckParam[0].Checked ? 1 : 0;

            SpecialValues = new double[] { -level, level };

            // Calculation
            int firstBar = periodCci + periodMa + 2;

            var cciIndicator = new CommodityChannelIndex();

            cciIndicator.Initialize(SlotType);
            cciIndicator.IndParam.ListParam[1].Index    = IndParam.ListParam[1].Index;
            cciIndicator.IndParam.ListParam[2].Index    = IndParam.ListParam[3].Index;
            cciIndicator.IndParam.NumParam[0].Value     = IndParam.NumParam[0].Value;
            cciIndicator.IndParam.CheckParam[0].Checked = IndParam.CheckParam[0].Checked;
            cciIndicator.Calculate(DataSet);

            double[] cci      = cciIndicator.Component[0].Value;
            double[] maCci    = MovingAverage(periodMa, 0, maMethod, cci);
            double[] buyZone  = new double[Bars];
            double[] sellZone = new double[Bars];

            for (int bar = firstBar; bar < Bars; bar++)
            {
                if (buyZone[bar - 1] > Epsilon && maCci[bar] < -level)
                {
                    // Continue Buy zone
                    buyZone[bar] = 1;
                    continue;
                }

                if (sellZone[bar - 1] > Epsilon && maCci[bar] > level)
                {
                    // Continue Sell zone
                    sellZone[bar] = 1;
                    continue;
                }

                if (cci[bar] > maCci[bar] + Epsilon && cci[bar - 1] <= maCci[bar - 1] &&
                    maCci[bar] < -level)
                {
                    // Start Buy zone
                    buyZone[bar] = 1;
                    continue;
                }

                if (cci[bar] < maCci[bar] - Epsilon && cci[bar - 1] >= maCci[bar - 1] &&
                    maCci[bar] > level)
                {
                    // Start Sell zone
                    sellZone[bar] = 1;
                }
            }

            // Shift signal if it is necessary
            if (previous > 0)
            {
                for (int bar = Bars - 1; bar >= firstBar; bar--)
                {
                    buyZone[bar]  = buyZone[bar - 1];
                    sellZone[bar] = sellZone[bar - 1];
                }
            }

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

            Component[0] = new IndicatorComp
            {
                CompName   = "CCI",
                DataType   = IndComponentType.IndicatorValue,
                ChartType  = IndChartType.Line,
                ChartColor = Color.RoyalBlue,
                FirstBar   = firstBar,
                Value      = cci
            };

            Component[1] = new IndicatorComp
            {
                CompName   = "MA CCI",
                DataType   = IndComponentType.IndicatorValue,
                ChartType  = IndChartType.Line,
                ChartColor = Color.Red,
                FirstBar   = firstBar,
                Value      = maCci
            };

            Component[2] = new IndicatorComp
            {
                ChartType = IndChartType.NoChart,
                FirstBar  = firstBar
            };

            Component[3] = new IndicatorComp
            {
                ChartType = IndChartType.NoChart,
                FirstBar  = firstBar
            };

            // Sets the Component's type
            if (SlotType == SlotTypes.OpenFilter)
            {
                Component[2].DataType = IndComponentType.AllowOpenLong;
                Component[2].CompName = "Is long entry allowed";
                Component[2].Value    = buyZone;
                Component[3].DataType = IndComponentType.AllowOpenShort;
                Component[3].CompName = "Is short entry allowed";
                Component[3].Value    = sellZone;
            }
            else if (SlotType == SlotTypes.CloseFilter)
            {
                Component[2].DataType = IndComponentType.ForceCloseLong;
                Component[2].CompName = "Close out long position";
                Component[2].Value    = sellZone;
                Component[3].DataType = IndComponentType.ForceCloseShort;
                Component[3].CompName = "Close out short position";
                Component[3].Value    = buyZone;
            }
        }
Esempio n. 2
0
        public override void Calculate(IDataSet dataSet)
        {
            DataSet = dataSet;

            // Reading the parameters
            var basePrice       = (BasePrice)IndParam.ListParam[2].Index;
            var referencePeriod = (int)IndParam.NumParam[2].Value;
            int previous        = IndParam.CheckParam[0].Checked ? 1 : 0;

            // Calculation

            // ---------------------------------------------------------
            var cci = new CommodityChannelIndex();

            cci.Initialize(SlotType);
            cci.IndParam.ListParam[1].Index    = IndParam.ListParam[1].Index;
            cci.IndParam.ListParam[2].Index    = IndParam.ListParam[2].Index;
            cci.IndParam.NumParam[0].Value     = IndParam.NumParam[0].Value;
            cci.IndParam.NumParam[2].Value     = IndParam.NumParam[1].Value;
            cci.IndParam.CheckParam[0].Checked = IndParam.CheckParam[0].Checked;
            cci.Calculate(DataSet);

            double[] indicatorMa = MovingAverage(referencePeriod, previous, MAMethod.Simple, cci.Component[0].Value);
            double[] marketMa    = MovingAverage(referencePeriod, previous, MAMethod.Simple, Price(basePrice));
            // ----------------------------------------------------------

            int firstBar = cci.Component[0].FirstBar + referencePeriod + 2;
            var cd       = new double[Bars];

            if (IndParam.ListParam[0].Text == "Convergence")
            {
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    cd[bar] = IsConvergence(indicatorMa, marketMa, bar);
                }
            }
            else if (IndParam.ListParam[0].Text == "Divergence")
            {
                for (int bar = firstBar; bar < Bars; bar++)
                {
                    cd[bar] = IsDivergence(indicatorMa, marketMa, bar);
                }
            }

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

            Component[0] = new IndicatorComp
            {
                CompName   = "CCI",
                DataType   = IndComponentType.IndicatorValue,
                ChartType  = IndChartType.Line,
                ChartColor = Color.RoyalBlue,
                FirstBar   = firstBar,
                Value      = cci.Component[0].Value
            };

            Component[1] = new IndicatorComp
            {
                CompName   = "CCI MA",
                DataType   = IndComponentType.IndicatorValue,
                ChartType  = IndChartType.Line,
                ChartColor = Color.Red,
                FirstBar   = firstBar,
                Value      = indicatorMa
            };

            Component[2] = new IndicatorComp
            {
                ChartType = IndChartType.NoChart,
                FirstBar  = firstBar,
                Value     = cd
            };

            Component[3] = new IndicatorComp
            {
                ChartType = IndChartType.NoChart,
                FirstBar  = firstBar,
                Value     = cd
            };

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