i_dayma_v1_1
Download scriptThis tool is designed to provide traders with a specialized Moving Average (MA) indicator that calculates averages based on the number of trading days elapsed, rather than just a fixed number of price bars. In standard technical analysis, indicators often count every single candle on a chart; however, this script adjusts its logic to account for "calendar time" to better reflect real-world trading sessions.
Core Functionality
Dynamic Day Counting The primary logic of the script revolves around identifying how many actual days have passed over a specific period. Instead of simply looking back at the last 20 or 50 candles (which might represent three days of trading or ten, depending on market volatility), the script scans the price history to count how many distinct calendar days are contained within a lookback window.
Time-Based Analysis To achieve this, the script performs a continuous check of the timestamps on each candle. It calculates a timeframe based on the user's settings and iterates through the price data. If it detects a change in the date—meaning a new day has started—it increments a counter. Once it identifies the total number of days in that range, it uses that specific count to calculate the Moving Average.
Smart Period Validation To ensure accuracy, the script includes a safety check regarding the chart's timeframe. It is designed to work on intraday charts and will provide an alert if used on high-level timeframes (specifically those larger than a Daily chart). This ensures that the "day counting" logic remains meaningful and doesn't break when the data resolution is too broad.
Visual Output The result of this calculation is plotted directly on your main trading chart as a magenta line. Because it uses day-based counts, the resulting line is intended to be more reflective of daily price trends, smoothing out the noise of individual candles while maintaining a connection to the actual passage of time.
Summary of How It Works for the Trader
- Input: You define how many days back you want the average to consider and which type of Moving Average (method) you prefer.
- Processing: The script looks at your current chart, counts every unique day that has passed in that timeframe, and determines the "true" count of trading days.
- Result: It draws a line on your chart representing the average price over that specific number of days, helping you see the broader trend more clearly than a standard candle-based moving average might provide.
//+------------------------------------------------------------------+
//| DayMA.mq4 |
//| Copyright 2010-2015, Excstrategy |
//| http://www.ExcStrategy.ru |
//+------------------------------------------------------------------+
#property copyright "ExcStrategy"
#property link "http://www.ExcStrategy.ru"
#property version "1.1"
#property description "Moving average"
#property strict
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Magenta
#property indicator_width1 1
//---- input parameters
extern int Ma_shift=0;
extern int Ma_method=1;
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,"MA");
//---
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]=iMA(NULL,0,barsday,Ma_method,Ma_shift,Applied_price,i);
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments