_Channel@DeM
Download scriptThis script is designed as a technical analysis tool for the MetaTrader platform to help traders identify significant market structural points—specifically "peaks" (highs) and "troughs" (lows)—while simultaneously visualizing price boundaries. It combines momentum sensing with trend geometry to give a clearer picture of where the market has been and where it might be heading.
Here is a breakdown of the core logic used by this tool:
1. Momentum Detection
The script utilizes a specific momentum calculation (DeMarker) as its primary "engine." This engine monitors the price action over a set period to determine if the market is currently in an "Overbought" or "OverSold" state.
- When the momentum reaches a high threshold, it signals that the price has pushed significantly upward.
- When it hits a low threshold, it signals a significant downward push.
2. Peak and Trough Identification
Unlike basic indicators that simply mark every high or low, this script uses a "search and confirm" logic:
- The Search: Once the momentum engine signals a potential turning point (e.g., Overbought), the script looks back through previous price bars to find the absolute highest high or lowest low within a specific window of time.
- State Management: The script tracks the "direction" of the market movement. If it just identified a peak, it switches its focus to looking for the next trough, and vice versa. This ensures that the chart displays alternating highs and lows rather than clustering multiple points in one direction.
3. Visual Output Options
The logic provides three different ways to view this data based on user preferences:
- ZigZag Mapping: The script can draw a continuous line connecting these peaks and troughs. This helps traders visualize the "swing" of the market, making it easier to see the overall trend. It offers two display modes: one that marks the points exactly where the price extremes occurred, and another that aligns them with the specific time the momentum signaled the turn.
- Channel Boundaries: The script can draw upper and lower horizontal lines (a channel). These lines represent the "ceiling" and "floor" established by the most recent significant peaks and troughs. This is useful for identifying support and resistance levels.
- Dynamic Sensitivity: Users can adjust the sensitivity of the momentum detection. By changing the "OverSold" percentage, you can decide whether the script reacts to minor price fluctuations or only to major, extreme market moves.
Summary for the Trader
In essence, this tool filters out the "noise" of constant price movement. It identifies the most significant structural landmarks by combining momentum signals with a retrospective search for actual price extremes. It then projects these landmarks onto your chart as either a visual path (ZigZag) or a boundary frame (Channel), allowing you to see the market's skeleton more clearly.
#property indicator_chart_window // â îêíå èíñòðóìåíòà
#property indicator_buffers 5
#property indicator_color1 Magenta // çèãçàã
#property indicator_color3 Green // âåðõí. ãð. êàíàëà
#property indicator_color4 Red // íèæí. ãð. êàíàëà
// âõîäíûå ïàðàìåòðû
extern double OverSold=0.3; // çîíà ïåðåïðîäàííîñòè â %% (çîíà ïåðåêóïëåííîñòè=100-çîíà ÏÏ)
extern int DeMperiod=14; // Ïåðèîä DeM
extern int ShowChannel=2; // ïîêàçàòü 1 - ïî ìåñòó ýêñòðåìóìîâ, 2 - ïî âðåìåíè îïðåäåëåíèÿ
extern int ShowZigZag=0; // ïîêàçàòü 1 - ïî ìåñòó ýêñòðåìóìîâ, 2 - ïî âðåìåíè îïðåäåëåíèÿ
// èíä.áóôåðû
double ZZpeak[], // ïèê
ZZtrough[], // âïàäèíà
ChTop[], // âåðõí. ãð. êàíàëà
ChBot[], // íèæí. ãð. êàíàëà
DeM[]; // DeM
// îáùèå ïåðåìåííûå
double OverBought; // çîíà ïåðåêóïëåííîñòè
bool dir; // 0-ïîéìàíà âïàäèíà, 1 - ïèê
double max,min; // òåêóùèå ìàêñ., ìèíèìóìû
double top,bot; // ïèêè, âïàäèíû
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);
OverBought=1.0-OverSold; // çîíà ïåðåêóïëåííîñòè
//---- 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,DeM);
SetIndexStyle(4,DRAW_NONE);
string _rsi="DeM("+DeMperiod+") ";
SetIndexLabel(4,_rsi);
//----
IndicatorShortName("ZZ@DeM");
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--) DeM[i]=iDeMarker(NULL,0,DeMperiod, i);
for(i=limit; i>0; i--)
{
double dem=DeM[i];
jDn++; jUp++; // ñ÷åò÷èêè ðàññòîÿíèÿ ìåæäó ýêñòðåìóìàìè
bool up=dem>=OverBought;
bool dn=dem<=OverSold;
if(dir) { // ëîâèòñÿ âïàäèíà
if(up) { // åñëè â çîíå ÏÊ, òî
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;
dir=0; // ïîéìàíà âïàäèíà öåíû
}
}
else { // ëîâèòñÿ ïèê
if(dn) { // åñëè â çîíå ÏÏ, òî
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; // ïîéìàí ïèê öåíû
}
}
if(up) jUp=-2; if(dn) jDn=-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(DeM,0.0);
dir=0;max=0;min=0;top=0;bot=0;jDn=0;jUp=0;
return(0);
}
Comments