Using OnTrade() or OnTradeTransaction() to find the last closed trade

 

I can't find anything on the internet about how to use those to run a command when a trade gets closed.

When a trade gets closed OnTrade() should find the last closed trade.

I also don't understand how i could do that from the documentation of OnTrade() and OnTradeTransaction() .

Any hints to sources where this gets explained would help me.

Thank you

 
tobqas :

I can't find anything on the internet about how to use those to run a command when a trade gets closed.

When a trade gets closed OnTrade() should find the last closed trade.

I also don't understand how i could do that from the documentation of OnTrade() and OnTradeTransaction() .

Any hints to sources where this gets explained would help me.

Thank you

Example: Alert Position Open

When a position is opened (either by an Expert Advisor or manually or when a pending order is triggered), the Expert Advisor signals this.

Can be turned on / off

  • signaling via Alert
  • signaling through file playback
  • printout to the "Experts" tab
  • screen print

Example of displaying messages:

Pending Buy Limit order triggered:

 2019.09 . 03 14 : 03 : 16.077 Alert Position Open (EURUSD,D1) deal # 420561644  buy 0.020 USDJPY at 106.152   [ # 443661270  buy limit]

Market position was set:

 2019.09 . 03 14 : 04 : 41.591 Alert Position Open (EURUSD,D1) deal # 420564269  buy 0.010 USDJPY at 106.171 


The Expert Advisor works as a "listener" in OnTradeTransaction . As soon as there is a transaction in type

  • TRADE_TRANSACTION_DEAL_ADD - adding a deal to history

- the advisor refers to the trading history and receives the necessary information from the ticket of the transaction.

 
tobqas:

I can't find anything on the internet about how to use those to run a command when a trade gets closed.

When a trade gets closed OnTrade() should find the last closed trade.

I also don't understand how i could do that from the documentation of OnTrade() and OnTradeTransaction() .

Any hints to sources where this gets explained would help me.

Thank you

Hi tobgas. When I wanna check the last deal I do this:

int deals_prev = 0; 

HistorySelect(date_from, date_until);

if (deals_prev < HistoryDealsTotal()) //Check if exists new deals
        {
                for (int deal = HistoryDealsTotal() - 1; deal >= deals_prev; deals--) //starting by the last deal
                        {
                                ulong ticket_deal = HistoryGetTicket(deal);
                                
                                if (ticket_deal > 0)
                                        {
                                                if (HistoryGetIntegre(ticket_deal, DEAL_TYPE) == ENTRY_OUT) //Check if this deal is closing/reducing position deal
                                                        {
                                                                //do your stuffs...
                                                        }
                
                                        }               
                        }
        }

deals_prev = HistoryDealsTotal(); //updating the number of deals

//This code works only if you do not starting new deals inside the OnTrade/OnTradeTransaction functions.
 
Tobias Michael Kerner:

I can't find anything on the internet about how to use those to run a command when a trade gets closed.

When a trade gets closed OnTrade() should find the last closed trade.

I also don't understand how i could do that from the documentation of OnTrade() and OnTradeTransaction() .

Any hints to sources where this gets explained would help me.

Thank you

Hi there..

Have you find a solution to your problem?

I think the answer to your question is the "MqlTradeResults &result  from OnTradeTransaction()

Every execution of an order go to this structure as result from MqlTradeRequest from OnTradeTransaction()

A simple example script using Ctrade library that encapsulate  both  MqlTradeRequest and MqlTradeResults:

//--------------------------------------------------

 #include<Trade/Trade.mqh>

 CTrade trade;

void OnStart()

{

  if(trade.Buy(lots,_Symbol,0,0,0,"Buy")){

    Alert("Succeful execution at the price: ", trade.ResultPrice()," - Position Ticket: ", trade.ResultOrder());

  }

}

Reason: