Modify_v1
Download scriptThis script is designed to automate the management of specific trading positions within the MetaTrader 4 platform. Its primary purpose is to scan your active trades and automatically apply predefined profit-taking and risk-management limits to them based on criteria you set upfront.
How the Logic Works
1. Configuration and Identification When you run the script, it looks at a specific set of instructions you provide. You define which currency pair you want to target (for example, EURUSD), what type of trade you are looking for (such as a standard Buy, Sell, or various Limit/Stop orders), and what your desired Stop Loss and Take Profit price levels should be.
2. Environment Verification Before taking any action, the script performs a quick "health check." It verifies two things:
- Is your computer currently connected to the trading server?
- Is your account configured to allow automated trading? If either of these conditions isn't met, the script will notify you with an alert rather than attempting to execute a command that might fail.
3. The Search Process The script systematically scans through every open trade in your account. It acts as a filter, checking each position to see if it matches all three of your requirements:
- Does the symbol match the one you specified?
- Does the order type match the one you selected?
- Is it currently active on the correct chart?
4. Automated Modification Once the script finds a trade that matches your criteria, it attempts to "modify" that trade. It pushes the specific Take Profit and Stop Loss values you entered into the live order.
5. Error Handling and Persistence The logic includes a "retry" mechanism. If the platform is busy or a temporary connection hiccup occurs while trying to update the order, the script won't simply give up immediately. It will wait for a second and try again, repeating this process several times to ensure the update is successful. If it encounters a persistent problem, it logs the specific error for you to review.
6. Completion Notification After the scan is finished, the script provides feedback. If it successfully updated your trades, it completes its cycle silently; if it went through all your open positions and couldn't find a single one that matched your settings, it will send an alert notifying you that no matching orders were found.
//+------------------------------------------------------------------+
//| MODIFY.mq4 |
//| Copyright © 2006, Shadow Trader |
//| mailto:ShadowDragon_Fil@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Shadow Trader"
#property link "http://forum.alpari-idc.ru/thread32559.html"
#property show_inputs
#include <stdlib.mqh>
extern double Take_Profit = 0;
extern double Stop_Loss = 0;
extern string Order_Type = "BUY";
extern string Order_Symbol = "EURUSD";
int start()
{
int type = 0;
if (Order_Type == "BUY") type = OP_BUY;
else if (Order_Type == "SELL") type = OP_SELL;
else if (Order_Type == "BUY LIMIT") type = OP_BUYLIMIT;
else if (Order_Type == "SELL LIMIT") type = OP_SELLLIMIT;
else if (Order_Type == "BUY STOP") type = OP_BUYSTOP;
else if (Order_Type == "SELL STOP") type = OP_SELLSTOP;
else
{
Alert("Íå âåðíî çàäàí òèï îðäåðà: " + Order_Type);
return (0);
}
if (!IsConnected()) { Alert("Îòñóòñòâóåò ñâÿçü ñ ñåðâåðîì!"); return (0); }
if (!IsTradeAllowed()) { Alert("Òîðãîâëÿ çàïðåùåíà!"); return (0); }
bool find = false;
// ïîèñê îðäåðà
for (int i = 0; i <= OrdersTotal(); i++)
{
if((OrderSelect(i, SELECT_BY_POS, MODE_TRADES )) &&
(OrderSymbol() == Order_Symbol) &&
(OrderType () == type))
if ( OrderSymbol() == Symbol() )
{
int j = 0;
while (!OrderModify(OrderTicket(),
OrderOpenPrice(),
NormalizeDouble(Take_Profit, Digits),
NormalizeDouble(Stop_Loss, Digits),
OrderExpiration(),
CLR_NONE))
{
int error = GetLastError();
if (error > 0)
{
Print("Îøèáêà ïðè ìîäèôèêàöèè! ", error, ", \"", ErrorDescription( error ), "\"" );
}
Sleep(1000);
if ((j > 2) || IsStopped()) break;
j++;
}
while (IsTradeContextBusy() && !IsStopped()) Sleep(1000);
find = true;
}
}
if (!find)
{
Alert("Íå íàéäåíû îðäåðà ïî óêàçàíûì ïàðàìåòðàì!");
}
return(0);
}
int deinit()
{
return(0);
}
Comments