HiLo_Act_Next_Profi5_v1
Download scriptThis technical indicator is designed to analyze and visualize price trends by synchronizing data between different timeframes on a trading chart. It specifically focuses on identifying whether the market is currently in an upward or downward trend based on high and low price averages.
Core Logic Overview
The script operates by comparing the current price action against moving averages calculated from a "Next" higher timeframe (for example, looking at 60-minute data while viewing a smaller chart). This multi-timeframe approach helps filter out "noise" and provides a clearer picture of the broader market direction.
Key Components of the Logic:
1. Multi-Timeframe Synchronization The script automatically aligns the time data of your current chart with a higher timeframe. It performs internal calculations to ensure that the price data being analyzed corresponds accurately to the specific periods of the larger timeframe, preventing "lag" or misalignment between the two views.
2. Trend Detection Mechanism The indicator calculates two primary types of moving averages:
- Upper Average: Based on the highest prices over a defined range.
- Lower Average: Based on the lowest prices over a defined range.
By comparing the closing price against these two averages, the script determines the "state" of the market. It looks for specific conditions where the price breaks above or below these levels to switch the trend identification.
3. Dynamic Trend Persistence The logic includes a "memory" feature. If the current price is not clearly breaking a trend level but was previously established as an upward or downward move, the script maintains the previous trend status. This prevents the indicator from flickering or switching trends rapidly during periods of sideways consolidation (choppy markets).
4. Visual Output The results are displayed on the chart as two distinct lines:
- One line represents the Upward Trend (DeepSkyBlue).
- One line represents the Downward Trend (Black).
When the trend shifts, the script "activates" the relevant line and hides the other, providing a clean visual cue of the current market bias.
User Configuration Options:
- Range: Defines how many candles/periods are used to calculate the high and low averages.
- NextTF: Allows you to specify which higher timeframe (in minutes) the script should use for its trend analysis.
- UseFullTF: A toggle that adjusts how strictly the script aligns the current chart data with the higher timeframe's calculation boundaries.
//+------------------------------------------------------------------+
//| 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 Black
//---- 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