trailingmaster
Download scriptThis script, titled TrailingMaster, is an automated trading tool designed to manage active positions by dynamically adjusting "Stop Loss" levels. Its primary purpose is to lock in profits as a trade moves in your favor, ensuring that if the market suddenly reverses, you preserve a portion of your gains.
How the Logic Works
The script constantly monitors your open trades and performs the following actions:
1. Profit Protection (Trailing Stop) When a trade becomes profitable, the script checks the current price against your pre-defined distance (measured in pips). If the price has moved significantly in your favor, the tool automatically "trails" the Stop Loss. It moves the safety net forward to follow the price, maintaining a consistent buffer between your exit point and the current market value.
2. Smart Filtering (Precision Targeting) Not every trade is treated the same way. The script allows you to filter which specific trades should be managed based on three distinct criteria:
- Magic Numbers: You can tell the script to only manage trades opened by a specific automated system or "ID."
- Order Comments: You can set specific text labels (comments) on your trades; the script will only move the Stop Loss for trades containing that exact phrase.
- Global Application: If no filters are set, the script will automatically manage every profitable trade on your account for that specific currency pair.
3. Dynamic Execution The logic is designed to be "non-destructive." It calculates whether a move is necessary before taking action. For example:
- For Buy Positions: It ensures the current price is high enough to justify moving the Stop Loss upward.
- For Sell Positions: It ensures the current price is low enough to justify moving the Stop Loss downward.
- Safety Check: It only modifies the order if the new calculated position is actually better than the existing one, preventing the script from accidentally moving a Stop Loss into a disadvantageous position.
Summary for the User
Essentially, TrailingMaster acts as an automated security guard. Instead of you having to manually move your Stop Loss every time the market fluctuates, this script watches the price 24/7. As soon as your profit reaches a certain threshold, it "locks" that profit in place by trailing your exit point behind the current price, helping to minimize risk while maximizing your realized gains.
//+------------------------------------------------------------------+
//| TrailingMaster |
//| Copyright 2016, Il Anokhin |
//| http://www.mql5.com/en/users/ilanokhin |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2016, Il Anokhin"
#property link "http://www.mql5.com/en/users/ilanokhin"
#property description ""
#property strict
//-------------------------------------------------------------------------
// Inputs
//-------------------------------------------------------------------------
input int TS = 50; //Trailing Stop (pips)
input bool UC = false; //Use Certain Order Comment
input string Comm = ""; //Certain Order Comment
input bool UM = false; //Use Certain Order Magic Number
input int Magic = 12345; //Certain Order Magic Number
//-------------------------------------------------------------------------
// Variables
//-------------------------------------------------------------------------
int i;
double pip;
bool w;
//-------------------------------------------------------------------------
// 1. Main function
//-------------------------------------------------------------------------
void OnTick(void)
{
Comment("Copyright © 2016, Il Anokhin\n"+TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
//--- 1.1. Define pip -----------------------------------------------------
if(Digits==4 || Digits<=2) pip=Point;
if(Digits==5 || Digits==3) pip=Point*10;
//--- 1.2. Trailing -------------------------------------------------------
for(i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
if(OrderSymbol()==Symbol() && TS>0 && OrderProfit()>0)
{
if(UC==true && OrderComment()==Comm && UM==true && OrderMagicNumber()==Magic && OrderType()==OP_BUY && OrderOpenPrice()+TS*pip<=Bid && OrderStopLoss()<Bid-TS*pip) w=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TS*pip,OrderTakeProfit(),0);
if(UC==true && OrderComment()==Comm && UM==true && OrderMagicNumber()==Magic && OrderType()==OP_SELL && OrderOpenPrice()-TS*pip>=Ask && (OrderStopLoss()>Ask+TS*pip || OrderStopLoss()==0)) w=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TS*pip,OrderTakeProfit(),0);
if(UC==true && OrderComment()==Comm && UM==false && OrderType()==OP_BUY && OrderOpenPrice()+TS*pip<=Bid && OrderStopLoss()<Bid-TS*pip) w=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TS*pip,OrderTakeProfit(),0);
if(UC==true && OrderComment()==Comm && UM==false && OrderType()==OP_SELL && OrderOpenPrice()-TS*pip>=Ask && (OrderStopLoss()>Ask+TS*pip || OrderStopLoss()==0)) w=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TS*pip,OrderTakeProfit(),0);
if(UC==false && UM==true && OrderMagicNumber()==Magic && OrderType()==OP_BUY && OrderOpenPrice()+TS*pip<=Bid && OrderStopLoss()<Bid-TS*pip) w=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TS*pip,OrderTakeProfit(),0);
if(UC==false && UM==true && OrderMagicNumber()==Magic && OrderType()==OP_SELL && OrderOpenPrice()-TS*pip>=Ask && (OrderStopLoss()>Ask+TS*pip || OrderStopLoss()==0)) w=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TS*pip,OrderTakeProfit(),0);
if(UC==false && UM==false && OrderType()==OP_BUY && OrderOpenPrice()+TS*pip<=Bid && OrderStopLoss()<Bid-TS*pip) w=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TS*pip,OrderTakeProfit(),0);
if(UC==false && UM==false && OrderType()==OP_SELL && OrderOpenPrice()-TS*pip>=Ask && (OrderStopLoss()>Ask+TS*pip || OrderStopLoss()==0)) w=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TS*pip,OrderTakeProfit(),0);
}
}
}
//--- 1.3. End of main function -------------------------------------------
return;
}
//-------------------------------------------------------------------------
Comments