Can't access objects's properties of a specified chartID! HELP PLEASE :)

To add comments, please log in or register
aphong
73
aphong  

Hi everyone,

Im new to mql4 & I got some problem working with object property.

Let's say I want to access to property "PRICE1" of Rectangles of all open charts if availalble.

My code looks fine :) & have no errors but there is something wrong & It doesnt work.

I think it may be these lines:

....

for (int j=ObjectsTotal(ID[i],0,-1)-1; j>=0; j--)

               {

                if(ObjectType(ObjectName(j)) == OBJ_RECTANGLE

....

attached is my code.

Could anyone help me? please

Thanks in advanced! & have a good day

Regard,

long ID[];
double price1[];               
//---
 int OnInit()
       {
        return(INIT_SUCCEEDED);
       }
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[])
  {
  //------
   ArrayFree(ID);
   ArrayResize(ID, ArraySize(ID) + 1);         
   ID[ArraySize(ID) - 1]  = ChartFirst();       
   while(ChartNext(ID[ArraySize(ID) - 1])!=-1)
     {   
         long x=0;
         x=ChartNext(ID[ArraySize(ID) - 1]);
         ArrayResize(ID, ArraySize(ID) + 1);    
         ID[ArraySize(ID) - 1]  = x;
     }                
  //-------
   for(int i=0;i<ArraySize(ID);i++)
     {
     if (ObjectsTotal(ID[i],0,OBJ_RECTANGLE)>0)
         {
            ArrayFree(price1);
            for (int j=ObjectsTotal(ID[i],0,-1)-1; j>=0; j--)
               {
                if(ObjectType(ObjectName(j)) == OBJ_RECTANGLE)
                  {
                     //--
                     ArrayResize(price1, ArraySize(price1) + 1);                                       
                     price1[ArraySize(price1) - 1]  = ObjectGetDouble(0,ObjectName(j),OBJPROP_PRICE1); 
                  }
               }
            for (int z=0; z< ArraySize(price1); z++) {Print(string(price1[z]));}
            Print (ChartSymbol(ID[i]));
            Print (string(ChartPeriod(ID[i])));
            Print (string(ID[i])); 
         }
      }
   return(rates_total); 
  }
nicholi shen
1890
nicholi shen  
aphong:

Hi everyone,

Im new to mql4 & I got some problem working with object property.

Let's say I want to access to property "PRICE1" of Rectangles of all open charts if availalble.

My code looks fine :) & have no errors but there is something wrong & It doesnt work.

I think it may be these lines:

....

for (int j=ObjectsTotal(ID[i],0,-1)-1; j>=0; j--)

               {

                if(ObjectType(ObjectName(j)) == OBJ_RECTANGLE

....

attached is my code.

Could anyone help me? please

Thanks in advanced! & have a good day

Regard,

Don't over-complicate your code by creating unnecessary collections to iterate over. You can iterate over the charts using a for loop. You should also specify the exact params in the Object functions.  And finally, you can use a CArrayDouble collection to dynamically add your prices to. 

Example:

#property strict
#include <Arrays\ArrayDouble.mqh>
//---
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
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[])
  {
//------
   CArrayDouble prices;
   for(long id=ChartFirst(); id>=0; id=ChartNext(id)){
      int total = ObjectsTotal(id, 0, OBJ_RECTANGLE);
      for(int j=0; j<total; j++){
         string name = ObjectName(id, j, 0, OBJ_RECTANGLE);
         prices.Add(ObjectGetDouble(id, name, OBJPROP_PRICE1));
      }
   }
   for(int i=prices.Total()-1; i>=0; i--)
      Print(DoubleToString(prices[i], _Digits));

   return(rates_total);
  }
//+------------------------------------------------------------------+
aphong
73
aphong  
nicholi shen:

Don't over-complicate your code by creating unnecessary collections to iterate over. You can iterate over the charts using a for loop. You should also specify the exact params in the Object functions.  And finally, you can use a CArrayDouble collection to dynamically add your prices to. 

Example:

Great! Thank you very much! :), in fact, Ive just learnt coding mlq for nearly a month!

It is so advanced to me! never knew it fefore! & I didnt know how to use library functions as well. 

It takes me all day to study standard library & understand your codes, now I can get it a  bit :).

Simplicity is the ultimate sophistication! You are a pro coder & very kind as well!

Thanks again & Good night! Nicholi

me, 00:00 am GMT+7

Leonardo da Vinci Quotes
Leonardo da Vinci Quotes
  • www.brainyquote.com
"Simplicity is the ultimate sophistication." - Leonardo da Vinci quotes from BrainyQuote.com
To add comments, please log in or register