trailing_profit
Download scriptThis automated trading tool is designed to manage active market positions by dynamically protecting profits. Instead of using a fixed "Take Profit" level, it monitors the live performance of your trades and reacts to fluctuations in real-time to ensure that gains are secured once they reach a certain threshold.
Core Logic Overview
The script operates based on three primary phases: monitoring, activation, and execution.
1. Profit Monitoring
The tool continuously scans all open positions in your trading account. It calculates the total current profit (including swap rates) and displays this information directly on your trading chart. At this stage, the tool is simply "watching" the market without interfering with your trades.
2. Dynamic Trailing Activation
The script is programmed to become active once your total profit exceeds a specific pre-defined amount (the "Minimum Profit"). Once this threshold is met, the tool begins calculating a "safety net."
It determines a trailing boundary based on a percentage of your current profit. For example, if you are in a significant profit, the script establishes a point slightly below your peak. As long as your profit continues to rise, the script moves this safety net upward with you.
3. Automated Protection (The "Trailing" Mechanism)
The core logic functions as an automated exit strategy:
- Profit Growth: If the market moves in your favor and your profit increases, the tool updates its internal target to keep pace with your gains.
- Profit Decline: If the market reverses and your profit begins to drop toward that safety net (the percentage-based threshold), the script immediately intervenes.
- Execution: The moment the profit drops below this calculated safety point, the tool automatically closes all open positions.
Key Features for the User
- Dynamic Safety Net: Because it uses a percentage-based drop rather than a fixed number, it scales with your success—the higher your profit goes, the more "room" the script gives the trade to breathe, while still ensuring you don't give back your gains.
- Automated Alerts: Once the tool successfully closes your positions and secures the profit, it provides an on-screen alert notifying you of the final result.
- Real-time Feedback: The script provides constant visual updates on your chart, showing you exactly when the protection is "Off" (waiting for profit), when it is "On" (tracking your gains), and what your current maximum profit peak is.
//+------------------------------------------------------------------+
//| òðàë.mq4 |
//| Copyright © 2008, Äåì¸õèí Âèòàëèé Åâãåíüåâè÷ |
//| vitalya_1983@list.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Äåì¸õèí Âèòàëèé Åâãåíüåâè÷"
#property link "vitalya_1983@list.ru"
extern double percent_of_profit =33;
extern double minimum_profit = 1000;
double profit,profit_off,result;
bool trail_enable=false,close_start=false;
int init()
{
profit_off=0;
result = 0;
Comment ("");
return(0);
}
int start()
{
while (IsExpertEnabled())
{
Sleep (50);
RefreshRates();
//----
if (close_start)
{
for (int i=OrdersTotal();i>=1;i--)
{
OrderSelect (i-1,SELECT_BY_POS,MODE_TRADES);
{
if (OrderType()==OP_BUY)
{
double price = MarketInfo (OrderSymbol(),MODE_BID);
OrderClose (OrderTicket(),OrderLots(),price,3,0);
result = OrderProfit()+result;
}
if (OrderType()==OP_SELL)
{
price = MarketInfo (OrderSymbol(),MODE_ASK);
OrderClose (OrderTicket(),OrderLots(),price,3,0);
result = OrderProfit()+result;
}
}
}
}
for (i=OrdersTotal();i>=1;i--)
{
OrderSelect(i-1,SELECT_BY_POS,MODE_TRADES);
profit = OrderProfit()+OrderSwap()+profit;
}
if (close_start&&OrdersTotal()==0)
{
trail_enable=false;
close_start= false;
Alert ("Ñîâåòíèê çàêðûë ïîçèöèè ñ ðåçóëüòàòîì ", result);
profit_off=0;
result = 0;
}
if (!trail_enable&&OrdersTotal()!=0)
{
Comment ("Ðåæèì òðàëà âûêëþ÷åí." ,"\n","Ñîâåòíèê íà÷íåò ñîïðîâîæäàòü îðäåðà ïðè ðîñòå ïðèáûëè äî ",minimum_profit,
" òåêóùàÿ ïðèáûëü ", profit);
}
if ((profit_off==0&&minimum_profit < profit)||(profit_off!=0&&profit_off<profit-profit*(percent_of_profit/100)))
{
trail_enable=true;
profit_off=profit-profit*(percent_of_profit/100);
Comment("Ðåæèì òðàëà âêëþ÷åí." ,"\n","Ñîâåòíèê çàêðîåò îðäåðà ïðè ïàäåíèè ïðèáûëè äî ",profit_off,
" ìàêñèìàëüíàÿ ïðèáûëü ", profit);
}
if (trail_enable&&profit_off>profit)
{
close_start=true;
}
profit =0;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments