Download MetaTrader 5

Inidicator deinitialisation after timeframe change - how to keep values for graphical object (HLINE)?

To add comments, please log in or register
How to earn at MQL5.community? Come and find out!
Wolfgang Gresch
208
Wolfgang Gresch 2012.12.13 22:26 

Hello all,


just wondering, if this is possible in MT5:

In an indicator I create a horizontal line.

Whenever this line is moved, the new price value is stored and can be obtained from an underlying object.

The line's name is created dynamically using the current time.

Unfortunately, whenever the timeframe is changed, OnDeinit() is called and the line is created again and the former price value is lost.


Which is the preferred way keeping information even OnDeinit() is called for an indicator? Or would that be impossible?


Thanks!


 z3tr4d3r 

 

 

 

Documentation on MQL5: Standard Constants, Enumerations and Structures / Objects Constants / Object Types
Documentation on MQL5: Standard Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
Standard Constants, Enumerations and Structures / Objects Constants / Object Types - Documentation on MQL5
phi nuts
2184
phi nuts 2012.12.14 04:06  
z3tr4d3r:

Hello all,


just wondering, if this is possible in MT5:

In an indicator I create a horizontal line.

Whenever this line is moved, the new price value is stored and can be obtained from an underlying object.

The line's name is created dynamically using the current time.

Unfortunately, whenever the timeframe is changed, OnDeinit() is called and the line is created again and the former price value is lost.


Which is the preferred way keeping information even OnDeinit() is called for an indicator? Or would that be impossible?


Thanks!


 z3tr4d3r 

The blue colored words is a link, click it please. 

1. Yes you can create a horizontal line using Object Functions and specify OBJ_HLINE in ObjectCreate .

2. Yes, you can get the the price value from that object line by specifying OBJPROP_PRICE in ObjectGetDouble function.

3. Yes, you can create it's name dynamically - actually, if there are more than one object, you must create those lines using different names.  You can get it's name using ObjectsTotal and ObjectName.

4. Fortunately you can save those object name, values, etc using File Functions 

Wolfgang Gresch
208
Wolfgang Gresch 2012.12.14 09:27  
phi.nuts:

The blue colored words is a link, click it please. 

1. Yes you can create a horizontal line using Object Functions and specify OBJ_HLINE in ObjectCreate .

2. Yes, you can get the the price value from that object line by specifying OBJPROP_PRICE in ObjectGetDouble function.

3. Yes, you can create it's name dynamically - actually, if there are more than one object, you must create those lines using different names.  You can get it's name using ObjectsTotal and ObjectName.

4. Fortunately you can save those object name, values, etc using File Functions 

Thanks very much for your detailed answer!

1. Yes you can create a horizontal line using Object Functions and specify OBJ_HLINE in ObjectCreate .

2. Yes, you can get the the price value from that object line by specifying OBJPROP_PRICE in ObjectGetDouble function.  

3. Yes, you can create it's name dynamically - actually, if there are more than one object, you must create those lines using different names.  

 I see - what I did was using a class (in the *.mqh file) that subclassed CChartObjectHLine. Here I used the standard Create(...) method to create the horizontal line.

 In the indicator file (*.mq5) I created an instance of the custom class using the 'new' operator and did not delete the instance when the symbol/timeframe chart event occurred. This b.t.w. leads to a warning about leaked memory, which is ok I guess as long I take care of deleting the instance in case the indicator or the line qet deleted.

 4. Fortunately you can save those object name, values, etc using File Functions 

 Ok, I see, then I go this way and learn a bit :-) 

 Thanks again for your help!

Wolfgang Gresch
208
Wolfgang Gresch 2012.12.19 16:59  
z3tr4d3r:


 Ok, I see, then I go this way and learn a bit :-) 

I got stuck and guess this is because of a design bug not allowing passing properties of graphical objects when the timeframe is changed.

 The indicator I'd like to use is as simple as you can imagine: It should just draw a line and do some action if the price level of the line was hit.

Problem is now: The Hline will be moved after first initalisation the price level gets internally stored (in a surrounding class object). If I switch to another timeframe, the price value is not stored (actually even if the surrounding class object is created with the 'new' operator) as it get's re-initialised.

If I would store the price level in a file and then re-initialize the price value in OnInit() - how could I differentiate between the creation of a new line (here: a third line) and the need of creating a new line?

IMHO it would be very handy being able to skip the timeframe change based reinitialisation for an indicator, e.g. with an INDICATOR_IGNORE_TIMEFRAME_CHANGE property.

Any more hints? 

 

graziani
1955
graziani 2012.12.19 17:31  
int OnInit(void)
{  
   ...

   if (GlobalVariableCheck("line000") == true)
   {  GlobalVariableGet("line000", value);
      ....
      GlobalVariableDel("line000");

   }

   ... }    void OnDeinit(const int reason) {  if (reason == REASON_CHARTCHANGE)       GlobalVariableSet("line000", value);           .... }

Wolfgang Gresch
208
Wolfgang Gresch 2012.12.20 20:12  
graziani:

Mille grazie, graziani ;-)

Your help is very appreciated. Using your global variable approach is more handy compared to using the filesystem. I can store all necessary values in the map key for example.

 

Unfortunately I cannot find out a solution of how to differentiate between a call of OnInit() when I put another indicator to the chart and a call when an existing indicator gets reinitialised.

Assume I create two indicators on the chart, storing a link to them in global vars with keys "line000" and "line001". Then I change the timeframe and reinitialise the line values. And now I put another indicator on the chart.

How would I differentiate between the reinitialisation and a clean new initialisation? As there is no "OnReinit()" method and as there is no handle, reference or whatever attached to each indicator, I think, this is not possible and a design flaw (or better: bug). 
Actually I even could not initialise two indicators of the same type.

The only way I see now is creating a panel/UI element from where I create the horizontal lines, but this seems to be quite clumsy to me. 

What I rather need - the more I think of it, it seems to be more obvious - are extensible graphical objects.

Any more hints? 

 



 
graziani
1955
graziani 2012.12.21 15:11  

prego ;),

you should consider the naming possibilities:

string name = MQL5InfoString(MQL5_PROGRAM_NAME) + "_" + _Symbol+ "_line" + IntegerToString(count) + "_" + TimeToString(scannedDateLast, TIME_DATE);

GlobalVariableSet(name, value);
phi nuts
2184
phi nuts 2012.12.23 11:22  
Let me get this correctly : You create a CI to draw a lines. When you change time frame, you want to access the data of these lines without creating another lines, and you want to attach this CI as many as you can and each will only taking care it's own line ?
To add comments, please log in or register