Help to build Hull Moving Average Crossover EA MT5

 

Hi Coders,

I am new to coding ( only 4 months of experience)

I have read the Expert Advisor Programming
for MetaTrader 5 by Andrew Young

But I still do not understand how code the  Hull Moving Average Crossover to do the following :

When the fast hull moving average crosses above the slow hull moving average, look for buy entries. (Both HMA should be green)

When the fast hull moving average crosses below the slow hull moving average, look for sell entries. (Both HMA should be red)


I have seen many examples of how to code a regular  Moving Average Crossover but not

Hull Moving Average Crossover 


Could someone help?


See my coding below:

#include<Trade\Trade.mqh>

// We create an instance of CTrade
CTrade trade;

void OnTick()
{   
      // create an empty String
      string entry ="";
     
      // Get the Ask price
      double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
   
      // Get the Bid price
      double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);       
    
     // create an Array for several prices
     double myHMA10[],myHMA100[];

     // define the properties of the Moving Average1
     int HMADefinition1 = iCustom (_Symbol,_Period,"Hull average 2",10,2.0,PRICE_CLOSE);

     // define the properties of the Moving Average2
     int HMADefinition2 = iCustom (_Symbol,_Period,"Hull average 2",100,2.0,PRICE_CLOSE);
     
     // sort the price array1 from the current candle downwards
     ArraySetAsSeries(myHMA10,true);

     // sort the price array2 from the current candle downwards
     ArraySetAsSeries(myHMA100,true);
     
     // Defined HMA1, one line,current candle,3 candles, store result
     CopyBuffer(HMADefinition1,0,0,3,myHMA10);
      
     // Defined HMA2, one line,current candle,3 candles, store result
     CopyBuffer(HMADefinition2,0,0,3,myHMA100);

     
      if (   // Check if the 10 candle EA is above the 100 candle EA  
            (myHMA10[0]>myHMA100[0])
         && (myHMA10[1]<myHMA100[1])
         )
            {
            entry="buy";
            }
            
      if (   // Check if the 100 candle EA is above the 10 candle EA          
            (myHMA10[0]<myHMA100[0])
         && (myHMA10[1]>myHMA100[1])
         )
            {
            entry="sell";
            }
      
        // sell 10 microlot
      if (entry =="sell" && PositionsTotal()<1)
      trade.Sell(0.10,NULL,Bid,0,(Bid-150 * _Point),NULL);
      
      // buy 10 microlot
      if (entry =="buy" && PositionsTotal()<1)
      trade.Buy(0.10,NULL,Ask,0,(Ask+150 * _Point),NULL);
       
  }
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...
 

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button


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

  2. Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020.03.08)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020.07.05)
              How to call indicators in MQL5 - MQL5 Articles (12 March 2010

  3. Shawn Gaskill: But I still do not understand how code the  Hull Moving Average Crossover to do the following :

    Yes you do,
      (myHMA10[0]>myHMA100[0])
    && (myHMA10[1]<myHMA100[1])
    double aPrev = …, aCurr = …,
           bPrev = …, bCurr = …;
    bool   wasUp = aPrev > bPrev,
            isUp = aCurr > bCurr,
           isCross = isUp != wasUp;
    except you should be checking completed bars.

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              New candle - MQL4 programming forum #3 (2014.04.04)

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011.05.06)

 
Shawn Gaskill :


You first have to fix your main mistake: you create SEVERAL indicator handles every tick. This is the BIGGEST MISTAKE !!! In MQL5, the indicator handle is created once - in OnInit, and the indicator is accessed via CopyBuffer.

Example: An example of working with iCustom

How to start with MQL5
How to start with MQL5
  • 2020.09.06
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
Reason: