Source » Accumulation Distribution - 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-10-21

using System;
using System.Drawing;

namespace Forex_Strategy_Builder
{
    /// <summary>
    /// Indicator: Accumulation Distribution
    /// </summary>
    public class Accumulation_Distribution : Indicator
    {
        /// <summary>
        /// The default constructor.
        /// </summary>
        public Accumulation_Distribution()
        {
        }

        /// <summary>
        /// Sets the default parameters for the designated slot type.
        /// </summary>
        /// <param name="slotType">The slot type.</param>
        public Accumulation_Distribution(SlotTypes slotType)
        {
            sIndicatorName  = "Accumulation Distribution";
            parameters      = new IndicatorParam();
            component       = new IndicatorComp[] { };
            fMaxValue       = float.MinValue;
            fMinValue       = float.MaxValue;
            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 AD rises",
                "The AD falls",
                "The AD changes its direction upward",
                "The AD 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";

            // 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
            int iPrvs = parameters.CheckParam[0].Checked ? 1 : 0;

            // Calculation
            int iFirstBar = 3;

            float[] afAD = new float[Bars];

            for (int iBar = 1; iBar < Bars; iBar++)
            {
                float fDelta = 0f;
                float fRange = High[iBar] - Low[iBar];

                if (fRange > 0)
                {
                    fDelta = Volume[iBar] * (2 * Close[iBar] - High[iBar] - Low[iBar]) / fRange;
                }

                afAD[iBar] = afAD[iBar - 1] + fDelta;
            }

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

            component[0] = new IndicatorComp();
            component[0].CompName   = "Accumulation Distribution";
            component[0].DataType   = IndComponentType.IndicatorValue;
            component[0].ChartType  = IndChartType.Line;
            component[0].ChartColor = Color.Blue;
            component[0].FirstBar   = iFirstBar;
            component[0].Value      = afAD;

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

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

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

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

            switch (parameters.ListParam[0].Text)
            {
                case "The AD rises":
                    indLogic = IndicatorLogic.The_indicator_rises;
                    break;

                case "The AD falls":
                    indLogic = IndicatorLogic.The_indicator_falls;
                    break;

                case "The AD changes its direction upward":
                    indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward;
                    break;

                case "The AD changes its direction downward":
                    indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward;
                    break;

                default:
                    break;
            }

            bIsCalculated = OscillatorLogic(iFirstBar, iPrvs, afAD, 0, 0, ref component[1], ref component[2], indLogic);

            return;
        }
    }
}

Top