e-Smart_Tralling
Download scriptThe e-Smart_Trailing script is an automated trade management tool designed to dynamically manage open positions by adjusting risk levels and securing profits as market conditions evolve. Instead of leaving a trade static, this script actively monitors the price movement and moves your "Stop Loss" (the point where a trade exits automatically) based on specific profit milestones.
Key Features and Logic:
1. Multi-Stage Profit Protection The script utilizes a three-tier logic system to manage trades as they become profitable. As the price moves in your favor, the script identifies three distinct "zones":
- Level 1: Once the trade reaches a specific initial profit target, the script moves the Stop Loss to a predefined point to protect your gains. It also includes an option to automatically close a fraction (one-third) of the position to lock in some profit immediately.
- Level 2: As the price continues to climb further, the script moves the Stop Loss again to an even tighter protection level.
- Level 3: At the highest profit milestone, the script moves the Stop Loss to its final safety point, ensuring that significant gains are shielded from sudden market reversals.
2. Dynamic Trailing Stop Beyond the fixed milestones, the script features a continuous "Trailing" mechanism. Once a trade exceeds a certain high-profit threshold, the script begins to follow the price like a shadow. If the market continues to move in your favor, the Stop Loss is pulled upward (for buy orders) or downward (for sell orders) at set intervals. This allows you to stay in a trending move for as long as possible while constantly "locking in" the profit made during that trend.
3. Visual and Audio Feedback To keep the trader informed without needing to constantly analyze complex charts, the script provides real-time updates:
- On-Screen Display: It calculates and displays the current status of your open orders (including opening price and Stop Loss distance) directly on the trading screen.
- Audio Alerts: The script can trigger a notification sound whenever a trade modification occurs (such as moving a Stop Loss or closing a portion of a trade), providing instant confirmation that the automation is working.
4. Precision Management The logic is designed to handle different types of assets accurately by calculating movements in "points" and "digits." It automatically detects whether a position is a Buy or a Sell and adjusts its math accordingly, ensuring that the trailing and protection logic remains consistent regardless of the specific currency pair or asset being traded.
//+--------------------------------------------------------+
//| e-Smart_Tralling |
//+--------------------------------------------------------+
extern bool UseOneAccount = true;
extern bool UseCloseOneThird = true;
extern int LevelProfit1 = 20;
extern int LevelMoving1 = 1;
extern int LevelProfit2 = 35;
extern int LevelMoving2 = 10;
extern int LevelProfit3 = 55;
extern int LevelMoving3 = 30;
extern int TrailingStop = 30;
extern int TrailingStep = 5;
extern int Slippage = 2;
extern bool ShowComment = true;
extern bool UseSound = true;
string var_132 = "expert.wav";
//+------------------------------------------------------------------+
void deinit()
{
Comment("");
}
//+------------------------------------------------------------------+
void start()
{
double point;
int digits;
string msg = "";
for (int i = 0; i < OrdersTotal(); i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if (UseOneAccount || (OrderSymbol() == Symbol()))
{
ThreeLevelSystemOfOutput();
digits = MarketInfo(OrderSymbol(),MODE_DIGITS);
point = MarketInfo(OrderSymbol(),MODE_POINT);
msg = msg + OrderSymbol() + " Öåíà: " + DoubleToStr(OrderOpenPrice(),digits) + " SL = " + DoubleToStr(OrderStopLoss(),digits) + " (" + StopLossInPoint() + ")\n";
}
}
}
if (ShowComment) Comment(msg);
}
//+------------------------------------------------------------------+
void ThreeLevelSystemOfOutput()
{
int profit = ProfitPosition();
int sl = StopLossInPoint();
int spread = MarketInfo(OrderSymbol(),MODE_SPREAD);
if ((profit > LevelProfit1) && (profit <= LevelProfit2) && (sl < LevelMoving1))
{
ModifyStopLossInPoint(LevelMoving1);
if (UseCloseOneThird) CloseOneThird();
}
if ((profit > LevelProfit2) && (profit <= LevelProfit3) && (sl < LevelMoving2)) ModifyStopLossInPoint(LevelMoving2);
if ((profit > LevelProfit3) && (sl < LevelMoving3)) ModifyStopLossInPoint(LevelMoving3);
if (profit > LevelMoving3 + TrailingStop + TrailingStep) TrailingPositions();
}
//+------------------------------------------------------------------+
void CloseOneThird()
{
bool result = false;
double lots = MathCeil(OrderLots() / 3.0 * 10.0) / 10.0;
if (lots > 0.0)
{
if (OrderType() == OP_BUY)
{
result = OrderClose(OrderTicket(),lots,Bid,Slippage,CLR_NONE);
}
if (OrderType() == OP_SELL)
{
result = OrderClose(OrderTicket(),lots,Ask,Slippage,CLR_NONE);
}
if (result && UseSound) PlaySound(var_132);
}
}
//+------------------------------------------------------------------+
void TrailingPositions()
{
double bid;
double ask;
double point = MarketInfo(OrderSymbol(),MODE_POINT);
if (OrderType() == OP_BUY)
{
bid = MarketInfo(OrderSymbol(),MODE_BID);
if (bid - OrderOpenPrice() > TrailingStop * point)
{
if (OrderStopLoss() < bid - (TrailingStop + TrailingStep - 1) * point)
{
ModifyStopLoss(bid - TrailingStop * point);
return;
}
}
}
if (OrderType() == OP_SELL)
{
ask = MarketInfo(OrderSymbol(),MODE_ASK);
if (OrderOpenPrice() - ask > TrailingStop * point)
{
if ((OrderStopLoss() > ask + (TrailingStop + TrailingStep - 1) * point) || (OrderStopLoss() == 0))
{
ModifyStopLoss(ask + TrailingStop * point);
}
}
}
}
//+------------------------------------------------------------------+
void ModifyStopLoss(double sl)
{
bool result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,CLR_NONE);
if (result && UseSound) PlaySound(var_132);
}
//+------------------------------------------------------------------+
void ModifyStopLossInPoint(int stoploss)
{
bool result;
double sl = 0;
double point = MarketInfo(OrderSymbol(),MODE_POINT);
if (OrderType() == OP_BUY) sl = OrderOpenPrice() + stoploss * point;
if (OrderType() == OP_SELL) sl = OrderOpenPrice() - stoploss * point;
result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,CLR_NONE);
if (result && UseSound) PlaySound(var_132);
}
//+------------------------------------------------------------------+
int ProfitPosition()
{
double bid;
double ask;
double point = MarketInfo(OrderSymbol(),MODE_POINT);
double profit = 0;
if (OrderType() == OP_BUY)
{
bid = MarketInfo(OrderSymbol(),MODE_BID);
profit = (bid - OrderOpenPrice()) / point;
}
if (OrderType() == OP_SELL)
{
ask = MarketInfo(OrderSymbol(),MODE_ASK);
profit = (OrderOpenPrice() - ask) / point;
}
return(MathRound(profit));
}
//+------------------------------------------------------------------+
int StopLossInPoint()
{
double point = MarketInfo(OrderSymbol(),MODE_POINT);
double sl = 0;
if (OrderType() == OP_BUY) sl = (OrderStopLoss() - OrderOpenPrice()) / point;
if (OrderType() == OP_SELL) sl = (OrderOpenPrice() - OrderStopLoss()) / point;
if (OrderStopLoss() == 0.0) sl = -OrderOpenPrice() / point;
return(MathRound(sl));
}
Comments