Questions from Beginners MQL5 MT5 MetaTrader 5 - page 996

 
People help. Trying to get the price at which a trade opened. At first I traded through CTrade but trade.resultprice was equal to zero. I started to trade through MqlTrade and result.price was showing good results in Strategy Tester, but it was equal to zero in Live. I tried it with OnTradeTransaction, but it is too slow in live trading and shows zero when auto-trading is stopped. What are some ways to get the price at which the buy trade went through?
 
mikhail_shmakov:
People help. Trying to get the price at which a trade opened. At first I traded through CTrade but trade.resultprice was equal to zero. I started to trade through MqlTrade and result.price was showing well in the Strategy Tester, but it was equal to zero in Live. I tried it with OnTradeTransaction, but it is too slow in live trading showing zero and shows the last trade price when auto-trading is stopped. What are some ways to get the price at which the buy trade went through?
struct MqlTradeTransaction 
  { 
   ulong                         deal;             // Тикет сделки 
   ulong                         order;            // Тикет ордера 
   string                        symbol;           // Имя торгового инструмента 
   ENUM_TRADE_TRANSACTION_TYPE   type;             // Тип торговой транзакции 
   ENUM_ORDER_TYPE               order_type;       // Тип ордера 
   ENUM_ORDER_STATE              order_state;      // Состояние ордера 
   ENUM_DEAL_TYPE                deal_type;        // Тип сделки 
   ENUM_ORDER_TYPE_TIME          time_type;        // Тип ордера по времени действия 
   datetime                      time_expiration;  // Срок истечения ордера 
   double                        price;            // Цена  
   double                        price_trigger;    // Цена срабатывания стоп-лимитного ордера 
   double                        price_sl;         // Уровень Stop Loss 
   double                        price_tp;         // Уровень Take Profit 
   double                        volume;           // Объем в лотах 
   ulong                         position;         // Тикет позиции 
   ulong                         position_by;      // Тикет встречной позиции 
  };
What do you mean by "in very fast trades"? OnTradeTransaction works for every trade. You just need to separate the type of transaction and take the price at TRADE_TRANSACTION_DEAL_ADD
Документация по MQL5: Константы, перечисления и структуры / Структуры данных / Структура торговой транзакции
Документация по MQL5: Константы, перечисления и структуры / Структуры данных / Структура торговой транзакции
  • www.mql5.com
Например, при отсылке рыночного ордера на покупку он обрабатывается, для счета создается соответствующий ордер на покупку, происходит исполнение ордера, его удаление из списка открытых, добавление в историю ордеров, далее добавляется соответствующая сделка в историю и создается новая позиция. Все эти действия являются торговыми транзакциями...
 

//+------------------------------------------------------------------+ 
//| Получает текущее количество отложенных ордеров с указанным ORDER_MAGIC      | 
//+------------------------------------------------------------------+ 
int GetOrdersTotalByMagic(long const magic_number) 
  { 
   ulong order_ticket; 
   int total=0; 
//--- пройдем по всем отложенным ордерам 
   for(int i=0;i<OrdersTotal();i++) 
      if((order_ticket=OrderGetTicket(i))>0) 
         if(magic_number==OrderGetInteger(ORDER_MAGIC)) total++; 
//--- 
   return(total); 
  }
Hello fellow programmers. Please help me to modify this function. The function from MQL5 Reference above. How to make it calculate the number of all open positions for a given Magic?
 
Kolya32:
Hello fellow programmers. Please help me to modify this function. I've already introduced this function in MQL5 Reference. What should I do to make it calculate the number of open positions for Magic?
We should at least change Orders*** to Positions*** and then check.
 
Alexey Viktorov:
At least change Orders*** to Positions*** everywhere and then check.

It seems to be working)) We can add this function to MQL5)) Although it's probably too early, I will test it)

//+------------------------------------------------------------------+ 
//| Получает текущее количество открытых позиций с указанным ORDER_MAGIC      | 
//+------------------------------------------------------------------+ 

int GetPositionsTotalByMagic(long const magic_number)  
  { 
   ulong position_ticket; 
   int total=0; 
//--- пройдем по всем открытым позициям 
   for(int i=0;i<PositionsTotal();i++) 
      if((position_ticket=PositionGetTicket(i))>0) 
         if(magic_number==PositionGetInteger(POSITION_MAGIC)) total++; 
//--- 
   return(total); 
  } 
 
Kolya32:

It seems to be working)) You can add this function to the MQL5 handbook)) Although it's probably too early, I'm still testing)

If you add every little thing like this to the handbook, the handbook will turn into a dumping ground. Sorry, it's not about the quality of the changes you made.
 
mikhail_shmakov:
People help. I'm trying to get the price at which a trade opened. At first I used CTrade, but trade.resultprice was equal to zero. I started to trade through MqlTrade and result.price was showing good results in Strategy Tester, but it was equal to zero in Live. I tried it with OnTradeTransaction, but it is too slow in live trading and shows zero when auto-trading is stopped. What are some ways to get the price at which the buy trade went through?

OrderSend sends an order. Then we have to wait for its execution and for the corresponding trade to appear in the history.

In order to do all this correctly, one must either be good at OnTradeTransaction or write rather heavy code for OrderSend once.

In the second case, CTrade will also work as desired.

 

What is the analogue for Digits that would return the number of decimal places after the decimal point that determines the accuracy of the price measurement of the selected chart symbol?

 
Aleksey Vyazmikin:

What is the analogue for Digits that would return the number of decimal places after the decimal point that determines the accuracy of the price measurement of the selected chart symbol?

SymbolInfoInteger("XXXYYY",SYMBOL_DIGITS);
 
Artyom Trishkin:

Thank you!

Reason: