Source » Oscillator of MACD - source code

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

using System;
using System.Drawing;

namespace Forex_Strategy_Builder
{

    /// <summary>
    /// indicator: Oscillator of MACD
    /// </summary>
    public class Oscillator_of_MACD : Indicator
    {
        /// <summary>
        /// The default constructor.
        /// </summary>
        public Oscillator_of_MACD()
        {
        }

        /// <summary>
        /// Sets the default parameters for the designated slot type.
        /// </summary>
        /// <param name="slotType">The slot type.</param>
        public Oscillator_of_MACD(SlotTypes slotType)
        {
            sIndicatorName  = "Oscillator of MACD";
            parameters      = new IndicatorParam();
            component       = new IndicatorComp[] { };
            afSpecValue     = new float[1] { 0f };
            bSeparatedChart = true;
            bIsCalculated   = false;

            typeOfIndicator = TypeOfIndicator.OscillatorOfIndicators;

            // 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 Oscillator rises",
                "The Oscillator falls",
                "The Oscillator is higher than the zero line",
                "The Oscillator is lower than the zero line",
                "The Oscillator crosses the zero line upward",
                "The Oscillator crosses the zero line downward",
                "The Oscillator changes its direction upward",
                "The Oscillator 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 oscillator";

            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 MACD value";

            parameters.ListParam[2].Caption  = "Base price";
            parameters.ListParam[2].ItemList = Enum.GetNames(typeof(BasePrice));
            parameters.ListParam[2].Index    = 3;
            parameters.ListParam[2].Text     = parameters.ListParam[2].ItemList[parameters.ListParam[2].Index];
            parameters.ListParam[2].Enabled  = true;
            parameters.ListParam[2].ToolTip  = "The price the indicator is based on";

            // The NumericUpDown parameters.
            parameters.NumParam[0].Caption = "MACD1 Slow line";
            parameters.NumParam[0].Value   = 26;
            parameters.NumParam[0].Min     = 1;
            parameters.NumParam[0].Max     = 200;
            parameters.NumParam[0].Enabled = true;
            parameters.NumParam[0].ToolTip = "The period of first MACD Slow line";

            parameters.NumParam[1].Caption = "MACD2 Slow line";
            parameters.NumParam[1].Value   = 32;
            parameters.NumParam[1].Min     = 1;
            parameters.NumParam[1].Max     = 200;
            parameters.NumParam[1].Enabled = true;
            parameters.NumParam[1].ToolTip = "The period of second MACD Slow line";

            parameters.NumParam[2].Caption = "MACD1 Fast line";
            parameters.NumParam[2].Value   = 12;
            parameters.NumParam[2].Min     = 1;
            parameters.NumParam[2].Max     = 200;
            parameters.NumParam[2].Enabled = true;
            parameters.NumParam[2].ToolTip = "The period of first MACD Fast line";

            parameters.NumParam[3].Caption = "MACD2 Fast line";
            parameters.NumParam[3].Value   = 21;
            parameters.NumParam[3].Min     = 1;
            parameters.NumParam[3].Max     = 200;
            parameters.NumParam[3].Enabled = true;
            parameters.NumParam[3].ToolTip = "The period of second MACD Fast line";

            // 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);
            BasePrice basePrice = (BasePrice)Enum.GetValues(typeof(BasePrice)).GetValue(parameters.ListParam[2].Index);
            int iPeriod1 = (int)parameters.NumParam[0].Value;
            int iPeriod2 = (int)parameters.NumParam[1].Value;
            int iPrvs    = parameters.CheckParam[0].Checked ? 1 : 0;

            // Calculation
            int iFirstBar = iPeriod1 + iPeriod2 + 2;
            float[] afIndicator1 = new float[Bars];
            float[] afIndicator2 = new float[Bars];
            float[] afOscllator  = new float[Bars];

// ---------------------------------------------------------
            MACD MACD1 = new MACD(slotType);
            MACD1.IndParam.ListParam[1].Index = parameters.ListParam[1].Index;
            MACD1.IndParam.ListParam[2].Index = parameters.ListParam[2].Index;
            MACD1.IndParam.NumParam[0].Value = parameters.NumParam[0].Value;
            MACD1.IndParam.NumParam[1].Value = parameters.NumParam[2].Value;
            MACD1.IndParam.CheckParam[0].Checked = parameters.CheckParam[0].Checked;
            MACD1.Calculate(slotType);

            MACD MACD2 = new MACD(slotType);
            MACD2.IndParam.ListParam[1].Index = parameters.ListParam[1].Index;
            MACD2.IndParam.ListParam[2].Index = parameters.ListParam[2].Index;
            MACD2.IndParam.NumParam[0].Value = parameters.NumParam[1].Value;
            MACD2.IndParam.NumParam[1].Value = parameters.NumParam[3].Value;
            MACD2.IndParam.CheckParam[0].Checked = parameters.CheckParam[0].Checked;
            MACD2.Calculate(slotType);

            afIndicator1 = MACD1.Component[0].Value;
            afIndicator2 = MACD2.Component[0].Value;
// ----------------------------------------------------------

            for (int iBar = iFirstBar; iBar < Bars; iBar++)
            {
                afOscllator[iBar] = afIndicator1[iBar] - afIndicator2[iBar];
            }

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

            component[0] = new IndicatorComp();
            component[0].CompName  = "Histogram";
            component[0].DataType  = IndComponentType.IndicatorValue;
            component[0].ChartType = IndChartType.Histogram;
            component[0].FirstBar  = iFirstBar;
            component[0].Value     = afOscllator;

            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 Oscillator rises":
                    indLogic = IndicatorLogic.The_indicator_rises;
                    break;

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

                case "The Oscillator is higher than the zero line":
                    indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line;
                    break;

                case "The Oscillator is lower than the zero line":
                    indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line;
                    break;

                case "The Oscillator crosses the zero line upward":
                    indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward;
                    break;

                case "The Oscillator crosses the zero line downward":
                    indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward;
                    break;

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

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

                default:
                    break;
            }

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

Top