SAR_Price_Targets

Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Indicators Used
Parabolic Stop and Reverse system
0 Views
0 Downloads
0 Favorites
SAR_Price_Targets
ÿþ//+------------------------------------------------------------------+

//|                                            SAR_Price_Targets.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 "Parabolic SAR Price Targets indicator"

#property indicator_chart_window

#property indicator_buffers 10

#property indicator_plots   8

//--- plot Change down

#property indicator_label1  "SAR Change down"

#property indicator_type1   DRAW_ARROW

#property indicator_color1  clrRed

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Change up

#property indicator_label2  "SAR Change up"

#property indicator_type2   DRAW_ARROW

#property indicator_color2  clrBlue

#property indicator_style2  STYLE_SOLID

#property indicator_width2  1

//--- plot Entry

#property indicator_label3  "Entry level"

#property indicator_type3   DRAW_ARROW

#property indicator_color3  clrBlue

#property indicator_style3  STYLE_SOLID

#property indicator_width3  1

//--- plot Stop

#property indicator_label4  "StopLoss"

#property indicator_type4   DRAW_ARROW

#property indicator_color4  clrRed

#property indicator_style4  STYLE_SOLID

#property indicator_width4  1

//--- plot Target 1

#property indicator_label5  "Target 1"

#property indicator_type5   DRAW_ARROW

#property indicator_color5  clrGreen

#property indicator_style5  STYLE_SOLID

#property indicator_width5  1

//--- plot Target 2

#property indicator_label6  "Target 2"

#property indicator_type6   DRAW_ARROW

#property indicator_color6  clrGreen

#property indicator_style6  STYLE_SOLID

#property indicator_width6  1

//--- plot Target 3

#property indicator_label7  "Target 3"

#property indicator_type7   DRAW_ARROW

#property indicator_color7  clrGreen

#property indicator_style7  STYLE_SOLID

#property indicator_width7  1

//--- plot Target 4

#property indicator_label8  "Target 4"

#property indicator_type8   DRAW_ARROW

#property indicator_color8  clrGreen

#property indicator_style8  STYLE_SOLID

#property indicator_width8  1

//--- input parameters

input double            InpStep           =  0.02;          // SAR Step

input double            InpMax            =  0.2;           // SAR Maximum

input double            InpLevel1         =  1.0;           // First target level

input double            InpLevel2         =  2.0;           // Second target level

input double            InpLevel3         =  3.0;           // Third target level

input double            InpLevel4         =  4.0;           // Fourth target level

input string            InpFontName       =  "Calibri";     // Font name

input uint              InpFontSize       =  8;             // Font size

input color             InpColorEntryB    =  clrBlue;       // Buy entry line color

input color             InpColorEntryS    =  clrRed;        // Sell entry line color

input uint              InpWidthEntry     =  1;             // Entry line width

input ENUM_LINE_STYLE   InpStyleEntry     =  STYLE_SOLID;   // Entry line style

input color             InpColorStop      =  clrRed;        // Stop line color

input uint              InpWidthStop      =  1;             // Stop line width

input ENUM_LINE_STYLE   InpStyleStop      =  STYLE_DOT;     // Stop line style

input color             InpColorTarget1   =  clrGreen;      // First target line color

input uint              InpWidthTarget1   =  1;             // First target line width

input color             InpColorTarget2   =  clrGreen;      // Second target line color

input uint              InpWidthTarget2   =  1;             // Second target line width

input color             InpColorTarget3   =  clrGreen;      // Third target line color

input uint              InpWidthTarget3   =  1;             // Third target line width

input color             InpColorTarget4   =  clrGreen;      // Fourth target line color

input uint              InpWidthTarget4   =  1;             // Fourth target line width

input ENUM_LINE_STYLE   InpStyleTargets   =  STYLE_DOT;     // Targets line style

//--- indicator buffers

double         BufferCahangeDN[];

double         BufferCahangeUP[];

double         BufferEntry[];

double         BufferStop[];

double         BufferTarget1[];

double         BufferTarget2[];

double         BufferTarget3[];

double         BufferTarget4[];

double         BufferSAR[];

double         BufferPOS[];

//--- global variables

string         prefix;

double         step_sar;

double         max_sar;

double         level1;

double         level2;

double         level3;

double         level4;

int            handle_sar;

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- set global variables

   prefix=MQLInfoString(MQL_PROGRAM_NAME)+"_";

   step_sar=(InpStep<0.0001 ? 0.0001 : InpStep);

   max_sar=(InpMax<0.0001 ? 0.0001 : InpMax);

   level1=InpLevel1;

   level2=InpLevel2;

   level3=InpLevel3;

   level4=InpLevel4;

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferCahangeDN,INDICATOR_DATA);

   SetIndexBuffer(1,BufferCahangeUP,INDICATOR_DATA);

   SetIndexBuffer(2,BufferEntry,INDICATOR_DATA);

   SetIndexBuffer(3,BufferStop,INDICATOR_DATA);

   SetIndexBuffer(4,BufferTarget1,INDICATOR_DATA);

   SetIndexBuffer(5,BufferTarget2,INDICATOR_DATA);

   SetIndexBuffer(6,BufferTarget3,INDICATOR_DATA);

   SetIndexBuffer(7,BufferTarget4,INDICATOR_DATA);

   SetIndexBuffer(8,BufferSAR,INDICATOR_CALCULATIONS);

   SetIndexBuffer(9,BufferPOS,INDICATOR_CALCULATIONS);

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

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

      PlotIndexSetInteger(i,PLOT_ARROW,158);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"SAR Price Targets("+DoubleToString(level1,1)+","+DoubleToString(level2,1)+","+DoubleToString(level3,1)+","+DoubleToString(level4,1)+")");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferCahangeDN,true);

   ArraySetAsSeries(BufferCahangeUP,true);

   ArraySetAsSeries(BufferEntry,true);

   ArraySetAsSeries(BufferStop,true);

   ArraySetAsSeries(BufferTarget1,true);

   ArraySetAsSeries(BufferTarget2,true);

   ArraySetAsSeries(BufferTarget3,true);

   ArraySetAsSeries(BufferTarget4,true);

   ArraySetAsSeries(BufferSAR,true);

   ArraySetAsSeries(BufferPOS,true);

//--- create SAR handle

   ResetLastError();

   handle_sar=iSAR(NULL,PERIOD_CURRENT,step_sar,max_sar);

   if(handle_sar==INVALID_HANDLE)

     {

      Print("The iSAR(",DoubleToString(step_sar,4),","+DoubleToString(max_sar,4),") object was not created: Error ",GetLastError());

      return INIT_FAILED;

     }

//---

   return(INIT_SUCCEEDED);

  }

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

//| Custom indicator deinitialization function                       |

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

void OnDeinit(const int reason)

  {

//--- timer

   ObjectsDeleteAll(0,prefix,0);

   ChartRedraw();

//---

  }

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

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

  {

//--- #AB0=>2:0 <0AA82>2 1CD5@>2 :0: B09<A5@89

   ArraySetAsSeries(open,true);

   ArraySetAsSeries(close,true);

   ArraySetAsSeries(time,true);

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

   if(rates_total<4) return 0;

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

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-2;

      ArrayInitialize(BufferCahangeDN,EMPTY_VALUE);

      ArrayInitialize(BufferCahangeUP,EMPTY_VALUE);

      ArrayInitialize(BufferEntry,EMPTY_VALUE);

      ArrayInitialize(BufferStop,EMPTY_VALUE);

      ArrayInitialize(BufferTarget1,EMPTY_VALUE);

      ArrayInitialize(BufferTarget2,EMPTY_VALUE);

      ArrayInitialize(BufferTarget3,EMPTY_VALUE);

      ArrayInitialize(BufferTarget4,EMPTY_VALUE);

      ArrayInitialize(BufferSAR,0);

      ArrayInitialize(BufferPOS,0);

     }



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

   int count=(limit>1 ? rates_total : 1);

   int copied=CopyBuffer(handle_sar,0,0,count,BufferSAR);

   if(copied!=count) return 0;

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

     {

      BufferCahangeUP[i]=BufferCahangeDN[i]=EMPTY_VALUE;

      BufferPOS[i]=(BufferSAR[i]<close[i] ? 1 : BufferSAR[i]>close[i] ? -1 : 0);

      if(BufferPOS[i]==1 && BufferPOS[i+1]!=1)

         BufferCahangeUP[i]=BufferSAR[i];

      else if(BufferPOS[i]==-1 && BufferPOS[i+1]!=-1)

         BufferCahangeDN[i]=BufferSAR[i];

     }

     

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

   BufferEntry[0]=BufferStop[0]=BufferTarget1[0]=BufferTarget2[0]=BufferTarget3[0]=BufferTarget4[0]=EMPTY_VALUE;

   int change=FindLastChange(rates_total);

   if(change!=WRONG_VALUE)

     {

      double delta=fabs(BufferSAR[change]-open[change]);

      double target1=(open[change]+(BufferPOS[change]==1 ? delta : -delta)*level1);

      double target2=(open[change]+(BufferPOS[change]==1 ? delta : -delta)*level2);

      double target3=(open[change]+(BufferPOS[change]==1 ? delta : -delta)*level3);

      double target4=(open[change]+(BufferPOS[change]==1 ? delta : -delta)*level4);

      //---

      BufferEntry[0]=open[0];

      BufferStop[0]=BufferSAR[change];

      BufferTarget1[0]=target1;

      BufferTarget2[0]=target2;

      BufferTarget3[0]=target3;

      BufferTarget4[0]=target4;

      //---

      color clr_entry=(BufferPOS[change]==1 ? InpColorEntryB : InpColorEntryS);

      string text_entry="Entry "+(BufferPOS[change]==1 ? "Buy" : "Sell")+": "+DoubleToString(open[change],Digits());

      int sl_pt=int((fabs(BufferSAR[change]-open[change]))/Point());

      ENUM_ANCHOR_POINT anchor=(BufferPOS[change]==1 ? ANCHOR_LEFT_UPPER : ANCHOR_LEFT_LOWER);

      string text_stop="SL "+(BufferPOS[change]==1 ? "Buy" : "Sell")+": "+DoubleToString(BufferSAR[change],Digits())+" ("+(string)sl_pt+")";

      string text_target1="Target 1: "+DoubleToString(target1,Digits())+" ("+IntegerToString(int(delta*level1/Point()))+")";

      string text_target2="Target 2: "+DoubleToString(target2,Digits())+" ("+IntegerToString(int(delta*level2/Point()))+")";

      string text_target3="Target 3: "+DoubleToString(target3,Digits())+" ("+IntegerToString(int(delta*level3/Point()))+")";

      string text_target4="Target 4: "+DoubleToString(target4,Digits())+" ("+IntegerToString(int(delta*level4/Point()))+")";

      //---

      DrawLine(prefix+"Entry",0,open[change],open[change],time[change],time[0]+PeriodSeconds(),clr_entry,InpWidthEntry,InpStyleEntry);

      DrawText(prefix+"EntryDescr",0,open[change],time[change],clr_entry,InpFontSize,anchor,text_entry);

      //---

      DrawLine(prefix+"Stop",0,BufferSAR[change],BufferSAR[change],time[change],time[0]+PeriodSeconds(),InpColorStop,InpWidthStop,InpStyleStop);

      DrawText(prefix+"StopDescr",0,BufferSAR[change],time[change],InpColorStop,InpFontSize,anchor,text_stop);

      //---

      DrawLine(prefix+"Lev1",0,target1,target1,time[change],time[0]+PeriodSeconds(),InpColorTarget1,InpWidthTarget1,InpStyleTargets);

      DrawText(prefix+"Lev1Descr",0,target1,time[change],InpColorTarget1,InpFontSize,anchor,text_target1);

      //---

      DrawLine(prefix+"Lev2",0,target2,target2,time[change],time[0]+PeriodSeconds(),InpColorTarget2,InpWidthTarget2,InpStyleTargets);

      DrawText(prefix+"Lev2Descr",0,target2,time[change],InpColorTarget2,InpFontSize,anchor,text_target2);

      //---

      DrawLine(prefix+"Lev3",0,target3,target3,time[change],time[0]+PeriodSeconds(),InpColorTarget3,InpWidthTarget3,InpStyleTargets);

      DrawText(prefix+"Lev3Descr",0,target3,time[change],InpColorTarget3,InpFontSize,anchor,text_target3);

      //---

      DrawLine(prefix+"Lev4",0,target4,target4,time[change],time[0]+PeriodSeconds(),InpColorTarget4,InpWidthTarget4,InpStyleTargets);

      DrawText(prefix+"Lev4Descr",0,target4,time[change],InpColorTarget4,InpFontSize,anchor,text_target4);

     }

   

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

   return(rates_total);

  }

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

//| >72@0I05B 10@ ?>A;54=59 A<5=K =0?@02;5=8O Parabolic SAR         |

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

int FindLastChange(const int rates_total)

  {

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

      if(BufferPOS[i]!=BufferPOS[i+1])

         return i;

   return WRONG_VALUE;

  }

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

//| K2>48B B@5=4>2CN ;8=8N                                          |

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

void DrawLine(const string name,

              const int sub_win,

              const double price1,

              const double price2,

              const datetime time1,

              const datetime time2,

              const color line_color,

              const int line_width,

              const ENUM_LINE_STYLE line_style,

              const bool ray_left=false,

              const bool ray_right=true,

              const string text="\n",

              const string tooltip="\n")

  {

   if(ObjectFind(0,name)<0)

     {

      ObjectCreate(0,name,OBJ_TREND,sub_win,time1,price1,time2,price2);

      ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);

      ObjectSetInteger(0,name,OBJPROP_SELECTED,false);

      ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);

      ObjectSetInteger(0,name,OBJPROP_RAY_LEFT,ray_left);

      ObjectSetInteger(0,name,OBJPROP_RAY_RIGHT,ray_right);

     }

   ObjectSetInteger(0,name,OBJPROP_TIME,0,time1);

   ObjectSetInteger(0,name,OBJPROP_TIME,1,time2);

   ObjectSetDouble(0,name,OBJPROP_PRICE,0,price1);

   ObjectSetDouble(0,name,OBJPROP_PRICE,1,price2);

//---

   ObjectSetInteger(0,name,OBJPROP_WIDTH,line_width);

   ObjectSetInteger(0,name,OBJPROP_COLOR,line_color);

   ObjectSetInteger(0,name,OBJPROP_STYLE,line_style);

   ObjectSetString(0,name,OBJPROP_TEXT,text);

   ObjectSetString(0,name,OBJPROP_TOOLTIP,tooltip);

  }

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

//| K2>48B B5:AB                                                    |

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

void DrawText(const string name,

              const int sub_win,

              const double price,

              const datetime time,

              const color text_color,

              const int font_size,

              const ENUM_ANCHOR_POINT anchor,

              const string text,

              const string font_name="Calibri",

              const string tooltip="\n")

  {

   if(ObjectFind(0,name)<0)

     {

      ObjectCreate(0,name,OBJ_TEXT,sub_win,time,price);

      ObjectSetInteger(0,name,OBJPROP_SELECTABLE,false);

      ObjectSetInteger(0,name,OBJPROP_SELECTED,false);

      ObjectSetInteger(0,name,OBJPROP_HIDDEN,true);

      ObjectSetString(0,name,OBJPROP_FONT,font_name);

     }

   ObjectSetInteger(0,name,OBJPROP_ANCHOR,anchor);

   ObjectSetInteger(0,name,OBJPROP_TIME,0,time);

   ObjectSetDouble(0,name,OBJPROP_PRICE,0,price);

//---

   ObjectSetInteger(0,name,OBJPROP_FONTSIZE,font_size);

   ObjectSetInteger(0,name,OBJPROP_COLOR,text_color);

   ObjectSetString(0,name,OBJPROP_TEXT,text);

   ObjectSetString(0,name,OBJPROP_TOOLTIP,tooltip);

  }

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

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