Errors, bugs, questions - page 52

 
It wasn't like that in MT4, thank you. just took it down
 

During testing of Expert Advisors:
Exp_TEMA.mq5, from the article: "Creation of an Expert Advisor that trades on different symbols". , expression Told[] Tnew[1]gets the following values:
Told[] Expression could not be evaluated
Tnew[1] Invalid array range.

And My_First_EA.mq5, from the article: "A Step-by-Step Guide to Writing MQL5 Expert Advisors for Beginners", the expression
New_Time[1] gets the value:Invalid array range

What do the values Expression could not be evaluated,Invalid array range, and how do they affect the experts' results?

 

Told - empty array with no size

Tnew[1] - out of array, the array is described as Tnew[1], so its elements can only be accessed as Tnew[0], because the index starts at zero.

 
Renat:

Told - empty array with no size

Tnew[1] - out of array, the array is described as Tnew[1], so its elements can only be accessed as Tnew[0], because the index starts at zero.


As for Tnew, I've already mentioned in another forum thread, apparently, a comment from developers was needed :)
 

Please tell me how to open only one trade per condition. With my code, the terminal opens several deals on this condition, but I need only one.

       if(Buy_Signal)
        {
         mrequest.price = NormalizeDouble(latest_price.ask,_Digits);           // последняя цена ask
         mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit
         mrequest.type = ORDER_TYPE_BUY;                                       // ордер на покупку
         //--- отсылаем ордер
         OrderSend(mrequest,mresult);        
        }        

       if(Sell_Signal)
        {
         mrequest.price = NormalizeDouble(latest_price.bid,_Digits);           // последняя цена Bid
         mrequest.sl = NormalizeDouble(latest_price.bid + STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(latest_price.bid - TKP*_Point,_Digits); // Take Profit
         mrequest.type= ORDER_TYPE_SELL;                                       // ордер на продажу                                          
         //--- отсылаем ордер
         OrderSend(mrequest,mresult);              
        }
      

The result is the following picture:

How do I check if a deal has been done with this condition or not? I tried it this way, but it doesn't work:

// в этом цикле поочередно перебираем все открытые позиции
   for(i=0;i<PositionsTotal();i++)
     {
      // выбираем позиции только по "нашему" инструменту
      if(Symbol()==PositionGetSymbol(i))
     ...
     }
 
AM2:

Please tell me how to open only one trade per condition. With my code, the terminal opens several deals on this condition, but I need only one.

The result is the following picture:

How do I check if a deal has been done with this condition or not? I tried it this way, but it doesn't work:

And why arranging search in the loop if there is such a wonderful thing as PositionSelect?

We just check existence of position on the interesting symbol and if it is true, we go to drink cognac and smoke cigars... :)

PS

As I understand it PositionGetString withoutPositionSelect should not work. We don't need to know the symbols of all open poses, right?

 
Interesting:

There is no need to search in the loop when there is such a wonderful thing as PositionSelect?

Simply check presence of a position on the tool we are interested in and if it comes back true we go quietly to drink cognac and smoke cigars... :)

PS

As I understand it PositionGetString withoutPositionSelect should not work. We don't need to know the symbols of all open poses, right?

This code takes into account the presence of an open position:

   if(Buy_Signal && PositionSelect(Symbol())==false)
     {
         mrequest.type = ORDER_TYPE_BUY;                                       // ордер на покупку
         mrequest.price = NormalizeDouble(latest_price.ask,_Digits);           // последняя цена ask
         mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit
         OrderSend(mrequest,mresult);                                          // отсылаем ордер                
     }

But it continues to open positions when the Buy_Signal condition is met. What I want is that before Sell_Signal occurs a position at Buy_Signal will not open again. I'm trying to implement it this way:

   Buy_Signal = true;
   Sell_Signal = true;                

   if(Buy_Signal && PositionSelect(Symbol())==false)
     {
         mrequest.type = ORDER_TYPE_BUY;                                       // ордер на покупку
         mrequest.price = NormalizeDouble(latest_price.ask,_Digits);           // последняя цена ask
         mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit
         OrderSend(mrequest,mresult);                                          // отсылаем ордер
         Buy_Signal = false;
         Sell_Signal = true;                
     }


   if(Sell_Signal && PositionSelect(Symbol())==false)
     {
         mrequest.type= ORDER_TYPE_SELL;                                       // ордер на продажу
         mrequest.price = NormalizeDouble(latest_price.bid,_Digits);           // последняя цена Bid
         mrequest.sl = NormalizeDouble(latest_price.bid + STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(latest_price.bid - TKP*_Point,_Digits); // Take Profit
         OrderSend(mrequest,mresult);                                          // отсылаем ордер    
         Buy_Signal = true;
         Sell_Signal = false;                
     }
It's not going.
 
AM2:

Such code takes into account the presence of an open position:

But it continues to open positions when Buy_Signal condition is met. I need to make sure that before Sell_Signal condition occurs no more positions will be opened by Buy_Signal. I am trying to implement it this way:

It does not.

Why this code? It hardly looks like what I meant (I doubt that everything is correct there).

   if(Buy_Signal && PositionSelect(Symbol())==false)
     {
         mrequest.type = ORDER_TYPE_BUY;                                     // ордер на покупку
         mrequest.price = NormalizeDouble(latest_price.ask,_Digits);          // последняя цена ask
         mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss
         mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit 
         OrderSend(mrequest,mresult);                                        // отсылаем ордер                
     }

And I meant something like this:

Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);

SL = NormalizeDouble(Ask - 500*_Point,_Digits);  // Stop Loss
TP = NormalizeDouble(Ask + 1500*_Point,_Digits); // Take Profit

  if(Buy_Signal)
  //Получен сигнал на market buy
  {
  
    if(!PositionSelect(_Symbol))
    //Позиции по данному символу отсутствуют
    {
    mrequest.action = TRADE_ACTION_DEAL; //Установка рыночного ордера (позиция)
    mrequest.type   = ORDER_TYPE_BUY;    //Ордер на покупку
    mrequest.magic = 18072010;          //ORDER_MAGIC
    mrequest.symbol = _Symbol;          //Инструмент
    mrequest.volume = 0.1;              //Объем в 0.1 лот
    mrequest.price = Ask;   //Цена открытия
    mrequest.sl = SL;       //Stop Loss
    mrequest.tp = TP;       //Take Profit
    mrequest.deviation = 5; //Отклонение в 5 пунктов   
    OrderSend(mrequest,mresult); //Отсылаем ордер
    }
  
  }

At the very least it could be done in a block

  if((Buy_Signal)&&(!PositionSelect(_Symbol)))
  //Получен сигнал market buy
  {
  ...........................................  
  }
 
Interesting:

Why this particular code? It's not even remotely similar to what I meant (I don't think it's right at all).

And I meant something like this:

At the very least everything can be done in a block.

if((Buy_Signal)&&(!PositionSelect(_Symbol)))
  //Получен сигнал market buy
  {
  ...........................................  
  }

This and above code does not open a position until it is closed, but opens anew after closing on the next bar if the condition is fulfilled. I have implemented it this way:

bool BuyOne = true, SellOne = true; // только один ордер.  глобальные переменные




   if(Buy_Signal &&                                                         // покупаем если есть сигнал на покупку
      PositionSelect(Symbol())==false &&                                    // ордер закрыт
      BuyOne)                                                               // при условии на покупку ставим только один ордер
     {
      mrequest.type = ORDER_TYPE_BUY;                                       // ордер на покупку
      mrequest.price = NormalizeDouble(latest_price.ask,_Digits);           // последняя цена ask
      mrequest.sl = NormalizeDouble(latest_price.ask - STP*_Point,_Digits); // Stop Loss
      mrequest.tp = NormalizeDouble(latest_price.ask + TKP*_Point,_Digits); // Take Profit
      OrderSend(mrequest,mresult);                                          // отсылаем ордер
      BuyOne = false;                                                       // на покупку только один ордер                                                  
      SellOne = true;                                                       // меняем флаг одного ордера на продажу          
     }

Thank you! I have taken all your wishes into account and everything is now working. I have posted the Expert Advisor for publication. I can make some more changes in the code. There is no limit to the seduction)))

 
I haven't found any description of response codes (Retcode) for the structure of MqlTradeCheckResult trade request check results in the documentation. Will there be any?
Документация по MQL5: Стандартные константы, перечисления и структуры / Структуры данных / Структура торгового запроса
Документация по MQL5: Стандартные константы, перечисления и структуры / Структуры данных / Структура торгового запроса
  • www.mql5.com
Стандартные константы, перечисления и структуры / Структуры данных / Структура торгового запроса - Документация по MQL5
Reason: