TrailingByShadows
Download scriptThis script is designed to automate the "trailing" of a stop-loss price for a specific open trade based on price action history, specifically focusing on the shadows (wicks) of previous candles. Instead of moving your stop-loss at fixed intervals, this tool looks at recent price fluctuations to determine where the safest "floor" or "ceiling" is located.
Core Functionality
The logic works by scanning a user-defined number of past candles to identify the most extreme price points reached during that period.
- For Buy Positions: The script looks for the lowest price point (the bottom shadow) across the specified number of recent candles.
- For Sell Positions: The script looks for the highest price point (the top shadow) across the specified number of recent candles.
Key Features and Customization
The tool allows for specific behaviors depending on how you want to manage your risk:
1. Dynamic Distance (Indent) You can set a specific "buffer" or "offset." The script won't place the stop-loss exactly at the tip of the shadow; it adds a small safety margin (in points) to ensure the trade isn't closed by minor price noise.
2. Profit Protection vs. Loss Recovery The script includes a toggle that determines when it is allowed to move your stop-loss:
- Standard Mode: The stop-loss will only be moved if the new calculated level is better than your current position and is already in profit (above your entry price for buys, or below for sells). This helps lock in gains as the trade moves in your favor.
- Loss Trailing Mode: If enabled, the script will also move the stop-loss even if the trade is currently in a losing position, provided it is moving toward a better "safe zone" based on the shadows.
3. Timeframe Awareness The logic is designed to be compatible with standard trading timeframes (ranging from 1 minute up to 43,200 minutes), ensuring that the "shadows" it analyzes are relevant to the speed of your trading strategy.
How it Operates Automatically
Once a specific trade ticket is identified, the script constantly monitors the market. Every time a new price extreme is formed within your chosen candle window, the script checks if moving the stop-loss would be beneficial based on your settings. If the conditions are met—meaning the new level is safer than the current one and respects the broker's minimum movement rules—it automatically updates the order for you.
//+------------------------------------------------------------------+
//| TrailingByShadows.mq4 |
//| I_D |
//| http://www.mymmk.com/ |
//+------------------------------------------------------------------+
#property copyright "I_D"
#property link "http://www.mymmk.com/"
extern int iTicket; // óíèêàëüíûé íîìåð (òèêåò) îòêðûòîé ïîçèöèè
extern int iTmfrm; // ïåðèîä, ïî áàðàì êîòîðîãî ñëåäóåò òðàëèòü (1, 5, 15, 30, 60, 240, 1440, 10080, 43200)
extern int iBars_n = 3; // êîë-âî áàðîâ, ïî êîòîðûì ñëåäóåò òðàëèòü
extern int iIndent = 3; // îòñòóï îò òåíè áàðà, íà êîòîðîì ðàçìåùàåòñÿ ñòîïëîññ
extern bool bTrlinloss = false; // ñëåäóåò ëè òðàëèòü íà ó÷àñòêå ëîññîâ (ìåæäó êóðñîì ñòîïëîññà è îòêðûòèÿ)
//+------------------------------------------------------------------+
//| ÒÐÅÉËÈÍà ÏÎ ÒÅÍßÌ N ÑÂÅ×ÅÉ |
//| Ïðè çàïóñêå ýêñïåðòà åìó íåîáõîäèìî óêàçàòü óíèêàëüíûé íîìåð |
//| (òèêåò) îòêðûòîé ïîçèöèè, à òàêæå îïðåäåëèòü ïàðàìåòðû òðåéëèíãà:|
//| êîëè÷åñòâî áàðîâ, ïî òåíÿì êîòîðûõ íåîáõîäèìî òðåéëèíãîâàòü |
//| (îò 1 è áîëüøå) è îòñòóï (ïóíêòîâ) - ðàññòîÿíèå îò ìàêñ. (ìèí.) |
//| ñâå÷è, íà êîòîðîå ïåðåíîñèòñÿ ñòîïëîññ (îò 0), ïåðåêëþ÷àòåëü |
//| trlinloss óêàçûâàåò, ñëåäóåò ëè òðåéëèíãîâàòü íà ó÷àñòêå |
//| "ñòîïëîññ-êóðñ îòêðûòèÿ ïîçèöèè. |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
TrailingByShadows(iTicket,iTmfrm,iBars_n,iIndent,bTrlinloss);
return(0);
}
//+------------------------------------------------------------------+
void TrailingByShadows(int ticket,int tmfrm,int bars_n, int indent,bool trlinloss)
{
int i; // counter
double new_extremum;
// ïðîâåðÿåì ïåðåäàííûå çíà÷åíèÿ
if ((bars_n<1) || (indent<0) || (ticket==0) || ((tmfrm!=1) && (tmfrm!=5) && (tmfrm!=15) && (tmfrm!=30) && (tmfrm!=60) && (tmfrm!=240) && (tmfrm!=1440) && (tmfrm!=10080) && (tmfrm!=43200)) || (!OrderSelect(ticket,SELECT_BY_TICKET)))
{
Print("Òðåéëèíã ôóíêöèåé TrailingByShadows() íåâîçìîæåí èç-çà íåêîððåêòíîñòè çíà÷åíèé ïåðåäàííûõ åé àðãóìåíòîâ.");
return(0);
}
// åñëè äëèííàÿ ïîçèöèÿ (OP_BUY), íàõîäèì ìèíèìóì bars_n ñâå÷åé
if (OrderType()==OP_BUY)
{
for(i=1;i<=bars_n;i++)
{
if (i==1) new_extremum = iLow(Symbol(),tmfrm,i);
else
if (new_extremum>iLow(Symbol(),tmfrm,i)) new_extremum = iLow(Symbol(),tmfrm,i);
}
// åñëè òðàëèì è â çîíå óáûòêîâ
if (trlinloss==true)
{
// åñëè íàéäåííîå çíà÷åíèå "ëó÷øå" òåêóùåãî ñòîïëîññà ïîçèöèè, ïåðåíîñèì
if ((((new_extremum - indent*Point)>OrderStopLoss()) || (OrderStopLoss()==0)) && (new_extremum - indent*Point<Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point))
if (!OrderModify(ticket,OrderOpenPrice(),new_extremum - indent*Point,OrderTakeProfit(),OrderExpiration()))
Print("Íå óäàëîñü ìîäèôèöèðîâàòü îðäåð ¹",OrderTicket(),". Îøèáêà: ",GetLastError());
}
else
{
// åñëè íîâûé ñòîïëîññ íå òîëüêî ëó÷øå ïðåäûäóùåãî, íî è êóðñà îòêðûòèÿ ïîçèöèè
if ((((new_extremum - indent*Point)>OrderStopLoss()) || (OrderStopLoss()==0)) && ((new_extremum - indent*Point)>OrderOpenPrice()) && (new_extremum - indent*Point<Bid-MarketInfo(Symbol(),MODE_STOPLEVEL)*Point))
if (!OrderModify(ticket,OrderOpenPrice(),new_extremum-indent*Point,OrderTakeProfit(),OrderExpiration()))
Print("Íå óäàëîñü ìîäèôèöèðîâàòü îðäåð ¹",OrderTicket(),". Îøèáêà: ",GetLastError());
}
}
// åñëè êîðîòêàÿ ïîçèöèÿ (OP_SELL), íàõîäèì ìèíèìóì bars_n ñâå÷åé
if (OrderType()==OP_SELL)
{
for(i=1;i<=bars_n;i++)
{
if (i==1) new_extremum = iHigh(Symbol(),tmfrm,i);
else
if (new_extremum<iHigh(Symbol(),tmfrm,i)) new_extremum = iHigh(Symbol(),tmfrm,i);
}
// åñëè òðàëèì è â çîíå óáûòêîâ
if (trlinloss==true)
{
// åñëè íàéäåííîå çíà÷åíèå "ëó÷øå" òåêóùåãî ñòîïëîññà ïîçèöèè, ïåðåíîñèì
if ((((new_extremum + (indent + MarketInfo(Symbol(),MODE_SPREAD))*Point)<OrderStopLoss()) || (OrderStopLoss()==0)) && (new_extremum + (indent + MarketInfo(Symbol(),MODE_SPREAD))*Point>Ask+MarketInfo(Symbol(),MODE_STOPLEVEL)*Point))
if (!OrderModify(ticket,OrderOpenPrice(),new_extremum + (indent + MarketInfo(Symbol(),MODE_SPREAD))*Point,OrderTakeProfit(),OrderExpiration()))
Print("Íå óäàëîñü ìîäèôèöèðîâàòü îðäåð ¹",OrderTicket(),". Îøèáêà: ",GetLastError());
}
else
{
// åñëè íîâûé ñòîïëîññ íå òîëüêî ëó÷øå ïðåäûäóùåãî, íî è êóðñà îòêðûòèÿ ïîçèöèè
if ((((new_extremum + (indent + MarketInfo(Symbol(),MODE_SPREAD))*Point)<OrderStopLoss()) || (OrderStopLoss()==0)) && ((new_extremum + (indent + MarketInfo(Symbol(),MODE_SPREAD))*Point)<OrderOpenPrice()) && (new_extremum + (indent + MarketInfo(Symbol(),MODE_SPREAD))*Point>Ask+MarketInfo(Symbol(),MODE_STOPLEVEL)*Point))
if (!OrderModify(ticket,OrderOpenPrice(),new_extremum + (indent + MarketInfo(Symbol(),MODE_SPREAD))*Point,OrderTakeProfit(),OrderExpiration()))
Print("Íå óäàëîñü ìîäèôèöèðîâàòü îðäåð ¹",OrderTicket(),". Îøèáêà: ",GetLastError());
}
}
}
//+------------------------------------------------------------------+
Comments