This script is designed as a specialized technical analysis tool for MetaTrader, specifically creating a variation of the Relative Strength Index (RSI). While a standard RSI typically looks at a fixed number of bars (like 14), this tool introduces a unique logic that adjusts its calculations based on actual timeframes to provide a more "calendar-aware" perspective on market momentum.
Core Logic and Functionality
Time-Based Dynamic Calculation The primary distinction of this script is how it determines the data window for its calculations. Instead of simply counting a set number of candles, the script attempts to identify and count the number of bars that occur within a specific daily timeframe. It looks back at the price history and calculates how many units of time have passed based on the user's chosen settings.
Dynamic Bar Counting The tool performs an automated scan of the market history. It iterates through the price data to detect "day changes." By identifying when one day ends and another begins, it determines the exact number of bars that constitute a period of time. This means that if you are looking at a timeframe with many candles per day (like 1-minute charts), or very few (like 4-hour charts), the script adapts to ensure the calculation remains consistent with the intended duration.
User Customization The script allows for two main points of manual input:
- Price Source: You can define which price point the indicator should monitor (e.g., the closing price, opening price, etc.).
- Calculation Period: This allows you to set the "look-back" window, determining how many days of data the script should analyze to generate its momentum reading.
Visual Representation The results are displayed in a separate window below your main chart on a scale of 0 to 100. It includes two visual "levels" (20 and 80) which act as standard benchmarks. In technical analysis, these levels typically help traders identify when a market is potentially "oversold" (low values) or "overbought" (high values).
Safety Features To ensure the logic remains accurate, the script includes a built-in restriction. It checks the current chart timeframe and will alert the user if the timeframe is set too large (specifically greater than a Daily chart). This is because the script's logic relies on detecting daily transitions; if the timeframe is larger than a day, the math required to count "days" would no longer be valid.
//+------------------------------------------------------------------+
//| DayRSI.mq4 |
//| Copyright 2010-2015, Excstrategy |
//| http://www.ExcStrategy.ru |
//+------------------------------------------------------------------+
#property copyright "ExcStrategy"
#property link "http://www.ExcStrategy.ru"
#property version "1.1"
#property description "RSI"
#property strict
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue
#property indicator_width1 1
#property indicator_minimum 0
#property indicator_maximum 100
#property indicator_level1 20.0
#property indicator_level2 80.0
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT
//----
extern int Applied_price=0;
//----
extern int DaysForCalculation=2;
//---- buffers
double Buffer1[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(1);
//---- indicator lines
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,Buffer1);
//----
SetIndexLabel(0,"RSI");
//---
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;
bool rangeday;
datetime Time1=Time[0],Time2;
//----
if(counted_bars>0) counted_bars--;
int limit=Bars-counted_bars;
//----
for(int i=0; i<limit; i++)
{
//----
rangeday= false;
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]=iRSI(NULL,0,barsday,Applied_price,i);
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments