forked from mnbvc188199/Warrior_EA
- Replaced standard library signal modules with custom implementations to allow for named patterns and improved voting. - Added new input parameters for module weights, allowing for optimization of individual signal contributions. - Enhanced the management of trades with new options for breakeven and management cut. - Introduced a mechanism for dynamic ranking of signal weights based on historical performance. - Improved initialization logic to ensure proper registration of filters and handling of trading conditions. - Added detailed logging for trading permissions and account status during initialization.
233 lines
11 KiB
MQL5
233 lines
11 KiB
MQL5
//+------------------------------------------------------------------+
| |
//| OscillatorDivergence.mqh |
| |
//| Shared extremum/divergence bit-map detector used by CSignalRSI |
| |
//| and CSignalMACD (formerly StateRSI/StateMain, ExtStateRSI/ |
| |
//| ExtState, CompareMaps - duplicated verbatim between the two |
| |
//| files except for the oscillator value source). |
| |
//| |
| |
//| MQL5 gives a class exactly one base and both signals already |
| |
//| extend CWarriorSignal, so this cannot be a common base |
| |
//| class - CDivergenceDetector is a composed collaborator, fed the |
| |
//| oscillator value and the price-extremum series through the |
| |
//| IOscillatorDivergenceSource view each owner implements via a |
| |
//| thin adapter (same view+adapter shape as Expert/*, see |
| |
//| project_oop_module_pattern.md). |
| |
//+------------------------------------------------------------------+
| |
#ifndef __OSCILLATOR_DIVERGENCE_MQH__
| |
#define __OSCILLATOR_DIVERGENCE_MQH__
| |
| |
//+------------------------------------------------------------------+
| |
//| What CDivergenceDetector needs from its owning signal: the |
| |
//| oscillator's own value series, and the price series' extremum |
| |
//| lookup (CiLow::MinValue / CiHigh::MaxValue) - both protected on |
| |
//| CExpertSignal, reached here only through the owner's public API. |
| |
//+------------------------------------------------------------------+
| |
class IOscillatorDivergenceSource
| |
{
| |
public:
| |
virtual double DivergenceOscillatorValue(int ind) = 0;
| |
virtual double DivergencePriceLow(int start, int count, int &index) = 0;
| |
virtual double DivergencePriceHigh(int start, int count, int &index) = 0;
| |
};
| |
| |
//+------------------------------------------------------------------+
| |
//| Class CDivergenceDetector. |
| |
//| Walks the bound oscillator counting consecutive up/down runs to |
| |
//| find local extrema, builds a 4-bit-per-extremum bit-map |
| |
//| comparing each new extreme against the one two positions back |
| |
//| for both the oscillator and price, then tests that bit-map |
| |
//| against a caller-supplied pattern. |
| |
//+------------------------------------------------------------------+
| |
class CDivergenceDetector
| |
{
| |
private:
| |
IOscillatorDivergenceSource *m_source;
| |
//--- variables
| |
double m_extr_osc[10]; // array of values of extremums of the oscillator
| |
double m_extr_pr[10]; // array of values of the corresponding extremums of price
| |
int m_extr_pos[10]; // array of shifts of extremums (in bars)
| |
uint m_extr_map; // resulting bit-map of ratio of extremums of the oscillator and the price
| |
| |
double Value(int ind) { return(m_source.DivergenceOscillatorValue(ind)); }
| |
double Diff(int ind) { return(Value(ind) - Value(ind + 1)); }
| |
int State(int ind);
| |
| |
public:
| |
CDivergenceDetector(void) : m_source(NULL), m_extr_map(0) {}
| |
void Bind(IOscillatorDivergenceSource *source) { m_source = source; }
| |
bool ExtState(int ind);
| |
bool CompareMaps(int map, int count, bool minimax = false, int start = 0);
| |
};
| |
//+------------------------------------------------------------------+
| |
//| Check of the oscillator state. |
| |
//+------------------------------------------------------------------+
| |
int CDivergenceDetector::State(int ind)
| |
{
| |
int res = 0;
| |
double var;
| |
//---
| |
for(int i = ind;; i++)
| |
{
| |
if(Value(i + 1) == EMPTY_VALUE)
| |
break;
| |
var = Diff(i);
| |
if(res > 0)
| |
{
| |
if(var < 0)
| |
break;
| |
res++;
| |
continue;
| |
}
| |
if(res < 0)
| |
{
| |
if(var > 0)
| |
break;
| |
res--;
| |
continue;
| |
}
| |
if(var > 0)
| |
res++;
| |
if(var < 0)
| |
res--;
| |
}
| |
//---
| |
return(res);
| |
}
| |
//+------------------------------------------------------------------+
| |
//| Extended check of the oscillator state consists |
| |
//| in forming a bit-map according to certain rules, |
| |
//| which shows ratios of extremums of the oscillator and price. |
| |
//+------------------------------------------------------------------+
| |
bool CDivergenceDetector::ExtState(int ind)
| |
{
| |
//--- operation of this method results in a bit-map of extremums
| |
//--- practically, the bit-map of extremums is an "array" of 4-bit fields
| |
//--- each "element of the array" definitely describes the ratio
| |
//--- of current extremums of the oscillator and the price with previous ones
| |
//--- purpose of bits of an element of the analyzed bit-map
| |
//--- bit 3 - not used (always 0)
| |
//--- bit 2 - is equal to 1 if the current extremum of the oscillator is "more extreme" than the previous one
| |
//--- (a higher peak or a deeper valley), otherwise - 0
| |
//--- bit 1 - not used (always 0)
| |
//--- bit 0 - is equal to 1 if the current extremum of price is "more extreme" than the previous one
| |
//--- (a higher peak or a deeper valley), otherwise - 0
| |
//--- in addition to them, the following is formed:
| |
//--- array of values of extremums of the oscillator,
| |
//--- array of values of price extremums and
| |
//--- array of "distances" between extremums of the oscillator (in bars)
| |
//--- it should be noted that when using the results of the extended check of state,
| |
//--- you should consider, which extremum of the oscillator (peak or valley)
| |
//--- is the "reference point" (i.e. was detected first during the analysis)
| |
//--- if a peak is detected first then even elements of all arrays
| |
//--- will contain information about peaks, and odd elements will contain information about valleys
| |
//--- if a valley is detected first, then respectively in reverse
| |
int pos = ind, off, index;
| |
uint map; // intermediate bit-map for one extremum
| |
//---
| |
m_extr_map = 0;
| |
for(int i = 0; i < 10; i++)
| |
{
| |
off = State(pos);
| |
if(off > 0)
| |
{
| |
//--- minimum of the oscillator is detected
| |
pos += off;
| |
m_extr_pos[i] = pos;
| |
m_extr_osc[i] = Value(pos);
| |
if(i > 1)
| |
{
| |
m_extr_pr[i] = m_source.DivergencePriceLow(pos - 2, 5, index);
| |
//--- form the intermediate bit-map
| |
map = 0;
| |
if(m_extr_pr[i - 2] < m_extr_pr[i])
| |
map += 1; // set bit 0
| |
if(m_extr_osc[i - 2] < m_extr_osc[i])
| |
map += 4; // set bit 2
| |
//--- add the result
| |
m_extr_map += map << (4 * (i - 2));
| |
}
| |
else
| |
m_extr_pr[i] = m_source.DivergencePriceLow(pos - 1, 4, index);
| |
}
| |
else
| |
{
| |
//--- maximum of the oscillator is detected
| |
pos -= off;
| |
m_extr_pos[i] = pos;
| |
m_extr_osc[i] = Value(pos);
| |
if(i > 1)
| |
{
| |
m_extr_pr[i] = m_source.DivergencePriceHigh(pos - 2, 5, index);
| |
//--- form the intermediate bit-map
| |
map = 0;
| |
if(m_extr_pr[i - 2] > m_extr_pr[i])
| |
map += 1; // set bit 0
| |
if(m_extr_osc[i - 2] > m_extr_osc[i])
| |
map += 4; // set bit 2
| |
//--- add the result
| |
m_extr_map += map << (4 * (i - 2));
| |
}
| |
else
| |
m_extr_pr[i] = m_source.DivergencePriceHigh(pos - 1, 4, index);
| |
}
| |
}
| |
//---
| |
return(true);
| |
}
| |
//+------------------------------------------------------------------+
| |
//| Comparing the bit-map of extremums with pattern. |
| |
//+------------------------------------------------------------------+
| |
bool CDivergenceDetector::CompareMaps(int map, int count, bool minimax, int start)
| |
{
| |
int step = (minimax) ? 4 : 8;
| |
int total = step * (start + count);
| |
//--- check input parameters for a possible going out of range of the bit-map
| |
if(total > 32)
| |
return(false);
| |
//--- bit-map of the patter is an "array" of 4-bit fields
| |
//--- each "element of the array" definitely describes the desired ratio
| |
//--- of current extremums of the oscillator and the price with previous ones
| |
//--- purpose of bits of an elements of the pattern of the bit-map pattern
| |
//--- bit 3 - is equal to if the ratio of extremums of the oscillator is insignificant for us
| |
//--- is equal to 0 if we want to "find" the ratio of extremums of the oscillator determined by the value of bit 2
| |
//--- bit 2 - is equal to 1 if we want to "discover" the situation when the current extremum of the "oscillator" is "more extreme" than the previous one
| |
//--- (current peak is higher or current valley is deeper)
| |
//--- is equal to 0 if we want to "discover" the situation when the current extremum of the oscillator is "less extreme" than the previous one
| |
//--- (current peak is lower or current valley is less deep)
| |
//--- bit 1 - is equal to 1 if the ratio of extremums is insignificant for us
| |
//--- it is equal to 0 if we want to "find" the ratio of price extremums determined by the value of bit 0
| |
//--- bit 0 - is equal to 1 if we want to "discover" the situation when the current price extremum is "more extreme" than the previous one
| |
//--- (current peak is higher or current valley is deeper)
| |
//--- it is equal to 0 if we want to "discover" the situation when the current price extremum is "less extreme" than the previous one
| |
//--- (current peak is lower or current valley is less deep)
| |
uint inp_map, check_map;
| |
int i, j;
| |
//--- loop by extremums (4 minimums and 4 maximums)
| |
//--- price and the oscillator are checked separately (thus, there are 16 checks)
| |
for(i = step * start, j = 0; i < total; i += step, j += 4)
| |
{
| |
//--- "take" two bits - patter of the corresponding extremum of the price
| |
inp_map = (map >> j) & 3;
| |
//--- if the higher-order bit=1, then any ratio is suitable for us
| |
if(inp_map < 2)
| |
{
| |
//--- "take" two bits of the corresponding extremum of the price (higher-order bit is always 0)
| |
check_map = (m_extr_map >> i) & 3;
| |
if(inp_map != check_map)
| |
return(false);
| |
}
| |
//--- "take" two bits - pattern of the corresponding oscillator extremum
| |
inp_map = (map >> (j + 2)) & 3;
| |
//--- if the higher-order bit=1, then any ratio is suitable for us
| |
if(inp_map >= 2)
| |
continue;
| |
//--- "take" two bits of the corresponding oscillator extremum (higher-order bit is always 0)
| |
check_map = (m_extr_map >> (i + 2)) & 3;
| |
if(inp_map != check_map)
| |
return(false);
| |
}
| |
//--- ok
| |
return(true);
| |
}
| |
#endif // __OSCILLATOR_DIVERGENCE_MQH__
| |
//+------------------------------------------------------------------+
|