Autostop2
Download scriptThis script is designed to automate the management of your trading positions by automatically applying "Take Profit" and "Stop Loss" levels to any open trades that lack them. It acts as a safety net and an automated exit strategy for your manual or automated orders, ensuring that every position has predefined limits to protect your capital.
Core Functionality
The primary purpose of the script is to scan your active trading account in real-time. If it detects an open trade that does not have a Take Profit (TP) or a Stop Loss (SL) price set, it will automatically calculate and apply the correct levels based on the settings you provide.
How the Logic Works
- Continuous Monitoring: The script constantly cycles through your open orders to check for any positions that are currently "unprotected" (i.e., they have no exit targets defined).
- Dynamic Price Calculation: When it identifies an order without a target, it looks at the price where you entered the trade. It then calculates the correct exit points by adding or subtracting your desired distance from that entry price.
- For Buy positions: It places the Stop Loss below your entry price and the Take Profit above it.
- For Sell positions: It places the Stop Loss above your entry price and the Take Profit below it.
- Precision Handling: The script automatically adjusts these calculations to match the specific decimal precision of the currency pair you are trading, ensuring the broker accepts the orders without errors.
- Smart Filtering: To prevent the script from interfering with other specialized trading robots, it is designed to only manage trades that do not have a specific "Magic Number" assigned (defaulting to standard manual trades).
Error Handling and Reliability
The script includes built-in "checks" to ensure smooth operation. If the market moves too quickly or if there is a temporary communication lag with the broker server, the script can detect these common errors and will automatically attempt to refresh the data and try again. It also provides visual alerts if trading is prohibited or if specific technical issues occur, keeping you informed of your account's status.
User Experience
From a user perspective, this tool removes the manual burden of having to precisely calculate pips and enter exit prices for every single trade. You simply define your desired risk parameters once, and the script ensures those rules are applied consistently to every new position you open.
//+------------------------------------------------------------------+
//| Autostop.mq4 |
//| Copyright © 2012, www.FxAutomated.com |
//| http://www.FxAutomated.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, www.FxAutomated.com"
#property link "http://www.FxAutomated.com"
extern string Product="Autostop v2";
extern string AboutAutostop="Automatically sets take profit and stop loss.";
extern bool MonitorTakeProfit=true;
extern bool MonitorStopLoss=true;
extern double TakeProfit=30;
extern double StopLoss=30;
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int StopMultd=10;
double TP=NormalizeDouble(TakeProfit*StopMultd,Digits);
double SL=NormalizeDouble(StopLoss*StopMultd,Digits);
//-------------------------------------------------------------------+
//Check open orders
//-------------------------------------------------------------------+
if(OrdersTotal()>0){
for(int i=1; i<=OrdersTotal(); i++) // Cycle searching in orders
{
if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
{
if((MonitorTakeProfit==true)&&(TakeProfit>0)&&(OrderMagicNumber()==0)){ // monitor tp
// Calculate take profit
double tpb=NormalizeDouble(OrderOpenPrice()+TP*Point,Digits);
double tps=NormalizeDouble(OrderOpenPrice()-TP*Point,Digits);
Comment("Modifying take profit");
if((OrderType()==OP_BUY)&&(OrderTakeProfit()==0)&&(OrderSymbol()==Symbol())){ OrderModify(OrderTicket(),0,OrderStopLoss(),tpb,0,CLR_NONE); }
if((OrderType()==OP_SELL)&&(OrderTakeProfit()==0)&&(OrderSymbol()==Symbol())){ OrderModify(OrderTicket(),0,OrderStopLoss(),tps,0,CLR_NONE); }
} // monitor tp
if((MonitorStopLoss==true)&&(StopLoss>0)&&(OrderMagicNumber()==0)){ // monitor sl
// Calculate stop loss
double slb=NormalizeDouble(OrderOpenPrice()-SL*Point,Digits);
double sls=NormalizeDouble(OrderOpenPrice()+SL*Point,Digits);
Comment("Modifying stop loss");
if((OrderType()==OP_BUY)&&(OrderStopLoss()==0)&&(OrderSymbol()==Symbol())){ OrderModify(OrderTicket(),0,slb,OrderTakeProfit(),0,CLR_NONE); }
if((OrderType()==OP_SELL)&&(OrderStopLoss()==0)&&(OrderSymbol()==Symbol())){ OrderModify(OrderTicket(),0,sls,OrderTakeProfit(),0,CLR_NONE); }
}// monitor sl
Comment("");
}
}
}
//----
int Error=GetLastError();
if(Error==130){Alert("Wrong stops. Retrying."); RefreshRates();}
if(Error==133){Alert("Trading prohibited.");}
if(Error==2){Alert("Common error.");}
if(Error==146){Alert("Trading subsystem is busy. Retrying."); Sleep(500); RefreshRates();}
//----------
return(0);
}
//----------
//+-----------------------------------END-------------------------------+
//+---------------------------------------------------------------------+
int init()
{
//----
Alert("Visit www.FxAutomated.com for more goodies!");
//----
return(0);
}
Comments