Close_all_Op
Download scriptThis script is designed to perform an automated "clean sweep" of your active trading positions and pending orders. Its primary function is to immediately neutralize all open market activities to ensure your account has no remaining exposure to the market.
How the Logic Works:
1. Scanning Your Portfolio The script begins by identifying every single order currently associated with your trading account. It doesn't matter if these are active trades currently moving in the market or pending orders waiting for a specific price to be hit; the script looks at the entire list.
2. Systematic Review It processes these orders one by one, starting from the most recent and moving backward through the list. This systematic approach ensures that no order is skipped during the execution process.
3. Identifying Order Types As it scans each item, the script categorizes the orders into two main groups:
- Buy Orders: This includes active "Buy" positions as well as "Buy Stop" and "Buy Limit" pending orders.
- Sell Orders: This includes active "Sell" positions as well as "Sell Stop" and "Sell Limit" pending orders.
4. Immediate Execution of Closure Once the script identifies an order belonging to either category, it triggers a command to close it instantly. It handles both types correctly by using the current market price (the Bid for sells and the Ask for buys) to ensure the trades are finalized at the best available market rate.
Summary of Result
When this script is triggered, it acts as a "Panic Button" or a "Reset Switch." It clears your workspace by closing all buy-side and sell-side activities simultaneously, leaving your trading account with zero open positions and no pending orders.
int start()
{
int total_order;
total_order=OrdersTotal();
for(int cnt=total_order;cnt>=0;cnt--)
{
if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)==true)
{
if(OrderType()==OP_BUY || OrderType()==OP_BUYSTOP || OrderType()==OP_BUYLIMIT) OrderClose(OrderTicket(),OrderLots(),Bid,3,Blue);
if(OrderType()==OP_SELL || OrderType()==OP_SELLSTOP || OrderType()==OP_SELLSTOP) OrderClose(OrderTicket(),OrderLots(),Ask,3,Blue);
}
}
}
Comments