Questions from Beginners MQL5 MT5 MetaTrader 5 - page 762

 
Alexey Kozitsyn:
And it is not positions or deals that need to be checked, but orders, because an order comes first, then a deal, then a position.

Why only orders? Doesn't a deal have a position ID (ticket)?

 
Alexey Viktorov:

Why only orders? Doesn't a deal have a position ID (ticket)?

Yes, you are right. Both orders and deals have a position identifier. However, in the general case we should know that the position is assigned an identifier of the first order that initiated the deal. Accordingly, the "entity" initiating the position is the order, and not the deal. And it is orders, not deals, that should be searched for.

From the documentation:

The position identifier is a unique number that is assigned to each newly opened position and does not change throughout its lifetime. It corresponds to the ticket of the order with which the position was opened.

The position identifier is specified in each order (ORDER_POSITION_ID) and each trade (DEAL_POSITION_ID) which opened, changed or closed it. Use this property to search for orders and trades related to the position.

 
Alexey Kozitsyn:

Yes, you are right. Both orders and trades have a position identifier. However, in general, you should know that the position is assigned the identifier of the first order that initiated the trade. Accordingly, the "entity" initiating the position is the order, and not the deal. And it is orders, not deals, that should be searched for.

From the documentation:

I'm not going to argue, that's exactly what the documentation says. But elsewhere in the same documentation it says"As a rule..." which means there may be inconsistencies. And this is the "as a rule" I once encountered...

position

The position ticket. It should be filled in when a position is changed and closed, for its unique identification. As a rule, it corresponds to the ticket of the order, as a result of which the position was opened.

I do not remember, and it is unlikely that I understood it, read it, made corrections and forgot. I have become more attentive to it since then.
 
Vitaly Muzichenko:
Please advise how to retrieve the ticket that was used for the trade. On the screenshot it is "63214735".

Hereyou are looking for the number of the Position, in the list of Deals, or else it's wrong.

Here is a script that demonstrates what HistorySelectByPosition gives

//--- input parameters
input int      Position;

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{ ulong Ticket;
  if ( HistorySelectByPosition(Position) )
  { Print("Position = "+IntegerToString(Position));
    int o=HistoryOrdersTotal()-1;
    while ( o>=0 )
    { Ticket=HistoryOrderGetTicket(o);
      Print("Order "+IntegerToString(o)+" = "+IntegerToString(Ticket));
      o--;
    }
    int d=HistoryDealsTotal()-1;
    while ( d>=0 )
    { Ticket=HistoryDealGetTicket(d);
      Print("Deal "+IntegerToString(d)+" = "+IntegerToString(Ticket));
      d--;
    }
  }
}

And this is how the result looks like :

Position = 65328142
Order 1 = 65347809
Order 0 = 65328142
Deal 1 = 4070777
Deal 0 = 4051254

0 = Open Position

1 = Close Position

If you don't get it = you didn't enter a Ticket Position

The same result can be obtained if we start with the Deal Ticket:

For the HistoryDealGetInteger() function

ENUM_DEAL_PROPERTY_INTEGER

Identifier

Description

Type

DEAL_TICKET

Deal ticket. Unique number to be assigned to each trade

long

DEAL_ORDER

Order, on the basis of which the trade was executed

long

DEAL_TIME

Deal execution time

datetime

DEAL_TIME_MSC

Deal execution time in milliseconds from 01.01.1970

long

DEAL_TYPE

Deal type

ENUM_DEAL_TYPE

DEAL_ENTRY

Deal direction - market entry, market exit or reversal

ENUM_DEAL_ENTRY

DEAL_MAGIC

Magic number for the deal (see ORDER_MAGIC)

long

DEAL_REASON

Reason or source of the deal

ENUM_DEAL_REASON

DEAL_POSITION_ID

The identifier of theposition in the opening, modification or closing of which this trade was involved. Each position has a unique identifier, which is assigned to all deals made on the instrument during the life time of the position.

long

Документация по MQL5: Стандартные константы, перечисления и структуры / Торговые константы / Свойства сделок
Документация по MQL5: Стандартные константы, перечисления и структуры / Торговые константы / Свойства сделок
  • www.mql5.com
Стандартные константы, перечисления и структуры / Торговые константы / Свойства сделок - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Ivan Ivanov:

Thank you!

I needed this:HistoryOrderGetTicket

 
Vitaly Muzichenko:

Thank you!

I needed this:HistoryOrderGetTicket

The order can be 1.0 lot. But it may be partially executed, for example, lot 0.9 - the remainder will be rejected (depends on the mode of execution). Then the trade will be lot 0.9. And when you start searching for lots on the order, there will be inconsistencies.

 

Please help - I need some sane code to get the OHLC environment value - the existing ones are very slow

Option 1

//-------------------------------------------------------------------
//==MQL4toMQL5
//+------------------------------------------------------------------+ 
//| Получим Open для заданного номера бара                           | 
//+------------------------------------------------------------------+ 
double Open(int index)
  {
   double open=0;
   ArraySetAsSeries(Open,true);
   int copied=CopyOpen(Symbol(),0,0,Bars(Symbol(),0),Open);
   if(copied>0 && index<copied) open=Open[index];
   return(open);
  }
//+------------------------------------------------------------------+ 
//| Получим Low для заданного номера бара                            | 
//+------------------------------------------------------------------+ 
double Low(int index)
  {
   double low=0;
   ArraySetAsSeries(Low,true);
   int copied=CopyLow(Symbol(),0,0,Bars(Symbol(),0),Low);
   if(copied>0 && index<copied) low=Low[index];
   return(low);
  }
//+------------------------------------------------------------------+ 
//| Получим High для заданного номера бара                           | 
//+------------------------------------------------------------------+ 
double High(int index)
  {
   double high=0;
   ArraySetAsSeries(High,true);
   int copied=CopyHigh(Symbol(),0,0,Bars(Symbol(),0),High);
   if(copied>0 && index<copied) high=High[index];
   return(high);
  }
//+------------------------------------------------------------------+ 
//| Получим Close для заданного номера бара                           | 
//+------------------------------------------------------------------+ 
double Close(int index)
  {
   double close=0;
   ArraySetAsSeries(Close,true);
//   int copied=CopyHigh(Symbol(),0,0,Bars(Symbol(),0),Close);
   int copied=CopyClose(Symbol(),0,0,Bars(Symbol(),0),Close);
   if(copied>0 && index<copied) close=Close[index];
   return(close);
  }
//+------------------------------------------------------------------+ 
//| Получим IOpen для заданного номера бара                           | 
//+------------------------------------------------------------------+ 
double iOpen(string symbol,ENUM_TIMEFRAMES timeframe,int index)
  {
   double open=0;
   ArraySetAsSeries(OpenI,true);
   int copied=CopyOpen(symbol,timeframe,0,Bars(symbol,timeframe),OpenI);
   if(copied>0 && index<copied) open=OpenI[index];
   return(open);
  }
//+------------------------------------------------------------------+ 
//| Получим iLow для заданного номера бара                            | 
//+------------------------------------------------------------------+ 
double iLow(string symbol,ENUM_TIMEFRAMES timeframe,int index)
  {
   double low=0;
   ArraySetAsSeries(LowI,true);
   int copied=CopyLow(symbol,timeframe,0,Bars(symbol,timeframe),LowI);
   if(copied>0 && index<copied) low=LowI[index];
   return(low);
  }
//+------------------------------------------------------------------+ 
//| Получим iHigh для заданного номера бара                           | 
//+------------------------------------------------------------------+ 
double iHigh(string symbol,ENUM_TIMEFRAMES timeframe,int index)
  {
   double high=0;
   ArraySetAsSeries(HighI,true);
   int copied=CopyHigh(symbol,timeframe,0,Bars(symbol,timeframe),HighI);
   if(copied>0 && index<copied) high=HighI[index];
   return(high);
  }
//+------------------------------------------------------------------+ 
//| Получим iClose для заданного номера бара                           | 
//+------------------------------------------------------------------+ 
double iClose(string symbol,ENUM_TIMEFRAMES timeframe,int index)
  {
   double close=0;
   ArraySetAsSeries(CloseI,true);
   int copied=CopyClose(symbol,timeframe,0,Bars(symbol,timeframe),CloseI);
   if(copied>0 && index<copied) close=CloseI[index];
   return(close);
  }


Option 2

//-------------------------------------------------------------------
//==MQL4toMQL5
//+------------------------------------------------------------------+ 
//| Получим Open для заданного номера бара                           | 
//+------------------------------------------------------------------+ 
double Open(int index)
  {
   double open=0;
   int copied=CopyOpen(Symbol(),0,index,1,Open);
   if(copied>0) open=Open[0];
   return(open);
  }
//+------------------------------------------------------------------+ 
//| Получим Low для заданного номера бара                            | 
//+------------------------------------------------------------------+ 
double Low(int index)
  {
   double low=0;
   int copied=CopyLow(Symbol(),0,index,1,Low);
   if(copied>0) low=Low[0];
   return(low);
  }
//+------------------------------------------------------------------+ 
//| Получим High для заданного номера бара                           | 
//+------------------------------------------------------------------+ 
double High(int index)
  {
   double high=0;
   int copied=CopyHigh(Symbol(),0,index,1,High);
   if(copied>0) high=High[0];
   return(high);
  }
//+------------------------------------------------------------------+ 
//| Получим Close для заданного номера бара                           | 
//+------------------------------------------------------------------+ 
double Close(int index)
  {
   double close=0;
   int copied=CopyClose(Symbol(),0,index,1,Close);
   if(copied>0) close=Close[0];
   return(close);
  }


  
//+------------------------------------------------------------------+ 
//| Получим IOpen для заданного номера бара                           | 
//+------------------------------------------------------------------+ 
double iOpen(string symbol,ENUM_TIMEFRAMES timeframe,int index)
  {
   double open=0;
   int copied=CopyOpen(symbol,timeframe,index,1,OpenI);
   if(copied>0) open=OpenI[0];
   return(open);
  }
//+------------------------------------------------------------------+ 
//| Получим iLow для заданного номера бара                            | 
//+------------------------------------------------------------------+ 
double iLow(string symbol,ENUM_TIMEFRAMES timeframe,int index)
  {
   double low=0;
   int copied=CopyLow(symbol,timeframe,index,1,LowI);
   if(copied>0) low=LowI[0];
   return(low);
  }
//+------------------------------------------------------------------+ 
//| Получим iHigh для заданного номера бара                           | 
//+------------------------------------------------------------------+ 
double iHigh(string symbol,ENUM_TIMEFRAMES timeframe,int index)
  {
   double high=0;
   int copied=CopyHigh(symbol,timeframe,index,1,HighI);
   if(copied>0) high=HighI[0];
   return(high);
  }
//+------------------------------------------------------------------+ 
//| Получим iClose для заданного номера бара                           | 
//+------------------------------------------------------------------+ 
double iClose(string symbol,ENUM_TIMEFRAMES timeframe,int index)
  {
   double close=0;
   int copied=CopyClose(symbol,timeframe,index,1,CloseI);
   if(copied>0) close=CloseI[0];
   return(close);
  }
 
Aleksey Vyazmikin:

Please help - I need some sane code to get the OHLC environment value - the existing ones are very slow

Option 1


Option 2

Post a snippet of code to show how you apply it.
 
Vitaly Muzichenko:
Post a piece of code how you apply them

I don't know which piece is slowing down - I apply them in different ways, including in loops...


here is an example


            for(int X=1;X<DonchianBarStart+1;X++)
              {
               PricePeresek=Donchianf(0,X+1);
               if((Low(X)>PricePeresek && High(X)>PricePeresek) || 
                  (Low(X)<PricePeresek && High(X)<PricePeresek))
                  calcBarPeresek++;
               else break;
              }
 
Aleksey Vyazmikin:

I don't know which piece is slowing down - I apply it in different ways, including in loops...

It seems so in loops, and you can dispense with the function

            double LOW[1],HIGH[1];
             for(int X=1;X<DonchianBarStart+1;X++)
              {
               PricePeresek=Donchianf(0,X+1);
               CopyLow(Symbol(),PERIOD_CURRENT,X,1,LOW);
               CopyHigh(Symbol(),PERIOD_CURRENT,X,1,HIGH);
               if((LOW[0]>PricePeresek && HIGH[0]>PricePeresek) || 
                  (LOW[0]<PricePeresek && HIGH[0]<PricePeresek))
                  calcBarPeresek++;
               else break;
              }
Reason: