Top Menu

Feedburner

Right-Side Top Menu

Forex Blog
My Forex experience and some Forex related information that might be useful to other traders

Drawing Filled Rectangles in MQL4/MQL5

July 26th, 2010

(This post will be interesting only to those who code in MQL4 or MQL5 languages.)

Drawing a rectangle in MQL-coded indicator or expert advisor is quite an easy task in both MetaTrader 4 and MetaTrader 5 platforms. All you have to do is to call the ObjectCreate() function with rather simple parameters. For example, in MQL4 it goes like this:

ObjectCreate("Rectangle", OBJ_RECTANGLE, 0, Time[0], price1, Time[1], price2);

And in MQL5 it would look almost the same:

ObjectCreate(0, "Rectangle", OBJ_RECTANGLE, 0, Time[0], price1, Time[1], price2);

But filling the rectangle in MQL5 or drawing it unfilled in MQL4 wasn’t so obvious to me. The rectangles come unfilled by default in MT5 and filled by default in MT4. OBJPROP_BACK is the property that responds for that. For example, to make a rectangle unfilled in MQL4 you can call this function:

ObjectSet("Rectangle", OBJPROP_BACK, false);

And to fill the rectangle in MQL5 you would change the property to true:

ObjectSetInteger(0, "Rectangle", OBJPROP_BACK, true);

If you have any questions or comments regarding drawing of the rectangles in MQL4 or MQL5, please, use the commentary form below.


4 Responses to “Drawing Filled Rectangles in MQL4/MQL5”

  1. Guillermo

    I am having problems building the rectangle because I am unable to convert the dates correctly.It has to do with the StringToTime and TimeTo String Functions. This indicator was developed correctly on MQ4 but I cant get it to work on MQ5..
    Could somebody please help here? I am including the source,and THE PROBLEM IS DEBUGGED WITH A COMMENT. thanks:

    //+------------------------------------------------------------------+
    //|                                                             Sessions.mq4 |
    //+------------------------------------------------------------------+
    #property indicator_chart_window
     
     
    //:::::::::::::::::::::::::::::::::::::::::::::
    #include 
    #include 
    #include 
    //#include 
    #include 
    #include 
    //Etc.
    //:::::::::::::::::::::::::::::::::::::::::::::::
     
    input			int			eiNumberOfDays		=	02;    // Number Of Days to Draw rectancle(history)
    input			int			eiBrokerHHOffset	=	03;    // Broker Offset (FXDD)
    input			color		ecSydneyColor		=	Khaki;
    input			color		ecTokyoColor		=	Magenta;
    input			color		ecLondonColor		=	Orange;
    input			color		ecNewYorkColor		=	Blue;
    input			bool		ebShowPrice			=	true;    // Show min/max session prices
    input			color		ecColorFont			=	Black;   // Text font Color
    input			int			eiSizeFont			=	8;     // Text Font Size
    input			int			eiOffSet			=	10;    // Text Font Adjustment
    input			bool		ebTesting			=	false;
     
    				int			iCountedBars		=	0;
    				datetime	tClockTime;
    				datetime	tStart;
    				string		sSessionName[]		=	{"      Sydney", "     Tokyo", "      London", "         New York"};
    				int			iSessionBegin[4]	=	{    00,      02,     10,       15};
    				color		cSessionColor[]		=	{    Khaki,     Magenta,    Orange,      Blue};
     
    //+------------------------------------------------------------------+
    //| Custom indicator initialization function                         |
    //+------------------------------------------------------------------+ing
     
    void OnInit()
    	{
    		//:::::::::::::::::::::::::::::::::::::::::::::
    		double	dAsk=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
    		double	dBid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
    		int		iBars=Bars(Symbol(),PERIOD_CURRENT);
    		double	dPoint=Point();
    		//Etc.
    		//:::::::::::::::::::::::::::::::::::::::::::::::
     
    		for(int o=0;o<4;o++)
    			iSessionBegin[o]+=eiBrokerHHOffset-3;
    		DeleteSessionObjects();
    		cSessionColor[0] = ecSydneyColor;
    		cSessionColor[1] = ecTokyoColor;
    		cSessionColor[2] = ecLondonColor;
    		cSessionColor[3] = ecNewYorkColor;
    		for (int i=0; i<eiNumberOfDays; i++)
    			for(int s=0; s=0;i--)
    			if(StringSubstr(ObjectName(i),0,8)=="Session.")
    				ObjectDelete(ObjectName (i)); 
    	}
     
    //+------------------------------------------------------------------+
    //| Custom indicator iteration function                              |
    //+------------------------------------------------------------------+
    int OnCalculate(const int RatesTotal,
                    const int PrevCalculated,
                    const datetime& time[],
                    const double& open[],
                    const double& high[],
                    const double& low[],
                    const double& close[],
                    const long& tickVolume[],
                    const long& volume[],
                    const int& spread[])
    	{
    		ArraySetAsSeries(open,true);
    		ArraySetAsSeries(high,true);
    		ArraySetAsSeries(low,true);
    		ArraySetAsSeries(close,true);
    		ArraySetAsSeries(time,true);
    		ArraySetAsSeries(volume,true);
    		ArraySetAsSeries(spread,true);
     
    		//:::::::::::::::::::::::::::::::::::::::::::::
    		double	dAsk=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
    		double	dBid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
    		int		iBars=Bars(Symbol(),PERIOD_CURRENT);
    		double	dPoint=Point();
    		//Etc.
    		//:::::::::::::::::::::::::::::::::::::::::::::::
     
    		if(ebTesting)
    			tClockTime = GlobalVariableGet("CurrentTime");
    		if(tClockTime ==0)
    			tClockTime = TimeCurrent();
    		datetime	dt=tClockTime+(60*60*24);
    		if(iBars==iCountedBars)
    			return(0);
    		iCountedBars = iBars;
    		for (int i=0; i<eiNumberOfDays; i++)
    			{
    				for(int s=0; s<4; s++)
    					{
    						if (ebShowPrice && i==0)
    							DrawSessionHiLo(dt, s);
    						DrawSession(dt, s,i);
    					}
    				dt-=60*60*24;
    			}
    		return(0);
    	}
     
    void DrawSession(datetime st, int iSession, int iNoOfDays)
    	{
    		datetime t1, t2;
    		double   p1, p2;
    		int      b1, b2;
     
    		t1=StringToTime(TimeToString(st,TIME_DATE))+iSessionBegin[iSession]*(60*60);
    		t2=t1+(9*60*60);
    		if(TimeDayOfWeek(st)==1&&iSession==0)
    			tStart=t1;
    		//if(TimeDayOfWeek(dt)==5&&iSession==3)
    		// t2=tStart;
    		b1=iBarShift(NULL, 0, t1);
    		b2=iBarShift(NULL, 0, t2);
    		p1=iHighest(NULL, 0, MODE_HIGH, b1-b2, b2);
    		p2=iLowest (NULL, 0, MODE_LOW , b1-b2, b2);
    		//p2	=	1.3948;
    		//p1	=	1.3893;
    		p1	=	ChartGetDouble(0,CHART_PRICE_MAX,0);
    		p2	=	ChartGetDouble(0,CHART_PRICE_MIN,0);
     
     
    		string sObjectName = "Session."+(iSession)+"."+iNoOfDays;
     
    		ObjectSet(sObjectName, OBJPROP_TIME1 , t1);
    		ObjectSet(sObjectName, OBJPROP_PRICE1, p1);
     
    		ObjectSet(sObjectName, OBJPROP_TIME2 , t2);
    		ObjectSet(sObjectName, OBJPROP_PRICE2, p2);
     
    		ObjectSet(sObjectName+".Name",OBJPROP_TIME1,t1);
    		ObjectSet(sObjectName+".Name",OBJPROP_PRICE1,p1);
    		//t1	=	5000;
    		//t2	=	t1+(9*60*60);
    		t1=StringToTime(TimeToString(st,TIME_DATE));//+iSessionBegin[iSession]*(60*60);
    		t2=t1+(40*60*60);
     
    		Comment("<<<<<t1:"+t1+",p1:"+ p1+"....t2:"+ t2+",p2:"+ p2+"   T1^T2 "+
    		TimeToString(t1,TIME_DATE)+TimeToString(t1,TIME_SECONDS)+",,,"+TimeToString(t2,TIME_DATE)+TimeToString(t2,TIME_SECONDS));
    	}
     
    void DrawSessionHiLo(datetime st, int iSession)
    	{
    		datetime t1, t2;
    		double   p1, p2;
    		int      b1, b2;      
     
    		t1=StringToTime(TimeToString(st,TIME_DATE))+iSessionBegin[iSession]*60*60;
    		t2=t1+(9*60*60);
     
    		b1=iBarShift(NULL, 0, t1);
    		b2=iBarShift(NULL, 0, t2);
    		p1=iHighest(NULL, 0, MODE_HIGH, b1-b2, b2);
    		p2=iLowest (NULL, 0, MODE_LOW , b1-b2, b2);
    		p1	=	ChartGetDouble(0,CHART_PRICE_MAX,0);
    		p2	=	ChartGetDouble(0,CHART_PRICE_MIN,0);
     
    	}
    //+------------------------------------------------------------------+

    Reply

    admin Reply:

    1. Never (and I mean NEVER) insert the code as a commentary. WP messes up code and it’s a pain in the ass to copy it into MQL. Host the file somewhere and give a link to it in the comment.

    2. You have a lot of improperly converted functions here (dozens!). Please, see other MQL5 indicators to see how the Object, i* and other functions are converted from MQL4 to MQL5.

    3. Why do you have those empty #include directives in the beginning?

    Reply

  2. Guillermo

    I apologize, sorry.. Was a typecasting problem…

    Reply

  3. German

    Hello: How can I put “style transparent red” ,for example, in a rectangle?. Thanks.

    Best Regards.

    Reply

Leave a Reply

required
required (will not be published)
optional

Follow EarnForex Blog on Facebook