Source » Date Filter - 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-01-17

using System;
using System.Drawing;

namespace Forex_Strategy_Builder
{
    /// <summary>
    /// indicator: Date Filter
    /// </summary>
    public class Date_Filter : Indicator
    {
        /// <summary>
        /// The default constructor.
        /// </summary>
        public Date_Filter()
        {
        }

        /// <summary>
        /// Sets the default parameters for the designated slot type.
        /// </summary>
        /// <param name="slotType">The slot type.</param>
        public Date_Filter(SlotTypes slotType)
        {
            sIndicatorName  = "Date Filter";
            parameters      = new IndicatorParam();
            component       = new IndicatorComp[] { };
            bIsCalculated   = false;

            typeOfIndicator = TypeOfIndicator.DateTime;

            // 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[]
            {
                "Do not use data before",
                "Do not use data after"
            };
            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 the date filter";

            // The NumericUpDown parameters.
            parameters.NumParam[0].Caption = "Year";
            parameters.NumParam[0].Value   = 2000;
            parameters.NumParam[0].Min     = 1900;
            parameters.NumParam[0].Max     = 2100;
            parameters.NumParam[0].Enabled = true;
            parameters.NumParam[0].ToolTip = "The year";

            parameters.NumParam[1].Caption = "Month";
            parameters.NumParam[1].Value   = 1;
            parameters.NumParam[1].Min     = 1;
            parameters.NumParam[1].Max     = 12;
            parameters.NumParam[1].Enabled = true;
            parameters.NumParam[1].ToolTip = "The month";
        }

        /// <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 iYear  = (int)parameters.NumParam[0].Value;
            int iMonth = (int)parameters.NumParam[1].Value;
            DateTime dtKeyDate = new DateTime(iYear, iMonth, 1);

            // Calculation
            int iFirstBar = 0;
            float[] afBars = new float[Bars];

            // Calculation of the logic.
            switch (parameters.ListParam[0].Text)
            {
                case "Do not use data after":

                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                        if (Date[iBar] < dtKeyDate)
                            afBars[iBar] = 1;

                    break;

                case "Do not use data before":

                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        if (Date[iBar] >= dtKeyDate)
                        {
                            iFirstBar = iBar;
                            break;
                        }
                    }

                    iFirstBar = (int)Math.Min(iFirstBar, Bars - 300);

                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        afBars[iBar] = 1;
                    }

                    break;

                default:
                    break;
            }

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

            component[0] = new IndicatorComp();
            component[0].CompName      = "Allow Open Long";
            component[0].DataType      = IndComponentType.AllowOpenLong;
            component[0].ChartType     = IndChartType.NoChart;
            component[0].ShowInDynInfo = false;
            component[0].FirstBar      = iFirstBar;
            component[0].Value         = afBars;

            component[1] = new IndicatorComp();
            component[1].CompName      = "Allow Open Short";
            component[1].DataType      = IndComponentType.AllowOpenShort;
            component[1].ChartType     = IndChartType.NoChart;
            component[1].ShowInDynInfo = false;
            component[1].FirstBar      = iFirstBar;
            component[1].Value         = afBars;

            bIsCalculated = true;
        }
    }
}

Top