Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1239

 
Alexey Viktorov:

I can't advise you on anything. I don't use optimisation. I consider it quackery.

I disagree. Optimisation, in my opinion, is the selection of the most suitable parameters for a given pair and strategy.

P.S. Now the spreads seem to be normal.

 
Youri Lazurenko:

I disagree.

I was not agitating you. Just giving my personal opinion.))

 
Alexey Viktorov:

I was not agitating you. I just gave my personal opinion.))

Maybe my opinion is wrong. I am not agitating either.

 

Hello. Need some help. I need to limit the lifetime of a pending order, preferably in the number of bars. I have used the function in mql4:

datetime DateExp()                                 
{
   datetime expDate = 0;
   
   if(Period() == PERIOD_M1)
      expDate = TimeCurrent() + ExpDate*60;
      
   if(Period() == PERIOD_M5)
      expDate = TimeCurrent() + ExpDate*5*60;
    
   if(Period() == PERIOD_M15)
      expDate = TimeCurrent() + ExpDate*15*60;
    
   if(Period() == PERIOD_M30)
      expDate = TimeCurrent() + ExpDate*30*60;
    
   if(Period() == PERIOD_H1)
      expDate = TimeCurrent() + ExpDate*60*60;
    
   if(Period() == PERIOD_H4)
      expDate = TimeCurrent() + ExpDate*4*60*60;
   
   if(Period() == PERIOD_D1)
      expDate = TimeCurrent() + ExpDate*24*60*60;      
    
   if(Period() == PERIOD_W1) 
      expDate = TimeCurrent() + ExpDate*7*24*60*60; 
   
   if(Period() == PERIOD_MN1)
      expDate = TimeCurrent() + ExpDate*30*24*60*60;
          
   return(expDate);  
}

where

extern intDate = 6; //number of bars of the order lifetime

Everything works.

I have tried something similar in mql5:

datetime DateExp()                                 
{
   datetime expDate = 0;
   
   if(Period() == PERIOD_M1)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_M1)*ExpDate;
      
   if(Period() == PERIOD_M2)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_M2)*ExpDate;
   
   if(Period() == PERIOD_M3)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_M3)*ExpDate;
   
   if(Period() == PERIOD_M4)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_M4)*ExpDate;
      
   if(Period() == PERIOD_M5)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_M5)*ExpDate;
   
   if(Period() == PERIOD_M6)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_M6)*ExpDate;
   
   if(Period() == PERIOD_M10)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_M10)*ExpDate;
   
   if(Period() == PERIOD_M12)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_M12)*ExpDate;
    
   if(Period() == PERIOD_M15)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_M15)*ExpDate;
   
   if(Period() == PERIOD_M20)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_M20)*ExpDate;
    
   if(Period() == PERIOD_M30)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_M30)*ExpDate;
    
   if(Period() == PERIOD_H1)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_H1)*ExpDate;
   
   if(Period() == PERIOD_H2)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_H2)*ExpDate;
   
   if(Period() == PERIOD_H3)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_H3)*ExpDate;
    
   if(Period() == PERIOD_H4)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_H4)*ExpDate;
   
   if(Period() == PERIOD_H6)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_H6)*ExpDate;
   
   if(Period() == PERIOD_H8)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_H8)*ExpDate;
   
   if(Period() == PERIOD_H12)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_H12)*ExpDate;
   
   if(Period() == PERIOD_D1)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_D1)*ExpDate;      
    
   if(Period() == PERIOD_W1) 
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_W1)*ExpDate; 
   
   if(Period() == PERIOD_MN1)
      expDate = TimeCurrent() + PeriodSeconds(PERIOD_MN1)*ExpDate;
          
   return(expDate);  
}

where

input int ExpDate = 6; //number of bars of the order lifetime

Then I tried it (the function shown above) to use it when placing a pending order, for example, to buy

               request.action       = TRADE_ACTION_PENDING;                                         
               request.magic        = Magic;                                                       
               request.symbol       = _Symbol;                                                      
               request.volume       = GetLots();                                                    
               request.price        = openBuy;                                                      
               request.sl           = slBuy;                                                        
               request.tp           = tpBuy;                                                        
               request.type         = ORDER_TYPE_BUY_STOP;                                          
               request.type_filling = ORDER_FILLING_FOK;                                           
               request.expiration   = DateExp();                                                    
               request.deviation    = SG; 

The pending order is placed but does not live until it is activated (in the tester). I have tried to set a simple number in request.expiration but it does not work. I did not find any information on google. What is wrong with me? Why isn't the order being deleted after a certain period of time?

P.S. When displayed in Comment, DateExp() function shows date, time.

Документация по MQL5: Константы, перечисления и структуры / Торговые константы / Свойства ордеров
Документация по MQL5: Константы, перечисления и структуры / Торговые константы / Свойства ордеров
  • www.mql5.com
Приказы на проведение торговых операций оформляются ордерами. Каждый ордер имеет множество свойств для чтения, информацию по ним можно получать с помощью функций Идентификатор позиции, который ставится на ордере при его исполнении. Каждый исполненный ордер порождает сделку, которая открывает новую или изменяет уже существующую позицию...
 
Youri Lazurenko:


You forgot to specify:

struct MqlTradeRequest
  {
   ENUM_TRADE_REQUEST_ACTIONS    action;           // Тип выполняемого действия
   ulong                         magic;            // Штамп эксперта (идентификатор magic number)
   ulong                         order;            // Тикет ордера
   string                        symbol;           // Имя торгового инструмента
   double                        volume;           // Запрашиваемый объем сделки в лотах
   double                        price;            // Цена 
   double                        stoplimit;        // Уровень StopLimit ордера
   double                        sl;               // Уровень Stop Loss ордера
   double                        tp;               // Уровень Take Profit ордера
   ulong                         deviation;        // Максимально приемлемое отклонение от запрашиваемой цены
   ENUM_ORDER_TYPE               type;             // Тип ордера
   ENUM_ORDER_TYPE_FILLING       type_filling;     // Тип ордера по исполнению
   ENUM_ORDER_TYPE_TIME          type_time;        // Тип ордера по времени действия
   datetime                      expiration;       // Срок истечения ордера (для ордеров типа ORDER_TIME_SPECIFIED)
   string                        comment;          // Комментарий к ордеру
   ulong                         position;         // Тикет позиции
   ulong                         position_by;      // Тикет встречной позиции
  };
 
Vladimir Karputov:

You forgot to specify:

Thanks, already found it myself, wanted to write that the question is removed. Before these, but I put, it turns out, wrong parameter request.type_time = ORDER_TIME_GTC; Corrected to request.type_time = ORDER_TIME_SPECIFIED; and everything works. But thank you very much for the feedback and the tip.

 
Artyom Trishkin:

Alexey told you that you should first make your indicator at least just draw candles. As they are. At least on the current bar. If you manage to do it, consider that the first step to understanding has passed. But it is desirable not to try and find it by trying different parameters, but with your own mind.

What does this have to do with a priori? You really need it, since you cannot draw a candle with only four values.

Here I have written a very simple test indicator, that should draw 5 candles (real ones) from the USDJPY H8 chart

//+------------------------------------------------------------------+
//|                                                        cTest.mq5 |
//|                                     Copyright 2020, Tabolin S.N. |
//|                           https://www.mql5.com/ru/users/vip.avos |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, Tabolin S.N."
#property link      "https://www.mql5.com/ru/users/vip.avos"
#property version   "1.00"
//#property indicator_separate_window
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_plots   1
//--- plot bars
#property indicator_label1  "bars"
#property indicator_type1   DRAW_CANDLES
#property indicator_color1  clrGold
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- indicator buffers
double   barsBuffer1[];
double   barsBuffer2[];
double   barsBuffer3[];
double   barsBuffer4[];
//+------------------------------------------------------------------+
double   newCandles_Open[5]   = {106.785, 106.724, 106.760, 106.767, 106.769};
double   newCandles_High[5]   = {106.792, 106.765, 106.780, 106.781, 106.769};
double   newCandles_Low[5]    = {106.716, 106.719, 106.746, 106.758, 106.715};
double   newCandles_Close[5]  = {106.724, 106.760, 106.766, 106.769, 106.725};

int      tick_count           = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
//--- indicator buffers mapping
   SetIndexBuffer(0, barsBuffer1, INDICATOR_DATA);
   SetIndexBuffer(1, barsBuffer2, INDICATOR_DATA);
   SetIndexBuffer(2, barsBuffer3, INDICATOR_DATA);
   SetIndexBuffer(3, barsBuffer4, INDICATOR_DATA);
   
   ArraySetAsSeries(barsBuffer1  ,true);
   ArraySetAsSeries(barsBuffer2  ,true);
   ArraySetAsSeries(barsBuffer3  ,true);
   ArraySetAsSeries(barsBuffer4  ,true);
   
   ArrayInitialize(barsBuffer1   ,0.0);
   ArrayInitialize(barsBuffer2   ,0.0);
   ArrayInitialize(barsBuffer3   ,0.0);
   ArrayInitialize(barsBuffer4   ,0.0);
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int  OnCalculate(
   const int        rates_total,       // размер входных таймсерий
   const int        prev_calculated,   // количество обработанных баров на предыдущем вызове
   const datetime&  time[],            // массив Time
   const double&    open[],            // массив Open
   const double&    high[],            // массив High
   const double&    low[],             // массив Low
   const double&    close[],           // массив Close
   const long&      tick_volume[],     // массив Tick Volume
   const long&      volume[],          // массив Real Volume
   const int&       spread[]           // массив Spread
   )
{
   if(rates_total - prev_calculated > 1)
   {
      if(prev_calculated == 0)
      {
         Print("~~~~ Предварительный расчёт индикатора.");
         for(int i = 0; i < 5; i++)
         {
            barsBuffer1[0] = newCandles_Open[i];
            barsBuffer2[0] = newCandles_High[i];
            barsBuffer3[0] = newCandles_Low[i];
            barsBuffer4[0] = newCandles_Close[i];
         }
         Print("~~~~ Предварительный расчёт индикатора закончен.");
      }
      else return(0);
   }
   else Print("tick_count = ",++tick_count);
//--- return value of prev_calculated for next call
   return(rates_total);
}
//+------------------------------------------------------------------+

It only draws those candlesticks, nothing more.

But when I run it, it just draws the damn thing...

So tell me, what am I doing wrong?


By the way, there is a typo in the documentation:

const datetime&  time{},            // массив Time
Документация по MQL5: Обработка событий / OnCalculate
Документация по MQL5: Обработка событий / OnCalculate
  • www.mql5.com
события Calculate для обработки изменений ценовых данных. Существуют два варианта функции, в пределах одного индикатора нельзя использовать оба варианта. [in]  Размер массива price[] или входных таймсерий, доступных индикатору для расчета. Во втором варианте функции значение параметра соответствует количеству баров на графике, на котором он...
Files:
cTest.mq5  8 kb
 
Сергей Таболин:

I wrote a very simple test indicator, which should draw 5 candlesticks (real ones) from USDJPY,H8 chart

It only draws these candlesticks, nothing more.

But when I run it, it just draws the damn thing...

How can I explain what I'm doing wrong?


By the way, there is a typo in the documentation:

In the debugger, check time[0] and answer the question: "Why are you trying to draw candlesticks from the LEFT FRONT of the chart?


I told you - use help for DRAW_CANDLES. Copy the example from the DRAW_CANDLES help. Figure it out. It's too early for you to get into ticks.

Документация по MQL5: Пользовательские индикаторы / Стили индикаторов в примерах / DRAW_CANDLES
Документация по MQL5: Пользовательские индикаторы / Стили индикаторов в примерах / DRAW_CANDLES
  • www.mql5.com
//|                                                 DRAW_CANDLES.mq5 | //|                        Copyright 2011, MetaQuotes Software Corp. | //|                                             https://www.mql5.com | "Рисует в отдельном окне разным цветом свечи по случайно выбранному из MarketWatch символу...
 
Vladimir Karputov:

In the debugger check time[0] and answer the question: "Why are you trying to draw candles from the LEFT LEFT edge of the chart?


I told you - use help for DRAW_CANDLES. Copy the example from the DRAW_CANDLES help. Figure it out. It is too early for you to get into ticks.

Vladimir, I have already understood what I was trying to draw from the leftmost edge. The test one has the right edge.

   ArraySetAsSeries(barsBuffer1  ,true);
   ArraySetAsSeries(barsBuffer2  ,true);
   ArraySetAsSeries(barsBuffer3  ,true);
   ArraySetAsSeries(barsBuffer4  ,true);

And the candlesticks' prices are real... And no ticks...

 
Сергей Таболин:

Vladimir, I've already understood that I was trying to draw from the leftmost edge. The test one is now drawing from the right edge

And the candlestick prices are real... And no ticks...

Take the example from the help! How many times? You don't have to use your own fictitious constructs if you don't understand their meaning. Use standard constructs - figure out how they work. After that, throw your constructs in the trash.


And use 'MQL Wizard' to generate a template.

Code

//+------------------------------------------------------------------+
//|                                                 Draw Candles.mq5 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots   1
//--- plot USDJPY
#property indicator_label1  "USDJPY"
#property indicator_type1   DRAW_CANDLES
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int      Input1=9;
//--- indicator buffers
double   Buffer1[];
double   Buffer2[];
double   Buffer3[];
double   Buffer4[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Buffer1,INDICATOR_DATA);
   SetIndexBuffer(1,Buffer2,INDICATOR_DATA);
   SetIndexBuffer(2,Buffer3,INDICATOR_DATA);
   SetIndexBuffer(3,Buffer4,INDICATOR_DATA);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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 limit=prev_calculated-1;
   if(prev_calculated==0)
      limit=0;
   for(int i=limit; i<rates_total; i++)
     {
      Buffer1[i]=open[i];
      Buffer2[i]=high[i];
      Buffer3[i]=low[i];
      Buffer4[i]=close[i];
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

and the result


Files:
Reason: