Automatic Stop Loss Setter
Download scriptThis automated trading assistant is designed to manage your active trades by dynamically adjusting safety nets (Stop Loss) and profit targets (Take Profit) based on real-time market movement. Its primary goal is to protect your capital while ensuring that winning trades are captured effectively.
How the System Operates:
1. Initial Setup and Validation When you first load the tool, it performs a safety check on your settings. It ensures that all your risk parameters—Stop Loss, Take Profit, and the profit threshold—are set to a minimum acceptable level. If the values are too low, the system will alert you and wait for adjustments before starting work.
2. Active Trade Monitoring The script continuously scans your open positions. It specifically looks for trades on the current currency pair and filters out positions that already have correctly placed safety levels, ensuring it only interacts with the trades that need management.
3. Smart Protection Logic The system uses three different "modes" of logic to handle your trades depending on how they are performing:
- Initial Protection: For new trades, the script automatically calculates and applies your chosen Stop Loss and Take Profit levels relative to your entry price. This ensures every trade has a defined boundary from the moment it opens.
- The "Break-Even" Transition: A key feature of this script is its ability to move your Stop Loss to a "safe" zone once a trade reaches a specific profit milestone (defined by the
ZeroStopLossProfitsetting). - Dynamic Profit Tracking: If a trade moves into profit but hasn't reached your full goal yet, the script monitors the price fluctuations. It tracks how far the price has moved in your favor; if the price retreats, it intelligently adjusts the Stop Loss to follow that movement, helping to "lock in" some gains while keeping the trade open for further potential profit.
4. User Feedback To keep you informed of its actions, the script provides visual and audio cues. Whenever it successfully modifies a trade or moves a safety level, it will update the on-screen status and play a notification sound, allowing you to know that the system is actively managing your risk.
extern int StopLoss = 18;
extern int TakeProfit = 25;
extern int ZeroStopLossProfit = 13; //ýòî óðîâåíü ïåðåâîäà ïîçèöèè â áåçóáûòîê +1 ïèïñ
extern bool MakeSound = true;
double MaxDeltaPrice;
string SymbolString;
bool Error;
/*=================================================================================================*/
void init ()
{
Error = false;
SymbolString = Symbol();
if ( (StopLoss < 13) ||
(TakeProfit < 13) ||
(ZeroStopLossProfit < 13) )
{ Comment ("Error: inputs cannot be less than 13. Change."); Error = true; return; }
else Comment ("The Expert is loaded and waiting data.");
}
/*=================================================================================================*/
void start ()
{
int i;
double OpenPrice;
double CurrentPrice;
double DeltaPrice;
double StopLossPrice;
double TakeProfitPrice;
int OrderDir;
int OrderCounter;
if (Error == true) return;
Comment (SymbolString,": The Expert is working...");
OrderCounter = OrdersTotal();
if (OrderCounter==0) return;
for (i=0; i<OrderCounter; i++)
{
OrderSelect (i,SELECT_BY_POS);
OpenPrice = OrderOpenPrice();
OrderDir = OrderType();
if (OrderSymbol() != Symbol()) continue;
if (OrderStopLoss() != 0.0)
{ if ( (OrderDir == OP_SELL) && (OrderOpenPrice() > OrderStopLoss()) ) continue;
if ( (OrderDir == OP_BUY) && (OrderOpenPrice() < OrderStopLoss()) ) continue;
if (OrderProfit() <= 0.0) continue;
CurrentPrice = OrderClosePrice();
DeltaPrice = MathAbs(CurrentPrice-OpenPrice);
if ((DeltaPrice/Point) < ZeroStopLossProfit)
{ if (DeltaPrice > MaxDeltaPrice)
{ if (OrderDir == OP_SELL) StopLossPrice = CurrentPrice + (StopLoss*Point);
if (OrderDir == OP_BUY) StopLossPrice = CurrentPrice - (StopLoss*Point);
OrderModify (OrderTicket(),OrderOpenPrice(),StopLossPrice,OrderTakeProfit(),0,CLR_NONE);
if (MakeSound == true) PlaySound ("tick.wav"); }
MaxDeltaPrice = MathMax(MaxDeltaPrice,DeltaPrice);
continue; }
if (OrderDir == OP_SELL) StopLossPrice = OpenPrice - Point;
if (OrderDir == OP_BUY) StopLossPrice = OpenPrice + Point;
OrderModify (OrderTicket(),OrderOpenPrice(),StopLossPrice,OrderTakeProfit(),0,CLR_NONE);
if (MakeSound == true) PlaySound ("tick.wav");
continue; }
if (OrderDir == OP_SELL)
{ StopLossPrice = OpenPrice + (StopLoss * Point); TakeProfitPrice = OpenPrice - TakeProfit*Point; }
if (OrderDir == OP_BUY)
{ StopLossPrice = OpenPrice - (StopLoss * Point); TakeProfitPrice = OpenPrice + TakeProfit*Point; }
OrderModify (OrderTicket(),OrderOpenPrice(),StopLossPrice,TakeProfitPrice,0,CLR_NONE);
if (MakeSound == true) PlaySound ("tick.wav");
}
}
/*=================================================================================================*/
Comments