Author: Copyright 2018, MetaQuotes Software Corp.
0 Views
0 Downloads
0 Favorites
MTF_SAR
ÿþ//+------------------------------------------------------------------+

//|                                                      MTF_SAR.mq5 |

//|                        Copyright 2018, MetaQuotes Software Corp. |

//|                                                 https://mql5.com |

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

#property copyright "Copyright 2018, MetaQuotes Software Corp."

#property link      "https://mql5.com"

#property version   "1.00"

#property description "Multitimeframe Parabolic SAR indicator"

#property indicator_chart_window

#property indicator_buffers 10

#property indicator_plots   5

//--- plot SAR1

#property indicator_label1  "SAR1"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot SAR2

#property indicator_label2  "SAR2"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrOrange

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot SAR3

#property indicator_label3  "SAR3"

#property indicator_type3   DRAW_ARROW

#property indicator_color3  clrGreen

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot SAR4

#property indicator_label4  "SAR4"

#property indicator_type4   DRAW_ARROW

#property indicator_color4  clrLime

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- plot SAR5

#property indicator_label5  "SAR5"

#property indicator_type5   DRAW_ARROW

#property indicator_color5  clrBlue

#property indicator_style5  STYLE_SOLID

#property indicator_width5  1

//--- enums

enum ENUM_DRAW_MODE

  {

   DRAW_MODE_STEPS,  // Steps

   DRAW_MODE_SLOPE   // Slope

  };

//---

enum ENUM_INPUT_YES_NO

  {

   INPUT_YES   =  1,    // Yes

   INPUT_NO    =  0     // No

  };

//--- input parameters

input ENUM_INPUT_YES_NO InpShowSAR1    =  INPUT_YES;  // Show first SAR

input ENUM_TIMEFRAMES   InpTimeframe1  =  PERIOD_M15; // First SAR timeframe

input double            InpStepSAR1    =  0.02;       // First SAR step

input double            InpMaxSAR1     =  0.2;        // First SAR max

//---

input ENUM_INPUT_YES_NO InpShowSAR2    =  INPUT_YES;  // Show second SAR

input ENUM_TIMEFRAMES   InpTimeframe2  =  PERIOD_M30; // Second SAR timeframe

input double            InpStepSAR2    =  0.02;       // Second SAR step

input double            InpMaxSAR2     =  0.2;        // Second SAR max

//---

input ENUM_INPUT_YES_NO InpShowSAR3    =  INPUT_YES;  // Show third SAR

input ENUM_TIMEFRAMES   InpTimeframe3  =  PERIOD_H1;  // Third SAR timeframe

input double            InpStepSAR3    =  0.02;       // Third SAR step

input double            InpMaxSAR3     =  0.2;        // Third SAR max

//---

input ENUM_INPUT_YES_NO InpShowSAR4    =  INPUT_YES;  // Show fourth SAR

input ENUM_TIMEFRAMES   InpTimeframe4  =  PERIOD_H4;  // Fourth SAR timeframe

input double            InpStepSAR4    =  0.02;       // Fourth SAR step

input double            InpMaxSAR4     =  0.2;        // Fourth SAR max

//---

input ENUM_INPUT_YES_NO InpShowSAR5    =  INPUT_YES;  // Show fifth SAR

input ENUM_TIMEFRAMES   InpTimeframe5  =  PERIOD_D1;  // Fifth SAR timeframe

input double            InpStepSAR5    =  0.02;       // Fifth SAR step

input double            InpMaxSAR5     =  0.2;        // Fifth SAR max

//---

//--- indicator buffers

double            BufferSAR1[];

double            BufferSAR2[];

double            BufferSAR3[];

double            BufferSAR4[];

double            BufferSAR5[];

double            BufferSAR1tmp[];

double            BufferSAR2tmp[];

double            BufferSAR3tmp[];

double            BufferSAR4tmp[];

double            BufferSAR5tmp[];

//--- global variables

ENUM_TIMEFRAMES   timeframe1;

ENUM_TIMEFRAMES   timeframe2;

ENUM_TIMEFRAMES   timeframe3;

ENUM_TIMEFRAMES   timeframe4;

ENUM_TIMEFRAMES   timeframe5;

double            step1;

double            step2;

double            step3;

double            step4;

double            step5;

double            max1;

double            max2;

double            max3;

double            max4;

double            max5;

int               handle_sar1;

int               handle_sar2;

int               handle_sar3;

int               handle_sar4;

int               handle_sar5;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- timer

   EventSetTimer(90);

//--- set global variables

   step1=(InpStepSAR1<0.0001 ? 0.0001 : InpStepSAR1);

   step2=(InpStepSAR2<0.0001 ? 0.0001 : InpStepSAR2);

   step3=(InpStepSAR3<0.0001 ? 0.0001 : InpStepSAR3);

   step4=(InpStepSAR4<0.0001 ? 0.0001 : InpStepSAR4);

   step5=(InpStepSAR5<0.0001 ? 0.0001 : InpStepSAR5);

   max1=(InpMaxSAR1<0.0001 ? 0.0001 : InpMaxSAR1);

   max2=(InpMaxSAR2<0.0001 ? 0.0001 : InpMaxSAR2);

   max3=(InpMaxSAR3<0.0001 ? 0.0001 : InpMaxSAR3);

   max4=(InpMaxSAR4<0.0001 ? 0.0001 : InpMaxSAR4);

   max5=(InpMaxSAR5<0.0001 ? 0.0001 : InpMaxSAR5);

   timeframe1=(InpTimeframe1>Period() ? InpTimeframe1 : Period());

   timeframe2=(InpTimeframe2>Period() ? InpTimeframe2 : Period());

   timeframe3=(InpTimeframe3>Period() ? InpTimeframe3 : Period());

   timeframe4=(InpTimeframe4>Period() ? InpTimeframe4 : Period());

   timeframe5=(InpTimeframe5>Period() ? InpTimeframe5 : Period());

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferSAR1,INDICATOR_DATA);

   SetIndexBuffer(1,BufferSAR2,INDICATOR_DATA);

   SetIndexBuffer(2,BufferSAR3,INDICATOR_DATA);

   SetIndexBuffer(3,BufferSAR4,INDICATOR_DATA);

   SetIndexBuffer(4,BufferSAR5,INDICATOR_DATA);

   SetIndexBuffer(5,BufferSAR1tmp,INDICATOR_CALCULATIONS);

   SetIndexBuffer(6,BufferSAR2tmp,INDICATOR_CALCULATIONS);

   SetIndexBuffer(7,BufferSAR3tmp,INDICATOR_CALCULATIONS);

   SetIndexBuffer(8,BufferSAR4tmp,INDICATOR_CALCULATIONS);

   SetIndexBuffer(9,BufferSAR5tmp,INDICATOR_CALCULATIONS);

//--- setting indicator parameters

   string label=TimeframeToString(timeframe1)+","+TimeframeToString(timeframe2)+","+TimeframeToString(timeframe3)+","+TimeframeToString(timeframe4)+","+TimeframeToString(timeframe5)+" Parabolic SAR";

   IndicatorSetString(INDICATOR_SHORTNAME,label);

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting plot buffer parameters

   PlotIndexSetString(0,PLOT_LABEL,TimeframeToString(timeframe1)+" SAR");

   PlotIndexSetString(1,PLOT_LABEL,TimeframeToString(timeframe2)+" SAR");

   PlotIndexSetString(2,PLOT_LABEL,TimeframeToString(timeframe3)+" SAR");

   PlotIndexSetString(3,PLOT_LABEL,TimeframeToString(timeframe4)+" SAR");

   PlotIndexSetString(4,PLOT_LABEL,TimeframeToString(timeframe5)+" SAR");

   PlotIndexSetInteger(0,PLOT_DRAW_TYPE,(InpShowSAR1 ? DRAW_ARROW : DRAW_NONE));

   PlotIndexSetInteger(1,PLOT_DRAW_TYPE,(InpShowSAR2 ? DRAW_ARROW : DRAW_NONE));

   PlotIndexSetInteger(2,PLOT_DRAW_TYPE,(InpShowSAR3 ? DRAW_ARROW : DRAW_NONE));

   PlotIndexSetInteger(3,PLOT_DRAW_TYPE,(InpShowSAR4 ? DRAW_ARROW : DRAW_NONE));

   PlotIndexSetInteger(4,PLOT_DRAW_TYPE,(InpShowSAR5 ? DRAW_ARROW : DRAW_NONE));

//--- setting a code from the Wingdings charset as the property of PLOT_ARROW

   for(int i=0;i<5;i++)

      PlotIndexSetInteger(i,PLOT_ARROW,158);

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferSAR1,true);

   ArraySetAsSeries(BufferSAR2,true);

   ArraySetAsSeries(BufferSAR3,true);

   ArraySetAsSeries(BufferSAR4,true);

   ArraySetAsSeries(BufferSAR5,true);

   ArraySetAsSeries(BufferSAR1tmp,true);

   ArraySetAsSeries(BufferSAR2tmp,true);

   ArraySetAsSeries(BufferSAR3tmp,true);

   ArraySetAsSeries(BufferSAR4tmp,true);

   ArraySetAsSeries(BufferSAR5tmp,true);

//--- create MA's handles

   ResetLastError();

   handle_sar1=iSAR(NULL,timeframe1,step1,max1);

   if(handle_sar1==INVALID_HANDLE)

     {

      Print(__LINE__,": The iSAR(",DoubleToString(step1,4),","+DoubleToString(max1,4),") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

   handle_sar2=iSAR(NULL,timeframe2,step2,max2);

   if(handle_sar2==INVALID_HANDLE)

     {

      Print(__LINE__,": The iSAR(",DoubleToString(step2,4),","+DoubleToString(max2,4),") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

   handle_sar3=iSAR(NULL,timeframe3,step3,max3);

   if(handle_sar3==INVALID_HANDLE)

     {

      Print(__LINE__,": The iSAR(",DoubleToString(step3,4),","+DoubleToString(max3,4),") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

   handle_sar4=iSAR(NULL,timeframe4,step4,max4);

   if(handle_sar4==INVALID_HANDLE)

     {

      Print(__LINE__,": The iSAR(",DoubleToString(step4,4),","+DoubleToString(max4,4),") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

   handle_sar5=iSAR(NULL,timeframe5,step5,max5);

   if(handle_sar5==INVALID_HANDLE)

     {

      Print(__LINE__,": The iSAR(",DoubleToString(step5,4),","+DoubleToString(max5,4),") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

//--- get timeframe

   Time(NULL,timeframe1,1);

   Time(NULL,timeframe2,1);

   Time(NULL,timeframe3,1);

   Time(NULL,timeframe4,1);

   Time(NULL,timeframe5,1);

//---

   return(INIT_SUCCEEDED);

  }

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

//| 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[])

  {

//--- @>25@:0 :>;8G5AB20 4>ABC?=KE 10@>2

   if(rates_total<4) return 0;

   if(Time(NULL,timeframe1,1)==0 || Time(NULL,timeframe2,1)==0 || Time(NULL,timeframe3,1)==0 || Time(NULL,timeframe4,1)==0 || Time(NULL,timeframe5,1)==0)

      return 0;

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-1;

      ArrayInitialize(BufferSAR1,EMPTY_VALUE);

      ArrayInitialize(BufferSAR2,EMPTY_VALUE);

      ArrayInitialize(BufferSAR3,EMPTY_VALUE);

      ArrayInitialize(BufferSAR4,EMPTY_VALUE);

      ArrayInitialize(BufferSAR5,EMPTY_VALUE);

      ArrayInitialize(BufferSAR1tmp,0);

      ArrayInitialize(BufferSAR2tmp,0);

      ArrayInitialize(BufferSAR3tmp,0);

      ArrayInitialize(BufferSAR4tmp,0);

      ArrayInitialize(BufferSAR5tmp,0);

     }

//--- >43>B>2:0 40==KE

   

   int bars1=(timeframe1==Period() ? rates_total : Bars(NULL,timeframe1));

   int bars2=(timeframe2==Period() ? rates_total : Bars(NULL,timeframe2));

   int bars3=(timeframe3==Period() ? rates_total : Bars(NULL,timeframe3));

   int bars4=(timeframe4==Period() ? rates_total : Bars(NULL,timeframe4));

   int bars5=(timeframe5==Period() ? rates_total : Bars(NULL,timeframe5));

   

   int count1=(limit>1 ? fmin(bars1,rates_total) : 1),copied=0;

   copied=CopyBuffer(handle_sar1,0,0,count1,BufferSAR1tmp);

   if(copied!=count1) return 0;



   int count2=(limit>1 ? fmin(bars2,rates_total) : 1);

   copied=CopyBuffer(handle_sar2,0,0,count2,BufferSAR2tmp);

   if(copied!=count2) return 0;



   int count3=(limit>1 ? fmin(bars3,rates_total) : 1);

   copied=CopyBuffer(handle_sar3,0,0,count3,BufferSAR3tmp);

   if(copied!=count3) return 0;



   int count4=(limit>1 ? fmin(bars4,rates_total) : 1);

   copied=CopyBuffer(handle_sar4,0,0,count4,BufferSAR4tmp);

   if(copied!=count4) return 0;



   int count5=(limit>1 ? fmin(bars5,rates_total) : 1);

   copied=CopyBuffer(handle_sar5,0,0,count5,BufferSAR5tmp);

   if(copied!=count5) return 0;

      

//---  0AGQB 8=48:0B>@0

   for(int i=limit; i>=0 && !IsStopped(); i--)

     {

      BufferSAR1[i]=BufferSAR2[i]=BufferSAR3[i]=BufferSAR4[i]=BufferSAR5[i]=EMPTY_VALUE;

      if(InpShowSAR1) DataConversion(rates_total,NULL,timeframe1,i,BufferSAR1tmp,BufferSAR1,DRAW_MODE_STEPS);

      if(InpShowSAR2) DataConversion(rates_total,NULL,timeframe2,i,BufferSAR2tmp,BufferSAR2,DRAW_MODE_STEPS);

      if(InpShowSAR3) DataConversion(rates_total,NULL,timeframe3,i,BufferSAR3tmp,BufferSAR3,DRAW_MODE_STEPS);

      if(InpShowSAR4) DataConversion(rates_total,NULL,timeframe4,i,BufferSAR4tmp,BufferSAR4,DRAW_MODE_STEPS);

      if(InpShowSAR5) DataConversion(rates_total,NULL,timeframe5,i,BufferSAR5tmp,BufferSAR5,DRAW_MODE_STEPS);

     }

   

//--- return value of prev_calculated for next call

   return(rates_total);

  }

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

//| Custom indicator timer function                                  |

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

void OnTimer()

  {

   Time(NULL,timeframe1,1);

   Time(NULL,timeframe2,1);

   Time(NULL,timeframe3,1);

   Time(NULL,timeframe4,1);

   Time(NULL,timeframe5,1);

  }

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

//| Transfering data from the source timeframe to current timeframe  |

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

void DataConversion(const int rates_total,

                    const string symbol_name,

                    const ENUM_TIMEFRAMES timeframe_src,

                    const int shift,

                    const double &buffer_src[],

                    double &buffer_dest[],

                    ENUM_DRAW_MODE mode=DRAW_MODE_STEPS

                   )

  {

   if(timeframe_src==Period())

     {

      buffer_dest[shift]=buffer_src[shift];

      return;

     }

   int bar_curr=BarToCurrent(symbol_name,timeframe_src,shift);

   if(bar_curr>rates_total-1)

      return;

   int bar_prev=BarToCurrent(symbol_name,timeframe_src,shift+1);

   int bar_next=(shift>0 ? BarToCurrent(symbol_name,timeframe_src,shift-1) : 0);

   if(bar_prev==WRONG_VALUE || bar_curr==WRONG_VALUE || bar_next==WRONG_VALUE)

      return;

   buffer_dest[bar_curr]=buffer_src[shift];

   if(mode==DRAW_MODE_STEPS)

      for(int j=bar_curr; j>=bar_next; j--)

         buffer_dest[j]=buffer_dest[bar_curr];

   else

     {

      if(bar_prev>rates_total-1) return;

      for(int j=bar_prev; j>=bar_curr; j--)

         buffer_dest[j]=EquationDirect(bar_prev,buffer_dest[bar_prev],bar_curr,buffer_dest[bar_curr],j);

      if(shift==0)

         for(int j=bar_curr; j>=0; j--)

            buffer_dest[j]=buffer_dest[bar_curr];

     }

  }

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

//| >72@0I05B 10@ 7040==>3> B09<D@59<0 :0: 10@ B5:CI53> B09<D@59<0  |

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

int BarToCurrent(const string symbol_name,const ENUM_TIMEFRAMES timeframe_src,const int shift,bool exact=false)

  {

   datetime time=Time(symbol_name,timeframe_src,shift);

   return(time!=0 ? BarShift(symbol_name,Period(),time,exact) : WRONG_VALUE);

  }

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

//| >72@0I05B A<5I5=85 10@0 ?> 2@5<5=8                              |

//| https://www.mql5.com/ru/forum/743/page11#comment_7010041         |

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

int BarShift(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const datetime time,bool exact=false)

  {

   int res=Bars(symbol_name,timeframe,time+1,UINT_MAX);

   if(exact) if((timeframe!=PERIOD_MN1 || time>TimeCurrent()) && res==Bars(symbol_name,timeframe,time-PeriodSeconds(timeframe)+1,UINT_MAX)) return(WRONG_VALUE);

   return res;

  }

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

//| >72@0I05B Time                                                  |

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

datetime Time(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const int shift)

  {

   datetime array[];

   ArraySetAsSeries(array,true);

   return(CopyTime(symbol_name,timeframe,shift,1,array)==1 ? array[0] : 0);

  }

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

//| #@02=5=85 ?@O<>9                                                 |

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

double EquationDirect(const int left_bar,const double left_price,const int right_bar,const double right_price,const int bar_to_search)

  {

   return(right_bar==left_bar ? left_price : (right_price-left_price)/(right_bar-left_bar)*(bar_to_search-left_bar)+left_price);

  }

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

//| Timeframe to string                                              |

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

string TimeframeToString(const ENUM_TIMEFRAMES timeframe)

  {

   return StringSubstr(EnumToString((timeframe==PERIOD_CURRENT ? Period() : timeframe)),7);

  }

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

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