Welcome Forex EA downloads & MT4/MT5 auto-trading resources — EAs, Gold EAs, quant tools and real-world automation.
Sign In Sign Up

TradeTransactions - MetaTrader 5 Library | Trading Script Download - MT4/MT5 Resources

author EAcpu | 2 reads | 0 comments |

Asynchronous trading orders have a huge advantage - they are fast when sent in batches. However, the propagation of such orders is hampered by an inconvenience - order result data can only be seen in OnTradeTransaction, which makes users need to build event models for their trading strategies if they want to be asynchronous. This is not always easy (EA) and sometimes even impossible (scripting).

Libraries solve this problem. Since the application is started, it has access to all transactions in the trading terminal (complete data of corresponding OnTradeTransaction) at any time, thus simplifying the process of program asynchronousization.

The following example shows how the library works:

 // Print out all transactions without OnTradeTransaction

#include // Access OnTradeTransaction data trade transactions anywhere in the program ; // Trade transactions

const boolean init = eventSet millisecondsTimer ( 100 ) ; blankTimer ( )
{
Total units at rest = 0 ;
Constant unit NewTotal = Transactions.Total(); // Number of saved transactions 
for ( unit i = total; i < new total; i++) // run new transaction {
MqlTrade transaction trans;
Mql transaction request requirements;
Mql transaction results results;
 
// Get all the data for the corresponding transaction and print them. print ( String time (transaction[i].Get(transaction, request, result)) + "\n" + 
          ToString(Trans) + ToString(request) + ToString(result)); 
         }
 total = new total;
}

    Run this EA and try to execute trade orders manually. The Expert Advisor will print all details as if it had OnTradeTransaction. Not yet though!

    There may be many practical scenarios for using such functions. For example, trade a basket of instruments. For example, if you need to quickly open multiple positions within a ticker. If they are open, you can perform other trading operations without leaving the "Open" function.

    Since we often need to wait for the results of a large number of asynchronous orders being sent, this library has appropriate functionality that can be quickly evaluated using an example like this

    // Example of a large number of asynchronous trading orders waiting for results.
    
    #include // https://www.mql5.com/en/code/16006
    #include // Access OnTradeTransaction data trade transactions anywhere in the program ; // Trade transactions
    
    // Open a position as soon as possible. Return when holding a position.
    boolean open interest ( constant integer amount = 10 )
    {
    Unit request ID[];
     
    for ( integer i = array resize (request_id, amount) - 1 ; i >= 0 ; i - )
      {
    const StringSymbol = SymbolName (i, true );
     
           Request ID[i] = order sent asynchronously (symbol, OP_BUY , 1 , symbol information double (symbol, SYMBOL_ASK ), 100 , 0 , 0 ); // Send asynchronous order }
     
    Return (Transaction.Wait(RequestID)); // Wait for server response for all asynchronous orders } // Close everything as quickly as possible. Return after confirming the operation.
    boolean closeall()
    {
    Unit request ID[];
     
    for ( integer i = array resize (request_id, total_orders ()) - 1 ; i >= 0 ; i - )
    if ( orderselect (i, SELECT_BY_POS ))
    //Send asynchronous order Request ID[i] = ( Order Type () <= OP_Sell )? Order closing asynchronous( booking (), order batch (), order closing price (), 100 ): OrderDeleteAsync( booking ());
     
    Return (Transaction.Wait(RequestID)); // Wait for the server's response to all asynchronous orders } Blank on startup ()
    {
    if (OpenPosition())
    print (closeall());
    }

    Of course, this is a script, but it still allows transactions to be handled via the library. This release demonstrates how to quickly open and close multiple trading positions/orders.


    Attachment download

    📎 tradetransactions.mq5 (1.4 KB)

    📎 tradetransactions_example.mq5 (2.65 KB)

    📎 ontradetransaction.mqh (0.83 KB)

    📎 resource.mqh (0.63 KB)

    📎 resourcedata.mqh (1.41 KB)

    📎 string.mqh (0.45 KB)

    📎 traderequest.mqh (2.12 KB)

    📎 traderesult.mqh (1.15 KB)

    📎 tradetransaction.mqh (2.06 KB)

    📎 tradetransactions.mqh (2.46 KB)

    📎 typetobytes.mqh (20.54 KB)

    Source: MQL5 #22166

    Verification code Refresh