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

        /// <summary>
        /// Sets the default parameters for the designated slot type.
        /// </summary>
        /// <param name="slotType">The slot type.</param>
        public Parabolic_SAR(SlotTypes slotType)
        {
            sIndicatorName  = "Parabolic SAR";
            parameters      = new IndicatorParam();
            component       = new IndicatorComp[] { };
            afSpecValue     = new float[] { };
            bSeparatedChart = false;
            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[] { "PSAR determines the position's direction" };
            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";

            // The NumericUpDown parameters.
            parameters.NumParam[0].Caption = "Starting AF";
            parameters.NumParam[0].Value   = 0.02f;
            parameters.NumParam[0].Min     = 0.00f;
            parameters.NumParam[0].Max     = 5.00f;
            parameters.NumParam[0].Point   = 2;
            parameters.NumParam[0].Enabled = true;
            parameters.NumParam[0].ToolTip = "The starting value of Acceleration Factor";

            parameters.NumParam[1].Caption = "Increment";
            parameters.NumParam[1].Value   = 0.02f;
            parameters.NumParam[1].Min     = 0.01f;
            parameters.NumParam[1].Max     = 5.00f;
            parameters.NumParam[1].Point   = 2;
            parameters.NumParam[1].Enabled = true;
            parameters.NumParam[1].ToolTip = "Increment value";

            parameters.NumParam[2].Caption = "Maximum AF";
            parameters.NumParam[2].Value   = 2.0f;
            parameters.NumParam[2].Min     = 0.01f;
            parameters.NumParam[2].Max     = 9.00f;
            parameters.NumParam[2].Point   = 2;
            parameters.NumParam[2].Enabled = true;
            parameters.NumParam[2].ToolTip = "The maximum value of the Acceleration Factor";
        }

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

            float flAFMin = parameters.NumParam[0].Value;
            float flAFInc = parameters.NumParam[1].Value;
            float flAFMax = parameters.NumParam[2].Value;

            // Reading the parameters
            int intDirNew;
            float flAF;
            float flPExtr;
            float flPSARNew = 0f;
            int[]   aiDir  = new int  [Bars];
            float[] afPSAR = new float[Bars];

            //----    Calculating the initial values
            afPSAR[0] = 0f;
            flAF      = flAFMin;
            intDirNew = 0;
            if (Close[1] > Open[0])
            {
                aiDir[0]  = 1;
                aiDir[1]  = 1;
                flPExtr   = Math.Max(High[0], High[1]);
                afPSAR[1] = Math.Min(Low[0], Low[1]);
            }
            else
            {
                aiDir[0]  = -1;
                aiDir[1]  = -1;
                flPExtr   = Math.Min(Low[0], Low[1]);
                afPSAR[1] = Math.Max(High[0], High[1]);
            }

            for (int iBar = 2; iBar < Bars; iBar++)
            {

                //----    PSAR for the current period
                if (intDirNew != 0)
                {    
                    // The direction was changed during the last period
                    aiDir[iBar]  = intDirNew;
                    intDirNew    = 0;
                    afPSAR[iBar] = flPSARNew + flAF * (flPExtr - flPSARNew);
                }
                else
                {
                    aiDir[iBar]  = aiDir[iBar - 1];
                    afPSAR[iBar] = afPSAR[iBar - 1] + flAF * (flPExtr - afPSAR[iBar - 1]);
                }

                // PSAR has to be out of the previous two bars limits
                if (aiDir[iBar] > 0 && afPSAR[iBar] > Math.Min(Low[iBar - 1], Low[iBar - 2]))
                    afPSAR[iBar] = Math.Min(Low[iBar - 1], Low[iBar - 2]);
                else if (aiDir[iBar] < 0 && afPSAR[iBar] < Math.Max(High[iBar - 1], High[iBar - 2]))
                    afPSAR[iBar] = Math.Max(High[iBar - 1], High[iBar - 2]);

                //----    PSAR for the next period

                // Определяне на нови стойности на flPExtr и flAF
                // ако има нова екстрмна цена по посока на PSAR.
                if (aiDir[iBar] > 0 && High[iBar] > flPExtr)
                {
                    flPExtr = High[iBar];
                    flAF    = Math.Min(flAF + flAFInc, flAFMax);
                }

                if (aiDir[iBar] < 0 && Low[iBar] < flPExtr)
                {
                    flPExtr = Low[iBar];
                    flAF    = Math.Min(flAF + flAFInc, flAFMax);
                }

                // Проверяваме, дали цената по време на този период достига PSAR.
                if (Low[iBar] <= afPSAR[iBar] && afPSAR[iBar] <= High[iBar])
                {
                    intDirNew = -aiDir[iBar];
                    flPSARNew = flPExtr;
                    flAF      = flAFMin;
                    if (intDirNew > 0)
                        flPExtr = High[iBar];
                    else
                        flPExtr = Low[iBar];
                }

            }
            int iFirstBar = 8;

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

            component[0] = new IndicatorComp();
            component[0].CompName = "PSAR value";
            if (slotType == SlotTypes.Close)
                component[0].DataType = IndComponentType.ClosePrice;
            else
                component[0].DataType = IndComponentType.IndicatorValue;
            component[0].ChartType  = IndChartType.Dot;
            component[0].ChartColor = Color.Purple;
            component[0].FirstBar   = iFirstBar;
            component[0].PosPriceDependence = PositionPriceDependence.BuyHigherSellLower;
            component[0].Value      = afPSAR;

            bIsCalculated = true;
        }
    }
}

Top