i_dayatr_v1_1

Download script
Author: ExcStrategy
i_dayatr_v1_1

This tool is designed to help traders understand market volatility by calculating a specific type of moving average known as the Average True Range (ATR). Unlike standard indicators that might look at a fixed number of price candles (like the last 14 minutes or hours), this script is engineered to adapt to actual calendar days.

Core Functionality

The primary goal of this logic is to measure how much an asset typically moves during a specific timeframe. Instead of treating every candle as equal, the script intelligently identifies the transition between different calendar days. It then calculates the average range based on the number of full days that have passed within your chosen window.

Key Features and Logic:

  • Dynamic Day Counting: The script automatically scans the price history to determine how many distinct days exist within the calculation period. By detecting when one day ends and another begins, it ensures the "Average" part of the calculation is consistent with real-world time rather than just a count of bars on a chart.
  • User-Defined Window: You can customize the length of the calculation. The script takes your input (the number of days) and adjusts the analysis window accordingly to provide a relevant view of recent volatility.
  • Timeframe Protection: To ensure accuracy, the logic includes a safety check. It prevents the tool from being used on extremely large timeframes (anything larger than one day). This ensures that the data remains granular enough for the script to accurately identify daily transitions and provide meaningful volatility readings.
  • Visual Representation: The resulting data is plotted as a smooth line in a separate window below your main chart. This allows you to observe whether the market is becoming more volatile (higher lines) or calmer (lower lines) over time.

How it Benefits the Trader

By providing a "Daily" context to volatility, this script helps traders set more realistic take-profit and stop-loss levels. Because it calculates based on actual days, the information remains consistent whether you are viewing a 15-minute chart or an hourly chart, helping you gauge how much the price is likely to swing in a typical daily session.

Indicators Used
Indicator of the average true range
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screen
11 Views
0 Downloads
0 Favorites
i_dayatr_v1_1
//+------------------------------------------------------------------+
//|                                                       DayATR.mq4 |
//|                                 Copyright 2010-2015, Excstrategy |
//|                                        http://www.ExcStrategy.ru |
//+------------------------------------------------------------------+
#property copyright "ExcStrategy"
#property link      "http://www.ExcStrategy.ru"
#property version   "1.1"
#property description "Average True Range"
#property strict
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue
#property indicator_width1 1  
//----
extern int DaysForCalculation=2;
//---- buffers
double Buffer1[];
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(1);
//---- indicator lines
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,Buffer1);
//---- 
   SetIndexLabel(0,"ATR");
//----
   DaysForCalculation=DaysForCalculation+1;
//----    
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   if(Period()>1400)
     {
      Alert("Error! Period can not be greater than D1");
      return(0);
     }
//----
   int counted_bars=IndicatorCounted();
   int barsday;
   datetime Time1=Time[0],Time2;
//----
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;
//----
   for(int i=0; i<limit; i++)
     {
      //----
      barsday=0;
      //----
      Time2=Time[i]+(1440*60*DaysForCalculation);
      if(Time1<Time2)
         if(i<Bars-MathRound(1500/Period()))
            for(int a=i; a<i+1441; a++)
              {
               //----
               barsday++;
               //----
               if(TimeDayOfYear(Time[a])!=TimeDayOfYear(Time[a+1])) a=i+1442;
              }
      //----
      Buffer1[i]=iATR(NULL,0,barsday,i);
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments