Source » Directional Indicators - source code

// Forex Strategy Builder
// Copyright (c) 2006 - 2008 Miroslav Popov - All rights reserved!
// http://forexsb.com
// info(a)forexsb.com
//
// Last changed on: 2006-09-01

using System;
using System.Drawing;

namespace Forex_Strategy_Builder
{
    /// <summary>
    /// Indicator : Directional Indicators
    /// </summary>
    public class Directional_Indicators : Indicator
    {
        /// <summary>
        /// The default constructor
        /// </summary>
        public Directional_Indicators()
        {
        }

        /// <summary>
        /// Sets the default parameters for the designated slot type
        /// </summary>
        /// <param name="slotType">The slot type.</param>
        public Directional_Indicators(SlotTypes slotType)
        {
            sIndicatorName  = "Directional Indicators";
            parameters      = new IndicatorParam();
            component       = new IndicatorComp[] { };
            fMinValue       = 0f;
            bSeparatedChart = true;
            bIsCalculated   = false;


            // The indicator name.
            parameters.IndicatorName = sIndicatorName;

            // The slot type.
            parameters.SlotType = slotType;

            // The ComboBox parameters.
            parameters.ListParam[0].Caption   = "Logic";
            parameters.ListParam[0].ItemList  = new string[]
            {
                "The ADI+ rises",
                "The ADI+ falls",
                "The ADI- rises",
                "The ADI- falls",
                "The ADI+ is higher than ADI-",
                "The ADI+ is lower than ADI-",
                "The ADI+ crosses the ADI- line upward",
                "The ADI+ crosses the ADI- line downward",
                "The ADI+ changes its direction upward",
                "The ADI+ changes its direction downward",
                "The ADI- changes its direction upward",
                "The ADI- changes its direction downward"
            };
            parameters.ListParam[0].Index    = 0;
            parameters.ListParam[0].Text     = parameters.ListParam[0].ItemList[parameters.ListParam[0].Index];
            parameters.ListParam[0].Enabled  = true;
            parameters.ListParam[0].ToolTip  = "Logic of application of the indicator";

            parameters.ListParam[1].Caption  = "Smoothing method";
            parameters.ListParam[1].ItemList = Enum.GetNames(typeof(MAMethod));
            parameters.ListParam[1].Index    = 2;
            parameters.ListParam[1].Text     = parameters.ListParam[1].ItemList[parameters.ListParam[1].Index];
            parameters.ListParam[1].Enabled  = true;
            parameters.ListParam[1].ToolTip  = "The Moving Average method used for smoothing the ADI";

            // The NumericUpDown parameters.
            parameters.NumParam[0].Caption   = "Period";
            parameters.NumParam[0].Value     = 10;
            parameters.NumParam[0].Min       = 5;
            parameters.NumParam[0].Max       = 200;
            parameters.NumParam[0].Enabled   = true;
            parameters.NumParam[0].ToolTip   = "The period of ADI";

            // The CheckBox parameters.
            parameters.CheckParam[0].Caption = "Use previous bar value";
            parameters.CheckParam[0].Checked = Data.Strategy.PrepareUsePrevBarValueCheckBox(slotType);
            parameters.CheckParam[0].Enabled = true;
            parameters.CheckParam[0].ToolTip = "Use the indicator value from the previous bar";
        }

        /// <summary>
        /// Calculates the indicator's components
        /// </summary>
        /// <param name="slotType">The slot type.</param>
        public override void Calculate(SlotTypes slotType)
        {
            if (parameters.SlotType == SlotTypes.NotDefined) return;

            // Reading the parameters
            MAMethod maMethod = (MAMethod)Enum.GetValues(typeof(MAMethod)).GetValue(parameters.ListParam[1].Index);
            int iNADI = (int)parameters.NumParam[0].Value;
            int iPrvs = parameters.CheckParam[0].Checked ? 1 : 0;

            // Calculation
            int iFirstBar = iNADI + 2;

            float[] afADIPos = new float[Bars];
            float[] afADINeg = new float[Bars];
            float[] afDIPos = new float[Bars];
            float[] afDINeg = new float[Bars];
            float fHiDiff;
            float fLoDiff;
            float fTR;

            afDIPos[0] = 0;
            afDINeg[0] = 0;

            for (int iBar = 1; iBar < Bars; iBar++)
            {
                fHiDiff = High[iBar] - High[iBar - 1];
                fLoDiff = Low[iBar - 1] - Low[iBar];
                fTR = Math.Max(Math.Abs(High[iBar] - Close[iBar - 1]), Math.Abs(Close[iBar - 1] - Low[iBar]));
                fTR = Math.Max(Math.Abs(High[iBar] - Low[iBar]), fTR);

                if (fHiDiff > 0 && fHiDiff > fLoDiff)
                    afDIPos[iBar] = 100 * fHiDiff / fTR;
                else
                    afDIPos[iBar] = 0;

                if (fLoDiff > 0 && fLoDiff > fHiDiff)
                    afDINeg[iBar] = 100 * fLoDiff / fTR;
                else
                    afDINeg[iBar] = 0;
            }

            afADIPos = MovingAverage(iNADI, 0, maMethod, afDIPos);
            afADINeg = MovingAverage(iNADI, 0, maMethod, afDINeg);


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

            component[0] = new IndicatorComp();
            component[0].CompName = "The ADI+";
            component[0].DataType = IndComponentType.IndicatorValue;
            component[0].ChartType = IndChartType.Line;
            component[0].ChartColor = Color.Green;
            component[0].FirstBar = iFirstBar;
            component[0].Value = afADIPos;

            component[1] = new IndicatorComp();
            component[1].CompName = "The ADI-";
            component[1].DataType = IndComponentType.IndicatorValue;
            component[1].ChartType = IndChartType.Line;
            component[1].ChartColor = Color.Red;
            component[1].FirstBar = iFirstBar;
            component[1].Value = afADINeg;

            component[2] = new IndicatorComp();
            component[2].ChartType = IndChartType.NoChart;
            component[2].FirstBar = iFirstBar;
            component[2].Value = new float[Bars];

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

            // Sets the component's type.
            if (slotType == SlotTypes.OpenFilter)
            {
                component[2].DataType = IndComponentType.AllowOpenLong;
                component[2].CompName = "Allows long positions opening";
                component[3].DataType = IndComponentType.AllowOpenShort;
                component[3].CompName = "Allows short positions opening";
            }
            else if (slotType == SlotTypes.CloseFilter)
            {
                component[2].DataType = IndComponentType.ForceCloseLong;
                component[2].CompName = "Forces long positions closing";
                component[3].DataType = IndComponentType.ForceCloseShort;
                component[3].CompName = "Forces short positions closing";
            }


            switch (parameters.ListParam[0].Text)
            {
                case "The ADI+ rises":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[2].Value[iBar] = afADIPos[iBar - iPrvs - 1] < afADIPos[iBar - iPrvs] ? 1 : 0;
                        component[3].Value[iBar] = afADIPos[iBar - iPrvs - 1] > afADIPos[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                case "The ADI+ falls":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[2].Value[iBar] = afADIPos[iBar - iPrvs - 1] > afADIPos[iBar - iPrvs] ? 1 : 0;
                        component[3].Value[iBar] = afADIPos[iBar - iPrvs - 1] < afADIPos[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                case "The ADI- rises":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[2].Value[iBar] = afADINeg[iBar - iPrvs - 1] < afADINeg[iBar - iPrvs] ? 1 : 0;
                        component[3].Value[iBar] = afADINeg[iBar - iPrvs - 1] > afADINeg[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                case "The ADI- falls":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[2].Value[iBar] = afADINeg[iBar - iPrvs - 1] > afADINeg[iBar - iPrvs] ? 1 : 0;
                        component[3].Value[iBar] = afADINeg[iBar - iPrvs - 1] < afADINeg[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                case "The ADI+ is higher than ADI-":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[2].Value[iBar] = afADIPos[iBar - iPrvs] > afADINeg[iBar - iPrvs] ? 1 : 0;
                        component[3].Value[iBar] = afADINeg[iBar - iPrvs] > afADIPos[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                case "The ADI+ is lower than ADI-":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[2].Value[iBar] = afADIPos[iBar - iPrvs] < afADINeg[iBar - iPrvs] ? 1 : 0;
                        component[3].Value[iBar] = afADINeg[iBar - iPrvs] < afADIPos[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                case "The ADI+ crosses the ADI- line upward":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[2].Value[iBar] = (afADIPos[iBar - iPrvs - 1] < afADINeg[iBar - iPrvs - 1] && afADIPos[iBar - iPrvs] > afADINeg[iBar - iPrvs]) ? 1 : 0;
                        component[3].Value[iBar] = (afADINeg[iBar - iPrvs - 1] < afADIPos[iBar - iPrvs - 1] && afADINeg[iBar - iPrvs] > afADIPos[iBar - iPrvs]) ? 1 : 0;
                    }
                    break;

                case "The ADI+ crosses the ADI- line downward":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[2].Value[iBar] = (afADIPos[iBar - iPrvs - 1] > afADINeg[iBar - iPrvs - 1] && afADIPos[iBar - iPrvs] < afADINeg[iBar - iPrvs]) ? 1 : 0;
                        component[3].Value[iBar] = (afADINeg[iBar - iPrvs - 1] > afADIPos[iBar - iPrvs - 1] && afADINeg[iBar - iPrvs] < afADIPos[iBar - iPrvs]) ? 1 : 0;
                    }
                    break;

                case "The ADI+ changes its direction upward":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[2].Value[iBar] = (afADIPos[iBar - iPrvs - 2] > afADIPos[iBar - iPrvs - 1] && afADIPos[iBar - iPrvs - 1] < afADIPos[iBar - iPrvs]) ? 1 : 0;
                        component[3].Value[iBar] = (afADIPos[iBar - iPrvs - 2] < afADIPos[iBar - iPrvs - 1] && afADIPos[iBar - iPrvs - 1] > afADIPos[iBar - iPrvs]) ? 1 : 0;
                    }
                    break;

                case "The ADI+ changes its direction downward":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[2].Value[iBar] = (afADIPos[iBar - iPrvs - 2] < afADIPos[iBar - iPrvs - 1] && afADIPos[iBar - iPrvs - 1] > afADIPos[iBar - iPrvs]) ? 1 : 0;
                        component[3].Value[iBar] = (afADIPos[iBar - iPrvs - 2] > afADIPos[iBar - iPrvs - 1] && afADIPos[iBar - iPrvs - 1] < afADIPos[iBar - iPrvs]) ? 1 : 0;
                    }
                    break;

                case "The ADI- changes its direction upward":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[2].Value[iBar] = (afADINeg[iBar - iPrvs - 2] > afADINeg[iBar - iPrvs - 1] && afADINeg[iBar - iPrvs - 1] < afADINeg[iBar - iPrvs]) ? 1 : 0;
                        component[3].Value[iBar] = (afADINeg[iBar - iPrvs - 2] < afADINeg[iBar - iPrvs - 1] && afADINeg[iBar - iPrvs - 1] > afADINeg[iBar - iPrvs]) ? 1 : 0;
                    }
                    break;

                case "The ADI- changes its direction downward":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[2].Value[iBar] = (afADINeg[iBar - iPrvs - 2] < afADINeg[iBar - iPrvs - 1] && afADINeg[iBar - iPrvs - 1] > afADINeg[iBar - iPrvs]) ? 1 : 0;
                        component[3].Value[iBar] = (afADINeg[iBar - iPrvs - 2] > afADINeg[iBar - iPrvs - 1] && afADINeg[iBar - iPrvs - 1] < afADINeg[iBar - iPrvs]) ? 1 : 0;
                    }
                    break;

                default:
                    break;
            }

            bIsCalculated = true;
        }
    }
}

Top