StopATR_auto
Download scriptThis script is a specialized trading tool designed to provide traders with dynamic support and resistance levels—specifically "Stop Levels"—that adjust automatically based on the prevailing market volatility.
Core Logic Overview
Unlike static indicators that use fixed numbers, this tool calculates price boundaries by analyzing how much the market has been moving over a specific period of time. It identifies the "noise" or "swing" of the market and projects potential exit points or trend boundaries above and below the current price.
Key Components of the Logic
1. Volatility Measurement (The Moving Average of Price Range) The script looks back at a predefined number of past candles (set by the user as the average period). It calculates the difference between the highest and lowest prices for each of these candles to determine the "average range" of the market movement. This ensures that if the market becomes very volatile, the levels widen, and if the market goes quiet, the levels tighten.
2. Dynamic Level Calculation The script then projects two specific lines on your chart:
- Upper Stop Level: Calculated by taking the current opening price and adding a multiplier of the average volatility (plus the current market spread).
- Lower Stop Level: Calculated by taking the current opening price and subtracting that same multiplier of the average volatility.
3. Visual Real-Time Updates The tool is designed to be highly interactive. It continuously updates two horizontal lines on your chart in real-time:
- Visual Lines: These provide a clear, constant horizontal reference for where the current technical "boundary" sits.
- Dynamic Labels: The script places text labels near the price action. These labels not only show the exact price of the boundary but also display the specific distance (in points/pips) from the current opening price. This allows a trader to see at a glance how much "room" the market has before hitting a volatility-based limit.
Summary for the Trader
Essentially, this tool acts as a smart safety net. Instead of guessing where a price might reverse or where a stop-loss should be placed based on a whim, it uses the actual math of the market's recent behavior to suggest these levels. It tells you: "Based on how much the price has been swinging lately, here is the mathematically adjusted boundary for your current position."
//+------------------------------------------------------------------+
//| Ðàññòàâëÿåò óðîâíè ñòîïîâ â çàâèñèìîñòè îò öåíû è ATR +
//| Copyright © 2004, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Below & Beloved"
#property link "http://www.metaquotes.net/"
//----
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Lime
#property indicator_color2 Aqua
#property indicator_color3 Red
//---- input parameters
//extern int ATRPeriod=5;
//extern int MA_Type_0_3=1;
//extern double Risk_0_5=3;
extern color ColorUpperStop=Blue;
extern color ColorLowerStop=Brown;
extern bool ShowGraf=true;
extern int CountBarsForShift=12;
extern int CountBarsForAverage=77;
extern double Target=2.5;
//extern int Count=200;
//---- indicator buffers
double MA[], MAUp[], MADn[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- line shifts when drawing
SetIndexShift(0,0);
//---- first positions skipped when drawing
IndicatorShortName("ATRAuto("+CountBarsForAverage+","+Target+")");
SetIndexDrawBegin(0,CountBarsForAverage);
SetIndexDrawBegin(1,CountBarsForAverage);
SetIndexDrawBegin(2,CountBarsForAverage);
//---- 3 indicator buffers mapping
SetIndexBuffer(0,MA);
SetIndexBuffer(1,MAUp);
SetIndexBuffer(2,MADn);
//---- drawing settings
SetIndexStyle(0,DRAW_NONE);
if (ShowGraf)
{
SetIndexStyle(1,DRAW_LINE,EMPTY,EMPTY, ColorUpperStop);
SetIndexStyle(2,DRAW_LINE,EMPTY,EMPTY, ColorLowerStop);
}
else
{
SetIndexStyle(1,DRAW_NONE,EMPTY,EMPTY, ColorUpperStop);
SetIndexStyle(2,DRAW_NONE,EMPTY,EMPTY, ColorLowerStop);
}
//---- index labels
SetIndexLabel(0,"MAX("+CountBarsForAverage+","+Target+")");
SetIndexLabel(1,"MAXUp("+CountBarsForAverage+","+Target+")");
SetIndexLabel(2,"MAXDn("+CountBarsForAverage+","+Target+")");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int deinit()
{
ObjectDelete("StopUpTxt");
ObjectDelete("StopDnTxt");
ObjectDelete("StopUpLine");
ObjectDelete("StopDnLine");
//ObjectDelete("Average");
return(0);
}
//+------------------------------------------------------------------+
//| Bill Williams' Alligator |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars=IndicatorCounted(), i, y, CountAverage;
double Spread=Ask-Bid;//MathPow(10,-MarketInfo(Symbol(),MODE_SPREAD));
double CandleSum=0, CandleAverage=0 ;
//if (MA_Type_0_3<0 || MA_Type_0_3>4) MA_Type_0_3=0;
//if (Risk_0_5>6) Risk_0_5=0;
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//-----
ObjectCreate("StopUpLine", OBJ_HLINE, 0, Time[0],Open[0]);
ObjectSet("StopUpLine", OBJPROP_STYLE, STYLE_SOLID);
ObjectSet("StopUpLine", OBJPROP_COLOR, ColorUpperStop);
ObjectCreate("StopUpTxt", OBJ_TEXT, 0, Time[0], Close[0]);
//
ObjectCreate("StopDnLine", OBJ_HLINE, 0, Time[0],Open[0]);
ObjectSet("StopDnLine", OBJPROP_STYLE, STYLE_SOLID);
ObjectSet("StopDnLine", OBJPROP_COLOR, ColorLowerStop);
ObjectCreate("StopDnTxt", OBJ_TEXT, 0, Time[0], Close[0]);
//ObjectCreate("Average", OBJ_LABEL, 0, 0,0);
//ObjectSet("Average", OBJPROP_CORNER, 3);
//ObjectSet("Average", OBJPROP_XDISTANCE, 2);
//ObjectSet("Average", OBJPROP_YDISTANCE, 2);
//---- main loop
for( i=limit; i>=0; i--)
{
CandleSum=0;
for(CountAverage=CountBarsForAverage; CountAverage>0; CountAverage--)
CandleSum+=MathAbs(High[CountAverage+i]-Low[CountAverage+i]);
CandleAverage=CandleSum/CountBarsForAverage;
//ATR=iCustom(NULL,0, "ATR", ATRPeriod,MA_Type_0_3,0,i+1);
//MA[i]=iMA(NULL,0,ATRPeriod,0, MA_Type_0_3, PRICE_OPEN,i);
//Comment(CandleAverage);
MAUp[i]=Open[i]+Target*CandleAverage+Spread;
MADn[i]=Open[i]-Target*CandleAverage;
ObjectMove("StopUpLine", 0, Time[0], MAUp[i]);
ObjectSetText("StopUpTxt", DoubleToStr(MAUp[i],Digits)+"(+"+
DoubleToStr((MAUp[i]-Open[i])*MathPow(10,Digits),0)+")", 12, "Terminal",ColorUpperStop);
ObjectMove("StopUpTxt",0, Time[0]+CountBarsForShift*60*Period(), MAUp[i]);
ObjectMove("StopUpTxt",1, Time[0]+CountBarsForShift*60*Period(), MAUp[i]);
//
ObjectMove("StopDnLine", 0, Time[0], MADn[i]);
ObjectSetText("StopDnTxt", DoubleToStr(MADn[i],Digits)+"("+
DoubleToStr((MADn[i]-Open[i])*MathPow(10,Digits),0)+")", 12, "Terminal",ColorLowerStop);
ObjectMove("StopDnTxt",0, Time[0]+CountBarsForShift*60*Period(), MADn[i]);
ObjectMove("StopDnTxt",1, Time[0]+CountBarsForShift*60*Period(), MADn[i]);
//Comment (GoUp);
}
//---- done
//Comment(Spread);
return(0);
}
//+------------------------------------------------------------------+
Comments