ulLOSE_ALLnMAGIK
Download scriptThis script is designed as an automated risk management tool for the MetaTrader 4 platform. Its primary purpose is to monitor open trading positions and automatically close them once specific profit or loss targets are reached, regardless of how many individual trades are currently active.
Core Functionality
The logic operates by scanning all active trades on your account and calculating their collective performance based on three different measurement types that you can choose from:
- Balance: Monitoring the total monetary change in your account equity.
- Points (Pips): Calculating the sum of price movements for both "Buy" and "Sell" positions to determine your total pips gained or lost.
- Percentage: Tracking the profit or loss as a percentage relative to your current account balance.
How it Works
- Filtering: You can choose whether the script should manage every single open trade on your account or only those associated with a specific "Magic Number" (a unique ID used to identify trades placed by a specific automated robot).
- Monitoring: The script constantly updates a real-time dashboard on your chart. This display shows you:
- The current profit/loss in points and currency for both Buy and Sell sides individually.
- The combined total pips and total dollar equity.
- The overall percentage gain or loss.
- Automatic Execution: You define a "Profit" target and a "Loss" limit. As soon as the script detects that the combined performance of your trades hits either of these limits, it triggers an alert and immediately executes a command to close all qualifying positions.
- Order Cleanup: Additionally, the script includes a feature to automatically delete any pending orders (orders waiting to be triggered) once the closing conditions are met, ensuring a clean slate for your next trading session.
Key Benefits for the User
- Safety Net: It acts as a global "circuit breaker," protecting your account from significant drawdown by closing positions if they hit a maximum loss limit.
- Targeted Profits: It allows you to take profit on a group of trades simultaneously once a collective goal is reached, rather than having to manage each trade individually.
- Visibility: It provides a consolidated view of your trading performance directly on the chart, making it easier to see your current exposure at a glance.
//+------------------------------------------------------------------+
//| ÑLOSE_ALL+MAGIK.mq4 |
//| MVS |
//| ICQ 588-948-516 ÏÈØÓ ÑÎÂÅÒÍÈÊÈ ÍÀ ÇÀÊÀÇ |
//+------------------------------------------------------------------+
#property copyright "MVS"
#property link "ICQ 588-948-516 ÏÈØÓ ÑÎÂÅÒÍÈÊÈ ÍÀ ÇÀÊÀÇ"
extern string ñlose ="= 1 -áàëàíñ, 2 -ïóíêòû ,3 -ïðîöåíòû =";
extern int close = 2; //çàêðûâàåò 1 ïî áàëàíñó
// 2 ïî ïóíêòàì
// 3 ïî ïðîöåíòàì
extern bool magic = True;//åñëè True òî ïî ìàãèêó,åñëè False òî âñå îòêðûòûå
extern int Magic = 0; // ñòàâèì íóæíûé Magic åêñïåðòà,åñëè 0 òî îòêðûòûå âðó÷íóþ
extern int Profit = 500; // ïðîôèò ïðè êîòîðîì çàêðûâàåì
extern int Loss = 5000; // ëîññ ïðè êîòîðîì çàêðûâàåì
int Slippage = 2; // ïðîñêàëüçûâàíèå
extern bool Orderdelete = True; // óäàëÿòü îòëîæåííûå îðäåðà True=ÄÀ,False=ÍÅÒ
extern bool comment = True; // Ïîêàçûâàòü êîììåíòàðèé True=ÄÀ,False=ÍÅÒ
//---------------------- Ãëîáàëüíûå ïåðåìåííûå ñîâåòíèêà --------------------------------------------------
double price;
int ordertype,err;
bool result,first;
#include <stdlib.mqh>
//+--------------------------------------------------------------------------------------------------------
int init() {
if (!IsTesting())
Comment(""); }
//+--------------------------------------------------------------------------------------------------------
int deinit() {
if (!IsTesting())
Comment(""); }
//-----------------------------------ïîåõàëè---------------------------------------------------------------
int start() {
double ask,bid,open,Prc,point,buy_e,sell_e,Equity,_close;
int Pips,buy_p,sell_p,flag=0;
string com,mg;
//-------------------------------ñìîòðèì îòêðûòûå----------------------------------------------------------
if( magic) mg=Magic;if(!magic) mg="ÍÅ ÂÛÁÐÀÍ";
for(int Q=0;Q<OrdersTotal();Q++) {
if (OrderSelect(Q,SELECT_BY_POS,MODE_TRADES))
if(!magic||OrderMagicNumber()==Magic){
if (OrderType()==OP_SELL) {
point=MarketInfo(OrderSymbol(),MODE_POINT);
ask=MathRound(MarketInfo(OrderSymbol(),MODE_ASK)/point);
open=MathRound(OrderOpenPrice()/point);
sell_e+=OrderProfit()+OrderSwap();
sell_p+=(open-ask);
}
if (OrderType()==OP_BUY) {
point=MarketInfo(OrderSymbol(),MODE_POINT);
bid=MathRound(MarketInfo(OrderSymbol(),MODE_BID)/point);
open=MathRound(OrderOpenPrice()/point);
buy_e+=OrderProfit()+OrderSwap();
buy_p+=(bid-open);
}
//-----------------------------------ñ÷èòàåì---------------------------------------------------------------
Pips=(buy_p+sell_p);
Equity=(buy_e+sell_e);
Prc=NormalizeDouble((Equity*100)/AccountBalance(),2);
//----------------------------------âûáèðàåì---------------------------------------------------------------
switch(close)
{
case 1: _close=Equity; com = "áàëàíñó"; break;
case 2: _close=Pips; com = "ïóíêòàì"; break;
case 3: _close=Prc; com = "ïðîöåíòàì"; break;
default: com = "ÍÅ ÓÊÀÇÀÍÎ"; break;
}
//--------------------Êîììåíòàðèè---------------------------------------------------------------------------
if(comment)
Comment(
"\nÇàêðûòèå ïî ",com," ",
"\nMagic íîìåð N ",mg," ",
"\nÏðèáûëü çàêðûòèÿ ",Profit," ",
"\nÓáûòîê çàêðûòèÿ ",Loss," ",
"\nÏðèáûëü BUY ïóíêòîâ ",buy_p," ï.",
"\nÏðèáûëü Sell ïóíêòîâ ",sell_p," ï.",
"\nÎáùèõ ïóíêòîâ ",Pips," ï.",
"\nÏðèáûëü BUY $ ",buy_e," $",
"\nÏðèáûëü Sell $ ",sell_e," $",
"\nÎáùèé Equity $ ",Equity," $",
"\nÏðèáûëü ïðîöåíò % ",Prc," %"
);
}}
//---------------------------óñëîâèÿ äëÿ çàêðûòèÿ---------------------------------------------------------------------
if (_close <=-Loss) { Alert(" ÇÀÊÐÛÂÀÅÌ ÓÁÛÒÎÊ O_o");flag=1;}
if (_close >=Profit){ Alert(" ÇÀÊÐÛÂÀÅÌ ÏÐÎÔÈÒ !!!"); flag=1;}
//-----------------------------âñå ïîçèöèè çàêðûâàåì------------------------------------------------------------------
if(flag>0)
for(int i=OrdersTotal()-1;i>=0;i--){
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
if(!magic||OrderMagicNumber()==Magic) {
ordertype=OrderType();
if(ordertype==OP_BUY || ordertype==OP_SELL){
while(true){
if(ordertype==OP_BUY)
price=MarketInfo(OrderSymbol(),MODE_BID);
else
price=MarketInfo(OrderSymbol(),MODE_ASK);
result=OrderClose(OrderTicket(),OrderLots(),price,Slippage,CLR_NONE);
if(result!=true) {err=GetLastError(); Print("Îøèáêà çàêðûòèÿ ",err);}
else err=0;
if(err==135 || err==138) RefreshRates();
else break;
}}
//---------------------------âñå îðäåðà óäàëÿåì-----------------------------------------------------------------------
if (Orderdelete) {
if ( ordertype == OP_BUYSTOP || ordertype == OP_SELLSTOP || ordertype == OP_BUYLIMIT || ordertype == OP_SELLLIMIT )
OrderDelete(OrderTicket(),CLR_NONE);
}
else Print( "âîçíèêëà îøèáêà ",GetLastError());
}}}
return(0);
}
Comments