iMA in MQL5 Help

 

Hi,

I'm trying to just add two moving averages to a chart from an EA, creating them at OnInit() with iMA(). Both MAs are added visually to the strategy tester graph as expected, however the the moving averages are very different lines compared to if I add them manually to a chart for the same M30 pair over the same time period. Anyone know why this might be? Basically the MA's always seem to flow across the top & bottom of candles and never cross. Am I supposed to be doing something to the created indicator in OnTick or OnCalculate? I assumed that should be default.


int OnInit()

  {

   hEMA1 = iMA(_Symbol, PERIOD_M30, 13, 0, MODE_EMA,  PRICE_CLOSE);

   hEMA2 = iMA(_Symbol, PERIOD_M30, 3, 0, MODE_EMA,  PRICE_CLOSE);

   if(hEMA1 == INVALID_HANDLE || hEMA2 == INVALID_HANDLE) {

      Print("ERROR: Create Failed, invalid handle");

   }

   return(INIT_SUCCEEDED);

  }


If I go to an M30 pair, and manually add a MA indicator of those two periods, I get very different lines to what the strat tester ends up drawing. 


Thanks for any tips!

The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
 

Exmaple: Bars after the last iMA crossing

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   ***
//---
   if(MQLInfoInteger(MQL_DEBUG) || MQLInfoInteger(MQL_VISUAL_MODE))
     {
      ChartIndicatorAdd(0,0,handle_iMA_Fast);
      ChartIndicatorAdd(0,0,handle_iMA_Slow);
     }
//---
   return(INIT_SUCCEEDED);
  }
How to start with MQL5
How to start with MQL5
  • 2020.04.09
  • www.mql5.com
This thread discusses MQL5 code examples. There will be examples of how to get data from indicators, how to program advisors...
 
Vladimir Karputov:

Exmaple: Bars after the last iMA crossing


Thanks a lot! It looks like somehow I turned on the bid & ask price lines under the strat tester graph and mistook them for the MA lines! :( Any idea how I turn them off? I solved it by deleting my userdata folder and re-running the EA with your ChartIndicatorAdd() suggestion which worked.

Reason: