Features of the mql5 language, subtleties and tricks - page 11

 
Leo59:
Thank you!
Since "Order is a trade order", only pending orders can be classified as "active orders", and "Positions - buy and sell" are not orders for a trade. Or am I missing something?

An order is translated into Russian as an order. You give an order (order), it can be any of 8.

1, 2, 3, 4 -buy-stop, sell-stop, buy-limit and sell-limit: when the order is triggered, a position appears.

5, 6 - buy or sell - these are not orders

7, 8 - Stop Loss and Take Profit - the orders, the client terminal's orders to exit the market.

Cases 1, 2, 3, 4 - pending orders

5, 6 - to capture the market), i.e. to take a certain position in the market, it is just like in the war, everyone has its own position at the front where they were sent by their commanders

 
Leo59:
Thank you!
Since "Order is an order to conduct a trade operation", only pending orders can be classified as "active orders", and "Positions - buy and sell" are not an order to conduct a trade operation. Or am I "missing something"?
You can also look at the link in the first section "Trading Terms".
 
Vitaly Muzichenko:

For example: i think i am buying or selling. 5, 6 - buy or sell - these are not orders any more.

The code below shows that these are valid orders.

Forum on trading, automated trading systems and trading strategies testing

Peculiarities of mql5 language, tips and tricks

fxsaber, 2017.02.25 16:30

// Триггер срабатывания SL/TP - работает только на демо/реале (не в тестере).
void OnTradeTransaction ( const MqlTradeTransaction &Trans, const MqlTradeRequest &Request, const MqlTradeResult &Result )
{
  if ((Trans.type == TRADE_TRANSACTION_ORDER_ADD) &&
       PositionSelectByTicket(Trans.position) && OrderSelect(Trans.order) &&
       (PositionGetInteger(POSITION_TYPE) == 1 - OrderGetInteger(ORDER_TYPE)))
  {
    const double Price = OrderGetDouble(ORDER_PRICE_OPEN);
    
    if (Price == PositionGetDouble(POSITION_TP))
      Print("Position #" + (string)Trans.position + " - triggered TP.");    
    else if (Price == PositionGetDouble(POSITION_SL))
      Print("Position #" + (string)Trans.position + " - triggered SL.");    
  }
}
 
fxsaber:
Active orders - trade orders, which are pending execution or cancellation, except for TP/SL and MarginCall orders. Active orders can be BUY and SELL.
Thank you!
As I understand it, the concept of an "active order" includes BUY and/or SELL positions as well as pending orders set (accepted by a broker).
If I have: Long and Short positions opened, and Buy Limit and Sell Stop set, then OrdersTotal() will return value =4. Right?
 
Leo59:
Thank you!
As I understand it, the concept of "active order" includes BUY and/or SELL positions, as well as pending orders set (accepted by the broker).
If I have: Long and Short positions open, and Buy Limit and Sell Stop set, then OrdersTotal() will return value =4. Right?
No.OrdersTotal() will return 2 (those are Buy Limit and Sell Stop pending orders ) and PositionsTotal() will also return 2 (Buy and Sell positions).
 
Vladimir Karputov:
No.OrdersTotal() will return 2 (these are Buy Limit and Sell Stop pending orders ) and PositionsTotal() will also return 2 (Buy and Sell positions).
)) This is already cool!!!! What does the function OrdersTotal() return?
 
Leo59:
)) Now that's cool!!!! What does the function OrdersTotal() return?
We can use the following terminology: position - something that has already been bought/sold or a pending order has triggered, and an order - a pending order to open a position if the price reaches a certain level.
 
Leo59:
)) This is already cool!!!! What does the OrdersTotal() function return?

Read the reference already :).

Study it:

//+------------------------------------------------------------------+
//|                                    OrdersTotalPositionsTotal.mq5 |
//|                              Copyright © 2017, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2017, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.00"
//---
#include <Trade\PositionInfo.mqh>
#include <Trade\OrderInfo.mqh>
CPositionInfo  m_position;                   // trade position object
COrderInfo     m_order;                      // pending orders object
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   int total=0;
   for(int i=PositionsTotal()-1;i>=0;i--) // returns the number of open positions
      if(m_position.SelectByIndex(i))     // selects the position by index for further access to its properties
        {
         //if(m_position.Symbol()==m_symbol.Name() && m_position.Magic()==m_magic)
         Print("position ",total,": ",m_position.Symbol()," ",EnumToString(m_position.PositionType()),
               " ",DoubleToString(m_position.Volume(),2)," ",DoubleToString(m_position.PriceOpen(),8));
         total++;
        }
   total=0;
   for(int i=OrdersTotal()-1;i>=0;i--) // returns the number of current orders
      if(m_order.SelectByIndex(i))     // selects the pending order by index for further access to its properties
        {
         //if(m_order.Symbol()==m_symbol.Name() && m_order.Magic()==m_magic)
         Print("pending order ",total,": ",m_order.Symbol()," ",EnumToString(m_order.OrderType()),
               " ",DoubleToString(m_order.VolumeInitial(),2)," ",DoubleToString(m_order.PriceOpen(),8));
         total++;
        }
  }
//+------------------------------------------------------------------+

Here is the status of the "Trade" tab:

commerce

As you can see there are two open positions (EURUSD buy 0.03 and USDCAD buy 0.02) and there are three LOCAL ORDERS (USDJPY buy limit, USDJPY sell limit and EURUSD buy limit),

and printout of the script:

2017.02.28 20:14:43.804 OrdersTotalPositionsTotal (EURUSD,H1)   position 0: USDCAD POSITION_TYPE_BUY 0.02 1.32797000
2017.02.28 20:14:43.804 OrdersTotalPositionsTotal (EURUSD,H1)   position 1: EURUSD POSITION_TYPE_BUY 0.03 1.06088000
2017.02.28 20:14:43.804 OrdersTotalPositionsTotal (EURUSD,H1)   pending order 0: EURUSD ORDER_TYPE_BUY_LIMIT 0.03 1.05879000
2017.02.28 20:14:43.804 OrdersTotalPositionsTotal (EURUSD,H1)   pending order 1: USDJPY ORDER_TYPE_SELL_LIMIT 0.01 112.71100000
2017.02.28 20:14:43.804 OrdersTotalPositionsTotal (EURUSD,H1)   pending order 2: USDJPY ORDER_TYPE_BUY_LIMIT 0.01 111.74500000
 
Maxim Khrolenko:
We can adopt the following terminology: a position is something that has already been bought/sold or manually or a pending order has triggered, and an order is a pending order to open a position if the price reaches a specified level.
Everything has been accepted a long time ago, and before writing documentation, it would probably be worthwhile to read Sergey Kovalev's tutorial (it is built into MQL4):


This is how a block can be constructed in which market and pending orders are analyzed:

   for (int i=1; i<=OrdersTotal(); i++)       //Цикл по всем ордерам,..
     {                                        //отражённым в терминале
      if(OrderSelect(i-1,SELECT_BY_POS)==true)//Если есть следующий
        {                                    
         // Здесь должен выполняться ..
         // ..анализ характеристик ордеров
        }
     }                                        //Конец тела цикла

The loop statement header contains the initial value i=1, and the condition for the loop termination is the expression i<=OrdersTotal(). Function OrdersTotal() returns the total amount of market and pending orders, i.e. those orders that are reflected in the Terminal on the Trade tab. Therefore, the number of iterations in the loop will be the number of orders present in the trade.

 
Leo59:
This is the code of MT4. Everything is already taken care of, and before writing the documentation, it would probably be worthwhile to read Sergei Kovalev's tutorial (it is built into MQL4):


This is how a block can be constructed in which market and pending orders are analyzed:

The loop statement header contains the initial value i=1, and the condition for the loop termination is the expression i<=OrdersTotal(). Function OrdersTotal() returns the total amount of market and pending orders, i.e. those orders that are reflected in the Terminal on the Trade tab. Therefore, there will be as many iterations in the loop as there are orders in the trade.

There's no need to churn)

This is MT4 code and there is no division of orders and positions - everything is mixed together in it

Reason: