Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1215

 

good afternoon everyone, I need a hint on how to translate code from MT4 to MT5...


function on MT4 - returns True or False using MA.


input int                 MinDist          = 30;            // Мин. пройденное расстояние( отклонение от средней линии в  тиках)
input int                 OpenDev          = 16;         // Отклонение для входа ( отклонение от средней линии в  тиках)
//*********************************************************************************************
bool SignalBuy()  
  {
   double MA0 = iMA(Symbol(), 0, PeriodMA, ShiftMA, MethodMA, PriceMA, 0);
   if(Bid <= MA0+OpenDev*Point)
     {
      if(High[0]-MA0 >= MinDist*Point)
         return(true);
      for(int i=1; i<=Bars-1; i++)
        {
         double MA = iMA(Symbol(), 0, PeriodMA, ShiftMA, MethodMA, PriceMA, i);
         if(Low[i] <= MA+OpenDev*Point)
            return(false);
         if(High[i]-MA >= MinDist*Point)
            return(true);
        }
     }
   return(false);
  }
//*********************************************************************************************

need to transfer it to MT5

The problem is in time series Low[i] and High[i] , i don't understand how to convert them.

 
Milhail Novgorodcev:

good afternoon everyone, I need a hint on how to translate code from MT4 to MT5...


function on MT4 - returns True or False using MA.


need to transfer it to MT5

The problem is in time series Low[i] and High[i] , i dont understand how to convert them.

You have to create indicator handle ONLY once. This is done in the OnInit. Example of creation:iMA

2. First fix point 1.

Документация по MQL5: Технические индикаторы / iMA
Документация по MQL5: Технические индикаторы / iMA
  • www.mql5.com
//|                                                     Demo_iMA.mq5 | //|                        Copyright 2011, MetaQuotes Software Corp. | //|                                             https://www.mql5.com | //| Перечисление способов создания хэндла                            |  Creation             type=Call_iMA;                ...
 
Vladimir Karputov:

1. the indicator handle MUST be created once. This is done in OnInit. Example of creation:iMA

2. First correct item 1.

Here is what you get:

int MA0;                                  // variable for storing the handle of the iMA indicator

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  
MA0 = iMA(o_symbol.Name(), Period(), PeriodMA, ShiftMA, MethodMA, PriceMA);
  
  //--- if the handle is not created
   if(handle_iMA==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  m_symbol.Name(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
 
Milhail Novgorodcev:

that's what I got:

Now tell me, why do you need such a huge cycle - across all the bars?

      for(int i=1; i<=Bars-1; i++)
        {

It's very irrational.


Describe the logic in your own words.

 
Vladimir Karputov:

Now tell me, why do you need such a huge cycle - across all the bars?

It's very irrational.


Describe the logic in your own words.

The condition - the price moves away from the midline atMinDist(30 pips). If it happens, then when it comes back to the midline and reachesOpenDev( 10 pips) - there is a signal to open a Buy order. (It is always Buy at the top of the average line)

 
Milhail Novgorodcev:

The condition is that the price moves away from the average line on theMinDist (30 pips), if it happens, then when the price returns to the average line and reachesOpenDev (10 pips) - there is a signal to open a Buy order. (It is always Buy at the top of the average line)

When should we check 'MinDist(30 pips)' and 'OpenDev(10 pips)' - at every tick or only at the moment of a new bar opening?

 

What is the reason for opening a position immediately after the EA has been started?

The conditions for opening a position are not met, but the position is opened

 if(
      (MovingAverage1[0]<MovingAverage2[0]) &&
      (MovingAverage1[1]>MovingAverage2[1])
   )
     {
      if(PositionsTotal()==0)
         trade.Sell(0.1);
     }

   if(
      (MovingAverage1[0]>MovingAverage2[0]) &&
      (MovingAverage1[1]<MovingAverage2[1])
   )
     {
      if(PositionsTotal()==1)
         trade.PositionClose(_Symbol);
        }

 
Pineapple88:

What is the reason for opening a position immediately after the EA has been started?

The conditions for opening a position are not fulfilled, but the position is opened

Where and how are the values of averages obtained? Did you get them at all? If the values are normally obtained, have you noticed the indexing in the array of averages?

 
Alexey Viktorov:

Where and how are the mean values obtained? Are they obtained at all? If these values are obtained normally, have you noticed the indexing in the arrays of averages?

Here is the code

 {

   double MovingAverage1[], MovingAverage2[]; //задаем два массиса для МА

   int MovingAverageIndic1 = iMA(_Symbol,0,20,0,MODE_EMA,PRICE_CLOSE);
   int MovingAverageIndic2 = iMA(_Symbol,0,200,0,MODE_EMA,PRICE_CLOSE);

   ArraySetAsSeries(MovingAverage1,true);
   ArraySetAsSeries(MovingAverage2,true);

   CopyBuffer(MovingAverageIndic1,0,0,3,MovingAverage1);
   CopyBuffer(MovingAverageIndic2,0,0,3,MovingAverage2);

   if(
      (MovingAverage1[0]<MovingAverage2[0]) &&
      (MovingAverage1[1]>MovingAverage2[1])
   )
     {
      if(PositionsTotal()==0)
         trade.Sell(0.1);
     }

   if(
      (MovingAverage1[0]>MovingAverage2[0]) &&
      (MovingAverage1[1]<MovingAverage2[1])
   )
     {
      if(PositionsTotal()==1)
         trade.PositionClose(_Symbol);
     }

  }
 
Pineapple88:

Here is the code

Indicator handle MUST be created once. This is done in OnInit. Example of creation:iMA

Документация по MQL5: Технические индикаторы / iMA
Документация по MQL5: Технические индикаторы / iMA
  • www.mql5.com
//|                                                     Demo_iMA.mq5 | //|                        Copyright 2011, MetaQuotes Software Corp. | //|                                             https://www.mql5.com | //| Перечисление способов создания хэндла                            |  Creation             type=Call_iMA;                ...
Reason: