GRat Order Exchange – library MetaTrader 5

The attached .mqh files are designed to export orders and trades from one EA and import them into another one (copying trades), which can be run both in the same terminal and in another terminal on the same computer. It is also possible to exchange trades between different symbols, for example, copy trades of an instrument of  spot market on the corresponding futures or any other instrument that correlates with it.

Export

You need to include the GRat_OrderExport.mqh file to your exporting EA with the following command:

 #include <GRat_OrderExport.mqh>

This must be done in the input parameters area exactly in the place where the export parameters should be:

  • Common  true – export to another terminal on the same computer; false – export to the same terminal.
  • KeepSeconds – how many seconds of exported trades are available for import. 0 – unlimited.

Then, in the EA code, wherever orders are executed by the OrderSend() function (or methods of the CTrade class), you need to add a call to the ExportOrder() function, for example:

 MqlTradeRequest request = {};
// Market sell
request.action = TRADE_ACTION_DEAL;
request.symbol = Symbol();
request.volume = 0.2;
request.type = ORDER_TYPE_SELL;
request.price = SymbolInfoDouble (Symbol(), SYMBOL_BID);
request.magic = 20000;
OrderExport(request);

    The MqlTradeRequest structure can be used the same as for the OrderSend() function, but you should fill additional MqlTradeRequest fields for actions 

    TRADE_ACTION_DEAL (for closing positions),  TRADE_ACTION_SLTP, TRADE_ACTION_REMOVE and TRADE_ACTION_MODIFY:

    • type (to modify positions it should be ORDER_TYPE_BUY – for a long position and ORDER_TYPE_SELL – for a short one)
    • volume
    • price (only for pending orders)
    • stoplimit (only for Stop Limit orders)
    • sl
    • tp
    • position (positive value, only for closing positions)
    Alternative:  Exp_AroonHornSign - EA MetaTrader 5

    An example implementation of an exporting EA is GRat_OrderExport .

    Import

    You need to include the GRat_OrderImport.mqh file to your importing EA with the following command:

     #include <GRat_OrderImport.mqh>

    You need to do this in the input parameters area exactly in the place where the import parameters should be:

    • Common – true – import from another terminal on the same computer; false – import from the same terminal.
    • VolumeFactor – multiplier for the volume. It can be used to change the risk of trading, or in the case when the lots of instruments differ, for example, when copying trades from spot to futures and vice versa.
    • PriceFactor  multiplier for the price. It can be used when the prices of correlated instruments differ, for example, when copying trades from spot to futures and vice versa.
    • DeleteAfter  true – delete all trades from the exchange file immediately after import. For the case when trades are imported into only one EA. For several EAs set to false .

    Then, to import trades, you need to call the ImportOrder() function. Call example:

    MqlTradeRequest aReq[];
    OrderImport(aReq, Magic, Symbol());

      As a result, the aReq array contains all the latest unprocessed trades with the Magic number and the symbol of the current chart. In the last parameter, specify a different symbol if it differs in imported trades.

      Next, in the cycle, you need to execute imporded trades on the desired symbol, adjusting the order parameters if necessary:

      Alternative:  StochasticMomentum - indicator MetaTrader 5
       for ( int i = 0 ; i < ArraySize (aReq); i++)
      {
              aReq[i].symbol = _Symbol ;
              aReq[i].price = NormalizeDouble (aReq[i].price, _Digits );
              aReq[i].stoplimit = NormalizeDouble (aReq[i].stoplimit, _Digits );
              aReq[i].sl = NormalizeDouble (aReq[i].sl, _Digits );
              aReq[i].tp = NormalizeDouble (aReq[i].tp, _Digits );
               if (aReq[i].action == TRADE_ACTION_DEAL )
              {
                      ...
              }
              ...
      }
      

      Attached EA is an example of the implementation of an importing EA that opens the imported trades on the symbol of EA’s chart.

      📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks