Download MetaTrader 5

custom indicator... I try to draw the line of the indicator

To add comments, please log in or register
Publish your article and get a reward!
Ac1do84
6
Ac1do84 2015.10.30 19:25 

Hello everyone,

 I am a newbie in programming mql5. So I try to learn it but its really hard.

My Question is, how can I draw the lines of the following indicator:

 

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#property copyright "sanderz"
#property link      "https://login.mql5.com/ru/users/sanderz/seller"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 11
#property indicator_plots 3 

#property indicator_label1  "SuperTr1"
#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  clrGray
#property indicator_width1  2

#property indicator_label2  "SuperTr2"
#property indicator_type2   DRAW_COLOR_LINE
#property indicator_color2  clrPurple
#property indicator_width2  2

#property indicator_label3  "SuperTr3"
#property indicator_type3   DRAW_COLOR_LINE
#property indicator_color3  clrGreen, clrRed
#property indicator_width3  2

//---
input string sect1   = "";    // SUPERTREND PARAMETERS
input int    PeriodST=14;     // Supertrend period
input double Multiplier=3;    // Supertrend multiplier 1h & 10 min Stop
input double Multiplier1=5;    // Supertrend multiplier 10 min Entry
//---
input string sect2   = "";    // TIMEFRAME PARAMETERS
input bool swTF1=true;       // Use TF 10 min
input bool swTF2=true;       // Use TF 10 min
input bool swTFH1=true;       // Use TF 1 h
input bool   Show_Filling=false;

//---
input string sect3   = "";    // DESIGN PARAMETERS
input int  sizeFont=8;        // Text size
input int  sizeP=1;           // Price mark size

int handleSTr1=INVALID_HANDLE,handleSTr2=INVALID_HANDLE,handleSTrH1=INVALID_HANDLE;

double SuperTr1[],SuperTr2[],SuperTrH1[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {

//--- 
   handleSTr1=iCustom(_Symbol,PERIOD_M10,"SuperTrend",PeriodST,Multiplier);
   SetIndexBuffer(0,SuperTr1,INDICATOR_DATA);
   if(handleSTr1==INVALID_HANDLE)
     {//--- Report the failure and return the error number      
      PrintFormat("Failed to create the handle of the Supertrend indicator. Error=",GetLastError());
      return(INIT_FAILED);
     }
   handleSTr2=iCustom(_Symbol,PERIOD_M10,"SuperTrend",PeriodST,Multiplier1);
   SetIndexBuffer(2,SuperTr2,INDICATOR_DATA);
   if(handleSTr2==INVALID_HANDLE)
     {//--- Report the failure and return the error number      
      PrintFormat("Failed to create the handle of the Supertrend indicator. Error=",GetLastError());
      return(INIT_FAILED);
     }
   handleSTrH1=iCustom(_Symbol,PERIOD_H1,"SuperTrend",PeriodST,Multiplier);
   SetIndexBuffer(4,SuperTrH1,INDICATOR_DATA);
   if(handleSTrH1==INVALID_HANDLE)
     {//--- Report the failure and return the error number      
      PrintFormat("Failed to create the handle of the Supertrend indicator. Error=",GetLastError());
      return(INIT_FAILED);
     }
//---
   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[])
  {
//--- indexing elements in arrays as in timeseries  
   ArraySetAsSeries(high,true);ArraySetAsSeries(low,true);ArraySetAsSeries(tick_volume,true);
   ArraySetAsSeries(time,true);ArraySetAsSeries(open,true);ArraySetAsSeries(spread,true);
   ArraySetAsSeries(close,true);ArraySetAsSeries(volume,true);
//---
   if(swTF1)
     {
      ArrayResize(SuperTr1,2);ArrayInitialize(SuperTr1,0.0);
      if(CopyBufferAsSeries(handleSTr1,2,0,2,true,SuperTr1))
        {
         color clr=clrBlack;
         if(close[0]<SuperTr1[0])clr=clrRed;
         if(close[0]>SuperTr1[0])clr=clrGreen;
         if(close[0]==SuperTr1[0]&&close[1]>SuperTr1[1])clr=clrGreen;
         if(close[0]==SuperTr1[0]&&close[1]<SuperTr1[1])clr=clrRed;
         ArrowRightPriceDelete(0,"LPB1");
         TextDelete(0,"2LPB1");
         TextCreate(0,"2LPB1",0,time[0]+PeriodSeconds(_Period),SuperTr1[0],"M10","Arial",sizeFont,clr,0,ANCHOR_RIGHT_UPPER,false,false,true,0);
         ArrowRightPriceCreate(0,"LPB1",0,time[0]+PeriodSeconds(_Period),SuperTr1[0],clr,DRAW_COLOR_LINE,STYLE_SOLID,sizeP,false,false,true,0);
         ChartRedraw();
        }
     }
   if(swTF2)
     {
      ArrayResize(SuperTr2,2);ArrayInitialize(SuperTr2,0.0);
      if(CopyBufferAsSeries(handleSTr2,2,0,2,true,SuperTr2))
        {
         color clr=clrBlack;
         if(close[0]<SuperTr2[0])clr=clrRed;
         if(close[0]>SuperTr2[0])clr=clrGreen;
         if(close[0]==SuperTr2[0]&&close[1]>SuperTr2[1])clr=clrGreen;
         if(close[0]==SuperTr2[0]&&close[1]<SuperTr2[1])clr=clrRed;
         ArrowRightPriceDelete(0,"LPB2");
         TextDelete(0,"2LPB2");
         TextCreate(0,"2LPB2",0, time[0]+PeriodSeconds(_Period),SuperTr2[0],"M10","Arial",sizeFont,clr,0,ANCHOR_RIGHT_UPPER,false,false,true,0);
         ArrowRightPriceCreate(0,"LPB2",0,time[0]+PeriodSeconds(_Period),SuperTr2[0],clr,DRAW_COLOR_LINE, STYLE_SOLID,sizeP,false,false,true,0);
         ChartRedraw();
        }
     }
   if(swTFH1)
     {
      ArrayResize(SuperTrH1,2);ArrayInitialize(SuperTrH1,0.0);
      if(CopyBufferAsSeries(handleSTrH1,2,0,2,true,SuperTrH1))
        {
         color clr=clrBlack;
         if(close[0]<SuperTrH1[0])clr=clrRed;
         if(close[0]>SuperTrH1[0])clr=clrGreen;
         if(close[0]==SuperTrH1[0]&&close[1]>SuperTrH1[1])clr=clrGreen;
         if(close[0]==SuperTrH1[0]&&close[1]<SuperTrH1[1])clr=clrRed;
         ArrowRightPriceDelete(0,"LPBH1");
         TextDelete(0,"2LPBH1");
         TextCreate(0,"2LPBH1",0,time[0]+PeriodSeconds(_Period),SuperTrH1[0],"H1","Arial",sizeFont,clr,0,ANCHOR_RIGHT_UPPER,false,false,true,0);
         ArrowRightPriceCreate(0,"LPBH1",0,time[0]+PeriodSeconds(_Period),SuperTrH1[0],clr,DRAW_COLOR_LINE,STYLE_SOLID,sizeP,false,false,true,0);
         ChartRedraw();
        }
     }
//--- 
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Creating the left price label                                    |
//+------------------------------------------------------------------+
bool ArrowRightPriceCreate(const long           chart_ID=0,         // chart ID
                           const string          name="RightPrice", // the name of the price label
                           const int             sub_window=0,      // subwindow number
                           datetime              time=0,            // time of the anchor point
                           double                price=0,           // price of the anchor point
                           const color           clr=clrRed,        // the color of the price label
                           const ENUM_LINE_STYLE style=STYLE_SOLID, // the style of the bordering line
                           const int             width=1,           // the size of the price label
                           const bool            back=false,        // in the background
                           const bool            selection=true,    // select to move
                           const bool            hidden=true,       // hidden in the list of objects
                           const long            z_order=0)         // priority for mouse click
  {//ArrowLeftPriceCreate(0,"LP",0,time[0],price[0],clr,STYLE_SOLID,1,false,false,true,0);
//--- set anchor point coordinates if they are not set
   ChangeArrowEmptyPoint(time,price);
//--- reset the error value
   ResetLastError();
//--- create a price label
   if(!ObjectCreate(chart_ID,name,OBJ_ARROW_RIGHT_PRICE,sub_window,time,price))
     {
      Print(__FUNCTION__,
            ": Failed to create the left price label! Error code = ",GetLastError());
      return(false);
     }
//--- set the label color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set the edging line style
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- set the label size
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of moving the label by mouse
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);
  }
//+------------------------------------------------------------------+
//| Delete the left price label from the chart                       |
//+------------------------------------------------------------------+
bool ArrowRightPriceDelete(const long   chart_ID=0,// chart ID
                           const string name="RightPrice") // the name of the label
  {
//--- reset the error value
   ResetLastError();
//--- delete the label
   if(!ObjectDelete(chart_ID,name))
     {
      Print(__FUNCTION__,
            ": Failed to delete the left price label! Error code = ",GetLastError());
      return(false);
     }
//--- successful execution
   return(true);
  }
// Copies the indicator values into an array, taking into account the indexing order            
bool CopyBufferAsSeries(int _handle,     // indicator handle
                        int bufer,       // the number of the indicator buffer
                        int start,       // where to start
                        int number,      // how many items to copy
                        bool asSeries,   // array indexing order
                        double &M[])     // the array, into which data will be copied
  {
//--- filling in the M array by the current indicator values
   if(CopyBuffer(_handle,bufer,start,number,M)<=0) return(false);
//--- creating the indexing order of the M array
//--- if asSeries=true, then the order of indexing of the M array is that of a timeseries
//--- if asSeries=false, then the default order of indexing of the M array is applied
   ArraySetAsSeries(M,asSeries);
//---
   return(true);
  }
//+------------------------------------------------------------------+
//| Checking anchor point values and setting default values          |
//| for empty ones                                                   |
//+------------------------------------------------------------------+
void ChangeArrowEmptyPoint(datetime &time,double &price)
  {
//--- if the point's time is not set, it will be on the current bar
   if(!time)
      time=TimeCurrent();
//--- if the point's price is not set, it will have Bid value
   if(!price)
      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  }
//+------------------------------------------------------------------+
//| Creating the "Text" object                                       |
//+------------------------------------------------------------------+
bool TextCreate(const long              chart_ID=0,               // chart ID
                const string            name="Text",              // the name of the object
                const int               sub_window=0,             // subwindow number
                datetime                time=0,                   // time of the anchor point
                double                  price=0,                  // price of the anchor point
                const string            text="Text",              // the text
                const string            font="Arial",             // font
                const int               font_size=10,             // font size
                const color             clr=clrRed,               // color
                const double            angle=0.0,                // slope
                const ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type
                const bool              back=false,               // in the background
                const bool              selection=false,          // select to move
                const bool              hidden=true,              // hidden in the list of objects
                const long              z_order=0)                // priority for mouse click
  {//TextCreate(0,"2LB",0,time[0],price[0],"text","Arial",12,clr,angle,ANCHOR_RIGHT_UPPER,false,false,true,0);
//--- set anchor point coordinates if they are not set
   ChangeTextEmptyPoint(time,price);
//--- reset the error value
   ResetLastError();
//--- create Text object
   if(!ObjectCreate(chart_ID,name,OBJ_TEXT,sub_window,time,price))
     {
      Print(__FUNCTION__,
            ": failed to create the \"Text\" object! Error code = ",GetLastError());
      return(false);
     }
//--- set the text
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
//--- set the text font
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
//--- set font size
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
//--- set the slope angle of the text
   ObjectSetDouble(chart_ID,name,OBJPROP_ANGLE,angle);
//--- set anchor type
   ObjectSetInteger(chart_ID,name,OBJPROP_ANCHOR,anchor);
//--- set color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of moving the object by mouse
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);
  }
//+------------------------------------------------------------------+
//| Deleting the "Text" object                                       |
//+------------------------------------------------------------------+
bool TextDelete(const long   chart_ID=0,  // chart ID
                const string name="Text") // object name
  {
//--- reset the error value
   ResetLastError();
//--- delete the object
   if(!ObjectDelete(chart_ID,name))
     {
      Print(__FUNCTION__,
            ": failed to delete the \"Text\" object! Error code = ",GetLastError());
      return(false);
     }
//--- successful execution
   return(true);
  }
//+------------------------------------------------------------------+
//| Checking anchor point values and setting default values          |
//| for empty ones                                                   |
//+------------------------------------------------------------------+
void ChangeTextEmptyPoint(datetime &time,double &price)
  {
//--- if the point's time is not set, it will be on the current bar
   if(!time)
      time=TimeCurrent();
//--- if the point's price is not set, it will have Bid value
   if(!price)
      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ArrowRightPriceDelete(0,"LPB1");
   ArrowRightPriceDelete(0,"LPB2");
   ArrowRightPriceDelete(0,"LPBH1");
   TextDelete(0,"2LPB1");
   TextDelete(0,"2LPB2");
   TextDelete(0,"2LPBH1");
   ChartRedraw();
  }
//+------------------------------------------------------------------+

 I tried and tried but nothing worked. Please give me a little help.

 

Thx in advance.

 

Best regards

Sandro 

To add comments, please log in or register