Watch how to download trading robots for free
Find us on Facebook!
Join our fan page
Interesting script?
So post a link to it -
let others appraise it
You liked the script? Try it in the MetaTrader 5 terminal
Libraries

TradeTransactions - library for MetaTrader 5

Views:
4364
Rating:
(30)
Published:
2018.10.25 18:37
\MQL5\Indicators\fxsaber\ \MQL5\Experts\fxsaber\ \MQL5\Include\fxsaber\TradeTransactions\
Resource.mqh (0.63 KB) view
String.mqh (0.45 KB) view
TradeResult.mqh (1.15 KB) view
\MQL5\Include\
TypeToBytes.mqh (20.54 KB) view
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance

Asynchronous trading orders have a huge advantage - high speed when sent in bulk. However, the spreading of such orders is hampered by the inconvenience - order result data can only be seen in OnTradeTransaction making users to build an event model of their trading strategy if they want asynchrony. This is not always easy (EAs), and sometimes even impossible (scripts).

The library solves this problem. At any time, it provides access to all transactions in the trading terminal (full data of the appropriate OnTradeTransaction) since the application launch simplifying the process of making your programs asynchronous.

Examples

The example below displays the library operation principle:

// Print out all transactions without OnTradeTransaction

#include <fxsaber\TradeTransactions\TradeTransactions.mqh> // Access to OnTradeTransaction data anywhere in the program

TRADETRANSACTIONS Transactions; // Trading transactions

const bool Init = EventSetMillisecondTimer(100);

void OnTimer()
{
  static uint Total = 0;
  const uint NewTotal = Transactions.Total(); // Number of transactions saved
  
  for (uint i = Total; i < NewTotal; i++) // Run through new transactions
  {
    MqlTradeTransaction Trans;
    MqlTradeRequest Request;
    MqlTradeResult Result;
    
    // Get all the data on the respective transaction and print them.
    Print(TimeToString(Transactions[i].Get(Trans, Request, Result)) + "\n" + 
          ToString(Trans) + ToString(Request) + ToString(Result));        
  }
  Total = NewTotal;
}

Run this EA and try performing trading orders manually. The Expert Advisor will print all the details as if it has OnTradeTransaction. Although it has not!

There can be many practical scenarios for using such functionality. For example, trading a basket of symbols. For example, if you need to quickly open several positions inside the code. If they are open, you can perform additional trading operations without leaving the On function.

Since we frequently need to wait for a result of a massive sending of asynchronous orders, the library features the appropriate ability that can be quickly assessed using such an example

// Example of massive asynchronous trade orders with waiting for a result.

#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006
#include <fxsaber\TradeTransactions\TradeTransactions.mqh> // Access to OnTradeTransaction data anywhere in the program

TRADETRANSACTIONS Transactions; // Trading transactions

// Open Amount positions as quickly as possible. Return when positions are open.
bool OpenPositions( const int Amount = 10 )
{
  uint RequestID[];
  
  for (int i = ArrayResize(RequestID, Amount) - 1; i >= 0; i--)
  {
    const string Symb = SymbolName(i, true);
    
    RequestID[i] = OrderSendAsync(Symb, OP_BUY, 1, SymbolInfoDouble(Symb, SYMBOL_ASK), 100, 0, 0); // Send asynchronous order
  }
  
  return(Transactions.Waiting(RequestID)); // Wait for a server response to all asynchronous orders
}

// Close everything as quickly as possible. Return when the action is confirmed.
bool CloseAll()
{
  uint RequestID[];
  
  for (int i = ArrayResize(RequestID, OrdersTotal()) - 1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS))
      // Send asynchronous order
      RequestID[i] = (OrderType() <= OP_SELL) ? OrderCloseAsync(OrderTicket(), OrderLots(), OrderClosePrice(), 100) : OrderDeleteAsync(OrderTicket());
  
  return(Transactions.Waiting(RequestID)); // Wait for a server response to all asynchronous orders
}

void OnStart()
{
  if (OpenPositions())
    Print(CloseAll());
}

Of course, this is a script, but it still allows working with transactions via the library. The launch shows how to quickly open and close multiple trading positions/orders.

Features

  • The code Indicators\fxsaber\TradeTransactions.mq5 should be compiled.
  • TypeToBytes library is used.
  • The library source code features (ResourceData.mqh) a universal class that allows you to conveniently save/read data to/from Resources.

Translated from Russian by MetaQuotes Ltd.
Original code: https://www.mql5.com/ru/code/22166

Skyscraper Skyscraper

A trend indicator of NRTR type with an additional middle line

XRSI_Candle_Vol_Zer_Alerts XRSI_Candle_Vol_Zer_Alerts

XRSI_Candle_Vol_Zer indicator provides alerts, sends email and push notifications at the incoming trading signals

Easy Canvas Easy Canvas

The library and iCanvas class simplify writing programs using Canvas.

Previous Candle Breakdown 3 Previous Candle Breakdown 3

"Previous Candle Breakdown" Expert Advisor.