CloseAllProfit
Download scriptThis script is designed to act as an automated "exit strategy" for your trading account, specifically helping you secure gains by closing out successful positions quickly. Instead of manually monitoring every trade to see when it has reached a positive state, this tool scans your active trades and performs the closing actions automatically based on specific criteria.
How the Logic Works
1. Scanning Active Positions The script begins by taking a snapshot of all currently open orders in your trading account. It moves through these positions one by one to evaluate whether they meet the requirements for closure.
2. Profit Verification The primary filter used by the script is profitability. It checks the current financial status of each trade; if a position is currently in a "plus" state (meaning it is not in a loss), it proceeds to the next step of the logic. If a trade is currently losing money, the script ignores it and leaves it open.
3. Market Synchronization Before attempting to close any trades, the script performs a real-time check of the current market environment. It refreshes the latest "Ask" and "Bid" prices for the specific currency pair or asset involved in the trade. This ensures that the script is acting on the most up-to-date data available, which helps prevent errors caused by outdated pricing.
4. Executing the Exit Once the script identifies a trade that is both profitable and valid based on current market conditions, it automatically sends a command to the server to close that position at the best available price. It calculates the necessary details (such as the correct volume and price) automatically so that you don't have to intervene manually.
5. Error Handling If for any reason the trade cannot be closed—such as a sudden loss of internet connection or a significant market move that makes the order impossible to fill—the script is designed to capture that specific error. It will log a message in the system diary, notifying you of exactly what went wrong with that specific order ticket so you can review it later.
Summary for the User
Essentially, this tool serves as an automated "Take Profit" button that triggers for all winning trades simultaneously. It is intended to help traders lock in gains across multiple positions at once, ensuring that profitable movements are captured immediately without the need for manual clicks.
//+------------------------------------------------------------------+
//| CloseAllProfit.mq4 |
//| Copyright © 2008, PRMQuotes Software Corp. |
//| Jedimedic77@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, PRMQuotes Software Corp."
#property link ""
//+------------------------------------------------------------------+
//| EX4 imports |
//+------------------------------------------------------------------+
#include <stdlib.mqh>
//+------------------------------------------------------------------+
//| global variables to program: |
//+------------------------------------------------------------------+
double Price[2];
int giSlippage;
bool CloseOrdersWithPlusProfit = true;
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
void start() {
int iOrders=OrdersTotal()-1, i;
if(CloseOrdersWithPlusProfit) {
for(i=iOrders; i>=0; i--) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) && (OrderProfit() >= 0)) {
if((OrderType()<=OP_SELL) && GetMarketInfo()) {
if(!OrderClose(OrderTicket(),OrderLots(),Price[1-OrderType()],giSlippage)) Print(OrderError());
}
}
}
}
}
//+------------------------------------------------------------------+
//| Function..: OrderError |
//+------------------------------------------------------------------+
string OrderError() {
int iError=GetLastError();
return(StringConcatenate("Order:",OrderTicket()," GetLastError()=",iError," ",ErrorDescription(iError)));
}
//+------------------------------------------------------------------+
//| Function..: GetMarketInfo |
//+------------------------------------------------------------------+
bool GetMarketInfo() {
RefreshRates();
Price[0]=MarketInfo(OrderSymbol(),MODE_ASK);
Price[1]=MarketInfo(OrderSymbol(),MODE_BID);
double dPoint=MarketInfo(OrderSymbol(),MODE_POINT);
if(dPoint==0) return(false);
giSlippage=(Price[0]-Price[1])/dPoint;
return(Price[0]>0.0 && Price[1]>0.0);
}
//+------------------------------------------------------------------+
Comments