HiLo_Act_Next_Profi5
Download scriptThis script is a technical analysis tool designed for the MetaTrader platform. It functions as a dynamic visual indicator that helps traders identify price trends by analyzing relationships between different timeframes and moving averages.
Core Functionality
The primary purpose of this tool is to track and display specific price levels—specifically "Upper" and "Lower" boundaries—based on moving average calculations. Unlike standard indicators that only look at the current timeframe, this script incorporates data from a higher, secondary timeframe to provide a more comprehensive view of market momentum.
Key Logical Components
Multi-Timeframe Synchronization The script is designed to operate by looking at a "Next Timeframe" (for example, looking at hourly data while viewing a 15-minute chart). It automatically aligns the current price data with the larger timeframe's structure. This allows it to identify significant price levels that might be invisible on a smaller, more volatile chart.
Dynamic Boundary Calculation The logic calculates two specific types of moving averages:
- Upper Boundaries: Based on the "High" prices of the market.
- Lower Boundaries: Based on the "Low" prices of the market.
By calculating these, the script establishes a corridor or zone. The tool then determines which boundary is currently relevant based on where the market price is closing in relation to these moving averages.
Trend Switching Logic The script features a specific decision-making process to determine which line to display:
- If the price closes above the calculated upper threshold, the indicator prioritizes the lower boundary logic.
- If the price closes below the lower threshold, it prioritizes the upper boundary logic.
- In neutral zones (where the price is between both boundaries), the script maintains the "memory" of the previous trend to ensure a smooth visual transition rather than flickering between lines.
Smart Initialization and Data Validation To ensure accuracy, the script includes a rigorous "startup" check. Before it begins drawing on your chart, it verifies that:
- Enough historical data exists for both the current and the higher timeframe.
- The data sets are perfectly synchronized in time.
- The chart is loaded with enough bars to calculate the required averages without errors.
If any of these conditions aren't met (e.g., if the history is too short), the script will notify the user that data needs to be updated, preventing the display of misleading or "broken" signals.
Summary for the User
Essentially, this tool acts as a "trend filter." It looks at the bigger picture (the higher timeframe) and project those key price levels onto your current chart. It automatically highlights whether the market is currently reacting to upper resistance or lower support, providing a cleaner visual path of market behavior by filtering out minor price noise.
//+------------------------------------------------------------------+
//| HiLo_Act_Next_Profi2.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| Profi_R |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "Profi_R"
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 DeepSkyBlue
#property indicator_color2 Magenta
//---- input parameters
extern int Range=3;
extern int NextTF=60;
extern bool UseFullTF=1;
//---- buffers
double Up[];
double Dn[];
double UpMA[];
double DnMA[];
int init_flag,d_b=0,no_dr_b;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
string short_name;
//---- indicators
IndicatorBuffers(4);
SetIndexStyle(0,DRAW_LINE,0,2);
SetIndexStyle(1,DRAW_LINE,0,2);
//SetIndexStyle(2,DRAW_LINE,0,2);
//SetIndexStyle(3,DRAW_LINE,0,2);
SetIndexBuffer(0,Up);
SetIndexBuffer(1,Dn);
SetIndexBuffer(2,UpMA);
SetIndexBuffer(3,DnMA);
SetIndexLabel(0,"Up");
SetIndexLabel(1,"Dn");
SetIndexEmptyValue(0,0.0);
SetIndexEmptyValue(1,0.0);
SetIndexEmptyValue(2,0.0);
SetIndexEmptyValue(3,0.0);
//----
short_name="HiLo_Act_Next_Profi2 ("+Range+",M"+NextTF+")";
IndicatorShortName(short_name);
init_flag=0;
//----
return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted(),limit,cb,i,j,bb_count,cb_count,sb_count,b_bar,end_bar,end_sbar;
datetime TimeArray[],TimeArray_c[],end_time,begin_time;
//----
if( init_flag!=1 )
{
if( minit() < 0 )
{
return(-1);
}
}
if( counted_bars<0 )
{
return(-1);
}
else
{
if( Bars-1-counted_bars < 0 )
{
limit=0;
}
else
{
if( Bars-1-counted_bars > Bars-1-no_dr_b)
{
limit=Bars-1-no_dr_b;
}
else
{
limit=Bars-1-counted_bars;
}
}
}
bb_count=ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),NextTF);
cb_count=ArrayCopySeries(TimeArray_c,MODE_TIME,Symbol(),Period());
if( bb_count<=0 || cb_count<=0 )
{
return(0);
}
for(cb=limit;cb>=0;cb--)
{
if( MathCeil(TimeArray[ArrayBsearch( TimeArray,Time[cb],0,0,MODE_ASCEND)]/NextTF/60)*NextTF*60 >
MathCeil(Time[cb]/Period()/60)*Period()*60)
{
b_bar =ArrayBsearch( TimeArray,Time[cb],0,0,MODE_ASCEND)+1+Range;
end_bar =ArrayBsearch( TimeArray,Time[cb],0,0,MODE_ASCEND)+2;
//end_time=(MathCeil(TimeArray[ArrayBsearch( TimeArray,Time[cb],0,0,MODE_ASCEND)+1]/NextTF/60))*NextTF*60;
//begin_time=(MathCeil(TimeArray[ArrayBsearch( TimeArray,Time[cb],0,0,MODE_ASCEND)+1+Range]/NextTF/60))*NextTF*60;
}
else
{
b_bar =ArrayBsearch( TimeArray,Time[cb],0,0,MODE_ASCEND)+Range;
end_bar =ArrayBsearch( TimeArray,Time[cb],0,0,MODE_ASCEND)+1;
//end_time=MathCeil(TimeArray[ArrayBsearch( TimeArray,Time[cb],0,0,MODE_ASCEND)]/NextTF/60)*NextTF*60;
//begin_time=(MathCeil(TimeArray[ArrayBsearch( TimeArray,Time[cb],0,0,MODE_ASCEND)+Range]/NextTF/60))*NextTF*60;
}
//Alert( TimeToStr(Time[cb])+" ", TimeToStr(iTime(NULL,NextTF,b_bar))+" ", TimeToStr(iTime(NULL,NextTF,end_bar)) );
//b_bar=ArrayBsearch( TimeArray_c,begin_time,0,0,MODE_ASCEND);
//end_sbar=ArrayBsearch( TimeArray_c,end_time,0,0,MODE_ASCEND)+1;
UpMA[cb]=iMA(Symbol(),NextTF,Range,0,MODE_SMA,PRICE_HIGH,end_bar);
DnMA[cb]=iMA(Symbol(),NextTF,Range,0,MODE_SMA,PRICE_LOW,end_bar);
//UpMA[cb]=iMA(Symbol(),Period(),b_bar+1-end_bar,0,MODE_SMA,PRICE_HIGH,end_bar);
//DnMA[cb]=iMA(Symbol(),Period(),b_bar+1-end_bar,0,MODE_SMA,PRICE_LOW,end_bar);
if( UseFullTF==0 )
{
if( Close[cb]>UpMA[cb] )
{
Dn[cb]=DnMA[cb];
Up[cb]=0.0;
}
else
{
if( Close[cb]<DnMA[cb] )
{
Up[cb]=UpMA[cb];
Dn[cb]=0.0;
}
else
{
if( Up[cb+1]>0 )
{
Up[cb]=UpMA[cb];
Dn[cb]=0.0;
}
if( Dn[cb+1]>0 )
{
Dn[cb]=DnMA[cb];
Up[cb]=0.0;
}
}
}
}
else
{
Print(TimeToStr(Time[cb])+" "+TimeToStr((MathCeil(TimeArray[end_bar]/NextTF/60)+1)*NextTF*60)+" "+TimeToStr((MathCeil(Time[cb]/Period()/60))*Period()*60));
if( Close[cb+1]>UpMA[cb+1] &&
(MathCeil(TimeArray[end_bar]/NextTF/60)+1)*NextTF*60==(MathCeil(Time[cb]/Period()/60))*Period()*60 )
{
Dn[cb]=DnMA[cb];
Up[cb]=0.0;
}
else
{
if( Close[cb+1]<DnMA[cb+1] &&
(MathCeil(TimeArray[end_bar]/NextTF/60)+1)*NextTF*60==(MathCeil(Time[cb]/Period()/60))*Period()*60 )
{
Up[cb]=UpMA[cb];
Dn[cb]=0.0;
}
else
{
if( Up[cb+1]>0 )
{
Up[cb]=UpMA[cb];
Dn[cb]=0.0;
}
if( Dn[cb+1]>0 )
{
Dn[cb]=DnMA[cb];
Up[cb]=0.0;
}
}
}
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Îïðåäåëåíèå íà÷àëüíîé òî÷êè îòðèñîâêè |
//+------------------------------------------------------------------+
int minit()
{
datetime TimeArray[];
int bb_count,cb,i;
//----
//ïîëó÷åíèå âðåìåíè íà÷àëà êàæäîãî áàðà ñ äðóãîãî òàéìôðåéìà
bb_count=ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),NextTF);
if( bb_count<1 )
{
Alert("Îòñóòñòâóþò äàííûå Next òàéìôðåéìà!");
return(-1);
}
//ïðîâåðêà íà íåîáõîäèìîñòü îáíîâëåíèÿ äàííûõ áîëüøåãî ò-ô
if( MathCeil(TimeArray[0]/NextTF/60)*NextTF*60 < MathCeil(Time[0]/Period()/60)*Period()*60-NextTF*60 )
{
Alert("Íåîáõîäèìî îáíîâèòü äàííûå Next òàéìôðåéìà!");
return(-1);
}
//ïðîâåðêà íà íåîáõîäèìîñòü îáíîâëåíèÿ äàííûõ òåêóùåãî ò-ô
if( MathCeil(TimeArray[0]/NextTF/60)*NextTF*60 > MathCeil(Time[0]/Period()/60)*Period()*60 )
{
Alert("Íåîáõîäèìî îáíîâèòü äàííûå òåêóùåãî òàéìôðåéìà!");
return(-1);
}
//åñëè áàðû íà òåêóùåì ò-ô íà÷èíàþòñÿ ñ ìîìåíòà îòðèñîâêè áàðîâ áîëüøåãî ò-ô èëè ïîçæå
if( MathCeil(Time[Bars-1]/Period()/60)*Period()*60 >= MathCeil(TimeArray[bb_count-1]/NextTF/60)*NextTF*60 )
{
for( cb=bb_count-1;cb>=0;cb--)
{
if( MathCeil(Time[Bars-1]/Period()/60)*Period()*60 >= MathCeil(TimeArray[cb]/NextTF/60)*NextTF*60 &&
MathCeil(Time[Bars-1]/Period()/60)*Period()*60 < MathCeil(TimeArray[cb-1]/NextTF/60)*NextTF*60 )
{
for( i=Bars-1;i>=0;i-- )
{
if( MathCeil(Time[i]/Period()/60)*Period()*60 >= MathCeil(TimeArray[cb-Range]/NextTF/60)*NextTF*60 )
{
d_b=i;
no_dr_b=Bars-1-d_b;
SetIndexDrawBegin(0,no_dr_b+1);
SetIndexDrawBegin(1,no_dr_b+1);
init_flag=1;
break;
}
}
if( d_b>0 )
{
break;
}
}
}
}
else
{
for(cb=Bars-1;cb>=0;cb--)
{
//åñëè âðåìÿ áàðà áîëüøå èëè ðàâíî âðåìåíè áàðà ñ êîòîðîãî íà÷èíàåì îòðèñîâêó...
if( MathCeil(Time[cb]/Period()/60)*Period()*60 >= MathCeil(TimeArray[bb_count-1-Range]/NextTF/60)*NextTF*60 )
{
d_b=cb;
no_dr_b=Bars-1-d_b;
SetIndexDrawBegin(0,no_dr_b+1);
SetIndexDrawBegin(1,no_dr_b+1);
init_flag=1;
break;
}
}
}
if( d_b<1 )
{
return(-1);
}
//----
return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
Comments