Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1010

 

MQL5

I can't seem to get it to work. Can someone suggest something?

Forum on trading, automated trading systems & strategy testing

Can someone please give me a hint.

Sergey Tabolin, 2019.03.07 08:14

Based on one indicator from the codebase I wrote an example of what I want. It works. But it gives an error on startup:

2019.03.06 21:24:26.091 my_MA_S (GBPUSD,M15)    array out of range in 'my_MA_S.mq5' (103,59)

Can you tell me where the error is?

//+------------------------------------------------------------------+
//|                                                       myMA_S.mq5 |
//|                                     Copyright 2019, Tabolin S.N. |
//|                           https://www.mql5.com/ru/users/vip.avos |
//+------------------------------------------------------------------+
#property   copyright   "Copyright 2019, Tabolin S.N."
#property   link        "https://www.mql5.com/ru/users/vip.avos"
#property   version     "1.07"
//#property   icon        "\\Images\\mi2.ico"
//----------------------------------------------------------------------------------------------
#define      GS          1.618
#define      PI          3.14159
//----------------------------------------------------------------------------------------------
#property strict
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1

#property indicator_label1  "myMA_S"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1 

//--- input parameters
input int      InpPeriodMA          = 45; // MA period
input int      InpShiftCorrection   = 9;  // Correction shift
//--- indicator buffers
double         Buffer1[];
int            handle_MA;                           // переменная для хранения хэндла индикатора HMA5
double         buffer_MA[];                         // массив для хранения значений индикатора HMA5
int            n=0;
int            ma_bars_calculated = 0; 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   ArraySetAsSeries(Buffer1,        true);
   ArraySetAsSeries(buffer_MA,      true);

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,-1);

   SetIndexBuffer(0,Buffer1,INDICATOR_DATA);
//--- set shortname and change label
   string short_name="myMA_S("+
                              IntegerToString(InpPeriodMA)+","+
                              IntegerToString(InpShiftCorrection)+")";
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
   PlotIndexSetString(0,PLOT_LABEL,short_name);
//--- set accuracy
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,InpPeriodMA);

   handle_MA = iMA(Symbol(),0,InpPeriodMA,0,MODE_SMA,PRICE_CLOSE);
   if(handle_MA == INVALID_HANDLE)                                       // проверяем наличие хендла индикатора
   {
      Comment("Не удалось получить хендл индикатора handle_MA");         // если хендл не получен, то выводим сообщение в лог об ошибке
      Print("Не удалось получить хендл индикатора handle_MA");
      return(INIT_FAILED);                                                 // завершаем работу с ошибкой
   }

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                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 ma_values_to_copy; 
   int ma_calculated = BarsCalculated(handle_MA); 
   if(ma_calculated <= 0){ 
      PrintFormat("BarsCalculated() вернул %d, код ошибки %d",ma_calculated,GetLastError()); 
      return(0); 
     }  
   if(prev_calculated == 0 || ma_calculated != ma_bars_calculated || rates_total > prev_calculated + 1){ 
      if(ma_calculated > rates_total) ma_values_to_copy = rates_total; 
      else ma_values_to_copy = ma_calculated; 
     } else { 
      ma_values_to_copy = (rates_total - prev_calculated) + 1; 
     } 
     
   if(CopyBuffer(handle_MA,0,0,ma_values_to_copy,buffer_MA) < 0 ) // копируем данные из индикаторного массива в массив buffer_HMA5
   {                                                                                // если не скопировалось
      Print("Не удалось скопировать данные из индикаторного буфера в buffer_MA");   // то выводим сообщение об ошибке
      return(0);                                                                    // и выходим из функции
   }

   for(int i = 0; i < ma_values_to_copy; i++)
     {
      Buffer1[i]  = buffer_MA[i]+(buffer_MA[i+1]-buffer_MA[i+InpShiftCorrection])/(InpShiftCorrection/GS);//1.314;
     }
   
   return(rates_total);
  }
//+------------------------------------------------------------------+

What where wrong?
Files:
my_MA_S.mq5  10 kb
 
Сергей Таболин:

MQL5

I can't seem to get it to work. Can someone give me a hint?

What is wrong?
Two buffers, but #property specifies one
 
Artyom Trishkin:
Two buffers and the #property specifies one

Thanks for the tip...

But nothing has changed. I'm a total minus in indicators (((

#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   1

#property indicator_label1  "myMA_S"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1 

//--- input parameters
input int      InpPeriodMA          = 45; // MA period
input int      InpShiftCorrection   = 9;  // Correction shift
//--- indicator buffers
double         Buffer1[];
int            handle_MA;                           // переменная для хранения хэндла индикатора HMA5
double         buffer_MA[];                         // массив для хранения значений индикатора HMA5
int            n=0;
int            ma_bars_calculated = 0; 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   ArraySetAsSeries(Buffer1,        true);
   ArraySetAsSeries(buffer_MA,      true);

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,-1);
   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,-1);

   SetIndexBuffer(0,Buffer1,INDICATOR_DATA);
   SetIndexBuffer(1,buffer_MA,INDICATOR_CALCULATIONS);
 
Сергей Таболин:

Thanks for the tip...

But nothing has changed. I'm a total minus in indicators (((

I can't tell from a mobile phone
 
Сергей Таболин:

Thanks for the tip...

But nothing has changed. I'm a total minus in indicators ((

Didn't really look at the code and it's not clear which line the error is in, but as if the error isn't here.

Try reducing the limit

for(int i = 0; i < ma_values_to_copy-5; i++)
 
Vitaly Muzichenko:

Didn't really look at the code and it's not clear which line the error is in, but it's not like the error is here.

Try reducing the limit

The error is exactly in this loop.

   for(int i = 0; i < ma_values_to_copy; i++)
     {
      Buffer1[i]  = buffer_MA[i]+(buffer_MA[i+1]-buffer_MA[i+InpShiftCorrection])/(InpShiftCorrection/GS);// ошибка тут
     }

But your suggestion has prompted me to make a small change:

   for(int i = 0; i < ma_values_to_copy-InpShiftCorrection; i++)
     {
      Buffer1[i]  = buffer_MA[i]+(buffer_MA[i+1]-buffer_MA[i+InpShiftCorrection])/(InpShiftCorrection/GS);//1.314;
     }

The error is gone. Thank you )))

 
I downloaded mt5 for my trading account. I am a Beginner, where to go and where to pay for real trading?
 
Раиль Алеев:
I have downloaded mt5. for my trading account. Can you tell me where and how to pay for the real trading? I am a NEWbie.

Switch on the computer. Boot up your browser. In the search bar type the words "open a MetaTrader 5 account".

 
Does CodeBase have an EA with "one trade per bar" function (excluding EA "on bar opening")?
 

Has this ever worked, or not?

How can I make it so that when a colour is changed in the input parameters, this colour is in"indicator_color1" ? Right now, no matter how you change it, it's the original

#property indicator_type1   DRAW_LINE
#property indicator_color1  clrAqua


int OnCalculate(const int rates_total,
                const int prev_calculated,
                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[])
  {
   Comment( indicator_color1 ); // постоянно clrAqua

   return(rates_total);
  }
Reason: