Trailing Stop v_tdavid1
Download scriptThis automated script is designed to manage open trading positions by dynamically adjusting your "Stop Loss" levels. Its primary goal is to protect your profits as a trade moves in your favor, ensuring that if the market suddenly turns against you, you exit the trade with a guaranteed minimum gain rather than letting a winning trade turn into a loss.
How the Logic Works
The script constantly monitors all active trades on your trading platform. It specifically looks for positions that meet three specific criteria before it takes any action:
- Market Alignment: It identifies trades occurring on the specific currency pair or asset you are currently viewing.
- Profit Threshold: It ensures the trade is actually in a profitable state. It won't move your stops unless the trade has already moved into positive territory.
- Distance Requirement: It checks if the price movement is significant enough to justify a trailing stop (based on the "TrailingStop" value you set).
Dynamic Protection Mechanism
Once a trade meets the conditions above, the script performs the following logic:
- For Buy Positions: As the market price rises, the script follows the price upward. It automatically moves your Stop Loss higher so that it stays at a fixed distance behind the current market price. If the price drops back down to that "trailing" line, the trade closes automatically.
- For Sell Positions: As the market price falls, the script moves your Stop Loss downward. Similarly, it maintains a consistent gap between the current price and your protection level. If the price bounces back up to hit that line, the trade is closed.
Key Features for the Trader
- Automatic Locking: You don't have to manually move your stop loss every time the market fluctuates. The script does this in real-time, "locking in" gains as they happen.
- Safety First: By only activating once a trade is profitable and has moved a certain distance, the script avoids interfering with trades that are still in their early development phase.
- Consistent Execution: The script ensures that your protection level remains consistent, providing a disciplined way to manage risk without emotional interference.
/*-----------------------------+
| |
| Shared by www.Aptrafx.com |
| |
+------------------------------*/
//+------------------------------------------------------------------+
//| Trailing Stop v_tdavid.mq4 |
//| transport_david , David W Honeywell |
//| transport.david@gmail.com |
//+------------------------------------------------------------------+
#property copyright "transport_david , David W Honeywell"
#property link "transport.david@gmail.com"
extern int MagicNumber = 123;
extern int TrailingStop = 15;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
int cnt,total=OrdersTotal();
for(cnt = total-1; cnt >= 0; cnt--)
{
RefreshRates();
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if ((OrderSymbol() == Symbol()) ) //&& (OrderMagicNumber() == MagicNumber))
{
if ((TrailingStop > 0) &&
(MathAbs(OrderClosePrice() - OrderOpenPrice()) > TrailingStop*Point) &&
(OrderProfit() > 0.00))
{
if ( ((OrderType() == OP_BUY) && (OrderClosePrice() - TrailingStop*Point) > OrderStopLoss()) ||
((OrderStopLoss() == 0) && (OrderClosePrice() - TrailingStop*Point) >= OrderOpenPrice()) )
OrderModify(OrderTicket(),OrderOpenPrice(),OrderClosePrice()-TrailingStop*Point,OrderTakeProfit(),0,White);
if ( ((OrderType() == OP_SELL) && (OrderClosePrice() + TrailingStop*Point) < OrderStopLoss()) ||
((OrderStopLoss() == 0) && (OrderClosePrice() + TrailingStop*Point) <= OrderOpenPrice()) )
OrderModify(OrderTicket(),OrderOpenPrice(),OrderClosePrice()+TrailingStop*Point,OrderTakeProfit(),0,White);
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments