Source » Random 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-07-30

using System;
using System.Drawing;

namespace Forex_Strategy_Builder
{
    /// <summary>
    /// Indicator: Random Filter
    /// </summary>
    public class Random_Filter : Indicator
    {
        /// <summary>
        /// The default constructor.
        /// </summary>
        public Random_Filter()
        {
        }

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

            typeOfIndicator = TypeOfIndicator.Additional;

            // The indicator name.
            parameters.IndicatorName = sIndicatorName;

            // The slot type.
            parameters.SlotType = slotType;

            // The NumericUpDown parameters.
            if (slotType == SlotTypes.OpenFilter)
            {
                parameters.NumParam[0].Caption = "Probability";
                parameters.NumParam[0].Value   = 80;
                parameters.NumParam[0].Min     = 0;
                parameters.NumParam[0].Max     = 100;
                parameters.NumParam[0].Enabled = true;
                parameters.NumParam[0].ToolTip = "The probability to allow a new position opening in %";

                parameters.NumParam[1].Caption = "Long vs Short";
                parameters.NumParam[1].Value   = 50;
                parameters.NumParam[1].Min     = 0;
                parameters.NumParam[1].Max     = 100;
                parameters.NumParam[1].Enabled = true;
                parameters.NumParam[1].ToolTip = "The probability to open Long vs. Short in %";
            }
            else
            {
                parameters.NumParam[0].Caption = "Probability";
                parameters.NumParam[0].Value   = 20;
                parameters.NumParam[0].Min     = 0;
                parameters.NumParam[0].Max     = 100;
                parameters.NumParam[0].Enabled = true;
                parameters.NumParam[0].ToolTip = "The probability to close the position in %";
            }
        }

        /// <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 iProbability = (int)parameters.NumParam[0].Value;
            int iLongShort = (int)parameters.NumParam[1].Value;

            Random randObj = new Random();

            // Saving the components
            if (slotType == SlotTypes.OpenFilter)
            {
                component = new IndicatorComp[2];

                component[0] = new IndicatorComp();
                component[0].ChartType = IndChartType.NoChart;
                component[0].FirstBar = 0;
                component[0].Value = new float[Bars];
                component[0].DataType = IndComponentType.AllowOpenLong;
                component[0].CompName = "Allows long positions opening";


                component[1] = new IndicatorComp();
                component[1].ChartType = IndChartType.NoChart;
                component[1].FirstBar = 0;
                component[1].Value = new float[Bars];
                component[1].DataType = IndComponentType.AllowOpenShort;
                component[1].CompName = "Allows short positions opening";


                // Calculation of the logic.
                for (int i = 0; i < Bars; i++)
                {
                    if (randObj.Next(100) < iProbability)
                    {
                        int iRandNumb = randObj.Next(100);
                        component[0].Value[i] = (iRandNumb <= iLongShort) ? 1F : 0F;
                        component[1].Value[i] = (iRandNumb >  iLongShort) ? 1F : 0F;
                    }
                    else
                    {
                        component[0].Value[i] = 0F;
                        component[1].Value[i] = 0F;
                    }
                }
            }
            else
            {
                component = new IndicatorComp[1];

                component[0] = new IndicatorComp();
                component[0].ChartType = IndChartType.NoChart;
                component[0].FirstBar  = 0;
                component[0].Value     = new float[Bars];
                component[0].DataType  = IndComponentType.ForceClose;
                component[0].CompName  = "Force Close";

                for (int i = 0; i < Bars; i++)
                {
                    component[0].Value[i] = (randObj.Next(100) < iProbability) ? 1F : 0F;
                }
            }

            bIsCalculated = true;
        }
    }
}

Top