ParabolicSAR Trend Line

Author: Copyright © 2020, Vladimir Karputov
0 Views
0 Downloads
0 Favorites
ParabolicSAR Trend Line
ÿþ//+------------------------------------------------------------------+

//|                                      ParabolicSAR Trend Line.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2020, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "1.000"

//--- indicator settings

#property indicator_chart_window

#property indicator_buffers 5

#property indicator_plots   3



#property indicator_label1  "SAR"         // Name of a plot for the Data Window 

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrDodgerBlue



#property indicator_label2  "SAR High"    // Name of a plot for the Data Window 

#property indicator_type2   DRAW_LINE     // Type of plotting is line 

#property indicator_color2  clrRed        // Line color 

#property indicator_style2  STYLE_SOLID   // Line style 

#property indicator_width2  2             // Line Width



#property indicator_label3  "SAR Low"     // Name of a plot for the Data Window 

#property indicator_type3   DRAW_LINE     // Type of plotting is line 

#property indicator_color3  clrBlue       // Line color 

#property indicator_style3  STYLE_SOLID   // Line style 

#property indicator_width3  2             // Line Width 

//--- SAR parametrs

input double         Inp_SAR_step      = 0.02;     // SAR: price increment step - acceleration factor

input double         Inp_SAR_maximum   = 0.2;      // SAR: maximum value of step

//---- buffers

double   ExtSARBuffer[];

double   ExtTrendHighBuffer[];

double   ExtTrendLowBuffer[];

double   ExtEPBuffer[];

double   ExtAFBuffer[];

//--- global variables

int      ExtLastRevPos;

bool     ExtDirectionLong;

double   ExtSarStep;

double   ExtSarMaximum;

double   ExtTrendMax;

double   ExtTrendMin;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

void OnInit()

  {

//--- checking input data

   if(Inp_SAR_step<0.0)

     {

      ExtSarStep=0.02;

      Print("Input parametr Inp_SAR_step has incorrect value. Indicator will use value",

            ExtSarStep,"for calculations.");

     }

   else

      ExtSarStep=Inp_SAR_step;

   if(Inp_SAR_maximum<0.0)

     {

      ExtSarMaximum=0.2;

      Print("Input parametr Inp_SAR_maximum has incorrect value. Indicator will use value",

            ExtSarMaximum,"for calculations.");

     }

   else

      ExtSarMaximum=Inp_SAR_maximum;

//---- indicator buffers

   SetIndexBuffer(0,ExtSARBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtTrendHighBuffer,INDICATOR_DATA);

   SetIndexBuffer(2,ExtTrendLowBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,ExtEPBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,ExtAFBuffer,INDICATOR_CALCULATIONS);

//--- set arrow symbol

   PlotIndexSetInteger(0,PLOT_ARROW,159);

//--- set indicator digits

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

//--- set as an empty value 0.0

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);

   PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0);

//--- name for Dindicator subwindow label

   IndicatorSetString(INDICATOR_SHORTNAME,"SAR("+DoubleToString(ExtSarStep,2)+","+DoubleToString(ExtSarMaximum,2)+")");

//--- set global variables

   ExtLastRevPos=0;

   ExtDirectionLong=false;

//----

  }

//+------------------------------------------------------------------+

//| Custom indicator iteration function                              |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//--- check for minimum rates count

   if(rates_total<3)

      return(0);

//--- detect current position

   int pos=prev_calculated-1;

//--- correct position

   if(pos<1)

     {

      //--- first pass, set as SHORT

      pos=1;

      ExtAFBuffer[0]=ExtSarStep;

      ExtAFBuffer[1]=ExtSarStep;

      ExtSARBuffer[0]=high[0];

      ExtLastRevPos=0;

      ExtDirectionLong=false;

      ExtSARBuffer[1]=GetHigh(pos,ExtLastRevPos,high);

      ExtEPBuffer[0]=low[pos];

      ExtEPBuffer[1]=low[pos];

      ExtTrendHighBuffer[0]=0.0;

      ExtTrendHighBuffer[1]=0.0;

      ExtTrendLowBuffer[0]=0.0;

      ExtTrendLowBuffer[1]=0.0;

     }

//---main cycle

   for(int i=pos; i<rates_total-1 && !IsStopped(); i++)

     {

      //--- check for reverse

      if(ExtDirectionLong)

        {

         if(ExtSARBuffer[i]>low[i])

           {

            //--- switch to SHORT

            ExtTrendMax=0.0;

            ExtTrendMin=low[i];

            ExtTrendHighBuffer[i]=ExtTrendMax;

            ExtTrendLowBuffer[i]=ExtTrendMin;



            ExtDirectionLong=false;

            ExtSARBuffer[i]=GetHigh(i,ExtLastRevPos,high);

            ExtEPBuffer[i]=low[i];

            ExtLastRevPos=i;

            ExtAFBuffer[i]=ExtSarStep;

           }

        }

      else

        {

         if(ExtSARBuffer[i]<high[i])

           {

            //--- switch to LONG

            ExtTrendMax=high[i];

            ExtTrendMin=0.0;

            ExtTrendHighBuffer[i]=ExtTrendMax;

            ExtTrendLowBuffer[i]=ExtTrendMin;



            ExtDirectionLong=true;

            ExtSARBuffer[i]=GetLow(i,ExtLastRevPos,low);

            ExtEPBuffer[i]=high[i];

            ExtLastRevPos=i;

            ExtAFBuffer[i]=ExtSarStep;

           }

        }

      //--- continue calculations

      if(ExtDirectionLong)

        {

         //--- check for new High

         if(high[i]>ExtEPBuffer[i-1] && i!=ExtLastRevPos)

           {

            ExtEPBuffer[i]=high[i];

            ExtAFBuffer[i]=ExtAFBuffer[i-1]+ExtSarStep;

            if(ExtAFBuffer[i]>ExtSarMaximum)

               ExtAFBuffer[i]=ExtSarMaximum;

           }

         else

           {

            //--- when we haven't reversed

            if(i!=ExtLastRevPos)

              {

               ExtAFBuffer[i]=ExtAFBuffer[i-1];

               ExtEPBuffer[i]=ExtEPBuffer[i-1];

              }

           }

         //--- calculate SAR for tomorrow

         ExtSARBuffer[i+1]=ExtSARBuffer[i]+ExtAFBuffer[i]*(ExtEPBuffer[i]-ExtSARBuffer[i]);

         if(high[i]>ExtTrendMax)

            ExtTrendMax=high[i];



         ExtTrendHighBuffer[i]=ExtTrendMax;

         ExtTrendHighBuffer[i+1]=ExtTrendMax;

         ExtTrendLowBuffer[i]=ExtTrendMin;

         ExtTrendLowBuffer[i+1]=ExtTrendMin;

         //--- check for SAR

         if(ExtSARBuffer[i+1]>low[i] || ExtSARBuffer[i+1]>low[i-1])

            ExtSARBuffer[i+1]=MathMin(low[i],low[i-1]);

        }

      else

        {

         //--- check for new Low

         if(low[i]<ExtEPBuffer[i-1] && i!=ExtLastRevPos)

           {

            ExtEPBuffer[i]=low[i];

            ExtAFBuffer[i]=ExtAFBuffer[i-1]+ExtSarStep;

            if(ExtAFBuffer[i]>ExtSarMaximum)

               ExtAFBuffer[i]=ExtSarMaximum;

           }

         else

           {

            //--- when we haven't reversed

            if(i!=ExtLastRevPos)

              {

               ExtAFBuffer[i]=ExtAFBuffer[i-1];

               ExtEPBuffer[i]=ExtEPBuffer[i-1];

              }

           }

         //--- calculate SAR for tomorrow

         ExtSARBuffer[i+1]=ExtSARBuffer[i]+ExtAFBuffer[i]*(ExtEPBuffer[i]-ExtSARBuffer[i]);

         if(low[i]<ExtTrendMin)

            ExtTrendMin=low[i];



         ExtTrendHighBuffer[i]=ExtTrendMax;

         ExtTrendHighBuffer[i+1]=ExtTrendMax;

         ExtTrendLowBuffer[i]=ExtTrendMin;

         ExtTrendLowBuffer[i+1]=ExtTrendMin;

         //--- check for SAR

         if(ExtSARBuffer[i+1]<high[i] || ExtSARBuffer[i+1]<high[i-1])

            ExtSARBuffer[i+1]=MathMax(high[i],high[i-1]);

        }



     }

//---- OnCalculate done. Return new prev_calculated.

   return(rates_total);

  }

//+------------------------------------------------------------------+

//| Find highest price from start to current position                |

//+------------------------------------------------------------------+

double GetHigh(int nPosition,int nStartPeriod,const double &HiData[])

  {

//--- calculate

   double result=HiData[nStartPeriod];

   for(int i=nStartPeriod; i<=nPosition; i++)

      if(result<HiData[i])

         result=HiData[i];

   return(result);

  }

//+------------------------------------------------------------------+

//| Find lowest price from start to current position                 |

//+------------------------------------------------------------------+

double GetLow(int nPosition,int nStartPeriod,const double &LoData[])

  {

//--- calculate

   double result=LoData[nStartPeriod];

   for(int i=nStartPeriod; i<=nPosition; i++)

      if(result>LoData[i])

         result=LoData[i];

   return(result);

  }

//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---