Create price for Horizontal line on price scale in MQL5 for MT5

 
Hi How can I create price for Horizontal line, like original Horizontal line in meta trader, in MQL5 for MT5?
 
sirous88:
Hi How can I create price for Horizontal line, like original Horizontal line in meta trader, in MQL5 for MT5?
https://www.mql5.com/en/forum/134003#comment_3408636
 
sirous88:
Hi How can I create price for Horizontal line, like original Horizontal line in meta trader, in MQL5 for MT5?

Hello .


If you mean the price on the price scale (case1) there is this command 

ObjectSetInteger(ChartID(),"line_name",OBJPROP_PRICE_SCALE,true);

If you mean a price label on the chart (case2) consult this small code :

#property version   "1.00"

int OnInit()
  {
    ObjectsDeleteAll(ChartID(),"TESTS_");
  //create a line 
    color line_color=clrRed;
    ObjectCreate(ChartID(),"TESTS_LINE_A",OBJ_HLINE,0,0,iClose(_Symbol,_Period,1));
    ObjectSetInteger(ChartID(),"TETS_LINE_A",OBJPROP_COLOR,line_color);
    //if you want case 1 
      ObjectSetInteger(ChartID(),"TESTS_LINE_A",OBJPROP_PRICE_SCALE,true);
    //if you want case 2 
      add_price_text_to_line(ChartID(),"TESTS_LINE_A",ANCHOR_RIGHT_UPPER,iTime(_Symbol,_Period,1),line_color,20,NULL);
      
    /* Anchor options
    ANCHOR_LEFT_UPPER 
    ANCHOR_LEFT 
    ANCHOR_LEFT_LOWER
    ANCHOR_LOWER
    ANCHOR_RIGHT_LOWER
    ANCHOR_RIGHT
    ANCHOR_RIGHT_UPPER
    ANCHOR_UPPER
    ANCHOR_CENTER 
    */    
   return(INIT_SUCCEEDED);
  }
/*
pass the line name first - which you have created before
*/ 
void add_price_text_to_line(long chart_id,//which chart id 
                            string line_name,//the name of the hline
                            ENUM_ANCHOR_POINT anchor,//anchor of the text ,or positioning around the display point
                            datetime price_label_time,//the position of the price label in the x axis (time)
                            color price_label_color,
                            int price_label_fontsize,
                            string price_label_text){//NULL for just price
//first get the price line 
  ResetLastError();//reset the last error before this command so you know if it fails 
  double price=ObjectGetDouble(chart_id,line_name,OBJPROP_PRICE,0);
  //if there are no errors 
    //which means if the line exists 
      if(GetLastError()==0)
      {
      //now you have the price and the time so you create the text of the price  
        //give it a handy name 
          string name=line_name+"_PRICE_LABEL";
          ObjectCreate(chart_id,name,OBJ_TEXT,0,price_label_time,price);
          //and adjust it
            ObjectSetInteger(chart_id,name,OBJPROP_COLOR,price_label_color);
            ObjectSetInteger(chart_id,name,OBJPROP_FONTSIZE,price_label_fontsize);
            ObjectSetInteger(chart_id,name,OBJPROP_ANCHOR,anchor);
            //final property , if the label text sent is not empty set it as the label text 
            if(price_label_text!=NULL&&StringLen(price_label_text)>0)
              {
              ObjectSetString(chart_id,name,OBJPROP_TEXT,price_label_text);
              }//otherwise just place the price in
              else{
              ObjectSetString(chart_id,name,OBJPROP_TEXT,DoubleToString(price,_Digits));
              }
      ChartRedraw(chart_id);
      }else{
      Print("Line "+line_name+" Does not exist ");
      }
}