Detected Country () : English
Line Vertical & Horizontal Object - codingforex.com

Welcome to CodingForex.Com!

Knowledge portal where you can learn how to create forex indicators and robot.

Click button below to choose level that suite you:

Line Vertical & Horizontal Object

Level Advance [2018-02-20 21:30:34] - by hillzx
Featured Image

This time we will create a horizontal and vertical line on the chart. Horizontal line uses price, while Vertical line uses Time reference.

By the way we will try to make an indicator that shows 3 pieces of line which are:

  1. One horizontal line at the opening price of a particular candle
  2. One Horizontal line at a certain candle closing price
  3. One Vertical line at a certain candle time

The first stage, we create a variable to accommodate the price data, color, line width, and index candle:

 

double upLine;
double downLine;
extern color colorUp=Yellow;
extern color colorDown=Lime;
extern color colorVertical=Red;
extern int widthLine1=2;
extern int widthLine2=2;
extern int candleIndex=1;

 

Create a DeleteObjects module to clear the screen of the object when the indicator is stopped:

 

void DeleteObjects()
{
  for (int i = ObjectsTotal() - 1; i > -1; i--)
  {
  if (StringFind(ObjectName(i), "HLine") >= 0) ObjectDelete(ObjectName(i));
  }
}

 

Clean the screen in the OnDeInit menu:

 

void OnDeinit(const int reason)
{
   DeleteObjects();
   return;
}



Create InitPrice () module to update pricing data, and create DrawLines () module to draw lines:

 

void InitPrice(){
upLine=Open[candleIndex];
downLine=Close[candleIndex];
}

void DrawLines(){
DeleteObjects();
ObjectCreate("HLineUp", OBJ_HLINE, 0, 0, upLine );
ObjectSet("HLineUp", OBJPROP_COLOR, colorUp);
ObjectSet("HLineUp", OBJPROP_WIDTH, widthLine1);
ObjectSet("HLineUp", OBJPROP_STYLE,STYLE_SOLID);  
ObjectSetText("HLineUp", "Upper Line", 12, "Arial", colorUp);     
 
ObjectCreate("HLineDown", OBJ_HLINE, 0, 0, downLine);
ObjectSet("HLineDown", OBJPROP_COLOR, colorDown);
ObjectSet("HLineDown", OBJPROP_WIDTH, widthLine2);
ObjectSet("HLineDown", OBJPROP_STYLE,STYLE_SOLID);     
ObjectSetText("HLineDown", "Bottom Line", 12, "Arial", colorDown);   

ObjectCreate("HLineV",OBJ_VLINE,0,Time[candleIndex],0);
ObjectSet("HLineV", OBJPROP_COLOR, colorVertical);
ObjectSet("HLineV", OBJPROP_WIDTH, widthLine2);
ObjectSet("HLineV", OBJPROP_STYLE,STYLE_SOLID);     
ObjectSetText("HLineV", "Bottom Line", 12, "Arial", colorVertical);   

}



Next call the two modules in OnInit module, meaning this code only runs 1 time in initialization.

 

int OnInit()
{
   InitPrice();
   DrawLines();
   return(INIT_SUCCEEDED);
}

Done .. First we must determine the index candle in the input menu, then the indicator will display the opening and closing price line of the intended candle along with vertical lines as a marker, see the following picture:

lines

Here's the complete source code:

 

//+------------------------------------------------------------------+
//|                                                        HLine.mq4 |
//|                                                 hillzx@gmail.com |
//|                                           http://codingforex.com |
//+------------------------------------------------------------------+
#property copyright "hillzx@gmail.com"
#property link      "http://codingforex.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

double upLine;
double downLine;
extern color colorUp=Yellow;
extern color colorDown=Lime;
extern color colorVertical=Red;
extern int widthLine1=2;
extern int widthLine2=2;
extern int candleIndex=1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   InitPrice();
   DrawLines();
   return(INIT_SUCCEEDED);
}
  
void OnDeinit(const int reason)
{
   DeleteObjects();
   return;
}
  
  
void DeleteObjects()
{
  for (int i = ObjectsTotal() - 1; i > -1; i--)
  {
  if (StringFind(ObjectName(i), "HLine") >= 0) ObjectDelete(ObjectName(i));
  }
}

//+------------------------------------------------------------------+
//| 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[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


void InitPrice(){
upLine=Open[candleIndex];
downLine=Close[candleIndex];
}

void DrawLines(){
DeleteObjects();
ObjectCreate("HLineUp", OBJ_HLINE, 0, 0, upLine );
ObjectSet("HLineUp", OBJPROP_COLOR, colorUp);
ObjectSet("HLineUp", OBJPROP_WIDTH, widthLine1);
ObjectSet("HLineUp", OBJPROP_STYLE,STYLE_SOLID);  
ObjectSetText("HLineUp", "Upper Line", 12, "Arial", colorUp);     
 
ObjectCreate("HLineDown", OBJ_HLINE, 0, 0, downLine);
ObjectSet("HLineDown", OBJPROP_COLOR, colorDown);
ObjectSet("HLineDown", OBJPROP_WIDTH, widthLine2);
ObjectSet("HLineDown", OBJPROP_STYLE,STYLE_SOLID);     
ObjectSetText("HLineDown", "Bottom Line", 12, "Arial", colorDown);   

ObjectCreate("HLineV",OBJ_VLINE,0,Time[candleIndex],0);
ObjectSet("HLineV", OBJPROP_COLOR, colorVertical);
ObjectSet("HLineV", OBJPROP_WIDTH, widthLine2);
ObjectSet("HLineV", OBJPROP_STYLE,STYLE_SOLID);     
ObjectSetText("HLineV", "Bottom Line", 12, "Arial", colorVertical);   

}


Good luck, do not forget to share and follow our official facebook and youtube, happy trading!



Previous Article

Featured Image

Button Object

Next Article

Featured Image

Rectangle Object




- Provided by HiLLzSoft Studio -