MQL5 - Create several horizontal lines

 

Please some kind soul can check the script for mql5 with following conditions:

1 - Creates a horizontal line on the opening price;

2 - Start drawing the lines from today onwards;

3- It has to be editable where I can delete or move the created lines.

I made the code based on some models, but the lines only appear in the current bar, that is, the previous lines are not saved in the graph.

I performed some tests manually and I believe it has to do with the name of the OpenLine object (as it is always the same name, it considers the last information).

Test performed: I changed the name OpenLine1 to OpenLine2 and compiled the two horizontal lines (OpenLine1 and OpenLine2) in the graph.

Note: I don't know if there is a variable string that changes OpenLine1,OpenLine2,OpenLine3, 4, 5,6,7,8...... I appreciate if anyone can help me, I'm a layman on the subject, my head was fried!!! Thank you from my heart, thank you very much.

#property indicator_chart_window
#property indicator_buffers 3

double op[],cl[]; 
double OpenLine;

int OnInit()
  {
   SetIndexBuffer(0,op,INDICATOR_DATA);
   SetIndexBuffer(1,cl,INDICATOR_DATA);
   
   return(INIT_SUCCEEDED);
  }


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 &TickVolume[],
               const long &Volume[],
               const int &Spread[])
  {
    int i = 0;
    if(prev_calculated == 0) i = 1;
    else i = prev_calculated-1;
    while(i < rates_total){
            
       op[i] = Open[i];
       cl[i] = Close[i];
       Abertura= op[i];
       
       
        if (op[i]>cl[i] && op[i]<cl[i-1]) )
        
        {
            CriarLinhaH(0,0,"OpenLine",Abertura,clrOrange,STYLE_DOT,1,true,false,true,"Candle Open");
        }

  else
        if (op[i]<cl[i] && op[i]>cl[i-1]) 
        
        {
            CriarLinhaH(0,0,"OpenLine",Abertura,clrBlue,STYLE_DOT,1,true,false,true,"Candle Open");
        }
        
        else
         {
           CriarLinhaH(0,0,"OpenLine",Abertura,clrWhite,STYLE_DOT,1,true,false,true,"Candle Open"); 
        }
        i++;
    }
   return(rates_total-1);
  }
  
  


void CriarLinhaH(const long janela,
                 const int subjanela,
                 const string nome,
                 double preco,
                 color cor,
                 const ENUM_LINE_STYLE estilo,
                 const int tamanho,
                 const bool oculto,
                 const bool fundo,
                 bool selecionavel,
                 string dica_=NULL)
  {
    if (ObjectFind(janela,nome)==-1)
    //{ObjectCreate(janela,nome,OBJ_HLINE,subjanela,0,preco);}
    ObjectCreate(janela,nome,OBJ_HLINE,subjanela,0,preco);
    ObjectSetDouble(janela,nome,OBJPROP_PRICE,preco);
    ObjectSetInteger(janela,nome,OBJPROP_COLOR,cor);
    ObjectSetInteger(janela,nome,OBJPROP_STYLE,estilo);
    ObjectSetInteger(janela,nome,OBJPROP_WIDTH,tamanho);
    ObjectSetInteger(janela,nome,OBJPROP_HIDDEN,oculto);
    ObjectSetInteger(janela,nome,OBJPROP_BACK,fundo);
    ObjectSetInteger(janela,nome,OBJPROP_SELECTABLE,selecionavel);
    ObjectSetString(janela,nome,OBJPROP_TOOLTIP,dica_);
  }
 
  1. TdNutricula: 

    1 - Creates a horizontal line on the opening price;

    2 - Start drawing the lines from today onwards;

    Make up your mind. Either you want a horizontal line, or you want a trend line that starts from today.

  2. I doubt you want opening prices as you would have thousands of lines.

  3. TdNutricula:
       SetIndexBuffer(0,op,INDICATOR_DATA);
       SetIndexBuffer(1,cl,INDICATOR_DATA);

    3- It has to be editable where I can delete or move the created lines.

    Make up your mind. Buffer drawn lines are not editable.

  4. TdNutricula: I believe it has to do with the name of the OpenLine object 
    Object names must be unique. Append time (as an int) or non-series index (Bars-i)
 
William Roeder:
  1. Se decidir. Ou você deseja uma linha horizontal ou uma linha de tendência que comece hoje.

  2. Duvido que você queira preços de abertura, pois teria milhares de linhas.

  3. Se decidir. As linhas desenhadas do buffer não são editáveis.

  4. Os nomes dos objetos devem ser exclusivos. Tempo de acréscimo (como um int ) ou índice não série ( Barras-i )


1 - Horizontal line

2 Yes it can be thousands of lines since it starts from today onwards. Later I will put some conditionals to limit the creation of the line;

3- The lines must be editable. because I'm deleting some as time goes by.

4 - As I said I'm a layman, will I be very grateful if you can help me through the revised script? Thank you very much!!

 
#property indicator_chart_window
double Open1,Open2,Close1,Close2;
int Linha = 0;

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])                             
  { 
      Open1 =iOpen(_Symbol,PERIOD_M1,1);
      Open2 =iOpen(_Symbol,PERIOD_M1,2);
      Close1=iClose(_Symbol,PERIOD_M1,1);
      Close1=iClose(_Symbol,PERIOD_M1,2);
     
        Linha +=1;
        Comment(Linha);
  
       
      if   (Open1>Close1 && Open1>Close2) //Abertura da primeira barra maior que o Fechamento da segunda barra (gap invertido/linha amarela)
                       
        {
           CriarLinhaH(0,0,IntegerToString(Linha),Open1,clrRed,STYLE_DOT,1,true,false,true,"Fechamento do dia");
        }

      else
            
       CriarLinhaH(0,0,IntegerToString(Linha),Open1,clrWhite,STYLE_DOT,1,true,false,true,"Fechamento do dia");
         
   return(rates_total);
  }

//CÓDIGO PARA A CRIAÇÃO DE LINHAS HORIZONTAIS:
void CriarLinhaH(const long janela,
                 const int subjanela,
                 const string nome,
                 double preco,
                 color cor,
                 const ENUM_LINE_STYLE estilo,
                 const int tamanho,
                 const bool oculto,
                 const bool fundo,
                 bool selecionavel,
                 string dica_=NULL)
  {
    if (ObjectFind(janela,nome)==-1)
    //{ObjectCreate(janela,nome,OBJ_HLINE,subjanela,0,preco);}
    ObjectCreate(janela,nome,OBJ_HLINE,subjanela,0,preco);
    ObjectSetDouble(janela,nome,OBJPROP_PRICE,preco);
    ObjectSetInteger(janela,nome,OBJPROP_COLOR,cor);
    ObjectSetInteger(janela,nome,OBJPROP_STYLE,estilo);
    ObjectSetInteger(janela,nome,OBJPROP_WIDTH,tamanho);
    ObjectSetInteger(janela,nome,OBJPROP_HIDDEN,oculto);
    ObjectSetInteger(janela,nome,OBJPROP_BACK,fundo);
    ObjectSetInteger(janela,nome,OBJPROP_SELECTABLE,selecionavel);
    ObjectSetString(janela,nome,OBJPROP_TOOLTIP,dica_);
  }
  

I changed the code and the lines are being created, but I need to change the logic of the int line because several lines are being created at the same price, because the counter is per tick.


Example if in a period of one minute there are 20 ticks, 20 lines are created at the same price.


Does anyone have a solution to only change the counter number for each new candle?


Here's a new code. Part to be corrected: int Line = 0; Line +=1;

Reason: