Close All Market Positions (CloseAllPositions)Âļ
â ī¸ EXTREMELY DANGEROUS BULK METHOD: Closes all open market positions with optional symbol/direction filtering. NUCLEAR OPTION!
API Information:
- Extension method:
MT5Service.CloseAllPositions(...)(fromMT5ServiceExtensions) - Package: Part of
mt5_term_apilibrary - Region: [11] BULK CONVENIENCE
- Underlying calls:
OpenedOrdersAsync()+CloseByTicket()(in loop)
Method SignatureÂļ
public static async Task<int> CloseAllPositions(
this MT5Service svc,
string? symbol = null,
bool? isBuy = null,
int timeoutSec = 30,
CancellationToken ct = default)
đŊ InputÂļ
| Parameter | Type | Description |
|---|---|---|
svc |
MT5Service |
MT5Service instance (extension method) |
symbol |
string? |
Optional symbol filter. null = close ALL symbols â ī¸â ī¸â ī¸ |
isBuy |
bool? |
Optional direction filter. true = buy positions only, false = sell positions only, null = both |
timeoutSec |
int |
RPC timeout in seconds (default: 30) |
ct |
CancellationToken |
Cancellation token |
âŦī¸ OutputÂļ
| Type | Description |
|---|---|
Task<int> |
Number of market positions successfully closed |
đŦ Just the essentialsÂļ
- What it is: NUCLEAR OPTION - closes all open market positions (BUY/SELL) matching the filter criteria.
- Why you need it: Emergency exit, strategy termination, or end-of-day position cleanup.
- â ī¸â ī¸â ī¸ WARNING: NO confirmation prompt! Closes ALL positions immediately. Can realize massive losses if used incorrectly!
đ¯ PurposeÂļ
Use it for:
- đ¨ EMERGENCY STOP - Flatten all positions immediately (market crash, news event)
- End-of-day cleanup - Close all positions before market close
- Strategy termination - Exit all positions when stopping a strategy
- Symbol-specific exit - Close all positions for one symbol
- Direction-specific exit - Close all buy OR all sell positions
đ§ Under the HoodÂļ
// Step 1: Get all opened orders (both market and pending)
var opened = await svc.OpenedOrdersAsync(
BMT5_ENUM_OPENED_ORDER_SORT_TYPE.Bmt5OpenedOrderSortByOpenTimeAsc, deadline, ct);
int closed = 0;
// Step 2: Iterate through orders via reflection (proto-agnostic)
foreach (var order in EnumerateOrders(opened))
{
var typeInt = GetOrderTypeInt(order);
// Step 3: Skip pending orders (only market positions)
if (!IsMarketType(typeInt)) continue;
// Market types: 0-1 (Buy, Sell)
// Step 4: Apply symbol filter
var orderSymbol = order.Symbol;
if (symbol != null && orderSymbol != symbol) continue;
// Step 5: Apply direction filter
bool isOrderBuy = IsBuyType(typeInt);
if (isBuy.HasValue && isOrderBuy != isBuy.Value) continue;
// Step 6: Close the position at market price
var ticket = order.Ticket;
await svc.CloseByTicket(ticket, null, timeoutSec, ct);
closed++;
}
return closed;
What it improves:
- Instant liquidation - Close entire portfolio in one call
- Flexible filtering - By symbol, direction, or both
- Safe targeting - Only market positions, never pending orders
- Returns count - Know exactly how many positions were closed
đ Usage ExamplesÂļ
Example 1: Close ALL Positions (â ī¸â ī¸â ī¸ NUCLEAR OPTION)Âļ
// â ī¸â ī¸â ī¸ EXTREMELY DANGEROUS: Closes ALL positions across ALL symbols!
// USE ONLY IN EMERGENCY!
int closed = await svc.CloseAllPositions();
Console.WriteLine($"đ¨ EMERGENCY EXIT: Closed {closed} positions");
Example 2: Close All Positions for One SymbolÂļ
// Close all EURUSD positions (both buy and sell)
int closed = await svc.CloseAllPositions(symbol: "EURUSD");
Console.WriteLine($"â
Closed {closed} EURUSD positions");
Example 3: Close Only Buy PositionsÂļ
// Close all buy positions across all symbols
int closed = await svc.CloseAllPositions(isBuy: true);
Console.WriteLine($"â
Closed {closed} buy positions");
Example 4: Close Specific Symbol + DirectionÂļ
// Close only GBPUSD sell positions
int closed = await svc.CloseAllPositions(symbol: "GBPUSD", isBuy: false);
Console.WriteLine($"â
Closed {closed} GBPUSD sell positions");
Example 5: End-of-Day Full ExitÂļ
public async Task EndOfDayFullExit(MT5Service svc)
{
Console.WriteLine("â ī¸ End of day - closing all positions...");
// Close ALL market positions
int closedPositions = await svc.CloseAllPositions();
// Cancel ALL pending orders
int cancelledOrders = await svc.CancelAll();
Console.WriteLine($"â
Closed {closedPositions} positions");
Console.WriteLine($"â
Cancelled {cancelledOrders} pending orders");
Console.WriteLine("â
Account fully flat");
}
// Usage at 16:55 (before market close):
await EndOfDayFullExit(svc);
Example 6: Emergency Stop with ConfirmationÂļ
// Add DOUBLE confirmation for safety
Console.WriteLine("â ī¸â ī¸â ī¸ EMERGENCY STOP - CLOSE ALL POSITIONS â ī¸â ī¸â ī¸");
Console.Write("Are you ABSOLUTELY SURE? Type 'YES' to confirm: ");
var input = Console.ReadLine();
if (input == "YES")
{
int closed = await svc.CloseAllPositions();
Console.WriteLine($"đ¨ EMERGENCY EXIT: Closed {closed} positions");
}
else
{
Console.WriteLine("â Emergency stop aborted");
}
Example 7: Close Losing Positions OnlyÂļ
// Custom logic: Close only positions with unrealized loss
public async Task<int> CloseLosers(MT5Service svc)
{
var positions = await svc.OpenedOrdersAllAsync();
int closed = 0;
foreach (var pos in positions.Orders)
{
// Skip pending orders
if (pos.Type != ENUM_ORDER_TYPE.OrderTypeBuy &&
pos.Type != ENUM_ORDER_TYPE.OrderTypeSell)
continue;
// Check if position is losing
if (pos.Profit < 0)
{
await svc.CloseByTicket(pos.Ticket);
closed++;
Console.WriteLine($" Closed losing position #{pos.Ticket}: ${pos.Profit:F2}");
}
}
Console.WriteLine($"â
Closed {closed} losing positions");
return closed;
}
// Usage:
await CloseLosers(svc);
Example 8: Error HandlingÂļ
try
{
int closed = await svc.CloseAllPositions(symbol: "EURUSD");
if (closed > 0)
{
Console.WriteLine($"â
Closed {closed} EURUSD positions");
}
else
{
Console.WriteLine("âšī¸ No open positions to close for EURUSD");
}
}
catch (Exception ex)
{
Console.WriteLine($"â Error closing positions: {ex.Message}");
}
Example 9: News Event Emergency ExitÂļ
public async Task HandleNewsEvent(MT5Service svc, string eventName)
{
Console.WriteLine($"â ī¸ HIGH IMPACT NEWS: {eventName}");
Console.WriteLine("â ī¸ Closing all positions for safety...");
int closed = await svc.CloseAllPositions();
int cancelled = await svc.CancelAll();
Console.WriteLine($"â
Closed {closed} positions");
Console.WriteLine($"â
Cancelled {cancelled} pending orders");
Console.WriteLine("â
Waiting for news event to pass...");
}
// Usage:
await HandleNewsEvent(svc, "NFP - Non-Farm Payrolls");
Example 10: Close with P&L ReportingÂļ
public async Task CloseAllWithReport(MT5Service svc)
{
// Get positions before closing
var positions = await svc.OpenedOrdersAllAsync();
double totalProfit = 0;
int count = 0;
foreach (var pos in positions.Orders)
{
if (pos.Type == ENUM_ORDER_TYPE.OrderTypeBuy ||
pos.Type == ENUM_ORDER_TYPE.OrderTypeSell)
{
totalProfit += pos.Profit;
count++;
}
}
Console.WriteLine($"About to close {count} positions");
Console.WriteLine($"Current unrealized P&L: ${totalProfit:F2}");
Console.Write("Continue? (y/n): ");
if (Console.ReadLine()?.ToLower() == "y")
{
int closed = await svc.CloseAllPositions();
Console.WriteLine($"â
Closed {closed} positions");
Console.WriteLine($"â
Realized P&L: ${totalProfit:F2}");
}
}
// Usage:
await CloseAllWithReport(svc);
đ Related MethodsÂļ
đĻ Methods used internally:
OpenedOrdersAsync()- Gets all orders (market + pending)CloseByTicket()- Closes individual position by ticket
đŦ Related Sugar methods:
CancelAll()- Cancels PENDING orders (NOT market positions!)CloseAllPending()- Alias for CancelAllCloseAll()- DIFFERENT METHOD from Region 06 (similar functionality)
â ī¸ CONFUSION WARNING:
CloseAllPositions()- Closes MARKET positions (this method)CancelAll()- Cancels PENDING ordersCloseAll()- From Region 06, also closes market positions
â ī¸ Common PitfallsÂļ
-
Confusing with CancelAll:
-
No confirmation prompt - instant execution:
-
Forgetting symbol parameter = ALL symbols:
-
Expecting pending orders to cancel:
-
Not considering slippage on close:
-
Not checking P&L before closing:
// â DANGEROUS: Closing without knowing current P&L await svc.CloseAllPositions(); // Might realize huge losses if positions are underwater! // â BETTER: Check P&L first var positions = await svc.OpenedOrdersAllAsync(); double totalPL = positions.Orders.Sum(o => o.Profit); Console.WriteLine($"Current P&L: ${totalPL:F2}"); Console.Write("Close all? (y/n): "); -
Filter logic confusion:
// â ī¸ FILTER LOGIC: // symbol: null â ALL symbols // isBuy: null â BOTH buy and sell await svc.CloseAllPositions(symbol: null, isBuy: true); // Closes ALL buy positions across ALL symbols! await svc.CloseAllPositions(symbol: "EURUSD", isBuy: null); // Closes BOTH buy and sell positions for EURUSD
đĄ SummaryÂļ
CloseAllPositions provides nuclear-option liquidation of all market positions:
- â Closes all market positions (BUY/SELL)
- â Optional filtering by symbol and direction
- â Returns count of closed positions
- â ī¸ NO pending orders affected (only market positions)
- â ī¸â ī¸â ī¸ NO confirmation prompt (executes immediately)
- â ī¸â ī¸â ī¸ CAN REALIZE MASSIVE LOSSES if used incorrectly
// Close all positions for EURUSD:
int closed = await svc.CloseAllPositions(symbol: "EURUSD");
// Close only sell positions:
int closed = await svc.CloseAllPositions(isBuy: false);
// Close EVERYTHING (â ī¸â ī¸â ī¸ NUCLEAR OPTION):
int closed = await svc.CloseAllPositions();
This is the PANIC BUTTON. Use with EXTREME caution! đ¨â ī¸đ¨