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

        /// <summary>
        /// Sets the default parameters for the designated slot type.
        /// </summary>
        /// <param name="slotType">The slot type.</param>
        public Alligator(SlotTypes slotType)
        {
            sIndicatorName  = "Alligator";
            parameters      = new IndicatorParam();
            bSeparatedChart = false;
            component       = new IndicatorComp[] { };
            afSpecValue     = new float[] { };
            fMinValue       = 0;
            fMaxValue       = 0;
            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 Lips rises",
                "The Lips falls",
                "The Teeth rises",
                "The Teeth falls",
                "The Jaws rises",
                "The Jaws falls",
                "The Lips crosses the Teeth upward",
                "The Lips crosses the Teeth downward",
                "The Lips crosses the Jaws upward",
                "The Lips crosses the Jaws downward",
                "The Teeth crosses the Jaws upward",
                "The Teeth crosses the Jaws 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    = 3;
            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 = "Jaws 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 Moving Average period";

            parameters.NumParam[1].Caption = "Jaws shift";
            parameters.NumParam[1].Value   = 8;
            parameters.NumParam[1].Min     = 0;
            parameters.NumParam[1].Max     = 200;
            parameters.NumParam[1].Enabled = true;
            parameters.NumParam[1].ToolTip = "How many bars to shift with";

            parameters.NumParam[2].Caption = "Teeth period";
            parameters.NumParam[2].Value   = 8;
            parameters.NumParam[2].Min     = 1;
            parameters.NumParam[2].Max     = 200;
            parameters.NumParam[2].Enabled = true;
            parameters.NumParam[2].ToolTip = "The Moving Average period";

            parameters.NumParam[3].Caption = "Teeth shift";
            parameters.NumParam[3].Value   = 5;
            parameters.NumParam[3].Min     = 0;
            parameters.NumParam[3].Max     = 200;
            parameters.NumParam[3].Enabled = true;
            parameters.NumParam[3].ToolTip = "How many bars to shift with";

            parameters.NumParam[4].Caption = "Lips period";
            parameters.NumParam[4].Value   = 5;
            parameters.NumParam[4].Min     = 1;
            parameters.NumParam[4].Max     = 200;
            parameters.NumParam[4].Enabled = true;
            parameters.NumParam[4].ToolTip = "The Moving Average period";

            parameters.NumParam[5].Caption = "Lips shift";
            parameters.NumParam[5].Value   = 3;
            parameters.NumParam[5].Min     = 0;
            parameters.NumParam[5].Max     = 200;
            parameters.NumParam[5].Enabled = true;
            parameters.NumParam[5].ToolTip = "How many bars to shift with";

            // 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)
        {
            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 iNJaws  = (int)parameters.NumParam[0].Value;
            int iSJaws  = (int)parameters.NumParam[1].Value;
            int iNTeeth = (int)parameters.NumParam[2].Value;
            int iSTeeth = (int)parameters.NumParam[3].Value;
            int iNLips  = (int)parameters.NumParam[4].Value;
            int iSLips  = (int)parameters.NumParam[5].Value;
            int iPrvs   =      parameters.CheckParam[0].Checked ? 1 : 0;

            int iFirstBar = iNJaws + iSJaws + 1;

            // Calculation
            float[] afJaws  = MovingAverage(iNJaws , iSJaws , maMethod, Price(basePrice));
            float[] afTeeth = MovingAverage(iNTeeth, iSTeeth, maMethod, Price(basePrice));
            float[] afLips  = MovingAverage(iNLips , iSLips , maMethod, Price(basePrice));

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

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

            component[1]            = new IndicatorComp();
            component[1].CompName   = "Teeth";
            component[1].DataType   = IndComponentType.IndicatorValue;
            component[1].ChartType  = IndChartType.Line;
            component[1].ChartColor = Color.Red;
            component[1].FirstBar   = iFirstBar;
            component[1].Value      = afTeeth;

            component[2]            = new IndicatorComp();
            component[2].CompName   = "Lips";
            component[2].DataType   = IndComponentType.IndicatorValue;
            component[2].ChartType  = IndChartType.Line;
            component[2].ChartColor = Color.Lime;
            component[2].FirstBar   = iFirstBar;
            component[2].Value      = afLips;

            component[3]           = new IndicatorComp();
            component[3].ChartType = IndChartType.NoChart;
            component[3].FirstBar  = iFirstBar;
            component[3].Value     = new float[Bars];

            component[4]           = new IndicatorComp();
            component[4].ChartType = IndChartType.NoChart;
            component[4].FirstBar  = iFirstBar;
            component[4].Value     = new float[Bars];

            // Sets the component's type.
            if (slotType == SlotTypes.OpenFilter)
            {
                component[3].DataType = IndComponentType.AllowOpenLong;
                component[3].CompName = "Allows long positions opening";
                component[4].DataType = IndComponentType.AllowOpenShort;
                component[4].CompName = "Allows short positions opening";
            }
            else if (slotType == SlotTypes.CloseFilter)
            {
                component[3].DataType = IndComponentType.ForceCloseLong;
                component[3].CompName = "Forces long positions closing";
                component[4].DataType = IndComponentType.ForceCloseShort;
                component[4].CompName = "Forces short positions closing";
            }

            switch (parameters.ListParam[0].Text)
            {
                case "The Jaws rises":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[3].Value[iBar] = afJaws[iBar - iPrvs - 1] < afJaws[iBar - iPrvs] ? 1 : 0;
                        component[4].Value[iBar] = afJaws[iBar - iPrvs - 1] > afJaws[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                case "The Jaws falls":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[3].Value[iBar] = afJaws[iBar - iPrvs - 1] > afJaws[iBar - iPrvs] ? 1 : 0;
                        component[4].Value[iBar] = afJaws[iBar - iPrvs - 1] < afJaws[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                case "The Teeth rises":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[3].Value[iBar] = afTeeth[iBar - iPrvs - 1] < afTeeth[iBar - iPrvs] ? 1 : 0;
                        component[4].Value[iBar] = afTeeth[iBar - iPrvs - 1] > afTeeth[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                case "The Teeth falls":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[3].Value[iBar] = afTeeth[iBar - iPrvs - 1] > afTeeth[iBar - iPrvs] ? 1 : 0;
                        component[4].Value[iBar] = afTeeth[iBar - iPrvs - 1] < afTeeth[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                case "The Lips rises":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[3].Value[iBar] = afLips[iBar - iPrvs - 1] < afLips[iBar - iPrvs] ? 1 : 0;
                        component[4].Value[iBar] = afLips[iBar - iPrvs - 1] > afLips[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                case "The Lips falls":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[3].Value[iBar] = afLips[iBar - iPrvs - 1] > afLips[iBar - iPrvs] ? 1 : 0;
                        component[4].Value[iBar] = afLips[iBar - iPrvs - 1] < afLips[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                case "The Lips crosses the Teeth upward":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[3].Value[iBar] = afLips[iBar - iPrvs - 1] < afTeeth[iBar - iPrvs - 1] && afLips[iBar - iPrvs] > afTeeth[iBar - iPrvs] ? 1 : 0;
                        component[4].Value[iBar] = afLips[iBar - iPrvs - 1] > afTeeth[iBar - iPrvs - 1] && afLips[iBar - iPrvs] < afTeeth[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                case "The Lips crosses the Teeth downward":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[3].Value[iBar] = afLips[iBar - iPrvs - 1] > afTeeth[iBar - iPrvs - 1] && afLips[iBar - iPrvs] < afTeeth[iBar - iPrvs] ? 1 : 0;
                        component[4].Value[iBar] = afLips[iBar - iPrvs - 1] < afTeeth[iBar - iPrvs - 1] && afLips[iBar - iPrvs] > afTeeth[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                case "The Lips crosses the Jaws upward":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[3].Value[iBar] = afLips[iBar - iPrvs - 1] < afJaws[iBar - iPrvs - 1] && afLips[iBar - iPrvs] > afJaws[iBar - iPrvs] ? 1 : 0;
                        component[4].Value[iBar] = afLips[iBar - iPrvs - 1] > afJaws[iBar - iPrvs - 1] && afLips[iBar - iPrvs] < afJaws[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                case "The Lips crosses the Jaws downward":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[3].Value[iBar] = afLips[iBar - iPrvs - 1] > afJaws[iBar - iPrvs - 1] && afLips[iBar - iPrvs] < afJaws[iBar - iPrvs] ? 1 : 0;
                        component[4].Value[iBar] = afLips[iBar - iPrvs - 1] < afJaws[iBar - iPrvs - 1] && afLips[iBar - iPrvs] > afJaws[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                case "The Teeth crosses the Jaws upward":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[3].Value[iBar] = afTeeth[iBar - iPrvs - 1] < afJaws[iBar - iPrvs - 1] && afTeeth[iBar - iPrvs] > afJaws[iBar - iPrvs] ? 1 : 0;
                        component[4].Value[iBar] = afTeeth[iBar - iPrvs - 1] > afJaws[iBar - iPrvs - 1] && afTeeth[iBar - iPrvs] < afJaws[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                case "The Teeth crosses the Jaws downward":
                    for (int iBar = iFirstBar; iBar < Bars; iBar++)
                    {
                        component[3].Value[iBar] = afTeeth[iBar - iPrvs - 1] > afJaws[iBar - iPrvs - 1] && afTeeth[iBar - iPrvs] < afJaws[iBar - iPrvs] ? 1 : 0;
                        component[4].Value[iBar] = afTeeth[iBar - iPrvs - 1] < afJaws[iBar - iPrvs - 1] && afTeeth[iBar - iPrvs] > afJaws[iBar - iPrvs] ? 1 : 0;
                    }
                    break;

                default:
                    break;
            }

            bIsCalculated = true;

            return;
        }
    }
}

Top