iMA compared to tradingview -- This is not the same and so not reliable. - What do I do?

 

Hello,


I am attempting to create an exponential moving average indicator but i am having an issue with the performance.   I am using tradingview.com with the indicator  "deloreanEMA"  it has 5 EMA indicators.  sample of 5, 13, 50, 200 and 800.


The 15 minute chart below shows what the ema indicators look like


tradingview example

The black arrow shows the candle at 00:00 on the 26

red line value  is 136.514

yellow line value is 136.496


I created a function to measure the exponential moving average with a sample of 5 and 13 but the values are not the same as tradingview. 

I am using the function iMA from the Trade library         #include </Trade/Trade.mqh>;


int CheckEntryEMA(ENUM_TIMEFRAMES period)    // get EMA for 5 samples and 13 and display the value
{
     double myMovingAverageArray5[],myMovingAverageArray13[]; 
     int movingAverageDefinition1 = iMA(_Symbol,period,5,0,MODE_EMA,PRICE_CLOSE);               //Properties of moving average 1
     int movingAverageDefinition2 = iMA(_Symbol,period,13,0,MODE_EMA,PRICE_CLOSE);              //Properties of moving average 2
     ArraySetAsSeries(myMovingAverageArray5,true);                                              
     ArraySetAsSeries(myMovingAverageArray13,true);                                             

     CopyBuffer(movingAverageDefinition1,0,0,3,myMovingAverageArray5);                          //define MA1, one line, current candle, 3 candles, store result
     CopyBuffer(movingAverageDefinition2,0,0,3,myMovingAverageArray13);                         //define MA1, one line, current candle, 3 candles, store result
     double yellow0 = myMovingAverageArray5[2];
     double yellow1 = myMovingAverageArray5[1];
     double red0 = myMovingAverageArray13[2];
     double red1 = myMovingAverageArray13[1];
     if(dotswitch == 1 && dot == 0 && period == PERIOD_M15)
     {
          Print("  --  EMA-MACD 145 | yellow0 = ", yellow0," | yellow1 = ",yellow1," | Red0 = ",red0);
          dot = 1;
     }
     return(1);
}


The output of this code is the following:

code output

the red value is 136.497

The yellow value is 136.509     --> Note :  this value is above the red value!  not the same as tradingview .... bad

There is a large enough error between the two systems to cause me grief.  It is so bad that it affects my trade.   the library function "iMA" shows the 13 sample (red line) value is below the 5 sample (yellow line) value.  Tradingview shows the red value is above the yellow value.   This is a completely different condition. 

I love accurate data but I can work with a small error between two systems...but when the data shows completely different information I cannot adjust for that.  This is not accurate enough for me to rely on.  I need good data to build proper software.   

Questions....

What is going on with the library iMA function?  

Has anyone seen this error before? 

Any ideas how i can solve this issue?  

Is there a better library that provides exponential moving average that will show the same results as tradingview?

Is there a different website I can use instead of tradingview that SHOWS EXACTLY the same results as MQL5 iMA?

 

Hi,

the iMA function calculates the correct values.

Do you use the same price data for the calculation?

Also make sure to create the indicator handles in OnInit.

double yellow0 = myMovingAverageArray5[2];
double yellow1 = myMovingAverageArray5[1];

Index [0] is the newest moving average value.

 
int CheckEntryEMA(ENUM_TIMEFRAMES period)    // get EMA for 5 samples and 13 and display the value
{
     double myMovingAverageArray5[],myMovingAverageArray13[]; 
     int movingAverageDefinition1 = iMA(_Symbol,period,5,0,MODE_EMA,PRICE_CLOSE);               //Properties of moving average 1
     int movingAverageDefinition2 = iMA(_Symbol,period,13,0,MODE_EMA,PRICE_CLOSE);           

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)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
          How to call indicators in MQL5 - MQL5 Articles (2010)

 

Are you reading the documentation?  Copybuffer transposes the data.  

in the "indicator buffer"  the newest is 0.  but when you copy the data it flips.   I discovered this fact the hard way.

please look at the diagram at the top of the page

https://www.mql5.com/en/docs/series/copybuffer




Does anyone have any comments about tradingview.com?

Has anyone seen this difference in data before?


I am a professional developer .  This is an advanced question about the data.  not on how to code.   Please do not correct my code. ....not important in this discussion.

Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
Documentation on MQL5: Timeseries and Indicators Access / CopyBuffer
  • www.mql5.com
CopyBuffer - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Chris Pinter #:

Are you reading the documentation?  Copybuffer transposes the data.  

in the "indicator buffer"  the newest is 0.  but when you copy the data it flips.   I discovered this fact the hard way.

So [0] is the newest moving average value.

It "flips" because you are using ArraySetAsSeries.

ArraySetAsSeries(myMovingAverageArray5,true);                                              
ArraySetAsSeries(myMovingAverageArray13,true);

https://www.mql5.com/en/docs/array/arraysetasseries


Try:

double yellow0 = myMovingAverageArray5[0];
Documentation on MQL5: Array Functions / ArraySetAsSeries
Documentation on MQL5: Array Functions / ArraySetAsSeries
  • www.mql5.com
ArraySetAsSeries - Array Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
trustfultrading #:

So [0] is the newest moving average value.

It "flips" because you are using ArraySetAsSeries.

https://www.mql5.com/en/docs/array/arraysetasseries


Try:

Thanks.


Did you ever see a difference between MQL5 iMA and the tradingview.com exponential moving average calculation?

 
Chris Pinter #:

Thanks.


Did you ever see a difference between MQL5 iMA and the tradingview.com exponential moving average calculation?

Happy to help!

To be honest, I don't use tradingview. So i don't know what data you get from tradingview.

But I think you don't get the same data as for MT5? Or am I wrong here?

If you get the same data, the moving average calculation will be the same.

But I am a noob when it comes to tradingview :D

 
Chris Pinter #: Did you ever see a difference between MQL5 iMA and the tradingview.com exponential moving average calculation?

I don't use Trading view, but I do know that the Exponential Moving Average can be implemented slightly differently on various platforms.

It usually only affects the EMA values during the beginning of the chart, very early data, but it becomes irrelevant on more recent data.

The reason for this, is how each platform defines the initial values of the EMA.

  • Some, begin the EMA sequence with the actual first data values.
  • Others, begin by first calculating a normal average of the first PERIOD count of bars.
  • A few, start with an initial value of "0.0".
  • And there may be other variations.

Other than the initial values, there should be no differences in later data.

Also, and this is very important, the data feed in TradingView is not the same as MetaTrader or on brokers. Unless the OHLC data is EXACTLY the same, you should not be comparing the EMA (or any other indicator) for an exact match. They will differ.

 
Fernando Carreiro #:

I don't use Trading view, but I do know that the Exponential Moving Average can be implemented slightly differently on various platforms.

It usually only affects the EMA values during the beginning of the chart, very early data, but it becomes irrelevant on more recent data.

The reason for this, is how each platform defines the initial values of the EMA.

  • Some, begin the EMA sequence with the actual first data values.
  • Others, begin by first calculating a normal average of the first PERIOD count of bars.
  • A few, start with an initial value of "0.0".
  • And there may be other variations.

Other than the initial values, there should be no differences in later data.

Also, and this is very important, the data feed in TradingView is not the same as MetaTrader or on brokers. Unless the OHLC data is EXACTLY the same, you should not be comparing the EMA (or any other indicator) for an exact match. They will differ.

Nice, thank you.

 
trustfultrading #:

Nice, thank you.

Fernando Carreiro #:

I don't use Trading view, but I do know that the Exponential Moving Average can be implemented slightly differently on various platforms.

It usually only affects the EMA values during the beginning of the chart, very early data, but it becomes irrelevant on more recent data.

The reason for this, is how each platform defines the initial values of the EMA.

  • Some, begin the EMA sequence with the actual first data values.
  • Others, begin by first calculating a normal average of the first PERIOD count of bars.
  • A few, start with an initial value of "0.0".
  • And there may be other variations.

Other than the initial values, there should be no differences in later data.

Also, and this is very important, the data feed in TradingView is not the same as MetaTrader or on brokers. Unless the OHLC data is EXACTLY the same, you should not be comparing the EMA (or any other indicator) for an exact match. They will differ.

Thank you Fernando,


This is helpful.   It is weird because the market can be traded in microseconds.  Many people use trading view to make decisions  and I guess it can work if you can really on the data.  The problem is that when you build software you would want to test it with some good data.   I am shocked to see that metatrader and tradingview are so different.   Of course, everyone want sot be different so there is absolutely no reason for the two systems to show the same data.

I have added EMA indicators to the charts in Metatrader 5 and will use those to check my software.

ema on graph

 
Chris Pinter #: Thank you Fernando, This is helpful.   It is weird because the market can be traded in microseconds.  Many people use trading view to make decisions  and I guess it can work if you can really on the data.  The problem is that when you build software you would want to test it with some good data.  I am shocked to see that metatrader and tradingview are so different.  Of course, everyone want sot be different so there is absolutely no reason for the two systems to show the same data. I have added EMA indicators to the charts in Metatrader 5 and will use those to check my software.


You are making a "dangerous" assumption that the Forex market is supposed to be centralised or uniform. It is not!

Brokers that are market makers or dealing desk create and manage their own liquidity for their customers (as they trade against them), while STP, ECN and non-dealing desk brokers draw upon larger external liquidity providers.

So which ever way you look at it, prices can vary from slightly different to downright incomparable.

Also, there is no such thing as "good data" in respect to the above. The more diverse the tests are against different data sources, the more you can test the resilience of your EA strategy against different conditions.

However, if your EA is too "curve-fitted" and dependant on a particular data set, then it will very quickly become unprofitable as soon as the conditions change, and it is guaranteed to change.