Download MetaTrader 5

Who can help tell why the code can't show "rectangle"?

To add comments, please log in or register
moonsboy
889
moonsboy  

With the code below, I want to show a rectangle frame around the latest 3 bars.  

Who can help tell why the code can't show the "rectangle frame"?  I couldn't find the root cause.

Also attached the print log below for reference.  Maybe you can copy the code below as an indicator for try too.

 

 

//+------------------------------------------------------------------+
//|                                                   CandleMean.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

int      xs,ys,xe,ye,xSize,ySize;
string objPrice="Mean_Price";
//+------------------------------------------------------------------+
//| Custom indicator initialization 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(rates_total);
  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
   if(id==CHARTEVENT_CLICK)
     {
      if(ObjectFind(objPrice)<0) ObjectCreate(ChartID(),objPrice,OBJ_RECTANGLE,0,0,0);
      ObjectSet(objPrice,OBJPROP_BORDER_TYPE,BORDER_FLAT);
      ObjectSet(objPrice,OBJPROP_WIDTH,3);
      ObjectSet(objPrice,OBJPROP_COLOR,clrDeepPink);

      ChartTimePriceToXY(ChartID(),0,Time[3],Low[0],xs,ye);
      ChartTimePriceToXY(ChartID(),0,Time[0],High[3],xe,ys);
      xSize=xe-xs;
      ySize=ye-ys; PrintFormat("xs %i,ys %i,xSiz %i,ySize %i",xs,ys,xSize,ySize);
      ObjectSet(objPrice,OBJPROP_XSIZE,xSize);
      ObjectSet(objPrice,OBJPROP_YSIZE,ySize);
      ObjectSet(objPrice,OBJPROP_XDISTANCE,xs);
      ObjectSet(objPrice,OBJPROP_YDISTANCE,ys);
     }
  }
//+------------------------------------------------------------------+
Keith Watford
Moderator
9641
Keith Watford  
OBJ_RECTANGLE is anchored by time and price, not pixels
moonsboy
889
moonsboy  

Thanks. But how to change the rectangle size? How to change it to a frame instead of solid filling? 

moonsboy
889
moonsboy  
Besides, how to create a line segment as object?
moonsboy
889
moonsboy  
Besides, how to skip weekend with code if counting bars for the object of "rectangle"?
Keith Watford
Moderator
9641
Keith Watford  

First decide what you want to draw

if OBJ_RECTANGLE 

change size by moving the anchor points

If set as background is true, it will show as a frame. If false, it will have a solid fill

I sometimes use 4 trend lines instead of a rectangle 

moonsboy
889
moonsboy  
GumRai:

First decide what you want to draw

if OBJ_RECTANGLE 

change size by moving the anchor points

If set as background is true, it will show as a frame. If false, it will have a solid fill

I sometimes use 4 trend lines instead of a rectangle 

 

 

Thank you very much! Realized! 

Zirkon
122
Zirkon  
Don't you need to use RECTANGLE_LABEL instead ?
To add comments, please log in or register