problem in drawing obj

 

Hi,

I started to study mql4 and I'm trying to draw some lines into the chart.

Using some examples, I developed the following (it simply draws horizontal lines on open,close and mid of the previous weekly candle), but I notice that sometimes it doesn't work for some instrument and sometime it take long times.

Is there something wrong in the code? or is it something related to initialization/refresh...

Many thanks if someone will help me!


string CLOSE = "CLOSE";
string OPEN = "OPEN";
string MID = "MID";


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
  ObjectDelete(CLOSE);
  ObjectDelete(OPEN);
  ObjectDelete(MID);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  
   deinit();

double OpenW = iOpen(NULL,PERIOD_W1,1);
double CloseW = iClose(NULL,PERIOD_W1,1);

double Delta=fabs(CloseW-OpenW);
double mid_price=fmin(CloseW,OpenW)+ Delta/2;


ObjectCreate(CLOSE, OBJ_HLINE , 0,Time[0], CloseW);

ObjectSet(CLOSE, OBJPROP_STYLE, STYLE_DOT);
           ObjectSet(CLOSE, OBJPROP_COLOR, Black);
           ObjectSet(CLOSE, OBJPROP_WIDTH, 1);

ObjectCreate(OPEN, OBJ_HLINE , 0,Time[0], OpenW);

ObjectSet(OPEN, OBJPROP_STYLE, STYLE_DOT);
           ObjectSet(OPEN, OBJPROP_COLOR, Black);
           ObjectSet(OPEN, OBJPROP_WIDTH, 1);
           
ObjectCreate(MID, OBJ_HLINE , 0,Time[0], mid_price);

ObjectSet(MID, OBJPROP_STYLE, STYLE_DOT);
           ObjectSet(MID, OBJPROP_COLOR, Blue);
           ObjectSet(MID, OBJPROP_WIDTH, 1);       




   return(0);
  }
 
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  
   deinit();
you call deinit() function directly after start, why?
 
MBZ13: I notice that sometimes it doesn't work for some instrument and sometime it take long times.
  1. The first time you run, there maybe no weekly data, and the lines are drawn at zero. After weekly history updates (10 seconds or so,) the next tick should show the lines properly. If the market is closed, just right click -> Refresh.
  2. I see no problems, except if you place two or more of them on the chart (because they use the same object names.)
  3. // double Delta=fabs(CloseW-OpenW);
    // double mid_price=fmin(CloseW,OpenW)+ Delta/2;
    double mid_price=(CloseW + OpenW) /2; // Simplified


Marco vd Heijden: you call deinit() function directly after start, why?
Just to delete the three lines, so they move at start of a new week.
 
Marco vd Heijden:
you call deinit() function directly after start, why?


Hi,

I added it because when a new candle start the lines previously drawn remain into the chart and in addition the new ones are drawn.

However I just tried to remove deinit line, but I have the same problem... sometimes lines are drawn sometimes not... 

many thanks

 
whroeder1:
  1. The first time you run, there maybe no weekly data, and the lines are drawn at zero. After weekly history updates (10 seconds or so,) the next tick should show the lines properly. If the market is closed, just right click -> Refresh.
  2. I see no problems, except if you place two or more of them on the chart (because they use the same object names.)


Just to delete the three lines, so they move at start of a new week.


Hi,

1. I thought to it ad I waited for today new weekly candle, but it is the same story. I tried also with Refresh.

2. not clear for me this point: the three objects have three different name (OPEN;CLOSE;MID)... maybe I'm interpreting wrongly what you wrote..


many thanks

 
MBZ13: 2. not clear for me this point: the three objects have three different name (OPEN;CLOSE;MID)... maybe I'm interpreting wrongly what you wrote..
If you add a second copy to the same chart, they will both be deleting and creating the same three objects.
 

IMO it's better to create them once and move afterwards.

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//----
   if(ObjectCreate(CLOSE, OBJ_HLINE , 0, 0, 0))
     {
      ObjectSet(CLOSE, OBJPROP_STYLE, STYLE_DOT);
      ObjectSet(CLOSE, OBJPROP_COLOR, Black);
      ObjectSet(CLOSE, OBJPROP_WIDTH, 1);
     }
   else
      return(INIT_FAILED);
//---
   if(ObjectCreate(OPEN, OBJ_HLINE , 0, 0, 0))
     { 
      ObjectSet(OPEN, OBJPROP_STYLE, STYLE_DOT);
      ObjectSet(OPEN, OBJPROP_COLOR, Black);
      ObjectSet(OPEN, OBJPROP_WIDTH, 1);
     }
   else
      return(INIT_FAILED);
//---
   if(ObjectCreate(MID, OBJ_HLINE , 0, 0, 0))
     { 
      ObjectSet(MID, OBJPROP_STYLE, STYLE_DOT);
      ObjectSet(MID, OBJPROP_COLOR, Blue);
      ObjectSet(MID, OBJPROP_WIDTH, 1);          
     }
   else
      return(INIT_FAILED);
//----
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectDelete(CLOSE);
   ObjectDelete(OPEN);
   ObjectDelete(MID);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
   static datetime prevTime;
   if(prevTime!=iTime(NULL,PERIOD_W1,0))
     {
      double OpenW = iOpen(NULL,PERIOD_W1,1);
      double CloseW = iClose(NULL,PERIOD_W1,1);
      
      double Delta=fabs(CloseW-OpenW);
      double mid_price=fmin(CloseW,OpenW)+ Delta/2;
      
      if(!ObjectSetDouble(ChartID(),CLOSE,OBJPROP_PRICE,CloseW) ||
         !ObjectSetDouble(ChartID(),OPEN,OBJPROP_PRICE,OpenW) ||
         !ObjectSetDouble(ChartID(),MID,OBJPROP_PRICE,mid_price))
         return(0);
      
      prevTime=iTime(NULL,PERIOD_W1,0);
     }
//---
   return(0);
  }
//+------------------------------------------------------------------+
 
Ernst Van Der Merwe:

IMO it's better to create them once and move afterwards.


Hi,

I tried to run your code but the result is the same, sometimes it works sometime not (I tried with GBP/USD and it works while nothing for USDOLLAR and XAU/USD)... also opening a new chart or refreshing...

the code seems to be correct... really I do not know.

many tanks for your suggestion. 

Reason: