Questions from Beginners MQL5 MT5 MetaTrader 5 - page 1564

 
Nauris Zukas position comments. How can I find out which Magic is set in the environment?

First you need to select the position you are going to work with using one of the functions below:

PositionGetSymbol

Returns the symbol of the corresponding open position

PositionSelect

Selects an open position for further work with it.

PositionSelectByTicket

Selects an open position for further work with it according to the specified ticket.

but just note that the PositionSelect() and PositionSelectByTicket() functions have a peculiarity:

PositionSelect() и PositionSelectByTicket()  копируют данные о позиции в программное окружение, и последующие вызовы PositionGetDouble(), PositionGetInteger() и 
PositionGetString() возвращают ранее скопированные данные. Это означает, что самой позиции может уже и не быть (или же она изменилась по объему, 
направлению и т.д.), а данные этой позиции можно еще получать. 
Для гарантированного получения свежих данных о позиции рекомендуется вызывать функцию PositionSelect() и PositionSelectByTicket() непосредственно перед обращением за ними.

then you need to get its Magic:

ulong position_magic = PositionGetInteger(POSITION_MAGIC);

and then you can use position_magic where you want.

Regards, Vladimir.

 
MrBrooklin #:

First select the position you are going to work with using one of the functions below:

PositionGetSymbol

Returns the symbol of the corresponding open position

PositionSelect

Selects the open position for further work with it.

PositionSelectByTicket

Selects an open position for further work with it by the specified ticket

but just note that the PositionSelect() and PositionSelectByTicket() functions have a peculiarity:

Then you need to get its Magic:

and then you can use position_magic wherever you want.

Regards, Vladimir.

The question was about something else
 

I even wondered, then what was the question?

Regards, Vladimir.

 
MrBrooklin #:

I was wondering, what was the question then?

Regards, Vladimir.

What magik is installed in the trading facility. I'd answer, but I'm not at my computer yet.
 
Artyom Trishkin #:
Which magik is installed in the commercial facility. I'd answer, but I'm not at my computer yet.

Something seems to me from the object is not available. Magik is in the protected area:

 

Here's a question I have.

I use the code in the Expert Advisor literally from the help

//| Trade function                                                   | 
//+------------------------------------------------------------------+ 
void OnTrade() 
  { 
//--- получим тикет последней сделки из истории торговли за неделю 
   ulong last_deal=GetLastDealTicket(); 
   if(HistoryDealSelect(last_deal)) 
     { 
      //--- время совершения сделки в миллисекундах от 01.01.1970 
      long deal_time_msc=HistoryDealGetInteger(last_deal,DEAL_TIME_MSC); 
      PrintFormat("Deal #%d DEAL_TIME_MSC=%i64 => %s", 
                  last_deal,deal_time_msc,TimeToString(deal_time_msc/1000)); 
     } 
   else 
      PrintFormat("HistoryDealSelect() failed for #%d. Eror code=%d", 
                  last_deal,GetLastError()); 
//--- 
  } 
//+------------------------------------------------------------------+ 
//| Возвращает тикет последней сделки в истории или -1               | 
//+------------------------------------------------------------------+ 
ulong GetLastDealTicket() 
  { 
//--- запросим историю за последние 7 дней 
   if(!GetTradeHistory(7)) 
     { 
      //--- сообщим о неудачном вызове и вернем -1 
      Print(__FUNCTION__," HistorySelect() вернул false"); 
      return -1; 
     } 
//---  
   ulong first_deal,last_deal,deals=HistoryOrdersTotal(); 
//--- если ордера есть, начинаем работать с ними 
   if(deals>0) 
     { 
      Print("Deals = ",deals); 
      first_deal=HistoryDealGetTicket(0); 
      PrintFormat("first_deal = %d",first_deal); 
      if(deals>1) 
        { 
         last_deal=HistoryDealGetTicket((int)deals-1); 
         PrintFormat("last_deal = %d",last_deal); 
         return last_deal; 
        } 
      return first_deal; 
     } 
//--- не нашли ни одной сделки, вернем -1 
   return -1; 
  } 
//+------------------------------------------------------------------+ 
//| Запрашивает историю за последние дни и вернет false при неудаче  | 
//+------------------------------------------------------------------+ 
bool GetTradeHistory(int days) 
  { 
//--- зададим недельный период времени для запроса торговой истории 
   datetime to=TimeCurrent(); 
   datetime from=to-days*PeriodSeconds(PERIOD_D1); 
   ResetLastError(); 
//--- сделаем запрос и проверим результат 
   if(!HistorySelect(from,to)) 
     { 
      Print(__FUNCTION__," HistorySelect=false. Error code=",GetLastError()); 
      return false; 
     } 
//--- история получена успешно 
   return true; 
  }

At the beginning it works correctly in the tester, but then I get an error in the log. Why?

2024.10.02 17:58:29.257 2008.01.16 07:15:00   Deals = 8
2024.10.02 17:58:29.257 2008.01.16 07:15:00   first_deal = 6
2024.10.02 17:58:29.257 2008.01.16 07:15:00   last_deal = 0
2024.10.02 17:58:29.257 2008.01.16 07:15:00   HistoryDealSelect() failed for #0.  Eror code=4755

Here is an example that it works a little earlier

2024.10.02 17:58:29.057 2008.01.15 23:15:00   Deals = 2
2024.10.02 17:58:29.057 2008.01.15 23:15:00   first_deal = 6
2024.10.02 17:58:29.057 2008.01.15 23:15:00   last_deal = 7
2024.10.02 17:58:29.057 2008.01.15 23:15:00   Deal #7 DEAL_TIME_MSC=213520441664 => 2008.01.15 21:04
 
Aleksey Vyazmikin #:

Here's a question I have.

I am using the code in the Expert Advisor literally from the help

At the beginning it works correctly in the tester, but then I get an error in the log. Why?

Here is an example that it works a little earlier

Because

last_deal = 0
 
Vladimir Deryagin #:

Because.

So that's the question, why zero?

 
Aleksey Vyazmikin #:

So that's the question, why zero?

   ulong first_deal,last_deal,deals=HistoryOrdersTotal(); 
//--- если ордера есть, начинаем работать с ними 
   if(deals>0) 
     { 
      Print("Deals = ",deals); 
      first_deal=HistoryDealGetTicket(0); 
      PrintFormat("first_deal = %d",first_deal); 
      if(deals>1) 
        { 
         last_deal=HistoryDealGetTicket((int)deals-1); 
         PrintFormat("last_deal = %d",last_deal); 
         return last_deal; 
        } 
      return first_deal; 
     } 
//--- не нашли ни одной сделки, вернем -1 
   return -1; 
HistoryDealGetTicket

Orders, deals and positions should not be confused with each other. Each deal is the result of some order execution, each position is the final result of one or more deals.

 
Vladimir Deryagin #:
HistoryDealGetTicket

Orders, deals and positions should not be confused with each other. Each deal is the result of the execution of an order, each position is the final result of one or more deals.

You copy the code I have given you for what purpose, what does it explain?