Reverse_Trades_For_all_Trades_and_Symbols_V1

Author: Copyright 2024, MetaQuotes Ltd.
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself It automatically opens orders when conditions are reached
1 Views
0 Downloads
0 Favorites
Reverse_Trades_For_all_Trades_and_Symbols_V1
//+------------------------------------------------------------------+
//|                     ReverseTradesForAllSymbols_Script.mq4        |
//|                        Copyright 2024, Your Company Name         |
//|                          http://www.yourwebsite.com              |
//+------------------------------------------------------------------+
#property copyright     "Copyright 2024, MetaQuotes Ltd."
#property link          "https://www.mql5.com"
#property version       "1.01"
#property description   "persinaru@gmail.com"
#property description   "IP 2024 - free open source"
#property description   "Reverse All Trades"
#property description   ""
#property description   "WARNING: Use this software at your own risk."
#property description   "The creator of this script cannot be held responsible for any damage or loss."
#property description   ""
#property strict
#property show_inputs
#property script_show_inputs

// Input parameters
extern bool reverseAllTrades = false;            // Reverse All Trades

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
    if (reverseAllTrades)
    {
    ReverseAllTrades();
    }
}

//+------------------------------------------------------------------+
//| Reverse all trades function                                      |
//+------------------------------------------------------------------+
void ReverseAllTrades()
{
    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
            int orderType = OrderType();
            double openPrice = OrderOpenPrice();
            double lotSize = OrderLots();
            string symbol = OrderSymbol();

            // Close existing order
            bool closeResult = OrderClose(OrderTicket(), lotSize, Bid, 5, clrNONE);
            if (closeResult)
            {
                Print("Closed order: ", OrderTicket(), " for symbol: ", symbol, " at price: ", Bid);
                
                // Open opposite order
                int oppositeType = (orderType == OP_BUY) ? OP_SELL : OP_BUY;
                double oppositePrice = (orderType == OP_BUY) ? Ask : Bid;
                
                int ticket = OrderSend(symbol, oppositeType, lotSize, oppositePrice, 5, 0, 0, "Reverse Trade", 0, 0, clrNONE);
                
                if (ticket > 0)
                {
                    Print("Opened opposite order: ", ticket, " for symbol: ", symbol, " at price: ", oppositePrice);
                }
                else
                {
                    Print("Failed to open opposite order for symbol: ", symbol);
                }
            }
            else
            {
                Print("Failed to close order: ", OrderTicket(), " for symbol: ", symbol);
            }
            // Pause for a short time to avoid rapid execution
            Sleep(1000);
        }
    }
}

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---