New MQL5 learner - Can someone please point me in the right direction

 

I've bough mql5 fundamentals on *** and busy watching youtube videos  by ***

Jimdandy explains things very well.

Problem is most of his videos are for mql4. I've been building an indicator translating from mql4 to mql5 and hit a bit of a bump.

Can someone please look at his code and make out where the problem lies.

Running realtime shows a red line at 

Ma_Array[i]=iMA(Symbol(), PERIOD_CURRENT, AdjustedMA, MaShift, MA_Method, MaAppliedTo);

Much appreciated.

Johann.

//+------------------------------------------------------------------+
//|                                              AutoAjusting MA.mq5 |
//|                                  Copyright 2021, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 1 
#property indicator_plots   1 

#property indicator_type1       DRAW_LINE                                                                       //      This type draws a simple line
#property indicator_label1 "Auto Adjusting Ma"                                                                          //      label to show in the data window
#property indicator_color1      clrRed                                                                          //      Line colour
#property indicator_style1 STYLE_SOLID                                                                  //      Solid, dotted etc
#property indicator_width1 1                                                                                            //      1

enum Creation 
  { 
   Call_iMA,               // use iMA 
   Call_IndicatorCreate    // use IndicatorCreate 
  }; 

 
input string Ma_Settings = "Setting for MA";
input string info = "Enter what you want your MA";
input string info2 = "to be on the 1H Timeframe.";
input int MyMaPeriod = 21;
input int MaShift = 1;
input ENUM_MA_METHOD MA_Method = MODE_SMMA;
input ENUM_APPLIED_PRICE MaAppliedTo = PRICE_CLOSE;
double MyMaMultiplier;
int AdjustedMA;
double Ma_Array[];
int ArraySize();
int bars=Bars(Symbol(),PERIOD_CURRENT);



 

//+------------------------------------------------------------------+
//| Custom indicator initiation function                             |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   int period = Period();
   MaAdjuster(period);
   AdjustedMA = MyMaPeriod*MyMaMultiplier;
   SetIndexBuffer(0,Ma_Array,INDICATOR_DATA);  //SetIndexBuffer(index,array,INDICATOR_DATA)
   //PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_LINE);
   //PlotIndexSetInteger(0,PLOT_LINE_COLOR,clrRed);
   //PlotIndexSetInteger(0,PLOT_LINE_STYLE,STYLE_SOLID);
   //PlotIndexSetInteger(0,PLOT_LINE_WIDTH,1); 
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,AdjustedMA); //SetIndexDrawBegin (0,AdjustedMA);
   //PlotIndexSetString(0,PLOT_LABEL,"Auto Adjusting Ma"); //SetIndexLabel (0,"Auto Adjusting Ma");
//---
   return(INIT_SUCCEEDED);
  }
  
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int prev_calculated,
                const int rates_total,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
                {
                int counted_bars=prev_calculated;
                if (counted_bars<0)return(-1);
                if (counted_bars>0) counted_bars--;
                int uncountedbars=bars-counted_bars;
                if (IsStopped()) return(0);                                                                                     

        

        //      Check that the moving averages have all been calculated
        //if (BarsCalculated(SlowHandle)<rates_total)           return(0);
        
   
// int counted_bars=IndicatorCounted(); mql4
// prev_calculated mql5

//---
for(int i=0;i<uncountedbars;i++)
{
     Ma_Array[i]=iMA(Symbol(), PERIOD_CURRENT, AdjustedMA, MaShift, MA_Method, MaAppliedTo);
     
//--- return value of prev_calculated for next call
}
   return(0);
}
  
//+------------------------------------------------------------------+
// Custom indicator multiplier function                              |
//+------------------------------------------------------------------+

void MaAdjuster(int number)
{
  switch(number)
  {
  case 5:MyMaMultiplier=12;break;
  case 15:MyMaMultiplier=4;break;
  case 30:MyMaMultiplier=2;break;
  case 60:MyMaMultiplier=1;break;
  case 240:MyMaMultiplier=0.25;break;
  }
}
 

https://www.mql5.com/en/forum/161496

Seems like converting mql4 to mql5 is going to be tricky.

convert iMA function from MT4 to MT5
convert iMA function from MT4 to MT5
  • 2016.11.18
  • www.mql5.com
Please someone help me with converting iMA function from MT4 to MT5. I need call iMA with other pair and different shift...
 

hello

MQL4 and MQL5 are quite two different language, a lot of things changed...


in MQL5, iMA() will return a handle , and you have to use CopyBuffer() to get MA data...


i guess, you have not learned MQL5 yet, 

 

you just want to write MQL5 program with MQL4... this is impossible...

please open the MQL5 reference, read it through carefully before you code with MQL5...


this wont take you much time...

 
Johann Strümpfer :


There is an important rule in MQL5: the indicator handle SHOULD be received ONLY ONCE !!! And you need to do this in OnInit () !!!

An example of an iMA-based indicator: MA Color N Bars

MA Color N Bars
MA Color N Bars
  • www.mql5.com
Индикатор iMA (Moving Average, MA) на базе графического стиля DRAW_COLOR_LINE - отображает три цвета: "нейтральный", "тренд вверх" и "тренд вниз"
 

Thanx for the info guyz.

I'm busy learning the basics of mql5 but sometimes the videos are for mql4.

When there are times I cant figure out how to convert, I just move on until I understand more.

I haven't been doing too bad so far but I can see it's going to take practise.

Much appreciated.

Johann.