Source » Moving Average Crossover - 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-09-20

using System;
using System.Drawing;

namespace Forex_Strategy_Builder
{
    /// <summary>
    /// Indicator: Moving Averages Crossover
    /// </summary>
    public class Moving_Averages_Crossover : Indicator
    {
        /// <summary>
        /// The default constructor.
        /// </summary>
        public Moving_Averages_Crossover()
        {
        }

        /// <summary>
        /// Sets the default parameters for the designated slot type.
        /// </summary>
        /// <param name="slotType">The slot type.</param>
        public Moving_Averages_Crossover(SlotTypes slotType)
        {
            sIndicatorName  = "Moving Averages Crossover";
            parameters      = new IndicatorParam();
            component       = new IndicatorComp[] { };
            bSeparatedChart = false;
            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 Fats MA crosses the Slow MA upward",
                "The Fats MA crosses the Slow MA downward",
                "The Fats MA is higher than the Slow MA",
                "The Fats MA is lower than the Slow MA",
            };
            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  = "Base price";
            parameters.ListParam[1].ItemList = Enum.GetNames(typeof(BasePrice));
            parameters.ListParam[1].Index    = 3;
            parameters.ListParam[1].Text     = parameters.ListParam[1].ItemList[parameters.ListParam[1].Index];
            parameters.ListParam[1].Enabled  = true;
            parameters.ListParam[1].ToolTip  = "The price the both Moving Averages are based on";

            parameters.ListParam[3].Caption  = "Fast MA method";
            parameters.ListParam[3].ItemList = Enum.GetNames(typeof(MAMethod));
            parameters.ListParam[3].Index    = 0;
            parameters.ListParam[3].Text     = parameters.ListParam[3].ItemList[parameters.ListParam[3].Index];
            parameters.ListParam[3].Enabled  = true;
            parameters.ListParam[3].ToolTip  = "The method used for smoothing the Fast Moving Averages";

            parameters.ListParam[4].Caption  = "Slow MA method";
            parameters.ListParam[4].ItemList = Enum.GetNames(typeof(MAMethod));
            parameters.ListParam[4].Index    = 0;
            parameters.ListParam[4].Text     = parameters.ListParam[4].ItemList[parameters.ListParam[4].Index];
            parameters.ListParam[4].Enabled  = true;
            parameters.ListParam[4].ToolTip  = "The method used for smoothing the slow Moving Averages";

            // The NumericUpDown parameters.
            parameters.NumParam[0].Caption   = "Fast MA period";
            parameters.NumParam[0].Value     = 13;
            parameters.NumParam[0].Min       = 1;
            parameters.NumParam[0].Max       = 200;
            parameters.NumParam[0].Enabled   = true;
            parameters.NumParam[0].ToolTip   = "The period of Fast MA";

            parameters.NumParam[1].Caption   = "Slow MA period";
            parameters.NumParam[1].Value     = 21;
            parameters.NumParam[1].Min       = 1;
            parameters.NumParam[1].Max       = 200;
            parameters.NumParam[1].Enabled   = true;
            parameters.NumParam[1].ToolTip   = "The period of Slow MA";

            parameters.NumParam[2].Caption   = "Fast MA shift";
            parameters.NumParam[2].Value     = 0;
            parameters.NumParam[2].Min       = 0;
            parameters.NumParam[2].Max       = 100;
            parameters.NumParam[2].Point     = 0;
            parameters.NumParam[2].Enabled   = true;
            parameters.NumParam[2].ToolTip   = "The shifting value of Fast MA";

            parameters.NumParam[3].Caption   = "Slow MA shift";
            parameters.NumParam[3].Value     = 0;
            parameters.NumParam[3].Min       = 0;
            parameters.NumParam[3].Max       = 100;
            parameters.NumParam[3].Point     = 0;
            parameters.NumParam[3].Enabled   = true;
            parameters.NumParam[3].ToolTip   = "The shifting value of Slow MA";

            // 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
            BasePrice basePrice = (BasePrice)Enum.GetValues(typeof(BasePrice)).GetValue(parameters.ListParam[1].Index);
            MAMethod  fastMAMethod = (MAMethod)Enum.GetValues(typeof(MAMethod)).GetValue(parameters.ListParam[3].Index);
            MAMethod  slowMAMethod = (MAMethod)Enum.GetValues(typeof(MAMethod)).GetValue(parameters.ListParam[4].Index);
            int       iNFastMA  = (int)parameters.NumParam[0].Value;
            int       iNSlowMA  = (int)parameters.NumParam[1].Value;
            int       iSFastMA  = (int)parameters.NumParam[2].Value;
            int       iSSlowMA  = (int)parameters.NumParam[3].Value;
            int       iPrvs     = parameters.CheckParam[0].Checked ? 1 : 0;

            int iFirstBar = (int)Math.Max(iNFastMA + iSFastMA, iNSlowMA + iSSlowMA) + 2;
            float[] afMAFast = MovingAverage(iNFastMA, iSFastMA, fastMAMethod, Price(basePrice));
            float[] afMASlow = MovingAverage(iNSlowMA, iSSlowMA, slowMAMethod, Price(basePrice));
            float[] afMAOscillator = new float[Bars];

            for (int iBar = iFirstBar; iBar < Bars; iBar++)
                afMAOscillator[iBar] = afMAFast[iBar] - afMASlow[iBar];

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

            component[0] = new IndicatorComp();
            component[0].CompName   = "Fast Moving Average";
            component[0].ChartColor = Color.Goldenrod;
            component[0].DataType   = IndComponentType.IndicatorValue;
            component[0].ChartType  = IndChartType.Line;
            component[0].FirstBar   = iFirstBar;
            component[0].Value      = afMAFast;

            component[1] = new IndicatorComp();
            component[1].CompName   = "Slow Moving Average";
            component[1].ChartColor = Color.IndianRed;
            component[1].DataType   = IndComponentType.IndicatorValue;
            component[1].ChartType  = IndChartType.Line;
            component[1].FirstBar   = iFirstBar;
            component[1].Value      = afMASlow;

            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";
            }

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

            switch (parameters.ListParam[0].Text)
            {
                case "The Fats MA crosses the Slow MA upward":
                    indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward;
                    break;
                case "The Fats MA crosses the Slow MA downward":
                    indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward;
                    break;
                case "The Fats MA is higher than the Slow MA":
                    indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line;
                    break;
                case "The Fats MA is lower than the Slow MA":
                    indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line;
                    break;
                default:
                    break;
            }

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

Top