^Dyn_Range
Download scriptThis script is designed for the MetaTrader 4 platform to automatically identify and plot key technical price levels on a trading chart. Specifically, it focuses on calculating "Pivot" points based on the price action of previous trading sessions.
Core Functionality
The tool analyzes historical price data to determine significant boundaries—highs and lows—and uses them to project specific support and resistance levels. Instead of providing a static line, it dynamically updates these levels as new trading days begin.
How the Logic Works
The script operates through three main processes:
1. Session Recognition The tool monitors the timeline of the market to detect the exact moment a new day begins. This allows the script to separate data between different trading sessions, ensuring that calculations for "today" are not influenced by "yesterday's" price extremes.
2. Price Point Calculation Once a new day is identified, the script looks back at the previous completed day to find its highest and lowest prices. It then uses these values, along with the previous day’s closing price, to calculate a central Pivot Point (FP).
The script offers two different mathematical formulas for this calculation:
- Standard Calculation: A balanced average of the high, low, and previous close.
- Weighted Calculation: A modified version that gives more importance to the previous day's closing price.
3. Dynamic Support and Resistance Projection Based on the calculated Pivot Point, the script projects two additional levels:
- Resistance (FR1): A target level located above the pivot.
- Support (FS1): A target level located below the pivot.
Visual Behavior
- Automatic Updating: The lines automatically reset and recalculate at the start of each new day.
- Real-Time Adjustment: For the current, active bar (the one moving in real-time), the script performs a special calculation to ensure the levels are adjusted based on the most recent price movements within that specific day.
- Compatibility: The indicator is specifically designed to function on lower timeframes (anything smaller than the 4-hour chart) to provide traders with precise, granular data.
Summary for Traders
In simple terms, this script automates the "manual" work of identifying where the market might face resistance or find support based on yesterday's range. It cleans up the chart by clearly marking these zones, helping you visualize potential turning points in price action more efficiently than doing the math by hand.
//+------------------------------------------------------------------+
//| ^Dyn_Pivot.mq4 |
//| Modest, Rosh conversed only |
//| http://forexsystems.ru/phpBB/index.php |
//+------------------------------------------------------------------+
#property copyright "Modest, Rosh conversed only"
#property link "http://forexsystems.ru/phpBB/index.php"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Blue
#property indicator_color2 Red
//---- input parameters
extern int Formula=0;
int i=1, j=1, shift=0;
double FP=0.0;
double FH=0.0, FL=0.0;
bool result;
//---- buffers
double ExtMapBuffer1[];
double FR1Buffer[];
double FS1Buffer[];
//+------------------------------------------------------------------+
//| Ïðîâåðêà íîâîãî äíÿ |
//+------------------------------------------------------------------+
bool isNewDay(int _shift)
{
//----
result=false;
if ( (TimeHour(Time[_shift])==0) && (TimeMinute(Time[_shift])==0) ) result=true;
//Print("Äàòà=",TimeToStr(Time[_shift])," isNewDay=",result);
//----
return(result);
}
//+------------------------------------------------------------------+
//| Óñòàíîâèòü óðîâíè FP, FR1 è FS1 ïðîøåäøåãî äíÿ |
//+------------------------------------------------------------------+
void SetLevels1(int _shift)
{
int prevDay=TimeDay(Time[_shift+1]);
//----
i=1;
while (TimeDay(Time[_shift+i])==prevDay) i++;
i--;
FH=High[Highest(NULL,0,MODE_HIGH,i,_shift+1)];
FL=Low[Lowest(NULL,0,MODE_LOW,i,_shift+1)];
if (Formula==0) FP=NormalizeDouble((FH+FL+Close[_shift+1])/3.0,Digits); else FP=NormalizeDouble((FH+FL+2*Close[_shift+1])/4.0,Digits);
//----
ExtMapBuffer1[_shift]=FP;
FR1Buffer[_shift]=NormalizeDouble(FP+(FP-FL),Digits);
FS1Buffer[_shift]=NormalizeDouble(FP-(FH-FP),Digits);
}
//+------------------------------------------------------------------+
//| Óñòàíîâèòü óðîâíè FP, FR1 è FS1 â íîëü |
//+------------------------------------------------------------------+
void SetLevels1Empty(int _shift)
{
ExtMapBuffer1[_shift]=0.0;
FR1Buffer[_shift]=0.0;
FS1Buffer[_shift]=0.0;
}
//+------------------------------------------------------------------+
//| Óñòàíîâêà óðîâíåé â íóëåâîì áàðå |
//+------------------------------------------------------------------+
void SetNullBarLevels(int _shift)
{
int cnt;
int currDay=TimeDay(Time[_shift]);
//----
i=1;
while (TimeDay(Time[_shift+i])==currDay) i++;
if (i>=2)
{
i=i-2;
for (cnt=i;cnt>0;cnt--)
{
ExtMapBuffer1[shift+cnt]=0.0;
FR1Buffer[_shift]=0.0;
FS1Buffer[_shift]=0.0;
}
FH=High[Highest(NULL,0,MODE_HIGH,i+1,_shift)];
FL=Low[Lowest(NULL,0,MODE_LOW,i+1,_shift)];
if (Formula==0) FP=NormalizeDouble((FH+FL+Close[_shift+1])/3.0,Digits); else FP=NormalizeDouble((FH+FL+2*Close[_shift+1])/4.0,Digits);
ExtMapBuffer1[_shift]=FP;
FR1Buffer[_shift]=NormalizeDouble(FP+(FP-FL),Digits);
FS1Buffer[_shift]=NormalizeDouble(FP-(FH-FP),Digits);
}
//----
return;
}
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(2);
if (Formula!=0)Formula=1;
string label1="FR1("+Formula+")";
string label2="FS1("+Formula+")";
//---- indicators
SetIndexStyle(0,DRAW_SECTION);
SetIndexBuffer(0,FR1Buffer);
SetIndexEmptyValue(0,0.0);
SetIndexLabel(0,label1);
SetIndexStyle(1,DRAW_SECTION);
SetIndexBuffer(1,FS1Buffer);
SetIndexEmptyValue(1,0.0);
SetIndexLabel(1,label2);
SetIndexBuffer(2,ExtMapBuffer1);
SetIndexEmptyValue(2,0.0);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit,firstDay;
int counted_bars=IndicatorCounted();
if (counted_bars<0) return(-1);
if (counted_bars==0)
{
limit=Bars-1;
i=1;
firstDay=TimeDay(Time[limit]);
while (TimeDay(Time[limit-i])==firstDay) i++;
limit=limit-i-PERIOD_D1/Period();
}
if (counted_bars>0) limit=Bars-counted_bars;
//----
if (Period()>PERIOD_H4) return;
for (shift=limit;shift>=0;shift--)
{
if (isNewDay(shift)) SetLevels1(shift); else SetLevels1Empty(shift);
if (shift==0) SetNullBarLevels(shift);
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments