How to draw lines on the chart using Expert Advisor

Hi ,

Learning how to draw lines - I made a code to draw Maximum and minimum of previous Day and current day. It works if printing - however, it does not work correctly with fixed lines. I mean, lines remain linking instead of fixed .

Started with EA …but, will code an indicator afterwards. with enum and functions. as I said, still learning .

  • It cannot let 4 lines drawn . Only two .

  • It does not respect bool variables false or true. It means, it shows Previous max even If I set it to false.

Does anyone would be able to help here ? Thanks.

To begin, remove ALL ERROR MESSAGES and ALL WARNINGS.

Also, I recommend using the styler - so you can easily detect errors.

Hi, tks - I did not know Styler … very useful . I have removed the warnings as well.

NO idea that warning could impact the functionally. Anyway,

Now, bool true and false… seens to be working … but, still getting confused why when enabled all (4) lines - only 2 are printed on chart.

Also, it does not respect the colors (input)…

At me the request: please, comments in the code write in English.

Note # 2: you need to create objects(lines) in OnInit (), and in Ontick () you just need to move these lines.

I have tried to adapt your indicator to show all lines. Just changed initial EA to indicator using functions. Unfortunatly still getting issues.

Indicator can print the values correctly- However, it cannot draw lines accorinagly .

Let me know if you have any idea why … Thanks !

Please, write all comments in the code in English. I do not understand half your text.

You have variables with the same names.

string strmaxant = "Maxima Anteriror";
string strminant = "Minima Anterior";
string strmaxatual = "Maxima Anteriror";
string strminatual = "Minima Anterior";

This is mistake.

Also, the creation of lines (in OnInit ()) must be coordinated with the input parameters:

//--- creating lines
   if(maxA)
      if(ObjectFind(0,strmax)<0)
         HLineCreate(0,strmax,0,0.0,cormaxD,eslinemaxD,larguramaxD);
...

Also I recommend making similar names for the input parameters and for the names of the lines:

input bool              max            = false;       // Max (Intraday)
input color             clr_max        = clrOrange;   // Select Color 
input ENUM_LINE_STYLE   e_s_line_max   = STYLE_DASH;  // Style    
input int               largura_max    = 1;           // Width  
...
double max;
...
string str_max = "Max";
...
//--- creating lines
   if(max)
      if(ObjectFind(0,str_max)<0)
         HLineCreate(0,str_max,0,0.0,cor_max,e_s_line_max,largura_max);

really appreciate your help afinneyq ! Thank you!

Please contact us. If you have questions about MQL5 - I’m always ready to help.

Almost there, as last question - I made all changes…and seens to be working .

Only issue is related to Current Max that is being printed correctly. However Line of Current Max is following the bid price. …Thereby, I have tried change the the function from SYMBOL_BID to LASTHIGH or ASKHIGH. it did not work!

Also, bool if not working - even set to false. it keeps showing the line. np so far…

Any Idea ?

//+------------------------------------------------------------------+
//|                                                  TesteMaxlow.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_plots   0

sinput string     _0_="***********************"; // Max and Min (Intraday)

input bool maxA=true;  //  Max (Intraday)
input color cormaxA=clrBlueViolet; // Select Color 
input ENUM_LINE_STYLE eslinemaxA=STYLE_DASH; // Style    
input int      larguramaxA=1;  // Width  

input bool minA=true; // Min  (Intraday) 
input color corminA=clrBlueViolet; // Select Color
input ENUM_LINE_STYLE eslineminA=STYLE_DASH; // Style   
input int  larguraminA=1;  // Width  

sinput string     _1_="***********************";//  Previous Max and Min -   ----

input bool maxD=true; // Previous Max
input color cormaxD=clrRed; // Color 
input ENUM_LINE_STYLE eslinemaxD=STYLE_SOLID; // Style 
input int  larguramaxD=1;  // Width 

input bool minD=true; // Previous Min 
input color corminD=clrRed; // Color 
input ENUM_LINE_STYLE eslineminD=STYLE_SOLID; // Style   
input int  larguraminD=1;  // Width  

double maxAtual;
double minAtual;
double maxAnterior;
double minAnterior;
double highh[],loww[];
string strmaxant = "Previous Max";
string strminant = "Previous Min";
string strmaxatual = "Current Max";
string strminatual = "Current Min";

double m_adjusted_point;
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {

   int digits_adjust=1;
   if(Digits()==3 || Digits()==5)
      digits_adjust=10;
   m_adjusted_point=Point()*digits_adjust;

// 
   if(maxD)
      if(ObjectFind(0,strmaxant)<0)
         HLineCreate(0,strmaxant,0,0.0,cormaxD,eslinemaxD,larguramaxD);

   if(minD)
      if(ObjectFind(0,strminant)<0)
         HLineCreate(0,strminant,0,0.0,corminD,eslineminD,larguraminD);

   if(maxA)
      if(ObjectFind(0,strmaxatual)<0)
         HLineCreate(0,strmaxatual,0,0.0,cormaxA,eslinemaxA,larguramaxA);

   if(minA)
      if(ObjectFind(0,strminatual)<0)
         HLineCreate(0,strminatual,0,0.0,corminA,eslineminA,larguraminA);

// Previous Max  
   double mAnterior=CopyHigh(_Symbol,PERIOD_D1,0,2,highh);
   if(mAnterior>0) Print("Precos Max Anterior atualizado");

// Previous Min
   double minPassada=CopyLow(_Symbol,PERIOD_D1,0,2,loww);
   if(minPassada>0) Print("Precos Minima Anterior atualizado");

//

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Indicator deinitialization function                              |
//+------------------------------------------------------------------+

void OnDeinit(const int reason)
  {
//---
   Print(__FUNCTION__,", ",reason);
   if(reason==1) // REASON_REMOVE
     {
      HLineDelete(0,strmaxant);
      HLineDelete(0,strminant);
      HLineDelete(0,strmaxatual);
      HLineDelete(0,strminatual);

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

// Previous Max 

   maxAnterior=highh[0];

   Print(" Maxima Anterior = ",maxAnterior);

   if(ObjectFind(0,strmaxant)<0)
      HLineCreate(0,strmaxant,0,0.0,cormaxD,eslinemaxD,larguramaxD);
   HLineMove(0,strmaxant,maxAnterior);

//  Previous Min

   minAnterior=loww[0];
   Print("Minima Anterior = ",minAnterior);

   if(ObjectFind(0,strminant)<0)
      HLineCreate(0,strminant,0,0.0,corminD,eslineminD,larguraminD);
   HLineMove(0,strminant,minAnterior);

// Current Max - Intraday 

   double maxima[];
   ArraySetAsSeries(maxima,true);
   CopyHigh(_Symbol,PERIOD_D1,0,1,maxima);
  
   int maxT=ArrayMaximum(maxima, 0, 0);
   maxAtual=maxima[maxT];
  
   if(maxAtual>0) Print("Maxima atual = ",maxAtual);

   if(ObjectFind(0,strmaxatual)<0)
      HLineCreate(0,strmaxatual,0,0.0,cormaxA,eslinemaxA,larguramaxA);
   HLineMove(0,strmaxatual,maxAtual);

/* Function for ArrayMaximum

double HighestHigh(string pSymbol, ENUM_TIMEFRAMES pPeriod, int pBars, int pStart = 0)
{
        double high[];
        ArraySetAsSeries(high,true);
        
        int copied = CopyHigh(pSymbol,pPeriod,pStart,pBars,high);
        if(copied == -1) return(copied);
        
        int maxIdx = ArrayMaximum(high);
        double maxAtual = high[maxIdx];
        
        return(highest);
        
*/

// Current Min - Intraday

   double lowww[];
   ArraySetAsSeries(lowww,true);
   CopyLow(_Symbol,PERIOD_D1,0,1,lowww);

   int minIdx=ArrayMinimum(lowww,0,0);
   minAtual=lowww[minIdx];

   if(minAtual>0) Print("Minima Atual = ",minAtual);

   if(ObjectFind(0,strmaxatual)<0)

      HLineCreate(0,strminatual,0,0.0,corminA,eslineminA,larguraminA);
   HLineMove(0,strmaxatual,minAtual);

/*
double LowestLow(string pSymbol, ENUM_TIMEFRAMES pPeriod, int pBars, int pStart = 0)
{
        double low[];
        ArraySetAsSeries(low,true);
        
        int copied = CopyLow(pSymbol,pPeriod,pStart,pBars,low);
        if(copied == -1) return(copied);
        
        int minIdx = ArrayMinimum(low);
        double lowest = low[minIdx];
        
        return(lowest);
*/

   return(rates_total);
  }
//+------------------------------------------------------------------+ 
//| Create the horizontal line                                       | 
//+------------------------------------------------------------------+ 
bool HLineCreate(const long            chart_ID=0,        // chart's ID 
                 const string          name="HLine",      // line name 
                 const int             sub_window=0,      // subwindow index 
                 double                price=0,           // line price 
                 const color           clr=clrRed,        // line color 
                 const ENUM_LINE_STYLE style=STYLE_SOLID, // line style 
                 const int             width=1,           // line width 
                 const bool            back=false,        // in the background 
                 const bool            selection=true,    // highlight to move 
                 const bool            hidden=true,       // hidden in the object list 
                 const long            z_order=0)         // priority for mouse click 
  {
//--- if the price is not set, set it at the current Bid price level 
   if(!price)
      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);
//--- reset the error value 
   ResetLastError();
//--- create a horizontal line 
   if(!ObjectCreate(chart_ID,name,OBJ_HLINE,sub_window,0,price))
     {
      Print(__FUNCTION__,
            ": failed to create a horizontal line! Error code = ",GetLastError());
      return(false);
     }
//--- set line color 
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set line display style 
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- set line width 
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);

   return(true);
  }
//+------------------------------------------------------------------+ 
//| Move horizontal line                                             | 
//+------------------------------------------------------------------+ 
bool HLineMove(const long   chart_ID=0,   // chart's ID 
               const string name="HLine", // line name 
               double       price=0)      // line price 
  {
//--- if the line price is not set, move it to the current Bid price level 
   if(!price)
      price=SymbolInfoDouble(Symbol(),SYMBOL_BID);
//--- reset the error value 
   ResetLastError();
//--- move a horizontal line 
   if(!ObjectMove(chart_ID,name,0,0,price))
     {
      Print(__FUNCTION__,
            ": failed to move the horizontal line! Error code = ",GetLastError());
      return(false);
     }
//--- successful execution 
   return(true);
  }
//+------------------------------------------------------------------+ 
//| Delete a horizontal line                                         | 
//+------------------------------------------------------------------+ 
bool HLineDelete(const long   chart_ID=0,   // chart's ID 
                 const string name="HLine") // line name 
  {
//--- reset the error value 
   ResetLastError();
//--- delete a horizontal line 
   if(!ObjectDelete(chart_ID,name))
     {
      Print(__FUNCTION__,
            ": failed to delete a horizontal line! Error code = ",GetLastError());
      return(false);
     }
//--- successful execution 
   return(true);
  }
//+----------------------------------------------------------------

TesteMaxlow: 1.001 - up to DeInit translated all the notations and comments into English.

Files:TesteMaxlow.mq5 21 KB