This automated trading script is designed to manage active trades by dynamically adjusting "Stop Loss" levels based on real-time market movement. It functions as a safety mechanism, ensuring that as a trade becomes profitable, the protection level moves forward to lock in gains.
How the Logic Works
The script continuously monitors all open positions associated with the specific currency pair you are trading. It follows a clear set of rules to determine when and how to modify your existing trades:
1. Minimum Threshold Requirement Before performing any actions, the script checks if the user-defined sensitivity setting (the "TS" value) is valid. If the value is set too low (less than 5 points), the script will remain idle to prevent erratic behavior in volatile markets.
2. Profit Recognition The script looks for trades that have moved into a profitable position. Specifically:
- For Buy positions: It checks if the current market price has risen above your entry price by the amount specified in your settings.
- For Sell positions: It checks if the current market price has dropped below your entry price by that same amount.
3. Dynamic Stop-Loss Adjustment (Trailing) Once a trade is sufficiently profitable, the script evaluates the current location of your Stop Loss. If the Stop Loss is still sitting at its original position (or behind the current profit level), the script automatically moves it forward.
The new Stop Loss is placed at a fixed distance from the current market price, based on your chosen sensitivity setting. This ensures that if the market suddenly reverses direction, you keep a portion of the profit gained during the move.
Summary of Behavior
Essentially, this tool acts as a "trailing" protector. It stays quiet while your trade is in a loss or just starting out, but once the market moves in your favor, it automatically "locks in" your progress by moving your safety net forward behind the price.
//+------------------------------------------------------------------+
//| Scalping.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.scalping.nm.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.scalping.nm.ru"
//---- input parameters
extern int TS=25;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int cnt;
//----
if (TS<5)
{
Print("Ìàëåíüêèé òðåéëèíã");
return(0);
}
for(cnt=0;cnt<OrdersTotal();cnt++)
{
if (OrderSelect(cnt,SELECT_BY_POS)==true)
{
if (OrderSymbol()==Symbol())
{
if (OrderType()==OP_BUY)
{
if (Bid-OrderOpenPrice()>TS*Point)
{
if (OrderStopLoss()<Bid-TS*Point)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TS*Point,OrderTakeProfit(),0,Red);
}
}
}
if (OrderType()==OP_SELL)
{
if (Ask-OrderOpenPrice()>TS*Point)
{
if (OrderStopLoss()<Ask-TS*Point)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TS*Point,OrderTakeProfit(),0,Blue);
}
}
}
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments