Using higher timeframe moving averages in expert advisor

 

Hi, 


I am trying to use the mtf_ma.ex4 indicator to work in an expert advisor, but it does nothing. The expert advisor works when I have normal iMA moving averages and I tried the hull moving average indicator with iCustom and that works also.

The iCustom for mtf ma looks like this:

double h20=iCustom(Symbol(),60,"mtf_ma.ex4",20,1,MODE_SMA,PRICE_CLOSE,0);

double h50=iCustom(Symbol(),60,"mtf_ma.ex4",50,1,MODE_SMA,PRICE_CLOSE,0);

double H20=iCustom(Symbol(),240,"mtf_ma.ex4",20,1,MODE_SMA,PRICE_CLOSE,0);

 double H50=iCustom(Symbol(),240,"mtf_ma.ex4",50,1,MODE_SMA,PRICE_CLOSE,0);

I appreciate any help. Thanks!

Moving Average - Trend Indicators - MetaTrader 5 Help
Moving Average - Trend Indicators - MetaTrader 5 Help
  • www.metatrader5.com
The Moving Average Technical Indicator shows the mean instrument price value for a certain period of time. When one calculates the moving average...
Files:
mtf_ma.mq4  4 kb
 
  1. Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  3. Are those lines inside a function? If not those are not assignments; they are 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), 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. Don't hard code constants. Use the approprate constant, i.e. PERIOD_H1. That 60 will fail if you ever migrate to MT5.


 
Thankyou for the reply! I am not able to modify the original post, but I will repost in the MT4 section.