Detected Country () : English
Rectangle 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:

Rectangle Object

Level Advance [2018-03-29 10:23:14] - by hillzx
Featured Image

Creating a rectangle on the chart is pretty easy, we need 4 components:

  • Time1, time position (vertical)
  • Price1, price position (horizontal)
  • Time2, time position (vertical)
  • Price2, price position (horizontal)

box1



OK, try to make a custom input, so we make extern variable:

 

extern int Time1=250; //Mengambil harga dari candle ke 250 dari kanan
extern double Price1=0; //Harga tidak disebut, nanti kita set di init
extern int Time2=1; //Mengambil harga dari candle ke 1 dari kanan
extern double Price2=0; //Harga tidak disebut, nanti kita set di init
extern color LineColor=Yellow; // Warna garis
extern bool FillBox=false; // Apakah box diwarnai isinya? atau hanya garis saja?
extern ENUM_LINE_STYLE LineStyle=STYLE_SOLID; // Jenis garis
extern int LineWidth=2; // Lebar garis, berlaku khusus untuk STYLE_SOLID saja

 

Next we set the Init module (the code will be called 1 time only in the beginning), OnDeinit, and DeleteObject to clear the screen. Please note Price1 and Price2 will be  set to price index 250 and the price index 1 by default, the price can be override in the input tab thereafter:

 

int OnInit()
  {
//--- indicator buffers mapping
      DeleteObjects();
      
      if (Price1==0){Price1=High[250];};
      if (Price2==0){Price2=Low[1];};
      
      
//---
   return(INIT_SUCCEEDED);
  }
  
void DeleteObjects()
{
  for (int i = ObjectsTotal() - 1; i > -1; i--)
  {
  if (StringFind(ObjectName(i), "HiLLzXBox") >= 0) ObjectDelete(ObjectName(i));
  }
}


int deinit()
{
   DeleteObjects();
   return(0);
}



Next we create a module CreateBox to create a box, this code is made modular so it can be used repeatedly (if you want to use it modularly, please remember to make each objectname unique):

 

void CreateBox(double time1,double price1,double time2,double price2,color lineColor,bool isFilled,ENUM_LINE_STYLE style,int width){

         ObjectCreate("HiLLzXBox", OBJ_RECTANGLE, 0,time1, price1, time2, price2);
         ObjectSet("HiLLzXBox", OBJPROP_SCALE, 1.0);
         ObjectSet("HiLLzXBox", OBJPROP_COLOR, lineColor);
         ObjectSet("HiLLzXBox", OBJPROP_BACK, isFilled);
         ObjectSet("HiLLzXBox", OBJPROP_STYLE, style);
         ObjectSet("HiLLzXBox", OBJPROP_WIDTH, width);
         
}

 

Finally we call the module CreateBox inside OnCalculate module:

 

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[])
  {
//---
   CreateBox(Time[Time1],Price1,Time[Time2],Price2,LineColor,FillBox,LineStyle,LineWidth);
//--- return value of prev_calculated for next call
   return(rates_total);
  }



Done, we can change the box configuration to do testing via input tab:

box2

 


Here's the complete source code:

 

//+------------------------------------------------------------------+
//|                                                          Box.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

extern int Time1=250;
extern double Price1=0;
extern int Time2=1;
extern double Price2=0;
extern color LineColor=Yellow;
extern bool FillBox=false;
extern ENUM_LINE_STYLE LineStyle=STYLE_SOLID;
extern int LineWidth=2;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
      DeleteObjects();
      
      if (Price1==0){Price1=High[250];};
      if (Price2==0){Price2=Low[1];};
      
      
//---
   return(INIT_SUCCEEDED);
  }
  
void DeleteObjects()
{
  for (int i = ObjectsTotal() - 1; i > -1; i--)
  {
  if (StringFind(ObjectName(i), "HiLLzXBox") >= 0) ObjectDelete(ObjectName(i));
  }
}


int deinit()
{
   DeleteObjects();
   return(0);
}

//+------------------------------------------------------------------+
//| 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[])
  {
//---
   CreateBox(Time[Time1],Price1,Time[Time2],Price2,LineColor,FillBox,LineStyle,LineWidth);
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


void CreateBox(double time1,double price1,double time2,double price2,color lineColor,bool isFilled,ENUM_LINE_STYLE style,int width){

         ObjectCreate("HiLLzXBox", OBJ_RECTANGLE, 0,time1, price1, time2, price2);
         ObjectSet("HiLLzXBox", OBJPROP_SCALE, 1.0);
         ObjectSet("HiLLzXBox", OBJPROP_COLOR, lineColor);
         ObjectSet("HiLLzXBox", OBJPROP_BACK, isFilled);
         ObjectSet("HiLLzXBox", OBJPROP_STYLE, style);
         ObjectSet("HiLLzXBox", OBJPROP_WIDTH, width);
         
}


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






- Provided by HiLLzSoft Studio -