Перевод небольшого индикатора с MQL4 на MQL5, прошу помощи

 

Есть вот такой индикатор на MQL4:


//+------------------------------------------------------------------+
//|                                          CandleTimeStationary.mq4|
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+

#property indicator_chart_window
//---- input parameters

extern color Color = Red;
extern int size = 10;
string objname="close";

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init(){

  ObjectCreate(objname, OBJ_LABEL,0, 0, 0);
  ObjectSet(objname, OBJPROP_CORNER, 1);
  ObjectSet(objname, OBJPROP_XDISTANCE, 10);
  ObjectSet(objname, OBJPROP_YDISTANCE, 2);    
  
   //----
   return(0);
}

void OnDeinit(const int reason)
  {
//---
   Comment("");
   ObjectDelete(0,objname);
   EventKillTimer();
  }

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{

  int tmp=Time[0]+Period()*60-TimeCurrent();
  
  int s=tmp%60;
  int m=((tmp-s)/60)%60;
  int h=((tmp-s)/60)/60;
  if(s<10) string _s="0";
  if(m<10) string _m="0";
  if(h<10) string _h="0";
  ObjectSetText(objname,"Время до закрытия свечи: "+_h+h+":"+_m+m+":"+_s+s, size, "Arial", Color);
  return(0);
}
//+---------------------------------------------------------

Хочу перевести на MQL5, но он не работает.. Помогите пожалуйста!

//+------------------------------------------------------------------+
//|                                                   time_close.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0

input color Color = clrRed;
input int size = 10;
string objname="time";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {

  ObjectCreate(0, objname, OBJ_TEXT,0, 0, 0);
  ObjectSetInteger(0, objname, OBJPROP_CORNER, 1);
  ObjectSetInteger(0, objname, OBJPROP_XDISTANCE, 10);
  ObjectSetInteger(0, objname, OBJPROP_YDISTANCE, 2);

   return(INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason)
  {
   Comment("");
   ObjectDelete(0,objname);
   EventKillTimer();
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 tmp=time[0]+Period()*60-TimeCurrent();
  
  int s=tmp%60;
  int m=((tmp-s)/60)%60;
  int h=((tmp-s)/60)/60;

  string _s = "";
  string _h = "";
  string _m = "";
  if(s<10) _s="0";
  if(m<10) _m="0";
  if(h<10) _h="0";

  ObjectSetString(0,objname,OBJPROP_TEXT,"Время до закрытия свечи: "+_h+IntegerToString(h)+":"+_m+IntegerToString(m)+":"+_s+IntegerToString(s));
  ObjectSetString(0,objname,OBJPROP_FONT,"Times New Roman");
  ObjectSetInteger(0,objname,OBJPROP_COLOR,Color);
  ObjectSetInteger(0,objname,OBJPROP_FONTSIZE,size);
  

   return(rates_total);
  }
//+------------------------------------------------------------------+
 

Из того, что сразу видно: Period заменить на PeriodSeconds

 
Второе: time[0] в ИНДИКАТОРЕ mql5 - это самый ЛЕВЫЙ БАР НА ГРАФИКЕ.
 

Третье: если графический объект создан и ему применили уже свойства шрифта, то уже в OnCalculate() не нужно на каждом тике переписывать ему свойства шрифта:

  ObjectSetString(0,objname,OBJPROP_FONT,"Times New Roman");
  ObjectSetInteger(0,objname,OBJPROP_COLOR,Color);
  ObjectSetInteger(0,objname,OBJPROP_FONTSIZE,size);
то есть, лучше эти три строки перенести в OnInit().
 
Vladimir Karputov:
Второе: time[0] в ИНДИКАТОРЕ mql5 - это самый ЛЕВЫЙ БАР НА ГРАФИКЕ.

А мне что надо использовать ?

Пока что так, но тоже ничего не выводит

//+------------------------------------------------------------------+
//|                                                   time_close.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0

input color Color = clrRed;
input int size = 10;
string objname="time";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {

  ObjectCreate(0, objname, OBJ_TEXT,0, 0, 0);
  ObjectSetInteger(0, objname, OBJPROP_CORNER, 1);
  ObjectSetInteger(0, objname, OBJPROP_XDISTANCE, 10);
  ObjectSetInteger(0, objname, OBJPROP_YDISTANCE, 2);
  ObjectSetString(0,objname,OBJPROP_FONT,"Times New Roman");
  ObjectSetInteger(0,objname,OBJPROP_COLOR,Color);
  ObjectSetInteger(0,objname,OBJPROP_FONTSIZE,size);

   return(INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason)
  {
   Comment("");
   ObjectDelete(0,objname);
   EventKillTimer();
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 tmp=TimeToString(time[0]+PeriodSeconds()*60-TimeCurrent());
  
  int s=tmp%60;
  int m=((tmp-s)/60)%60;
  int h=((tmp-s)/60)/60;

  string _s = "";
  string _h = "";
  string _m = "";
  if(s<10) _s="0";
  if(m<10) _m="0";
  if(h<10) _h="0";

  ObjectSetString(0,objname,OBJPROP_TEXT,"Время до закрытия свечи: "+_h+IntegerToString(h)+":"+_m+IntegerToString(m)+":"+_s+IntegerToString(s));
      
  return(rates_total);
  }
//+------------------------------------------------------------------+
 
investr777:
А мне что надо использовать?

Вариантов два:

  • или перевернуть массив (ArraySetAsSeries(time,true) - поставить пеhвой строкой в OnCalculate())
  • или писать time[rates_total-1]
 
Vladimir Karputov:

Вариантов два:

  • или перевернуть массив (ArraySetAsSeries(time,true) - поставить пеhвой строкой в OnCalculate())
  • или писать time[rates_total-1]

Сделал все как вы сказали:

//+------------------------------------------------------------------+
//|                                                   time_close.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 0
#property indicator_plots   0

input color Color = clrRed;
input int size = 10;
string objname="time";
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {

  ObjectCreate(0, objname, OBJ_TEXT,0, 0, 0);
  ObjectSetInteger(0, objname, OBJPROP_CORNER, 1);
  ObjectSetInteger(0, objname, OBJPROP_XDISTANCE, 10);
  ObjectSetInteger(0, objname, OBJPROP_YDISTANCE, 2);
  ObjectSetString(0,objname,OBJPROP_FONT,"Times New Roman");
  ObjectSetInteger(0,objname,OBJPROP_COLOR,Color);
  ObjectSetInteger(0,objname,OBJPROP_FONTSIZE,size);

   return(INIT_SUCCEEDED);
  }
  
void OnDeinit(const int reason)
  {
   Comment("");
   ObjectDelete(0,objname);
   EventKillTimer();
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 tmp=time[rates_total-1]+PeriodSeconds()*60-TimeCurrent();
  
  int s=tmp%60;
  int m=((tmp-s)/60)%60;
  int h=((tmp-s)/60)/60;

  string _s = "";
  string _h = "";
  string _m = "";
  if(s<10) _s="0";
  if(m<10) _m="0";
  if(h<10) _h="0";

  ObjectSetString(0,objname,OBJPROP_TEXT,"Время до закрытия свечи: "+_h+IntegerToString(h)+":"+_m+IntegerToString(m)+":"+_s+IntegerToString(s));
      
  return(rates_total);
  }
//+------------------------------------------------------------------+

Не хочет ничего выводить почему-то. Может еще я где-нибудь упустил что-то...

 
investr777:

Сделал все как вы сказали:

Не хочет ничего выводить почему-то. Может еще я где-нибудь упустил что-то...


Время не может быть int.

Нужно переводить в long:

long tmp=(long)time[rates_total-1]+(long)(PeriodSeconds()*60)-(long)TimeCurrent();
 

Обратите внимание, на координаты Вашего графического объекта

Графический объект далеко за пределами чарта

 

Пример работы в объектом OBJ_TEXT.

 
Vladimir Karputov:

Пример работы в объектом OBJ_TEXT.

Хм, не могу пока понять в чем проблема

Причина обращения: