初学者的问题 MQL5 MT5 MetaTrader 5 - 页 1010

 

MQL5

我似乎无法让它发挥作用。谁能提出一些建议?

关于交易、自动交易系统和策略测试的论坛

谁能给我一个提示。

Sergey Tabolin, 2019.03.07 08:14

基于代码库中的一个指标,我写了一个我想要的例子。它是有效的。但它在启动时出现了错误。

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

你能告诉我错误在哪里吗?

//+------------------------------------------------------------------+
//|                                                       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);
  }
//+------------------------------------------------------------------+

哪里出了问题?
附加的文件:
my_MA_S.mq5  10 kb
 
Сергей Таболин:

MQL5

我似乎无法让它发挥作用。谁能给我一个提示?

有什么问题吗?
两个缓冲区,但#属性指定的是一个
 
Artyom Trishkin:
两个缓冲区,#属性指定一个

谢谢你的提示...

但一切都没有改变。我在指标方面完全是个负数(())。

#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);
 
Сергей Таболин:

谢谢你的提示...

但一切都没有改变。我在指标方面完全是个负数(())。

我从手机上看不出来
 
Сергей Таболин:

谢谢你的提示...

但一切都没有改变。我在指标方面完全是个负数(()。

没有认真看代码,不清楚错误在哪一行,但好像错误不在这里。

尝试减少限制

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

没有认真看代码,也不清楚错误在哪一行,但错误好像不在这里。

尝试减少限制

错误正是在这个循环中。

   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);// ошибка тут
     }

但你的建议促使我做了一个小小的改变。

   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;
     }

这个错误已经消失了。谢谢你))))。

 
我为我的交易账户下载了mt5。我是一个初学者,该去哪里,去哪里支付真实的交易?
 
Раиль Алеев:
我已经为我的交易账户下载了mt5。你能告诉我在哪里以及如何支付真正的交易吗? 我是一个新手。

开启电脑。开启你的浏览器。在搜索栏中输入 "开设MetaTrader 5账户"。

 
CodeBase是否有一个具有 "每条交易 "功能的EA(不包括 "开条 "的EA)?
 

这是否曾经起过作用,或者没有?

我怎样才能使它在输入参数中改变颜色时,这个颜色在"indicator_color1" ?现在,无论你如何改变它,它都是原来的样子。

#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);
  }