Source » Accelerator Oscillator - source code

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

using System;
using System.Drawing;

namespace Forex_Strategy_Builder
{
    /// <summary>
    /// Indicator: Accelerator Oscillator
    /// </summary>
    public class Accelerator_Oscillator : Indicator
    {
        /// <summary>
        /// The default constructor.
        /// </summary>
        public Accelerator_Oscillator()
        {
        }

        /// <summary>
        /// Sets the default parameters for the designated slot type.
        /// </summary>
        /// <param name="slotType">The slot type.</param>
        public Accelerator_Oscillator(SlotTypes slotType)
        {
            sIndicatorName  = "Accelerator Oscillator";
            parameters      = new IndicatorParam();
            component       = new IndicatorComp[] { };
            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 AC rises",
                "The AC falls",
                "The AC is higher than the Level line",
                "The AC is lower than the Level line",
                "The AC crosses the Level line upward",
                "The AC crosses the Level line downward",
                "The AC changes its direction upward",
                "The AC 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 indicator";

            parameters.ListParam[1].Caption  = "Smoothing method";
            parameters.ListParam[1].ItemList = Enum.GetNames(typeof(MAMethod));
            parameters.ListParam[1].Index    = 0;
            parameters.ListParam[1].Text     = parameters.ListParam[1].ItemList[parameters.ListParam[1].Index];
            parameters.ListParam[1].Enabled  = true;
            parameters.ListParam[1].ToolTip  = "The method of Moving Average used for the calculations";

            parameters.ListParam[2].Caption  = "Base price";
            parameters.ListParam[2].ItemList = Enum.GetNames(typeof(BasePrice));
            parameters.ListParam[2].Index    = 4;
            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 = "Slow MA period";
            parameters.NumParam[0].Value   = 34;
            parameters.NumParam[0].Min     = 1;
            parameters.NumParam[0].Max     = 200;
            parameters.NumParam[0].Enabled = true;
            parameters.NumParam[0].ToolTip = "The period of slow Moving Average";
 
            parameters.NumParam[1].Caption = "Fast MA period";
            parameters.NumParam[1].Value   = 5;
            parameters.NumParam[1].Min     = 1;
            parameters.NumParam[1].Max     = 200;
            parameters.NumParam[1].Enabled = true;
            parameters.NumParam[1].ToolTip = "The period of fast Moving Average";

            parameters.NumParam[2].Caption = "Forming MA period";
            parameters.NumParam[2].Value   = 5;
            parameters.NumParam[2].Min     = 1;
            parameters.NumParam[2].Max     = 200;
            parameters.NumParam[2].Enabled = true;
            parameters.NumParam[2].ToolTip = "The period of additional Moving Average";

            parameters.NumParam[3].Caption = "Level";
            parameters.NumParam[3].Value   = 0;
            parameters.NumParam[3].Min     = -10;
            parameters.NumParam[3].Max     = 10;
            parameters.NumParam[3].Point   = 4;
            parameters.NumParam[3].Enabled = true;
            parameters.NumParam[3].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
            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       nSlow     = (int)parameters.NumParam[0].Value;
            int       nFast     = (int)parameters.NumParam[1].Value;
            int       nAccel    = (int)parameters.NumParam[2].Value;
            float     fLevel    =      parameters.NumParam[3].Value;
            int       iPrvs     =      parameters.CheckParam[0].Checked ? 1 : 0;

            // Calculation
            int     iFirstBar = nSlow + nAccel + 2;
            float[] smaSlow   = MovingAverage(nSlow, 0, maMethod, Price(basePrice));
            float[] smaFast   = MovingAverage(nFast, 0, maMethod, Price(basePrice));
            float[] afAO      = new float[Bars];
            float[] afAC      = new float[Bars];

            for (int iBar = nSlow - 1; iBar < Bars; iBar++)
            {
                afAO[iBar] = smaFast[iBar] - smaSlow[iBar];
            }

            float[] afAOMA = MovingAverage(nAccel, 0, maMethod, afAO);

            for (int iBar = iFirstBar; iBar < Bars; iBar++)
            {
                afAC[iBar] = afAO[iBar] - afAOMA[iBar];
            }

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

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

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

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

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

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

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

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

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

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

                default:
                    break;
            }

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

            return;
        }
    }
}

Top