I don't know if you can use OnTradeTransaction for tracking the last trade type. Create global boolean flags, and change the state of the flags at the time of making the trade for whether it's a pending order or market order.
For when the trade was successfully executed:
if(trade.Buy(lotSize, symbol, entryPrice, slPrice, tpPrice)) { BuyMarketOrder = true; SellMarketOrder = false; BuyPendingOrder = false; SellPendingOrder = false; }
OnTradeTransaction is mostly about tracking historical deals, and trade events. A closed sell position is understood as a buy deal on DEAL_OUT, because a sell position closes with a buy deal. You only have sell or buy on historical deals, we don't talk about deals when talking about pending orders, because pending orders turn into buy or sell positions when activated.
I've never used ENUM_ORDER_TYPE in this event handler, but maybe there is a way to track the order type in it using this, I'm not sure.
TRADE_TRANSACTION_ORDER_ADD maybe because that kind of order is executed immediately, so market orders aren't reliable to detect with that option, but pending orders seem to be detected without any issues as they stay in the order book.
So if you really want to make this work reliably in OnTradeTransaction, you will need to do this:
bool BuyMarketOrder = false; bool SellMarketOrder = false; bool BuyPendingOrder = false; bool SellPendingOrder = false; void OnTradeTransaction(const MqlTradeTransaction &trans, const MqlTradeRequest &request, const MqlTradeResult &result) { ENUM_TRADE_TRANSACTION_TYPE type = trans.type; if(type == TRADE_TRANSACTION_ORDER_ADD) { if(OrderSelect(trans.order)) { ENUM_ORDER_TYPE orderType = (ENUM_ORDER_TYPE)OrderGetInteger(ORDER_TYPE); if(orderType == ORDER_TYPE_BUY_LIMIT || orderType == ORDER_TYPE_BUY_STOP) { BuyPendingOrder = true; BuyMarketOrder = false; SellMarketOrder = false; SellPendingOrder = false; Print("Buy Pending Order added. Ticket: ", trans.order, ", Type: ", EnumToString(orderType)); } else if(orderType == ORDER_TYPE_SELL_LIMIT || orderType == ORDER_TYPE_SELL_STOP) { SellPendingOrder = true; BuyMarketOrder = false; SellMarketOrder = false; BuyPendingOrder = false; Print("Sell Pending Order added. Ticket: ", trans.order, ", Type: ", EnumToString(orderType)); } } } else if(type == TRADE_TRANSACTION_REQUEST && result.retcode == TRADE_RETCODE_DONE) { if(request.action == TRADE_ACTION_DEAL && request.position == 0) { if(request.type == ORDER_TYPE_BUY) { BuyMarketOrder = true; SellMarketOrder = false; BuyPendingOrder = false; SellPendingOrder = false; Print("Buy Market Order requested (manual/external). Ticket: ", result.order); } else if(request.type == ORDER_TYPE_SELL) { SellMarketOrder = true; BuyMarketOrder = false; BuyPendingOrder = false; SellPendingOrder = false; Print("Sell Market Order requested (manual/external). Ticket: ", result.order); } } } }
that while market orders as well as pending orders are included in ENUM_ORDER_TYPE
I did it on end of tester.
I looped through HistoryDealsTotal,
I found the ENUM_DEAL_ENTRY == in (i kept the variables i needed like coment ,open price ,open time etc )
i then looped through HistoryOrdersTotal
I matched the ORDER_POSITION_ID with DEAL_POSITION_ID
AND IF
ENUM_ORDER_TYPE order_type = (ENUM_ORDER_TYPE)HistoryOrderGetInteger(order_ticket, ORDER_TYPE); if(order_type == ORDER_TYPE_BUY_STOP || order_type == ORDER_TYPE_BUY_LIMIT || order_type == ORDER_TYPE_SELL_STOP || order_type == ORDER_TYPE_SELL_LIMIT) { Print(deal_symbol," Pending ... skiping"); ended=true; }
I Then looped again through a fresh loop of HistoryDealsTotal
I found the DEAL_ENTRY == out (i kept the variables i needed like close price ,close time etc )
I matched the DEAL_POSITION_ID from out with DEAL_POSITION_ID from in
and i kept my trade log :)
I then execute the trade log on Python and now i have my visual chart

I did it on end of tester.
I looped through HistoryDealsTotal,
I found the ENUM_DEAL_ENTRY == in (i kept the variables i needed like coment ,open price ,open time etc )
i then looped through HistoryOrdersTotal
I matched the ORDER_POSITION_ID with DEAL_POSITION_ID
AND IF
I Then looped again through a fresh loop of HistoryDealsTotal
I found the DEAL_ENTRY == out (i kept the variables i needed like close price ,close time etc )
I matched the DEAL_POSITION_ID from out with DEAL_POSITION_ID from in
and i kept my trade log :)
I then execute the trade log on Python and now i have my visual chart
That will work fine for pending orders, but tracking market orders is a bit tricky in OnTradeTransaction and there are options to use HistoryDealSelect or this way with global flags:
if(type == TRADE_TRANSACTION_REQUEST && result.retcode == TRADE_RETCODE_DONE) { if(request.action == TRADE_ACTION_DEAL && request.position == 0) { if(request.type == ORDER_TYPE_BUY) { BuyMarketOrder = true; SellMarketOrder = false; BuyPendingOrder = false; SellPendingOrder = false; Print("Buy Market Order requested (manual/external). Ticket: ", result.order); } else if(request.type == ORDER_TYPE_SELL) { SellMarketOrder = true; BuyMarketOrder = false; BuyPendingOrder = false; SellPendingOrder = false; Print("Sell Market Order requested (manual/external). Ticket: ", result.order); } } }
Note that I included
request.position == 0 in the condition, because without this it will flip the flag to false when the position is closed, and you don't want that
because remember that a buy position closes with a sell deal and a sell position closes with a buy deal
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
How can i determine if the last trade is Direct trade, (Market) or Pending trigered?