TrailingStop
Download scriptThis script is an automated trading tool designed to manage active positions by dynamically adjusting your "Stop Loss" levels as the market moves in your favor. Its primary goal is to lock in profits automatically, ensuring that if a winning trade suddenly reverses, you don't end up losing the gains you had already accumulated.
How it Works
The tool constantly monitors all currently open trades in your account. It looks for specific conditions to determine when it should step in and protect your profit:
- Profit Awareness: The script only takes action on trades that are currently in a profitable state. It will not interfere with trades that are still in a loss.
- Dynamic Distance (Trailing): You can set a specific distance (the "Trailing Stop" value) from the current market price. The script calculates this distance based on the currency's minimum price movement.
- Automatic Adjustment:
- For Buy Positions: If the price rises, the script moves your Stop Loss upward to stay a set distance behind the current price.
- For Sell Positions: If the price drops, the script moves your Stop Loss downward to stay a set distance behind the current price.
- Safety Checks: The tool is designed to respect broker rules. If you set a trailing distance that is too small for the broker to allow, the script automatically adjusts itself to the minimum allowed level to ensure the order remains valid.
Key Features for the Trader
- Persistence: The script includes a retry mechanism. If it fails to update a trade due to a momentary network glitch or price fluctuation, it will attempt to re-apply the new Stop Loss multiple times until it succeeds.
- Profit Tracking: It keeps an internal tally of how many times it has successfully moved your stop loss into a guaranteed profit zone. This helps you see how effectively the tool is securing gains during a live trading session.
- Hands-Free Management: Instead of you having to manually watch the charts and move your Stop Loss every time the price moves a few pips, this script handles the "trailing" logic automatically in real-time.
In short, this is a safety net tool. It allows you to let winning trades "breathe" while ensuring that as the price moves into your favor, your profit is continuously locked in behind it.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| Universal Trailing Stop.mq4 |
//| Joao Barbosa |
//| obarbosa2001@yahoo.com.br |
//+------------------------------------------------------------------+
#property copyright "João Barbosa"
#property link "obarbosa2001@yahoo.com.br"
extern int TrailingStop = 20; // Valor do Trailing Stop
int Tentativas = 10; // número de tentativas para executar a ordem
int B=0; // Contagem de acionamentos = +- quantidade de pips garantidos
// isso indica o quanto esse EA rendeu em pips em cada seçao para quem usa
int init()
{Print("Garante Lucro Universal iniciado (",TimeToStr(CurTime(),TIME_DATE)," - ",TimeToStr(CurTime(),TIME_SECONDS),") ");
start();return(0);}
int deinit()
{Print("Garante Lucro Universal finalizado (",TimeToStr(CurTime(),TIME_DATE)," - ",TimeToStr(CurTime(),TIME_SECONDS),") ");return(0);}
int start()
{Comment("Universal Trailing Stop(",TimeToStr(CurTime(),TIME_DATE)," - ",TimeToStr(CurTime(),TIME_SECONDS),") ");
for(int J = OrdersTotal()-1; J >= 0; J--)
{OrderSelect(J, SELECT_BY_POS, MODE_TRADES);
double OrderPoint=MarketInfo(OrderSymbol(),MODE_POINT);
double SLMin=MarketInfo(OrderSymbol(),MODE_STOPLEVEL);
int TS=TrailingStop;
int Mudou=Tentativas;
if (TrailingStop<SLMin) TS=SLMin;
if ( (TrailingStop>0) && (OrderProfit()>0.0) &&
(MathAbs(OrderStopLoss()-OrderClosePrice())/OrderPoint>TS) )
{ if (OrderType()==OP_SELL)
{ Print("Modificando ordem ",OrderSymbol()," SL: ", OrderClosePrice()+TS*OrderPoint);
while (Mudou>0)
{ Mudou=Mudou-1;
if (OrderModify(OrderTicket(),OrderOpenPrice(),OrderClosePrice()+TS*OrderPoint,OrderTakeProfit(),0,Red)==0) Mudou=0;
else Sleep(1000);
}
B=B+1;}
else if (OrderType()==OP_BUY)
{ Print("Modificando ordem ",OrderSymbol()," SL: ", OrderClosePrice()-TS*OrderPoint);
while (Mudou>0)
{ Mudou=Mudou-1;
if (OrderModify(OrderTicket(),OrderOpenPrice(),OrderClosePrice()-TS*OrderPoint,OrderTakeProfit(),0,Red)==0) Mudou=0;
else Sleep(1000);
}
B=B+1;}
}
}
return(0);
}
Comments