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

 

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
 
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 

 
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!

 
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? 

 

 
int OnInit(void)
{  
   ...

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

   }

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

 
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? 

 



 
 

prego ;),

you should consider the naming possibilities:

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

GlobalVariableSet(name, value);
 
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 ?
Reason: