m_order.Select(trans.order) works, but m_deal.Ticket(trans.deal) does not (without first HistorySelect() - HistoryDealSelect())

 

Greets,


Hope someone can shed some light on this question. I must either be missing something, or it just is what it is.


I am using OnTradeTransaction() to deal with events. All good, but I'm trying to use the classes (such as CDealInfo, etc) to be as MQL5 idiomatic as possible.


The following works as expected:

void OnTradeTransaction(
        const MqlTradeTransaction&    trans,
        const MqlTradeRequest&        request,
        const MqlTradeResult&         result) {

        CDealInfo m_deal;

        if (trans.type == TRADE_TRANSACTION_DEAL_ADD) {
                HistorySelect(0, TimeCurrent()-1);
                if (HistoryDealSelect(trans.deal))
                        m_deal.Ticket(trans.deal); // this seems to be required, even if redundant seeming
                else {
                        Print("Unable to select history deal " + IntegerToString(trans.deal));
                        return;
                }
		// ... proceed to use m_deal.DealType() etc which works fine.
        }
}

However, I would like to know if there is a way to use m_deal.Ticket(trans.deal) for cleaner code (as with the Order sample further below) - which the documentation says is for "Select the position for further work" - but the following replacement code does NOT work:

void OnTradeTransaction(
        const MqlTradeTransaction&    trans,
        const MqlTradeRequest&        request,
        const MqlTradeResult&         result) {

        CDealInfo m_deal;

        if (trans.type == TRADE_TRANSACTION_DEAL_ADD) {
                m_deal.Ticket(trans.deal); // returns void, so we cannot check if error, but ok
                // ... proceed to use m_deal.DealType() etc which does NOT work
                                        
        }
}


When working with Orders, this is nice and clean and works:

void OnTradeTransaction(
        const MqlTradeTransaction&    trans,
        const MqlTradeRequest&        request,
        const MqlTradeResult&         result) {

        COrderInfo m_order;

        if (trans.type == TRADE_TRANSACTION_ORDER_DELETE) {
                if (trans.order_state == ORDER_STATE_EXPIRED) {
			// old code not using m_order.Select()
			//if (HistoryOrderSelect(trans.order)) {
			//	ulong magic;
			//	if (!m_order.InfoInteger(ORDER_MAGIC, magic)) {
			//		Print("Failed to get magic for order ", IntegerToString(trans.order));
			//	}
			//	else if (magic == EAMagic) expired++;
			//}
			//else {
			//	Print("Failed to select history order ", IntegerToString(trans.order));
			//}
			
			// cleaner replacement works fine:
                        if (!m_order.Select(trans.order))
                                Print("Failed to select history order ", IntegerToString(trans.order));
                        else if (m_order.Magic() == EAMagic) 
                                expired++;
                }
                                        
        }
}

And I would like to do something similar for selecting/working with deals. I may be wrong, but it doesn't seem possible.


/Edit to add: there is no m_deal.Select(), only m_deal.SelectByIndex(), even though m_deal.Ticket(ticket) *seems* to be the same as m_order.Select(order) - at least that's what the docs imply.


/Edit 2: added the commented out previous Order code which also works

Documentation on MQL5: Standard Library / Trade Classes / CDealInfo
Documentation on MQL5: Standard Library / Trade Classes / CDealInfo
  • www.mql5.com
CDealInfo is a class for easy access to the deal properties. CDealInfo class provides access to the deal properties...