Source » Money Flow Index - 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: Money Flow Index
    /// </summary>
    public class Money_Flow_Index : Indicator
    {
        /// <summary>
        /// The default constructor.
        /// </summary>
        public Money_Flow_Index()
        {
        }

        /// <summary>
        /// Sets the default parameters for the designated slot type.
        /// </summary>
        /// <param name="slotType">The slot type.</param>
        public Money_Flow_Index(SlotTypes slotType)
        {
            sIndicatorName  = "Money Flow Index";
            parameters      = new IndicatorParam();
            component       = new IndicatorComp[] { };
            fMinValue       = 0f;
            fMaxValue       = 100f;
            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 MFI rises",
                "The MFI falls",
                "The MFI is higher than the Level line",
                "The MFI is lower than the Level line",
                "The MFI crosses the Level line upward",
                "The MFI crosses the Level line downward",
                "The MFI changes its direction upward",
                "The MFI 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 MFI";

            // The NumericUpDown parameters.
            parameters.NumParam[0].Caption = "Level";
            parameters.NumParam[0].Value   = 50 ;
            parameters.NumParam[0].Min     = 0  ;
            parameters.NumParam[0].Max     = 100;
            parameters.NumParam[0].Enabled = true;
            parameters.NumParam[0].ToolTip = "A critical level (for the appropriate logic)";

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

            int iFirstBar = 14;
            float[] afMF  = new float[Bars];
            float[] afMFI = new float[Bars];

            for (int iBar = 1; iBar < Bars; iBar++)
            {
                float fAVG = (High[iBar] + Low[iBar] + Close[iBar]) / 3;
                float fAVG1 = (High[iBar - 1] + Low[iBar - 1] + Close[iBar - 1]) / 3;
                if (fAVG > fAVG1)
                    afMF[iBar] = afMF[iBar - 1] + fAVG * Volume[iBar] / 1000f;
                else if (fAVG < fAVG1)
                    afMF[iBar] = afMF[iBar - 1] - fAVG * Volume[iBar] / 1000f;
                else
                    afMF[iBar] = afMF[iBar - 1];
            }

            for (int iBar = 12; iBar < Bars; iBar++)
            {
                float fPMF = 0f;
                float fNMF = 0f;
                for (int index = 0; index < 10; index++)
                {
                    if (afMF[iBar - index] > afMF[iBar - index - 1])
                        fPMF += afMF[iBar - index] - afMF[iBar - index - 1];
                    if (afMF[iBar - index] < afMF[iBar - index - 1])
                        fNMF += afMF[iBar - index - 1] - afMF[iBar - index];
                }
                if (fNMF == 0f)
                    afMFI[iBar] = 100.0f;
                else
                    afMFI[iBar] = 100.0f - (100.0f / (1.0f + (fPMF / fNMF)));
            }
            
            // Saving the components
            component = new IndicatorComp[3];

            component[0] = new IndicatorComp();
            component[0].CompName   = "Money Flow Index";
            component[0].DataType   = IndComponentType.IndicatorValue;
            component[0].ChartType  = IndChartType.Line;
            component[0].ChartColor = Color.Blue;
            component[0].FirstBar   = iFirstBar;
            component[0].Value      = afMFI;

            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 MFI rises":
                    indLogic = IndicatorLogic.The_indicator_rises;
                    afSpecValue = new float[1] { 50f };
                    break;

                case "The MFI falls":
                    indLogic = IndicatorLogic.The_indicator_falls;
                    afSpecValue = new float[1] { 50f };
                    break;

                case "The MFI is higher than the Level line":
                    indLogic = IndicatorLogic.The_indicator_is_higher_than_the_level_line;
                    afSpecValue = new float[2] { fLevel, 100 - fLevel };
                    break;

                case "The MFI is lower than the Level line":
                    indLogic = IndicatorLogic.The_indicator_is_lower_than_the_level_line;
                    afSpecValue = new float[2] { fLevel, 100 - fLevel };
                    break;

                case "The MFI crosses the Level line upward":
                    indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_upward;
                    afSpecValue = new float[2] { fLevel, 100 - fLevel };
                    break;

                case "The MFI crosses the Level line downward":
                    indLogic = IndicatorLogic.The_indicator_crosses_the_level_line_downward;
                    afSpecValue = new float[2] { fLevel, 100 - fLevel };
                    break;

                case "The MFI changes its direction upward":
                    indLogic = IndicatorLogic.The_indicator_changes_its_direction_upward;
                    afSpecValue = new float[1] { 50f };
                    break;

                case "The MFI changes its direction downward":
                    indLogic = IndicatorLogic.The_indicator_changes_its_direction_downward;
                    afSpecValue = new float[1] { 50f };
                    break;

                default:
                    break;
            }

            bIsCalculated = OscillatorLogic(iFirstBar, iPrvs, afMFI, fLevel, 100 - fLevel, ref component[1], ref component[2], indLogic);
        }
    }
}

Top