issue on getting open price using HistoryOrderGetDouble(trans.position,ORDER_PRICE_OPEN) - page 2

 
ArashB :

Dear vlad,


I know what I'm talking about and it is not my mis perception on the different trades definitions. to get aligned you may see my definitions below :

  1. Position : is a trade which is already open and profit can be changed on each tick
  2. Order : is the pending orders which you may put by LIMIT and STOP
  3. Deal : is the action for opening and closing a position or trigger a pending order


the order which i mentioned is not pending order or so... over trade history if you change the view model to deals, you may see the same titles there.

It is the MT5 and MQ5 which is using this ORDER number and return these values on TradeTransaction as well.


Finally to answer your question and clear up the requirement in a sentence:

I'm looking for the OPEN_PRICE of the original Position which its SL hit at the moment.

I'll think about the code, the preliminary algorithm is as follows:

We caught a transaction - Stop Loss triggered. Find the deal that generated this transaction. Get the 'Position identifier' property from this deal. We select all trades using 'HistorySelectByPosition'. In this case, finding the very first deal is very easy.

 

Thanks again for the comment


i checked some buy i couldn't find what do you mean by position Identifier over the deal

can you please explain more or provide me with the function or variable which i can find this identifier?

 
ArashB:

Thanks again for the comment


i checked some buy i couldn't find what do you mean by position Identifier over the deal

can you please explain more or provide me with the function or variable which i can find this identifier?

Code:

Price of position opening.mq5

//+------------------------------------------------------------------+
//|                                    Price of position opening.mq5 |
//|                              Copyright © 2020, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2020, Vladimir Karputov"
#property version   "1.000"
/*
   barabashkakvn Trading engine 3.143
*/
#include <Trade\DealInfo.mqh>
//---
CDealInfo      m_deal;                       // object of CDealInfo class
//--- input parameters
input int      Input1=9;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
  }
//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      ResetLastError();
      if(HistoryDealSelect(trans.deal))
         m_deal.Ticket(trans.deal);
      else
        {
         Print(__FILE__," ",__FUNCTION__,", ERROR: ","HistoryDealSelect(",trans.deal,") error: ",GetLastError());
         return;
        }
      if(m_deal.DealType()==DEAL_TYPE_BUY || m_deal.DealType()==DEAL_TYPE_SELL)
        {
         if(m_deal.Entry()==DEAL_ENTRY_OUT)
           {
            long position_id=m_deal.PositionId();
            if(HistorySelectByPosition(position_id))
              {
               uint     total = HistoryDealsTotal();
               ulong    ticket= 0;
               //--- for all deals
               for(uint i=0; i<total; i++)
                 {
                  //--- try to get deals ticket
                  if((ticket=HistoryDealGetTicket(i))>0)
                    {
                     //--- get deals properties
                     long     lng_time=HistoryDealGetInteger(ticket,DEAL_TIME);
                     datetime dat_time=(datetime)lng_time;
                     double   price    = HistoryDealGetDouble(ticket,DEAL_PRICE);
                     Print(dat_time,", ",DoubleToString(price,8));
                    }
                 }
              }
           }
        }
     }
  }
//+------------------------------------------------------------------+
 
This template is a blank. There is no check here: "the position was closed due to the Stop Loss triggering"?
Basic Principles - Trading Operations - MetaTrader 5 Help
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
is an instruction given to a broker to buy or sell a financial instrument. There are two main types of orders: Market and Pending. In addition, there are special Take Profit and Stop Loss levels. is the commercial exchange (buying or selling) of a financial security. Buying is executed at the demand price (Ask), and Sell is performed at the...
 

thanks a lot Vlad...


i got the concept and what i should looking for.

Aas I'm working directly with OrderSend and not using CTrade, my code will be a bit different. but I will check and inform you the results.


Thanks again

 

Thanks Vlad for your support.


the issue has solved with below code easily

     HistorySelect(0,time[0]+PeriodSeconds());
     long posid=HistoryDealGetInteger(trans.deal,DEAL_POSITION_ID);
     if (HistorySelectByPosition(posid))
     {
       ulong ticket;
       int dealtot=HistoryDealsTotal();
       ticket=HistoryDealGetTicket(0);
       if (ticket>0)
       {
          sy = HistoryDealGetString(ticket,DEAL_SYMBOL);
          prc = HistoryDealGetDouble(ticket,DEAL_PRICE);
          lot = HistoryDealGetDouble(ticket,DEAL_VOLUME);
   
          magic = HistoryDealGetInteger(ticket,DEAL_MAGIC);
          otype = ENUM_ORDER_TYPE(HistoryOrderGetInteger(trans.position,ORDER_TYPE));
       }
     }
     else return false;


but still it remained unanswered why the original function cannot resolve the proper value

 
ArashB #:

Thanks Vlad for your support.


the issue has solved with below code easily


but still it remained unanswered why the original function cannot resolve the proper value

This is my question too!!!

I'm using this code for last closed positions information such as TP/SL/opened price/lot.. . 

I'm selecting history correctly (tested).

I'm selecting Order correctly(tested).

But just Loti size can be returned correctly by this way!! 

LastOrderTP = HistoryOrderGetDouble(Ticket, ORDER_TP); 
LastOrderSL = HistoryOrderGetDouble(Ticket, ORDER_SL);
LastOrderOpen = HistoryOrderGetDouble(Ticket, ORDER_PRICE_OPEN);
LastTradeLot = HistoryOrderGetDouble(Ticket, ORDER_VOLUME_INITIAL);
Reason: