what really happens when dragging another currency pair to current chart?

 

I just wrote an indicator for a friend, which draws several TEXT objects near/at each Low[] of several bars, only on D1 chart. I also added a fixed "padding" to move those objects a tiny bit away from Low[] price.

This indicator works fine for me. But my friend has a habit: he often opens a chart, uses some indicators, then drags other currency pair to this chart. I tried, if the dragged in currency pair is "new" (never opened during the test), two problems will probably occur: 1), only the last object (for the last bar) will draw (which should be re-drawn for every new tick, and other objects should be drawn only once at the beginning); 2), the padding does not work. 

If I close the indicator and re-apply, problems solved. If I change timeframe and change it back, problems solved.

I've added ChartRedraw() at the end of OnCalculate().

So, what really happens when dragging another currency pair to current chart? - indicators get kept, but obviously there are something different.

I don't want to bother you guys with detailed code, hope my description is clear enough.

Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
Documentation on MQL5: Constants, Enumerations and Structures / Objects Constants / Object Types
  • www.mql5.com
Object Types - Objects Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
joshatt: So, what really happens when dragging another currency pair to current chart? - indicators get kept, but obviously there are something different.

Indicators are removed from the chart, symbol changes, indicator are reloaded. EA is not removed, static/global variables remain intact.

joshatt: two problems will probably occur:

Fix your broken indicator, likely global variable initialization and multiple OnCalculate(prev=0) thus:

That is not an assignment; it's initialization of a common (globally declared), or static variable with a constant. They work exactly the same way in MT4/MT5/C/C++.

  1. They are initialized once on program load.

  2. They don't update unless you assign to them.

  3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

    MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and

    Don't try to use any price or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

  4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
              external static variable - MQL4 programming forum #2 (2013)

 
William Roeder #:

Indicators are removed from the chart, symbol changes, indicator are reloaded. EA is not removed, static/global variables remain intact.

Fix your broken indicator, likely global variable initialization and multiple OnCalculate(prev=0) thus:

Thanks for your help. There's no global variable in this indicator, and no static anywhere. I just found something weird - incerting some print lines and the result is different....working on it....