KillLoss
Download scriptThis script is designed as a dynamic risk management tool for MetaTrader 4, specifically created to manage "Stop Loss" levels in real-time. Its primary purpose is to react to market price movements when they begin to move against an open position, effectively attempting to mitigate losses by moving the stop loss in the opposite direction of the price trend.
Core Logic and Mechanism
The script functions by monitoring the distance between the current market price and your active Stop Loss. It operates based on three main components:
1. Dynamic Tracking The tool identifies a specific trade using its unique ticket number. Once a trade is selected, it constantly calculates the "gap" between the current price and the Stop Loss. If the market moves toward your Stop Loss (increasing your potential loss), the script detects this change immediately.
2. The Acceleration Coefficient (dSpeedCoeff)
This is the primary setting that determines how aggressively the script reacts:
- Neutral Response (Coefficient = 1): If set to 1, the script moves the Stop Loss by the exact same amount that the price has moved against you. It essentially tries to maintain a constant distance between the price and the stop level.
- Aggressive Defense (Coefficient > 1): If the value is higher than 1, the Stop Loss will move faster than the market price. For example, if the price moves 3 points toward your loss, a coefficient of 2.0 would cause the script to attempt to move the Stop Loss by 6 points. This "pulls" the protection further away from the danger zone more rapidly.
- Conservative Adjustment (Coefficient < 1): If set below 1, the Stop Loss moves slower than the price movement, providing a less drastic reaction to market volatility.
3. Safety and Validation The script includes built-in safeguards to ensure stability:
- Execution Limits: It checks if the move is mathematically possible based on the broker's "Stop Level" (the minimum distance required by the exchange). If a requested move would place the Stop Loss too close to the current price, the script will skip that specific update to avoid an error.
- Directional Integrity: The logic specifically looks for instances where the loss is growing. It ignores movements that favor your position, ensuring that the protection only activates when necessary.
Summary of Behavior
In simple terms, this tool acts like a "reactive shield." Instead of a static Stop Loss that sits in one place, this script creates a moving boundary. As the market pushes against you, the script pushes the boundary back—either at the same speed as the market or faster—depending on how you configure the acceleration coefficient.
//+------------------------------------------------------------------+
//| KillLoss.mq4 |
//| I_D |
//| http://www.mymmk.com/ |
//+------------------------------------------------------------------+
#property copyright "I_D"
#property link "http://www.mymmk.com/ Ñîôò äëÿ óïðàâëåíèÿ êàïèòàëîì"
extern int iTicket; // óíèêàëüíûé íîìåð (òèêåò) îòêðûòîé ïîçèöèè
extern double dSpeedCoeff = 1; // êîýôôèöèåíò óñèëåíèÿ, îïðåäåëÿþùèé, êàê áûñòðî äâèæåòñÿ ñòîïëîññ íàâñòðå÷ó êóðñó â çîíå ëîññîâ
//+------------------------------------------------------------------+
//| ÒÐÅÉËÈÍÃ KillLoss |
//| Ïðèìåíÿåòñÿ íà ó÷àñòêå ëîññîâ. Ñóòü: ñòîïëîññ äâèæåòñÿ íàâñòðå÷ó |
//| êóðñó ñî ñêîðîñòüþ äâèæåíèÿ êóðñà õ êîýôôèöèåíò (dSpeedCoeff). |
//| Ïðè ýòîì êîýôôèöèåíò ìîæíî "ïðèâÿçàòü" ê ñêîðîñòè óâåëè÷åíèÿ |
//| óáûòêà - òàê, ÷òîáû ïðè áûñòðîì ðîñòå ëîññà ïîòåðÿòü ìåíüøå. Ïðè |
//| êîýôôèöèåíòå = 1 ñòîïëîññ ñðàáîòàåò ðîâíî ïîñðåäèíå ìåæäó óðîâ- |
//| íåì ñòîïëîññà è êóðñîì íà ìîìåíò çàïóñêà ôóíêöèè, ïðè êîýôô.>1 |
//| òî÷êà âñòðå÷è êóðñà è ñòîïëîññà áóäåò ñìåùåíà â ñòîðîíó èñõîä- |
//| íîãî ïîëîæåíèÿ êóðñà, ïðè êîýôô.<1 - íàîáîðîò, áëèæå ê èñõîäíî- |
//| ìó ñòîïëîññó. |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
KillLoss(iTicket,dSpeedCoeff);
return(0);
}
//+------------------------------------------------------------------+
void KillLoss(int iTicket,double dSpeedCoeff)
{
// ïðîâåðÿåì ïåðåäàííûå çíà÷åíèÿ
if ((iTicket==0) || (!OrderSelect(iTicket,SELECT_BY_TICKET)) || (dSpeedCoeff<0.1))
{
Print("Òðåéëèíã ôóíêöèåé KillLoss() íåâîçìîæåí èç-çà íåêîððåêòíîñòè çíà÷åíèé ïåðåäàííûõ åé àðãóìåíòîâ.");
return(0);
}
double dStopPriceDiff; // ðàññòîÿíèå (ïóíêòîâ) ìåæäó êóðñîì è ñòîïëîññîì
double dToMove; // êîë-âî ïóíêòîâ, íà êîòîðîå ñëåäóåò ïåðåìåñòèòü ñòîïëîññ
// òåêóùèé êóðñ
double dBid = MarketInfo(OrderSymbol(),MODE_BID);
double dAsk = MarketInfo(OrderSymbol(),MODE_ASK);
// òåêóùåå ðàññòîÿíèå ìåæäó êóðñîì è ñòîïëîññîì
if (OrderType()==OP_BUY) dStopPriceDiff = dBid - OrderStopLoss();
if (OrderType()==OP_SELL) dStopPriceDiff = (OrderStopLoss() + MarketInfo(OrderSymbol(),MODE_SPREAD)*MarketInfo(OrderSymbol(),MODE_POINT)) - dAsk;
// ïðîâåðÿåì, åñëè òèêåò != òèêåòó, ñ êîòîðûì ðàáîòàëè ðàíüøå, çàïîìèíàåì òåêóùåå ðàññòîÿíèå ìåæäó êóðñîì è ñòîïëîññîì
if (GlobalVariableGet("zeticket")!=iTicket)
{
GlobalVariableSet("sldiff",dStopPriceDiff);
GlobalVariableSet("zeticket",iTicket);
}
else
{
// èòàê, ó íàñ åñòü êîýôôèöèåíò óñêîðåíèÿ èçìåíåíèÿ êóðñà
// íà êàæäûé ïóíêò, êîòîðûé ïðîõîäèò êóðñ â ñòîðîíó ëîññà,
// ìû äîëæíû ïåðåìåñòèòü ñòîïëîññ åìó íà âñòðå÷ó íà dSpeedCoeff ðàç ïóíêòîâ
// (íàïðèìåð, åñëè ëîññ óâåëè÷èëñÿ íà 3 ïóíêòà çà òèê, dSpeedCoeff = 1.5, òî
// ñòîïëîññ ïîäòÿãèâàåì íà 3 õ 1.5 = 4.5, îêðóãëÿåì - 5 ï. Åñëè ïîäòÿíóòü íå
// óäà¸òñÿ (ñëèøêîì áëèçêî), íè÷åãî íå äåëàåì.
// êîë-âî ïóíêòîâ, íà êîòîðîå ïðèáëèçèëñÿ êóðñ ê ñòîïëîññó ñ ìîìåíòà ïðåäûäóùåé ïðîâåðêè (òèêà, ïî èäåå)
dToMove = (GlobalVariableGet("sldiff") - dStopPriceDiff) / MarketInfo(OrderSymbol(),MODE_POINT);
// çàïèñûâàåì íîâîå çíà÷åíèå, íî òîëüêî åñëè îíî óìåíüøèëîñü
if (dStopPriceDiff<GlobalVariableGet("sldiff"))
GlobalVariableSet("sldiff",dStopPriceDiff);
// äàëüøå äåéñòâèÿ íà ñëó÷àé, åñëè ðàññòîÿíèå óìåíüøèëîñü (ò.å. êóðñ ïðèáëèçèëñÿ ê ñòîïëîññó, óáûòîê ðàñòåò)
if (dToMove>0)
{
// ñòîïëîññ, ñîîòâåòñòâåííî, íóæíî òàêæå ïåðåäâèíóòü íà òàêîå æå ðàññòîÿíèå, íî ñ ó÷åòîì êîýôô. óñêîðåíèÿ
dToMove = MathRound(dToMove * dSpeedCoeff) * MarketInfo(OrderSymbol(),MODE_POINT);
// òåïåðü ïðîâåðèì, ìîæåì ëè ìû ïîäòÿíóòü ñòîïëîññ íà òàêîå ðàññòîÿíèå
if (OrderType()==OP_BUY)
{
if (dBid - (OrderStopLoss() + dToMove)>MarketInfo(OrderSymbol(),MODE_STOPLEVEL)* MarketInfo(OrderSymbol(),MODE_POINT))
OrderModify(iTicket,OrderOpenPrice(),OrderStopLoss() + dToMove,OrderTakeProfit(),OrderExpiration());
}
if (OrderType()==OP_SELL)
{
if ((OrderStopLoss() - dToMove) - dAsk>MarketInfo(OrderSymbol(),MODE_STOPLEVEL) * MarketInfo(OrderSymbol(),MODE_POINT))
OrderModify(iTicket,OrderOpenPrice(),OrderStopLoss() - dToMove,OrderTakeProfit(),OrderExpiration());
}
}
}
}
//+------------------------------------------------------------------+
Comments