Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

Experts

Opposite trade - expert for MetaTrader 5

| English Русский 中文 Deutsch 日本語 Português

Views:
215
Rating:
votes: 16
Published:
2017.11.03 12:25

Opening a position opposite to the closed one, with the same volume. It works for any symbol and any magic number.

For example, we have an open AUDUSD BUY 0.01 position. Once this position is closed (e.g. we close it manually) the Opposite trade Expert Advisor will immediately open a new AUDUSD positions, this now a SELL position.

The entire code is contained in the OnTradeTransaction function:

//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value 
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      long     deal_type         =-1;
      long     deal_entry        =-1;
      double   deal_volume       =0.0;
      string   deal_symbol       ="";
      if(HistoryDealSelect(trans.deal))
        {
         deal_type         =HistoryDealGetInteger(trans.deal,DEAL_TYPE);
         deal_entry        =HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         deal_volume       =HistoryDealGetDouble(trans.deal,DEAL_VOLUME);
         deal_symbol       =HistoryDealGetString(trans.deal,DEAL_SYMBOL);
        }
      else
         return;
      if(deal_entry==DEAL_ENTRY_OUT)
        {
         switch((int)deal_type)
           {
            case  DEAL_TYPE_BUY:
               m_trade.Buy(deal_volume,deal_symbol);
               break;
            case  DEAL_TYPE_SELL:
               m_trade.Sell(deal_volume,deal_symbol);
               break;
            default:
               break;
           }
        }
     }
  }

Here we wait for the position closing deal (DEAL_ENTRY_OUT). Once this deal appears, we check the deal position (if we close a BUY, this will be a Sell deal, and vice versa) and we open a new position.

Translated from Russian by MetaQuotes Software Corp.
Original code: https://www.mql5.com/ru/code/18904

up3x1 Investor up3x1 Investor

Candlestick size analysis. The idea of the trading system: candlestick parameters matter after news releases.

New Random New Random

Trading based on a random number generator or in one of the following sequences: BUY - SELL - BUY or SELL - BUY - SELL.

HTML file converter for the economic calendar HTML file converter for the economic calendar

HTML file converter for the economic calendar. The script parses a page downloaded from http://www.investing.com/economic-calendar, and forms a CSV file with the list of news.

Psychological Psychological

A classic oscillator imported from the FXAccuCharts platform.