_Channel@MA
Download scriptThis script is a technical trading tool designed to identify significant price movements and trend boundaries by analyzing the relationship between price action and a Moving Average. It combines three distinct visual elements: a ZigZag-style trend tracker, a dynamic channel, and a smoothed price average.
Here is a breakdown of how the logic functions:
Core Tracking Mechanism
Instead of simply reacting to every minor price tick, the script uses a "Sensitivity" threshold. This prevents the tool from creating "noise" by ensuring that it only recognizes a new peak or trough when the price moves significantly enough beyond the previous point.
The Movement Logic
The script operates in two primary modes, switching back and forth automatically:
- Finding Peaks: When the logic is looking for a high point, it monitors the Moving Average. If the average price drops significantly below the most recent recorded maximum (exceeding your chosen sensitivity), the script "locks in" that high point as a peak.
- Finding Troughs: Conversely, when searching for a low point, the script waits for the Moving Average to rise significantly above the most recent recorded minimum. Once this threshold is crossed, it identifies and locks in that low point.
Visual Output Features
Depending on how you configure the settings, the script displays this data in different ways:
- ZigZag (Trend Tracking): This draws a continuous line connecting the identified peaks and troughs. It helps traders see the "skeleton" of the market trend by stripping away minor fluctuations.
- Price Channels: The script can draw boundaries (top and bottom lines) based on these extreme points. These lines represent the recent range of the price, helping to define the current trading corridor.
- Moving Average: A smoothed line is plotted on the chart. This serves as the primary foundation for the logic; rather than using raw price data which can be erratic, the script uses this smoothed average to decide when a true trend reversal or peak has occurred.
Dynamic Adjustments
The tool includes internal "counters" that allow it to look back at historical candles to find the absolute highest high or lowest low during the period between two confirmed turning points. This ensures that even if the moving average confirms a move slightly late, the visual peak or trough marked on your chart remains the most accurate extreme price reached during that move.
#property indicator_chart_window // â îêíå èíñòðóìåíòà
#property indicator_buffers 5
#property indicator_color1 Magenta // çèãçàã
#property indicator_color3 Green // âåðõí. ãð. êàíàëà
#property indicator_color4 Red // íèæí. ãð. êàíàëà
#property indicator_color5 Blue // MA
// âõîäíûå ïàðàìåòðû
extern double Sensitivity=5; // ïîðîã ÷óâñòâ. â ï.ï.
extern int ma_period=5; // ïåðèîä MA
extern int ma_method=0; // 0-SMA, 1-EMA, 2-SMMA, 3-LWMA
extern int applied_price=0; // 0-CLOSE, 1-OPEN, 2-HIGH, 3-LOW, 4-MEDIAN, 5-TYPICAL, 6-WEIGHTED
extern int ShowChannel=2; // ïîêàçàòü 1 - ïî ìåñòó ýêñòðåìóìîâ, 2 - ïî âðåìåíè îïðåäåëåíèÿ
extern int ShowZigZag=0; // ïîêàçàòü 1 - ïî ìåñòó ýêñòðåìóìîâ, 2 - ïî âðåìåíè îïðåäåëåíèÿ
// èíä.áóôåðû
double ZZpeak[], // ïèê
ZZtrough[], // âïàäèíà
ChTop[], // âåðõí. ãð. êàíàëà
ChBot[], // íèæí. ãð. êàíàëà
MA[]; // MA
// îáùèå ïåðåìåííûå
double sens; // ÷óâñòâ. â öåíàõ
bool dir;
double tx,px,bot,top;
int jDn,jUp; // ñ÷åò÷èêè ðàññòîÿíèÿ ìåæäó ýêñòðåìóìàìè
bool count=1; // íàïðàâëåíèå ïîèñêà öåíîâîãî ýêñòðåìóìà; 1 - îò ñòàðøèõ áàðîâ ê ìëàäøèì
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
if(ShowZigZag<0 || ShowChannel<0) count=0;
ShowZigZag=MathAbs(ShowZigZag); ShowChannel=MathAbs(ShowChannel);
sens=Sensitivity*Point; // ÷óâñòâ. â öåíàõ
//---- indicators
SetIndexBuffer(0,ZZpeak);
SetIndexEmptyValue(0,0.0);
SetIndexLabel(0,"Peak");
SetIndexBuffer(1,ZZtrough);
SetIndexEmptyValue(1,0.0);
SetIndexLabel(1,"Trough");
if(ShowZigZag>0) {
SetIndexStyle(0,DRAW_ZIGZAG);
SetIndexStyle(1,DRAW_ZIGZAG);
}
else {
SetIndexStyle(0,DRAW_NONE);
SetIndexStyle(1,DRAW_NONE);
}
SetIndexBuffer(2,ChTop);
SetIndexEmptyValue(2,0.0);
SetIndexStyle(2,DRAW_LINE);
SetIndexLabel(2,"ChTop");
SetIndexBuffer(3,ChBot);
SetIndexEmptyValue(3,0.0);
SetIndexStyle(3,DRAW_LINE);
SetIndexLabel(3,"ChBot");
if(ShowChannel>0) {
SetIndexStyle(2,DRAW_LINE);
SetIndexStyle(3,DRAW_LINE);
}
else {
SetIndexStyle(2,DRAW_NONE);
SetIndexStyle(3,DRAW_NONE);
}
SetIndexBuffer(4,MA);
SetIndexStyle(4,DRAW_LINE);
string _ma="MA("+ma_period+") "+ma_method+", "+applied_price;
SetIndexLabel(4,_ma);
//----
IndicatorShortName("ZZ@MA");
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int ic=IndicatorCounted();
if(Bars-ic-1>1) ic=reinit();
int limit=Bars-ic-1; // êîë-âî ïåðåñ÷åòîâ
for(int i=limit; i>=0; i--) MA[i]=iMA(NULL,0,ma_period,0,ma_method,applied_price, i);
for(i=limit; i>0; i--)
{
double c=MA[i];
jDn++; jUp++; // ñ÷åò÷èêè ðàññòîÿíèÿ ìåæäó ýêñòðåìóìàìè
//------------- Ëîâëÿ âïàäèíû -------------------------------------------
if(dir) // Åñëè ôëàã=1, òî ëîâèì âïàäèíó
{
// if(l>Low[i]) {l=Low[i]; jt=0;}
tx=MathMin(tx,c);
if(c >= tx+sens) // Åñëè öåíà ñòàëà áîëüøå ïîñëåäíåãî ìèíèìóìà çà ïîðîã, òî
{
double l=High[i]*100;
if(count) {for(int k=jUp; k>=0; k--) if(Low[i+k]<l) {int sh=i+k;l=Low[i+k];}}
else {for(k=0; k<=jUp; k++) if(Low[i+k]<l) {sh=i+k;l=Low[i+k];}}
bot=Low[sh];
if(ShowZigZag==1) ZZtrough[sh]=bot; else ZZtrough[i]=bot;
if(ShowChannel==1) for(k=sh; k>i; k--) ChBot[k]=bot;
px=c;
dir=0; // ôëàã óñòàíîâëåí "ïîéìàíà âïàäèíà"
jDn=-2;
}
}
//------------- Ëîâëÿ ïèêà -----------------------------------------
else // Åñëè ôëàã=0, òî ëîâèì ïèê
{
px=MathMax(px,c);
if(c <= px-sens) // Åñëè öåíà ñòàëà ìåíüøå ïîñëåäíåãî ìàêñèìóìà çà ïîðîã, òî
{
tx=c;
double h=0;
if(count) {for(k=jDn; k>=0; k--) if(High[i+k]>h) {sh=i+k; h=High[i+k];}}
else {for(k=0; k<=jDn; k++) if(High[i+k]>h) {sh=i+k; h=High[i+k];}}
top=High[sh];
if(ShowZigZag==1) ZZpeak[sh]=top; else ZZpeak[i]=top;
if(ShowChannel==1) for(k=sh; k>i; k--) ChTop[k]=top;
dir=1; // ôëàã "ïîéìàí ïèê"
jUp=-2;
}
}
//--------------------------------------------------------------------
for(k=0; k<2; k++) {ChTop[i-k]=top; ChBot[i-k]=bot;}
}
return(0);
}
// ô-ÿ äîïîëíèòåëüíîé èíèöèàëèçàöèè
int reinit()
{
ArrayInitialize(ZZpeak,0.0);
ArrayInitialize(ZZtrough,0.0);
ArrayInitialize(ChTop,0.0);
ArrayInitialize(ChBot,0.0);
ArrayInitialize(MA,0.0);
dir=0; tx=0;px=0;top=0;bot=0;jUp=0;jDn=0;
return(0);
}
Comments