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

        /// <summary>
        /// Sets the default parameters for the designated slot type.
        /// </summary>
        /// <param name="slotType">The slot type.</param>
        public Previous_High_Low(SlotTypes slotType)
        {
            sIndicatorName  = "Previous High Low";
            parameters      = new IndicatorParam();
            component       = new IndicatorComp[] { };
            afSpecValue     = new float[] { };
            typeOfIndicator = TypeOfIndicator.Additional;
            bSeparatedChart = false;
            bIsCalculated   = false;

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

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

            // The ComboBox parameters.
            parameters.ListParam[0].Caption = "Logic";

            if (slotType == SlotTypes.Open)
                parameters.ListParam[0].ItemList = new string[]
                {
                    "Enter long at the previous High",
                    "Enter long at the previous Low"
                };

            else if (slotType == SlotTypes.OpenFilter)
                parameters.ListParam[0].ItemList = new string[]
                {
                    "The position opens above the previous High",
                    "The position opens below the previous High",
                    "The position opens above the previous Low",
                    "The position opens below the previous Low"
                };

            else if (slotType == SlotTypes.Close)
                parameters.ListParam[0].ItemList = new string[]
                {
                    "Exit long at the previous High",
                    "Exit long at the previous Low"
                };

            else if (slotType == SlotTypes.CloseFilter)
                parameters.ListParam[0].ItemList = new string[]
                {
                    "The bar closes above the previous High",
                    "The bar closes below the previous High",
                    "The bar closes above the previous Low",
                    "The bar closes below the previous Low"
                };

            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  = "Base price";
            parameters.ListParam[1].ItemList = new string[] { "High & Low" };
            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  = "Used price from the indicator";
        }

        /// <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;

            // Calculation
            float[] afUpBand = new float[Bars];
            float[] afDnBand = new float[Bars];

            int iFirstBar = 2;

            for (int iBar = iFirstBar; iBar < Bars; iBar++)
            {
                afUpBand[iBar] = High[iBar - 1];
                afDnBand[iBar] = Low[iBar - 1];
            }

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

            component[0] = new IndicatorComp();
            component[0].CompName = "Previous High";
            component[0].DataType = IndComponentType.IndicatorValue;
            component[0].ChartType = IndChartType.NoChart;
            component[0].FirstBar = iFirstBar;
            component[0].Value = afUpBand;

            component[1] = new IndicatorComp();
            component[1].CompName = "Previous Low";
            component[1].DataType = IndComponentType.IndicatorValue;
            component[1].ChartType = IndChartType.NoChart;
            component[1].FirstBar = iFirstBar;
            component[1].Value = afDnBand;

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

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

            // Sets the component's type.
            if (slotType == SlotTypes.Open)
            {
                component[2].DataType = IndComponentType.OpenLongPrice;
                component[2].CompName = "Long positions opening price";
                component[3].DataType = IndComponentType.OpenShortPrice;
                component[3].CompName = "Short positions opening price";
            }
            else if (slotType == SlotTypes.OpenFilter)
            {
                component[2].DataType = IndComponentType.AllowOpenLong;
                component[2].CompName = "Allows long positions opening";
                component[3].DataType = IndComponentType.AllowOpenShort;
                component[3].CompName = "Allows short positions opening";
            }
            else if (slotType == SlotTypes.Close)
            {
                component[2].DataType = IndComponentType.CloseLongPrice;
                component[2].CompName = "Long positions closing price";
                component[3].DataType = IndComponentType.CloseShortPrice;
                component[3].CompName = "Short positions closing price";
            }
            else if (slotType == SlotTypes.CloseFilter)
            {
                component[2].DataType = IndComponentType.ForceCloseLong;
                component[2].CompName = "Forces long positions closing";
                component[3].DataType = IndComponentType.ForceCloseShort;
                component[3].CompName = "Forces short positions closing";
            }

            if (slotType == SlotTypes.Open || slotType == SlotTypes.Close)
            {
                if (parameters.ListParam[0].Text == "Enter long at the previous High" ||
                    parameters.ListParam[0].Text == "Exit long at the previous High")
                {
                    component[2].Value = afUpBand;
                    component[3].Value = afDnBand;
                }
                else
                {
                    component[2].Value = afDnBand;
                    component[3].Value = afUpBand;
                }
            }
            else
            {
                switch (parameters.ListParam[0].Text)
                {
                    case "The bar opens below the previous High":
                        for (int iBar = iFirstBar; iBar < Bars; iBar++)
                        {
                            component[2].Value[iBar] = Open[iBar] < afUpBand[iBar] ? 1 : 0;
                            component[3].Value[iBar] = Open[iBar] > afDnBand[iBar] ? 1 : 0;
                        }
                        break;

                    case "The bar opens above the previous High":
                        for (int iBar = iFirstBar; iBar < Bars; iBar++)
                        {
                            component[2].Value[iBar] = Open[iBar] > afUpBand[iBar] ? 1 : 0;
                            component[3].Value[iBar] = Open[iBar] < afDnBand[iBar] ? 1 : 0;
                        }
                        break;

                    case "The bar opens below the previous Low":
                        for (int iBar = iFirstBar; iBar < Bars; iBar++)
                        {
                            component[2].Value[iBar] = Open[iBar] < afDnBand[iBar] ? 1 : 0;
                            component[3].Value[iBar] = Open[iBar] > afUpBand[iBar] ? 1 : 0;
                        }
                        break;

                    case "The bar opens above the previous Low":
                        for (int iBar = iFirstBar; iBar < Bars; iBar++)
                        {
                            component[2].Value[iBar] = Open[iBar] > afDnBand[iBar] ? 1 : 0;
                            component[3].Value[iBar] = Open[iBar] < afUpBand[iBar] ? 1 : 0;
                        }
                        break;

                    case "The position opens above the previous High":
                        component[0].PosPriceDependence = PositionPriceDependence.PriceBuyHigher;
                        component[1].PosPriceDependence = PositionPriceDependence.PriceSellLower;
                        component[2].DataType = IndComponentType.Other;
                        component[3].DataType = IndComponentType.Other;
                        component[2].ShowInDynInfo = false;
                        component[3].ShowInDynInfo = false;
                        break;

                    case "The position opens below the previous High":
                        component[0].PosPriceDependence = PositionPriceDependence.PriceBuyLower;
                        component[1].PosPriceDependence = PositionPriceDependence.PriceSellHigher;
                        component[2].DataType = IndComponentType.Other;
                        component[3].DataType = IndComponentType.Other;
                        component[2].ShowInDynInfo = false;
                        component[3].ShowInDynInfo = false;
                        break;

                    case "The position opens above the previous Low":
                        component[0].PosPriceDependence = PositionPriceDependence.PriceSellLower;
                        component[1].PosPriceDependence = PositionPriceDependence.PriceBuyHigher;
                        component[2].DataType = IndComponentType.Other;
                        component[3].DataType = IndComponentType.Other;
                        component[2].ShowInDynInfo = false;
                        component[3].ShowInDynInfo = false;
                        break;

                    case "The position opens below the previous Low":
                        component[0].PosPriceDependence = PositionPriceDependence.PriceSellHigher;
                        component[1].PosPriceDependence = PositionPriceDependence.PriceBuyLower;
                        component[2].DataType = IndComponentType.Other;
                        component[3].DataType = IndComponentType.Other;
                        component[2].ShowInDynInfo = false;
                        component[3].ShowInDynInfo = false;
                        break;

                    case "The bar closes below the previous High":
                        for (int iBar = iFirstBar; iBar < Bars; iBar++)
                        {
                            component[2].Value[iBar] = Close[iBar] < afUpBand[iBar] ? 1 : 0;
                            component[3].Value[iBar] = Close[iBar] > afDnBand[iBar] ? 1 : 0;
                        }
                        break;

                    case "The bar closes above the previous High":
                        for (int iBar = iFirstBar; iBar < Bars; iBar++)
                        {
                            component[2].Value[iBar] = Close[iBar] > afUpBand[iBar] ? 1 : 0;
                            component[3].Value[iBar] = Close[iBar] < afDnBand[iBar] ? 1 : 0;
                        }
                        break;

                    case "The bar closes below the previous Low":
                        for (int iBar = iFirstBar; iBar < Bars; iBar++)
                        {
                            component[2].Value[iBar] = Close[iBar] < afDnBand[iBar] ? 1 : 0;
                            component[3].Value[iBar] = Close[iBar] > afUpBand[iBar] ? 1 : 0;
                        }
                        break;

                    case "The bar closes above the previous Low":
                        for (int iBar = iFirstBar; iBar < Bars; iBar++)
                        {
                            component[2].Value[iBar] = Close[iBar] > afDnBand[iBar] ? 1 : 0;
                            component[3].Value[iBar] = Close[iBar] < afUpBand[iBar] ? 1 : 0;
                        }
                        break;

                    default:
                        break;
                }
            }

            bIsCalculated = true;

            return;
        }
    }
}

Top