Download MetaTrader 5
To add comments, please log in or register
Traders have already contributed more than 300 articles. Write your own one!
mostafalotfi
185
mostafalotfi 2014.03.20 15:34 

Hi Guys

could you please have a look at the below code and let me know what is the problem? this expert supposed to calculate some information when user drop the Rectangle on the chart! it works fine when i put the Rectangle for the first time but when i put another Rectangle, it doesn't work .... when user put another rectangle on the chart then 1) expert should clean everything and then calculate everything and draw the lines from beginning based on the new rectangle

//+------------------------------------------------------------------+
//|                                             OnChartEventTest.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {

       
       //---
   ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_CREATE,true);
//--- enable object delete events
   ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_DELETE,true);
 
//---
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   Comment("");
  
     int k;
     int  obj_total= ObjectsTotal();  
  for(k= obj_total; k>=0; k--)
    {
   string name= ObjectName(k);
  if (StringSubstr(name,0,10)=="Rectangle"){ObjectDelete(name);}
  if (StringSubstr(name,0,9)=="MLMOHITI "){ObjectDelete(name);}
    }

  }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,         // Event identifier  
                  const long& lparam,   // Event parameter of long type
                  const double& dparam, // Event parameter of double type
                  const string& sparam) // Event parameter of string type
  {
     

   datetime time1=0;
   datetime time2=0;
   datetime timerast=0;
   datetime timechap=0;
   
    RefreshRates();
//--- the mouse has been clicked on the graphic object
         if(id==CHARTEVENT_OBJECT_CREATE || id==CHARTEVENT_OBJECT_CHANGE)
         {      
        
                 int k33;
                 int  obj_total33= ObjectsTotal();
                 for(k33= obj_total33; k33>=0; k33--)
                   {
                  string name33= ObjectName(k33);
                   if (StringSubstr(name33,0,9)=="Rectangle")
                     { 
                        time1=ObjectGet(name33,0);
                        time2=ObjectGet(name33,2);
                        if(time1>time2)timerast=time1;
                        if(time1>time2)timechap=time2;
                        if(time1<time2)timerast=time2;
                        if(time1<time2)timechap=time1;
                        
                        int k24;
                        int  obj_total24= ObjectsTotal();  
                        for(k24= obj_total24; k24>=0; k24--)
                         {
                           string name24= ObjectName(k24);
                           if (StringSubstr(name24,0,9)=="MLMOHITI "){ObjectDelete(name24);}
                         }
                     }
                   }                   

                     
                                         
                     int i;
                     int body;
                     double supply=0;
                     int supplycount=0;
                     double demand=0;
                     int demandcount=0;
                    
                     for(i=iBarShift(NULL,0,timerast);i<=iBarShift(NULL,0,timechap);i++)
                     {
                        body=((iClose(NULL,0,i)-iOpen(NULL,0,i))/Point);
                        
                        if (body>0)
                           {
                           supply=body+supply;
                           supplycount++;
                           }
                        if (body<0)
                           {
                           demand=body+demand;
                           demandcount++;
                           }   
                     }
                     
                     double highest=0;
                     double lowest=1000;
                     
                     for(i=iBarShift(NULL,0,timerast);i<=iBarShift(NULL,0,timechap);i++)
                     {
                     double high= iHigh(NULL,0,i);
                     if(high>highest) highest= high;
                     
                     double low= iLow(NULL,0,i);
                     if(low<lowest) lowest= low;
                         
                     }
                     

                     
                     double supplypower=MathAbs(supply/supplycount);
                     double demandpower=MathAbs(demand/demandcount);
                     double middleline=((highest-lowest)/2)+lowest;
           
                     
                     ObjectCreate("MLMOHITI Left",OBJ_TREND,0,timechap,lowest,timechap,highest);
                     ObjectSet("MLMOHITI Left",OBJPROP_RAY,0);
                     ObjectSet("MLMOHITI Left",OBJPROP_COLOR,Brown);
                     
                     
                     ObjectCreate("MLMOHITI Right",OBJ_TREND,0,timerast,lowest,timerast,highest);
                     ObjectSet("MLMOHITI Right",OBJPROP_RAY,0);
                     ObjectSet("MLMOHITI Right",OBJPROP_COLOR,Brown);
                     
                     ObjectCreate("MLMOHITI low",OBJ_TREND,0,timechap,lowest,timerast,lowest);
                     ObjectSet("MLMOHITI low",OBJPROP_RAY,0);
                     ObjectSet("MLMOHITI low",OBJPROP_COLOR,Brown);
                     
                     ObjectCreate("MLMOHITI up",OBJ_TREND,0,timechap,highest,timerast,highest);
                     ObjectSet("MLMOHITI up",OBJPROP_RAY,0);
                     ObjectSet("MLMOHITI up",OBJPROP_COLOR,Brown);
                     
                     ObjectCreate("MLMOHITI middle",OBJ_TREND,0,timechap,middleline,timerast,middleline);
                     ObjectSet("MLMOHITI middle",OBJPROP_RAY,0);
                     ObjectSet("MLMOHITI middle",OBJPROP_COLOR,Brown);
                     ObjectSet("MLMOHITI middle",OBJPROP_WIDTH,2);
                     
                     ObjectCreate("MLMOHITI SupplyPower",OBJ_LABEL,0,0,0,0,0);
                     ObjectSet("MLMOHITI SupplyPower",OBJPROP_CORNER,3);
                     ObjectSet("MLMOHITI SupplyPower",OBJPROP_XDISTANCE,70);
                     ObjectSet("MLMOHITI SupplyPower",OBJPROP_YDISTANCE,70);
                     ObjectSetText("MLMOHITI SupplyPower","Suppply Power = "+DoubleToStr(supplypower,3),14,"arial",Red);  
                     
                     ObjectCreate("MLMOHITI DemandPower",OBJ_LABEL,0,0,0,0,0);
                     ObjectSet("MLMOHITI DemandPower",OBJPROP_CORNER,3);
                     ObjectSet("MLMOHITI DemandPower",OBJPROP_XDISTANCE,70);
                     ObjectSet("MLMOHITI DemandPower",OBJPROP_YDISTANCE,90);
                     ObjectSetText("MLMOHITI DemandPower","Demand Power = "+DoubleToStr(demandpower,3),14,"arial",Red);
                     
                     int k2;
                       int  obj_total2= ObjectsTotal();  
                       for(k2= obj_total2; k2>=0; k2--)
                         {
                        string name= ObjectName(k2);
                       if (StringSubstr(name,0,9)=="Rectangle"){ObjectDelete(name);}
                         }
                                                                          
                     //Comment(supplypower,"\n",demandpower               
                               
                            
         }
         
     if(id==CHARTEVENT_CLICK)
     {
      Comment("The coordinates of the mouse click on the chart are: x = ",lparam,"  y = ",dparam);
      
     }


//+------------------------------------------------------------------+
   }
Ovo Cz
2954
Ovo Cz 2014.03.20 16:07  
The object name is in the sparam parameter, you do not need to guess it. Add some Print statements to the event handler to learn if the event is fired or no.
qjol
3243
qjol 2014.03.20 16:24  
just for curiosity what's the RefreshRates(); doing in your code ?
mostafalotfi
185
mostafalotfi 2014.03.20 19:31  
qjol:
just for curiosity what's the RefreshRates(); doing in your code ?

honestly, im totally blind and i dono why its not working ... im trying everything :(
mostafalotfi
185
mostafalotfi 2014.03.20 19:33  
Ovo:
The object name is in the sparam parameter, you do not need to guess it. Add some Print statements to the event handler to learn if the event is fired or no.


Hi Ovo,

Could you please explain it little more?

qjol
3243
qjol 2014.03.20 19:53  
//+------------------------------------------------------------------+
//|                                             OnChartEventTest.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

datetime time1=0;
datetime time2=0;
datetime timerast=0;
datetime timechap=0;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
  ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_CREATE,true);
//--- enable object delete events
  ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_DELETE,true);
//---
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
   {
   //---
   Comment("");
   
   int k;
   int  obj_total= ObjectsTotal();  
   for(k= obj_total; k>=0; k--)
      {
      string name= ObjectName(k);
      if (StringSubstr(name,0,10)=="Rectangle"){ObjectDelete(name);}
      if (StringSubstr(name,0,9)=="MLMOHITI "){ObjectDelete(name);}
      }
   }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,         // Event identifier  
                  const long& lparam,   // Event parameter of long type
                  const double& dparam, // Event parameter of double type
                  const string& sparam) // Event parameter of string type
  {
    
//--- the mouse has been clicked on the graphic object
   if(id==CHARTEVENT_OBJECT_CREATE || id==CHARTEVENT_OBJECT_CHANGE)
      {      
      int k33;
      int  obj_total33= ObjectsTotal();
      for(k33= obj_total33; k33>=0; k33--)
         {
         string name33= ObjectName(k33);
         if (StringSubstr(name33,0,9)=="Rectangle")
            { 
            time1=ObjectGet(name33,0);
            time2=ObjectGet(name33,2);
            if(time1>time2)timerast=time1;
            if(time1>time2)timechap=time2;
            if(time1<time2)timerast=time2;
            if(time1<time2)timechap=time1;
            
            int k24;
            int  obj_total24= ObjectsTotal();  
            for(k24= obj_total24; k24>=0; k24--)
               {
               string name24= ObjectName(k24);
               if (StringSubstr(name24,0,9)=="MLMOHITI "){ObjectDelete(name24);}
               }
            }
         }                   
      
      int i;
      int body;
      double supply=0;
      int supplycount=0;
      double demand=0;
      int demandcount=0;
      
      for(i=iBarShift(NULL,0,timerast);i<=iBarShift(NULL,0,timechap);i++)
         {
         body=((iClose(NULL,0,i)-iOpen(NULL,0,i))/Point);
         
         if (body>0)
            {
            supply=body+supply;
            supplycount++;
            }
         if (body<0)
            {
            demand=body+demand;
            demandcount++;
            }   
         }
      
      double highest=0;
      double lowest=1000;
      
      for(i=iBarShift(NULL,0,timerast);i<=iBarShift(NULL,0,timechap);i++)
         {
         double high= iHigh(NULL,0,i);
         if(high>highest) highest= high;
            double low= iLow(NULL,0,i);
         if(low<lowest) lowest= low;
         }
      
      double supplypower=MathAbs(supply/supplycount);
      double demandpower=MathAbs(demand/demandcount);
      double middleline=((highest-lowest)/2)+lowest;
      
      
      ObjectCreate("MLMOHITI Left",OBJ_TREND,0,timechap,lowest,timechap,highest);
      ObjectSet("MLMOHITI Left",OBJPROP_RAY,0);
      ObjectSet("MLMOHITI Left",OBJPROP_COLOR,Brown);
      
      
      ObjectCreate("MLMOHITI Right",OBJ_TREND,0,timerast,lowest,timerast,highest);
      ObjectSet("MLMOHITI Right",OBJPROP_RAY,0);
      ObjectSet("MLMOHITI Right",OBJPROP_COLOR,Brown);
      
      ObjectCreate("MLMOHITI low",OBJ_TREND,0,timechap,lowest,timerast,lowest);
      ObjectSet("MLMOHITI low",OBJPROP_RAY,0);
      ObjectSet("MLMOHITI low",OBJPROP_COLOR,Brown);
      
      ObjectCreate("MLMOHITI up",OBJ_TREND,0,timechap,highest,timerast,highest);
      ObjectSet("MLMOHITI up",OBJPROP_RAY,0);
      ObjectSet("MLMOHITI up",OBJPROP_COLOR,Brown);
      
      ObjectCreate("MLMOHITI middle",OBJ_TREND,0,timechap,middleline,timerast,middleline);
      ObjectSet("MLMOHITI middle",OBJPROP_RAY,0);
      ObjectSet("MLMOHITI middle",OBJPROP_COLOR,Brown);
      ObjectSet("MLMOHITI middle",OBJPROP_WIDTH,2);
      
      ObjectCreate("MLMOHITI SupplyPower",OBJ_LABEL,0,0,0,0,0);
      ObjectSet("MLMOHITI SupplyPower",OBJPROP_CORNER,3);
      ObjectSet("MLMOHITI SupplyPower",OBJPROP_XDISTANCE,70);
      ObjectSet("MLMOHITI SupplyPower",OBJPROP_YDISTANCE,70);
      ObjectSetText("MLMOHITI SupplyPower","Suppply Power = "+DoubleToStr(supplypower,3),14,"arial",Red);  
      
      ObjectCreate("MLMOHITI DemandPower",OBJ_LABEL,0,0,0,0,0);
      ObjectSet("MLMOHITI DemandPower",OBJPROP_CORNER,3);
      ObjectSet("MLMOHITI DemandPower",OBJPROP_XDISTANCE,70);
      ObjectSet("MLMOHITI DemandPower",OBJPROP_YDISTANCE,90);
      ObjectSetText("MLMOHITI DemandPower","Demand Power = "+DoubleToStr(demandpower,3),14,"arial",Red);
      
      int k2;
      int  obj_total2= ObjectsTotal();  
      for(k2= obj_total2; k2>=0; k2--)
         {
         string name= ObjectName(k2);
         if (StringSubstr(name,0,9)=="Rectangle"){ObjectDelete(name);}
         }
      //Comment(supplypower,"\n",demandpower               
      }
   if(id==CHARTEVENT_CLICK)
      {
      Comment("The coordinates of the mouse click on the chart are: x = ",lparam,"  y = ",dparam);
      }


//+------------------------------------------------------------------+
   }
mostafalotfi
185
mostafalotfi 2014.03.20 20:09  

Hi Gjol

Really thanks,

/
To add comments, please log in or register