This code snippet appears to be part of an Expert Advisor (EA) or Indicator for MetaTrader 4/5. It's designed to detect and potentially trade based on divergence and convergence patterns in price action. Let's break down the code and explain its purpose.
Overall Purpose
The code aims to identify:
- Divergence: When price and an indicator (likely a momentum oscillator like RSI or MACD) are moving in opposite directions. This suggests a potential weakening of the current trend.
- Convergence: When price and an indicator start moving in the same direction after a period of divergence. This can signal a trend reversal.
Key Variables and Structures
Div
: An integer variable that likely tracks the type or level of divergence detected.Div1Bar
,Div2Bar
,Div3Bar
: Bar indices representing the bars where divergence patterns are identified.Div1Wave
,Div2Wave
,Div3Wave
: Wave numbers associated with the divergence bars. This likely relates to Elliott Wave theory or a similar wave-based analysis.UpWaveNum
: Number of waves in an uptrend.BeginBar
: The starting bar for the analysis.ContinueBar
: The bar to continue the analysis from.Conv
: An integer variable that likely tracks the type or level of convergence detected.Conv1Bar
,Conv2Bar
,Conv3Bar
: Bar indices representing the bars where convergence patterns are identified.Conv1Wave
,Conv2Wave
,Conv3Wave
: Wave numbers associated with the convergence bars.DownWaveNum
: Number of waves in a downtrend.BeginBarDown
: The starting bar for the convergence analysis.ContinueBarDown
: The bar to continue the convergence analysis from.sAddpips
: A variable to add pips to the price.bAddpips
: A variable to subtract pips from the price.BearsPeriod
: Period for bear market.BullPeriod
: Period for bull market.
Functions
-
dinstall(int StrBar)
(Divergence Installation)- Purpose: Initializes variables related to divergence detection.
StrBar
: The starting bar for the divergence analysis.- Sets initial values for divergence-related variables.
BeginBar
andContinueBar
are set toStrBar
andStrBar + 1
respectively.
-
cinstall(int StrBar)
(Convergence Installation)- Purpose: Initializes variables related to convergence detection.
StrBar
: The starting bar for the convergence analysis.- Sets initial values for convergence-related variables.
DownWaveNum
is initialized to 0.BeginBarDown
andContinueBarDown
are set toStrBar
andStrBar + 1
respectively.
-
AddpipInit(int spips, int bpip)
- Purpose: Initializes the
sAddpip
andbAddpip
variables. spips
: Value to add to the price.bpips
: Value to subtract from the price.
- Purpose: Initializes the
-
PeriodInit(int P1, int P2)
- Purpose: Initializes the
BearPeriod
andBullPeriod
variables. P1
: Period for bear market.P2
: Period for bull market.
- Purpose: Initializes the
Logic and Algorithms
The core logic lies within the ConvKind
and ConvKind
functions. These functions are designed to be called for each bar to check for divergence and convergence patterns.
-
Divergence Detection (
ConvKind
):- It checks if the current bar (
SeekBar
) is beyond theBeginBar
and compares the price level with the lowest price level of the previous bars. - It uses
iLow()
to get the low of the bar andLowest()
to find the lowest low of the previous bars. - The logic is structured as a series of
OR
conditions. If any of these conditions are met, it indicates a potential divergence pattern.
- It checks if the current bar (
-
Convergence Detection (
ConvKind
):- Similar to divergence detection, it checks if the current bar (
SeekBar
) is beyond theBeginBarDown
and compares the price level with the highest price level of the previous bars. - It uses
iHigh()
to get the high of the bar andHighest()
to find the highest high of the previous bars. - The logic is structured as a series of
OR
conditions. If any of these conditions are met, it indicates a potential convergence pattern.
- Similar to divergence detection, it checks if the current bar (
Important Considerations
- Indicator Dependency: The code doesn't explicitly mention which indicator is being used for divergence/convergence detection. It's likely an oscillator like RSI, MACD, or Stochastic. The
iLow()
andiHigh()
functions suggest that the price itself is being compared to the indicator. - Elliott Wave Theory: The
Wave
numbers (Div1Wave
,Div2Wave
, etc.) suggest a possible integration of Elliott Wave theory or a similar wave-based analysis. - Optimization: The code likely needs to be optimized for performance, especially if it's being run on every tick.
- Filtering: The code probably needs additional filtering to reduce false signals. This could involve using other indicators, price action patterns, or volume analysis.
- Trading Logic: The code only detects divergence and convergence. It doesn't include any trading logic (e.g., entry/exit rules, money management).
In summary, this code snippet is a foundation for an Expert Advisor or Indicator that aims to identify divergence and convergence patterns. It requires further development to integrate a specific indicator, implement trading logic, and optimize performance.
//+------------------------------------------------------------------+
//| Diver.mq4 |
//| Mishell |
//| Ôóíêöèè îïðåäåëÿþùèå íàëè÷èå äèâåðãååíöèè è åå âèä |
//+------------------------------------------------------------------+
#property copyright "Mishell"
#property link ""
#property library
int sAddpips;
int bAddpips;
int BearsPeriod=13;
int BullsPeriod=13;
// ----- Êîíñòàíòû-ñèãíàëû ------
int Div; // Ñ÷åò÷èê äèâåðãåíöèé
int Div1Bar;
int Div2Bar;
int Div3Bar;
int Div1Wave; // íîìåð ïèêà èíäèêàòîðà ñèëû áûêîâ äëÿ äèâåðãåíöèè 1
int Div2Wave;
int Div3Wave;
int Div1Main=0;
int Conv; //Ñ÷åò÷èê êîíâåðãåíöèé
int Conv1Bar;
int Conv2Bar;
int Conv3Bar;
int Conv1Wave; // íîìåð ïèêà èíäèêàòîðà ñèëû ìåäâåäåé äëÿ êîíâåðãåíöèè 1
int Conv2Wave;
int Conv3Wave;
int BeginBar; // áàð îò êîòîðîãî èùåòñÿ äèâåðãåíöèÿ
int BeginBarDown;// áàð îò êîòîðîãî èùåòñÿ êîíâåðãåíöèÿ
int ContinueBar;// òåêóùèé áàð ïîèñêà äèâåðãåíöèè
int ContinueBarDown; //òåêóùèé áàð ïîèñêà êîíâåðãåíöèè
int UpWaveNum=0 ; // ñ÷åò÷èê âåðøèí BullsPower
int DownWaveNum=0 ; // ñ÷åò÷èê ÿì BearsPower
//+--------------------------------------------------------------------------+
//| Îïðåäåëåíèå íàëè÷èÿ ñèãíàëà î ñìåíå âîñõîäÿùåãî òðåíäà èëè íà÷àëå îòêàòà
//+--------------------------------------------------------------------------+
int BreackUpTrendCondition (int TimeFrame, int MaxWaveNum)
{
int TopBar;// òåêóùèé ïðîâåðÿåìûé áàð
// Print("Begin div while");
TopBar=ContinueBar; //
//Ïðîâåëÿåì íàëè÷èå äèâåðãåíöèè
while (! ( ((iHigh(NULL,TimeFrame,BeginBar)+sAddpips*Point)>=iHigh(NULL,TimeFrame,TopBar) &&
iBullsPower( NULL, TimeFrame, BullsPeriod, PRICE_CLOSE, BeginBar)<
iBullsPower( NULL, TimeFrame, BullsPeriod, PRICE_CLOSE, TopBar)) ||
(UpWaveNum==MaxWaveNum ||
iBullsPower( NULL, TimeFrame, BullsPeriod, PRICE_CLOSE, TopBar)<0)
)
)
{ //while
//Äèâåðãåíöè íåò. Òîãäà,
// Åñëè ìû íàøëè ïèê èíäèêàòîðà, òî óâåëè÷èâàåì ñ÷åò÷èê ïèêîâ íà 1
if ( (iBullsPower( NULL, TimeFrame, BullsPeriod, PRICE_CLOSE, TopBar)>
iBullsPower( NULL, TimeFrame, BullsPeriod, PRICE_CLOSE, TopBar-1)
&&
iBullsPower( NULL, TimeFrame, BullsPeriod, PRICE_CLOSE, TopBar)>
iBullsPower( NULL, TimeFrame, BullsPeriod, PRICE_CLOSE, TopBar+1)) )
{//1
UpWaveNum++ ;
}//1
// Åñëè æå ïèêà íåò,
// ïðè óñëîâèè, ÷òî ñ÷åò÷èê "áû÷üèõ" ïèêîâ = 0 è äâèæåíèå îò ñîñåäíåãî áàðà - íèñõîäÿùåå
// óñòàíàâëèâàåì ýòîò ñîñåäíèé áàð êàê íà÷àëüíûé
else
if ( (UpWaveNum==0 && Div==0) &&
(iBullsPower(NULL, TimeFrame, BullsPeriod, PRICE_CLOSE, BeginBar)<
iBullsPower(NULL, TimeFrame, BullsPeriod, PRICE_CLOSE, BeginBar+1) ) )
{ BeginBar++; }
TopBar++; //ïåðåõîäèì íà ñëåäóþùèé áàð
}//while
if (UpWaveNum< MaxWaveNum )
{//Åñëè äèâåðãåíöèÿ íàéäåíà óâåëè÷èâàåì ñ÷åò÷èê äèâåðãåíöèè
Div++;
// Ïðîâåðÿåì íå ïèê ëè ýòî èíäèêàòîðà
if ( (iBullsPower( NULL, TimeFrame, BullsPeriod, PRICE_CLOSE, TopBar)>
iBullsPower( NULL, TimeFrame, BullsPeriod, PRICE_CLOSE, TopBar-1)
&&
iBullsPower( NULL, TimeFrame, BullsPeriod, PRICE_CLOSE, TopBar)>
iBullsPower( NULL, TimeFrame, BullsPeriod, PRICE_CLOSE, TopBar+1)) )
UpWaveNum++ ;
switch(Div)
{//sw
case 1 : Div1Bar=TopBar;
Div1Wave=UpWaveNum;
break;
case 2 : Div2Bar=TopBar;
Div2Wave=UpWaveNum;
break;
case 3 : Div3Bar=TopBar;
Div3Wave=UpWaveNum;
break;
}//switch
}//div
//---------------------
return(TopBar);
//---------------------
}
//+--------------------------------------------------------------------------+
//| Îïðåäåëåíèå íàëè÷èÿ ñèãíàëà î ñìåíå íèñõîäÿùåãî òðåíäà èëè íà÷àëå îòêàòà
//+--------------------------------------------------------------------------+
int BreackDownTrendCondition (int TimeFrame, int WaveNum)
{
int TopBar;
// Print("Begin conv while");
TopBar=ContinueBarDown; //
while (!(
(iLow(NULL,TimeFrame,BeginBarDown)-bAddpips*Point<=iLow(NULL, TimeFrame,TopBar) &&
iBearsPower( NULL, TimeFrame, BearsPeriod, PRICE_CLOSE, BeginBarDown)>
iBearsPower( NULL, TimeFrame, BearsPeriod, PRICE_CLOSE, TopBar))
||
( DownWaveNum==WaveNum //cWaveTopNum
||
iBearsPower( NULL, TimeFrame, BearsPeriod, PRICE_CLOSE, TopBar)>0)
))
{
if ( (iBearsPower( NULL, TimeFrame, BearsPeriod, PRICE_CLOSE, TopBar)<
iBearsPower( NULL, TimeFrame, BearsPeriod, PRICE_CLOSE, TopBar-1) &&
iBearsPower( NULL, TimeFrame, BearsPeriod, PRICE_CLOSE, TopBar)<
iBearsPower( NULL, TimeFrame, BearsPeriod, PRICE_CLOSE, TopBar+1)) )
{ DownWaveNum++ ;
}
else
if (DownWaveNum==0 && Conv==0 &&
(iBearsPower( NULL, TimeFrame, BearsPeriod, PRICE_CLOSE, BeginBarDown)>
iBearsPower( NULL, TimeFrame, BearsPeriod, PRICE_CLOSE, BeginBarDown+1) ))
{BeginBarDown++;}
TopBar++;
}
if (DownWaveNum<WaveNum
)
{Conv++;
if ( (iBearsPower( NULL, TimeFrame, BearsPeriod, PRICE_CLOSE, TopBar)<
iBearsPower( NULL, TimeFrame, BearsPeriod, PRICE_CLOSE, TopBar-1) &&
iBearsPower( NULL, TimeFrame, BearsPeriod, PRICE_CLOSE, TopBar)<
iBearsPower( NULL, TimeFrame, BearsPeriod, PRICE_CLOSE, TopBar+1)) )
DownWaveNum++ ;
switch(Conv)
{
case 1 : Conv1Bar=TopBar;
Conv1Wave=DownWaveNum;
break;
case 2 : Conv2Bar=TopBar;
Conv2Wave=DownWaveNum;
break;
case 3 : Conv3Bar=TopBar;
Conv3Wave=DownWaveNum;
break;
}//switch
}//if
//---------------------
return(TopBar);
//---------------------
}
//+------------------------------------------------------------------+
// Èùåì äèâåðãåíöèþ
//+------------------------------------------------------------------+
bool SeachDiv(int TimeFrame, int MaxWaveNum, int StartBar=0)
{int BullDivBar=0; // Áàð äèâåðãåíöèè áûêîâ
dinstall(StartBar);
while (!( UpWaveNum==MaxWaveNum || Div>2 ||
iBullsPower( NULL, TimeFrame, BullsPeriod, PRICE_CLOSE,BullDivBar)<0 ) )
{
BullDivBar=BreackUpTrendCondition (TimeFrame, MaxWaveNum);//íàõîäèì áàð äèâåðãåíöèè
//óñòàíàâëèâàåì ñîïóòñòâóþùèå ñèãíàëüíûå ïåðåìåííûå
ContinueBar=BullDivBar+1;//óñòàíàâëèâàåì áàð äëÿ ñëåäóþùåãî ïîèñêà äèâåðãåíöèè
}//end while
// Èòîãîâûå ñîîáùåíèÿ
if (UpWaveNum < MaxWaveNum && Div>0)
{
// Alert(Symbol(),"Äèâåã1:Áàð ¹ ",Div1Bar," âîëíà ",Div1Wave,";Äèâåã2:Áàð ¹ ",Div2Bar," âîëíà ",Div2Wave,
// ";Äèâåã3:Áàð ¹ ",Div3Bar," âîëíà ",Div3Wave," Íà÷àëüíûé áàð=",BeginBar);
return(true);
}
else
return(false);
}
//+------------------------------------------------------------------+
// Èùåì êîíâåðãåíöèþ
//+------------------------------------------------------------------+
bool SeachConv(int TimeFrame, int cMaxWaveNum, int StartBar=0)
{ int BearConvBar=0; //Áàð êîíâåðãåíöèè ìåäâåäåé
//Print("Íà÷àëî áîëüøîãî öèêëà ïîèñêà êîâåðãåíöèè");
cinstall(StartBar);
while (!( DownWaveNum==cMaxWaveNum
|| Conv>2 ||
iBearsPower( NULL, TimeFrame, BearsPeriod, PRICE_CLOSE,BearConvBar)>0 )
//iBearsPower( NULL, TimeFrame, BearsPeriod, PRICE_CLOSE,ContinueBarDown)>0 )//ïðîïóñêàåò "óòîïëåííûõ" ìåäâåäåé
)
{ BearConvBar=BreackDownTrendCondition(TimeFrame, cMaxWaveNum );
ContinueBarDown=BearConvBar+1;
}//end while
// Èòîãîâûå ñîîáùåíèÿ
if (DownWaveNum<cMaxWaveNum
&& Conv>0)
{
// Alert(Symbol()," ",TimeFrame," ",buyADX," ",WaitUp ," Êîíâåã1:Áàð ¹ ",Conv1Bar," âîëíà ",Conv1Wave,";Êîíâåã2:Áàð ¹ ",Conv2Bar,
// " âîëíà ",Conv2Wave,";Êîíâåã3:Áàð ¹ ",Conv3Bar," âîëíà ",Conv3Wave," Íà÷àëüíûé áàð=",BeginBarDown);
return(true);
}
else
return(false);
}
bool DiverKind(int SeekBar, int TimeFrame)
{double MetaClose;
if (SeekBar==0)
MetaClose=Close[0]+sAddpips*Point;
else
MetaClose = iHigh(NULL,TimeFrame,SeekBar)+sAddpips*Point;
if( BeginBar<SeekBar+1
&&
(//1&&
(Div==1
&& MetaClose>iHigh(NULL,TimeFrame,Highest(NULL,TimeFrame,MODE_HIGH,Div1Bar,1))
&&
Div1Bar==Highest(NULL,TimeFrame,MODE_HIGH,3,Div1Bar-1)
)
||
(Div==2
&& MetaClose>iHigh(NULL,TimeFrame,Highest(NULL,TimeFrame,MODE_HIGH,Div2Bar,1))
&&
(Div2Bar==Highest(NULL,TimeFrame,MODE_HIGH,3,Div2Bar-1)
||
Div1Bar==Highest(NULL,TimeFrame,MODE_HIGH,3,Div1Bar-1)
)
)
||
(Div>=3
&&
MetaClose>iHigh(NULL,TimeFrame,Highest(NULL,TimeFrame,MODE_HIGH,Div3Bar,1) )
&&
(Div2Bar==Highest(NULL,TimeFrame,MODE_HIGH,3,Div2Bar-1)
//&&
||
Div3Bar==Highest(NULL,TimeFrame,MODE_HIGH,3,Div3Bar-1)
)
)
)//1&&
)
return(true);
else
return(false);
}
//+------------------------------------------------------------------+
// Òðåáîâàíèÿ ê êîíâåðãåíöèè
//+------------------------------------------------------------------+
bool ConvKind(int SeekBar, int TimeFrame)
{ double lowLevel;
if (SeekBar==0)
lowLevel=Close[0]-bAddpips*Point;
else
lowLevel=iLow(NULL,TimeFrame,SeekBar)-bAddpips*Point;
if ( BeginBarDown<SeekBar+1
&&
(//1
(Conv==1
&& lowLevel< iLow(NULL,TimeFrame,Lowest(NULL,TimeFrame,MODE_LOW,Conv1Bar,SeekBar+1))
&&
Conv1Bar==Lowest(NULL,TimeFrame,MODE_LOW,3,Conv1Bar-1))
||
(Conv==2
&& lowLevel< iLow(NULL,TimeFrame,Lowest(NULL,TimeFrame,MODE_LOW,Conv2Bar,SeekBar+1))
&&
(Conv1Bar==Lowest(NULL,TimeFrame,MODE_LOW,3,Conv1Bar-1)
||
Conv2Bar==Lowest(NULL,TimeFrame,MODE_LOW,3,Conv2Bar-1)
)
)
||
(Conv>2 //2
&& lowLevel< iLow(NULL,TimeFrame,Lowest(NULL,TimeFrame,MODE_LOW,Conv3Bar,SeekBar+1))
&&
(//3
Conv1Bar==Lowest(NULL,TimeFrame,MODE_LOW,3,Conv1Bar-1)
||
Conv2Bar==Lowest(NULL,TimeFrame,MODE_LOW,3,Conv2Bar-1)
||
Conv3Bar==Lowest(NULL,TimeFrame,MODE_LOW,3,Conv3Bar-1)
)//3
)//2
)//1
)
return(true);
else
return(false);
}
//+------------------------------------------------------------------+
//| expert initialization variables |
//+------------------------------------------------------------------+
void dinstall(int StrBar)
{ Div=0; // Èçíà÷àëüíî ñ÷èòàåì, ÷òî äèâèðãåíöèè íåò
Div1Bar=0;
Div2Bar=0;
Div3Bar=0;
Div1Wave=0; // íîìåð âîëíû äèâåðãåíöèè 1
Div2Wave=0; // íîìåð âîëíû äèâåðãåíöèè 2
Div3Wave=0; // íîìåð âîëíû äèâåðãåíöèè 3
UpWaveNum=0 ;
//=== áàðû ñ êîòîðûìè îïðåäåëÿþòñÿ äèâåðãåíöèÿ è êîíâåðãåíöèÿ ===
BeginBar=StrBar;
ContinueBar=BeginBar+1;
return;
}
void cinstall(int StrBar)
{
Conv =0 ;
Conv1Bar=0;
Conv2Bar=0;
Conv3Bar=0;
Conv1Wave=0 ; // íîìåð âîëíû êîíâåðãåíöèè 1
Conv2Wave=0; // íîìåð âîëíû êîíâåðãåíöèè 2
Conv3Wave=0; // íîìåð âîëíû êîíâåðãåíöèè 3
DownWaveNum=0 ;
BeginBarDown=StrBar;
ContinueBarDown=StrBar+1;
return;
}
void AddpipsInit(int spips,int bpips)
{
sAddpips=spips;
bAddpips=bpips;
return;
}
void PeriodInit(int P1,int P2)
{
BearsPeriod=P1;
BullsPeriod=P2;
return;
}
//+------------------------------------------------------------------+
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---