Looking for a sample of MQL5 EA with an indicator calculated entirely with code located inside the EA.

 

Hi,

I am looking for a sample of MQL5 EA with an indicator calculated entirely with code located inside the EA.
And that requires reading historical data.
Or said in another way not from iMA, etc.

Regards,

Pierre

 
Pierre Rougier:

Hi,

I am looking for a sample of MQL5 EA with an indicator calculated entirely with code located inside the EA.
And that requires reading historical data.
Or said in another way not from iMA, etc.

Regards,

Pierre

You need the formula of the indicator and recalculate it with data needed (high, low, close). You can find formula on the net or inside a sample indicator code.

 
  1. Don't try do that. There are no buffers, no IndicatorCounted() or prev_calculated. No way to know if older bars have changed or been added (history update.)
    Just get the value(s) of the indicator(s) into the EA and do what you want with it.
    You should encapsulate your iCustom calls to make your code self-documenting.
              Detailed explanation of iCustom - MQL4 and MetaTrader 4 - MQL4 programming forum

  2. You can also embed the other indicator(s) inside your indicator/EA. Add the CI(s) to your code as a resource.
              Use the publicly released code - Expert Advisors and Automated Trading - MQL5 programming forum

 

Say you wanted to calculate the moving average of the highs of the last 8 bars

1. Get the rates using CopyRates() into an array. You could do this in OnTick(), but note: calculations can become intensive when calculating a large moving average on every tick. So, if your EA strategy and logic permits, you might calculate only on a "new bar."

2. Loop over the last 8 rates adding all the highs, then divide by 8.

Your result is the SMA(8) of highs at the current bar.

That's it.

If you want to see the average at the previous bar, you shift your "window" by changing your loop's start position.


If you are using MQL4, there is a handy function:

https://docs.mql4.com/indicators/imaonarray


Want an EMA? There's a handy macro for that:

// P == Previous EMA value, i.e. EMA of previous day's price
// C == Current value (e.g. close price)
// L == Time period, e.g. a 10 day moving average
// Example: ExtAverageBuffer[i]=EMA(ExtAverageBuffer[i-1], dCurrVolume, InpMovingAvgPeriod);
#define EMA(P, C, L) ((P) + (2.0/((L)+1.0))*((C)-(P)))
 
Anthony Garot:


If you are using MQL4, there is a handy function:

https://docs.mql4.com/indicators/imaonarray


The topic is obviously about mql5.

Forum on trading, automated trading systems and testing trading strategies

Should i move from MQL4 to MQL5 ?

Alain Verleyen, 2015.12.04 11:20

Yes it can. iMAOnArray() is obsolete and ineffective, replace it with calls to functions from MovingAverages.mqh library (standard with MT4, provided in Include folder).


You can always have bugs in MT4 and MT5 as in any software.

Reason: