e-TSAtr_NRTR_Last
Download scriptThis script is designed to automate the management of open trades by dynamically adjusting "Stop Loss" levels. Its primary purpose is to protect your profits and limit potential losses as the market moves in your favor, using a logic known as trailing.
The tool offers two distinct modes for determining how it calculates these safety boundaries:
Core Logic Modes
Users can choose between two different calculation methods to determine how far behind the current price the protection should sit:
- Mode 1 (Volatility-Based): This mode calculates the "Average True Range" (ATR), which measures how much the price typically fluctuates over a specific period. The script uses this average movement to set your safety distances. It creates a buffer based on market volatility; if the market is moving wildly, the buffer widens, and if it is calm, the buffer tightens.
- Mode 2 (Range-Based): This mode looks at the highest high and lowest low prices over a recent set of bars (the "range"). It calculates the distance between these two extremes. The trailing logic then uses this specific price range to determine your stop loss distances. This method is designed to adapt to the physical boundaries of recent price action.
Operational Behavior
Regardless of which mode is selected, the script performs the following automated actions:
- Initial Protection: When a new position is opened without a pre-set stop loss, the script automatically calculates and places an initial safety level based on the chosen logic.
- Dynamic Trailing: As the price moves favorably (e.g., the price goes up for a Buy order or down for a Sell order), the script constantly monitors the current market peak. If the price moves far enough to clear a specific threshold, the script automatically "drags" the stop loss upward (or downward) to lock in a portion of the profit.
- Real-Time Monitoring: The script scans all open positions on your current chart. Every time it detects that the market has moved into a new territory that justifies a tighter stop loss, it updates the order immediately and provides an audible alert.
- On-Screen Information: To keep you informed, the script displays a live dashboard in the corner of your trading chart. This shows the current version in use, the calculated volatility (ATR), the current price range, and the exact numerical values being used for your Stop Loss and Trailing distances at that very moment.
//+------------------------------------------------------------------+
//| e-TSATR_NRTR_Last.mq4 |
//| Copyright © 2005, Al (idea from Oleg VS) |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
/*
Äîáàâëåíà íîâàÿ âñòîðîåííàÿ âåðñèÿ òðåéëèíãîâàíèÿ, êîòîðàÿ çàäà¸òñÿ ïîëüçîâàòåëüñêîé ïåðåìåííîé Vts.
Ïðè óñòàíîâêå Vts=1 ýêñïåðò òðåéëèíãóåò ñòîïû, êàê è â ïðåæíåé âåðñèè, â çàâèñèìîñòè îò çíà÷åíèÿ ATR íà
ïåðâîì áàðå(íóëåâîé áàð â ðàññ÷åò íå ïðèíèìàåòñÿ). Ïðè óñòàíîâêå Vts=2 ñòîï òðåéëèíãóåòñÿ îò ïîñëåäíèõ
ýêñòðåìóìîâ öåíû íà ðàññòîÿíèè àìïëèòóäû(øèðèíû äèàïàçîíà) íà çàäàííîì ïåðèîäå(ïî óìîë÷àíèþ Prange=5).
Ïðèìåíåíèå òàêîãî ñïîñîáà òðåéëèíãîâàíèÿ ñòîïà èñïûòàíî â ðåàëüíîì âðåìåíè è ïðîâåðåíî ïóò¸ì ïîäñòàíîâêè
â ðàçëè÷íûå ýêñïåðòû è ñèñòåìû.  ðåçóëüòàòå òåñòîâ âûÿâëåíî óâåëè÷åíèå ïðîôèòíîñòè íà 15-25% â ðàçëè÷íûõ
ýêñïåðòàõ. Âñå çàäàííûå ïàðàìåòðû è òåêóùèå ðàññ÷åòíûå çíà÷åíèÿ êîììåíòèðóþòñÿ â ëåâîì âåðõíåì óãëó ãðàôèêà.
*/
extern double Patr=9;
extern double Prange=5;
extern double Kstop=1.5;
extern double kts=2;
extern double Vts=2;
double cnt=0,ValATR=0,hi=0,lo=0,SL=0,TS=0,prevBars=0;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
int i,mode,ticket,total;
if (prevBars!=Bars)
{
ValATR=0;
for(i=1; i<=Patr; i++) { if(i<=Patr) { ValATR+=High[i]-Low[i]; } }
ValATR=ValATR/Patr;
hi=High[Highest(NULL,0,MODE_HIGH,Prange,Prange)];
lo=Low[Lowest(NULL,0,MODE_LOW,Prange,Prange)];
if (Vts==1) {TS=kts*ValATR; SL=Kstop*ValATR;}
if (Vts==2) {TS=(hi-lo); SL=Kstop*(hi-lo);}
prevBars = Bars;
}
if (Vts<1 || Vts>2) return(0);
//-------------------------------------------------------
for (cnt=0; cnt<=OrdersTotal(); cnt++)
{
OrderSelect(cnt,SELECT_BY_POS);
mode=OrderType();
if(OrderSymbol()==Symbol())
{
//First Stop---------------------------------
if (mode==OP_BUY && OrderStopLoss()== 0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Low[0]-SL,OrderTakeProfit(),0,CLR_NONE);
PlaySound("expert.wav");
return(0);
}
if (mode==OP_SELL && OrderStopLoss()==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),High[0]+SL,OrderTakeProfit(),0,CLR_NONE);
PlaySound("expert.wav");
return(0);
}
//Main Trailing-------------------------------
if ((mode==OP_BUY && High[0]-OrderOpenPrice()>TS && OrderStopLoss()<High[0]-TS) || OrderStopLoss()==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),High[0]-TS,OrderTakeProfit(),0,CLR_NONE);
PlaySound("expert.wav");
return(0);
}
if ((mode==OP_SELL && OrderOpenPrice()-Low[0]>TS && OrderStopLoss()>Low[0]+TS) || OrderStopLoss()==0)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Low[0]+TS,OrderTakeProfit(),0,CLR_NONE);
PlaySound("expert.wav");
return(0);
}
}
}
Comment("Versia: ",Vts,"\n",
"Per_ATR: ",Patr,"\n",
"Per_Range: ",Prange,"\n",
"Range: ",(High[0]-Low[0]),"\n",
"ATR: ",ValATR,"\n",
"SL: ",SL,"\n",
"TS: ",TS);
return(0);
}
Comments