Problem with object naming

 

fibo-name1 fibo-name2

Hi,

I'm currently in need of help to understand the formula behind object naming especially in Fibonacci Indicator from MT4 as you can see above. The moment i drop the same indicator on the same chart, it will come up with different number automatically.

I'm trying to simulate the same idea for my indicator, but have no luck yet.

I've try using some random method with MathSrand, MathRand, GetTickCount, etc, but all of these values will always re-generated if i change the timeframe.

There must be some trick at least to preserve the random numbers until the indicator has been unloaded from the chart manually.

Anyone can help ?

Thanks

 
I'd just use
string on = name + IntegerToString(long( Time[iBar] ));
 

i usually make a static counter to use in the object name and increment it each time the object is drawn.

 
WHRoeder:
I'd just use
Hi WHRoeder, but the value of "on" will keep changing if i change the timeframe.
 
SDC:

i usually make a static counter to use in the object name and increment it each time the object is drawn.


Hi SDC, do you have the sample code which i can study ? Sorry i'm still new in programming.
 

Here is a simple function to put a wingding arrow.

void MakeArrow(datetime time=0,                              // function in parameters
               double   price=0,
               int      wingding=0,
               color    clr=CLR_NONE)
{
 static int counter = 10000;                                 // initialize counter to 10,000 to preserved numerical order in object list
 long chart = ChartID();                                     // get id of current chart
 string name = StringConcatenate("indyname ",counter);       // build name string with counter

 if(ObjectCreate(chart,name,OBJ_ARROW,0,time,price))         // if create arrow is successful...
 {ObjectSetInteger(chart,name,OBJPROP_ARROWCODE,wingding);   // set the wingding for the arrow
  ObjectSetInteger(chart,name,OBJPROP_COLOR,clr);            // set the wingding's color
  counter++;                                                 // increment the counter.
}}
 
SDC:

Here is a simple function to put a wingding arrow.


Thanks SDC, looks like i'have to recreate my object inside the counter.
 

Hi Again,

I've tried with your increment method, but it does not counting if i attach the same indicator. Or did i miss something ?

//+------------------------------------------------------------------+
//|                                                    fibo-test.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
#property indicator_chart_window

extern color   clrColor       = Red;   
extern int     intWidth       = 1;       // From 1 (thin) to 5 (thick) 
extern int     intLineStyle   = STYLE_DOT;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
  
Comment("");
//----
 
return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----

Comment("");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  //----     
    
  fibo();
  
  //----
   return(0);
  }

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Fibo                                                             |
//+------------------------------------------------------------------+

void fibo() {

   string   strPrefix   =  "Fibo:";
   string   strObjName;
   //string   strObjRef;
   int      intCount    =  0;
   //int      intLimit    =  ObjectsTotal();
   
   int fibHigh = iHighest(Symbol(),Period(),MODE_HIGH,20,1);
   int fibLow  = iLowest(Symbol(),Period(),MODE_LOW,20,1);
     
   datetime highTime = Time[fibHigh];
   datetime lowTime  = Time[fibLow];
   
   strObjName  =  StringConcatenate(strPrefix, intCount);
      
   if(ObjectCreate(strObjName,OBJ_FIBO,0,highTime,High[fibHigh],lowTime,Low[fibLow]))
   {
   ObjectSet(strObjName,OBJPROP_COLOR,clrColor);
   ObjectSet(strObjName,OBJPROP_WIDTH,intWidth);
   ObjectSet(strObjName,OBJPROP_STYLE,intLineStyle);
   ObjectSet(strObjName,OBJPROP_RAY,false);
   ObjectSet(strObjName,OBJPROP_FIBOLEVELS,4);
   ObjectSet(strObjName,OBJPROP_FIRSTLEVEL+0,0.0);
   ObjectSet(strObjName,OBJPROP_FIRSTLEVEL+1,0.50);
   ObjectSet(strObjName,OBJPROP_FIRSTLEVEL+2,1.0);
   ObjectSet(strObjName,OBJPROP_FIRSTLEVEL+3,1.618);
   ObjectSetFiboDescription( strObjName, 0,"%$  --> 0.0%"); 
   ObjectSetFiboDescription( strObjName, 1,"%$ --> 50.0%");
   ObjectSetFiboDescription( strObjName, 2,"%$ --> 100.0%");
   ObjectSetFiboDescription( strObjName, 3,"%$ --> 161.8%");
   intCount++;   
   }

}
Files:
fibo-test.mq4  4 kb
 
stillnew:

Hi Again,

I've tried with your increment method, but it does not counting if i attach the same indicator. Or did i miss something ?


There is no loop in your code and you are setting intCount to zero every call.

So you are only trying to create an object named "Fibo:0"

 
stillnew:

Hi Again,

I've tried with your increment method, but it does not counting if i attach the same indicator. Or did i miss something ?

Look at my example code again, the data type of the counter is static. It is important to understand the difference between static variables and simple local variables.

I think the documentation on static variables uses the wrong terminology in a few places. I have edited it:

A static variable can be initialized by a constant or constant expression corresponding to its type, unlike a simple local variable, which can be initialized by any expression.

Static variables exist from the moment of program execution and are initialized only once after the program is loaded. If the initial values are not specified, variables of the static storage class are taking zero initial values.

The scope of the static variables is the same as the scope of the global variables: the lifetime of the mql4-program.

The lifetime of the static variable is the same as the lifetime of global variables: the lifetime of the mql4 program.

The scope of a static variable is local to the block in which the variable is defined.

Local variables declared with the static keyword retain their values throughout the function lifetime.

Local variables declared with the static keyword retain their vales throughout the mql4 program lifetime.

With each next function call, such local variables contain the values that they had during the previous call.

Any variables in a block, except formal parameters of a function, can be defined as static. If a variable declared on a local level is not a static one, memory for such a variable is allocated automatically at a program stack.

Reason: