Warrior_EA/Signals/SignalMACD.mqh

367 lines
17 KiB
MQL5

//+------------------------------------------------------------------+
//| SignalMACD.mqh |
//| Copyright 2000-2023, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#include "..\Expert\WarriorSignal.mqh"
#include "OscillatorDivergence.mqh"
// wizard description start
//+------------------------------------------------------------------+
//| Description of the class |
//| Title=Signals of oscillator 'MACD' |
//| Type=SignalAdvanced |
//| Name=MACD |
//| ShortName=MACD |
//| Class=CSignalMACD |
//| Page=signal_macd |
//| Parameter=PeriodFast,int,12,Period of fast EMA |
//| Parameter=PeriodSlow,int,26,Period of slow EMA |
//| Parameter=PeriodSignal,int,9,Period of averaging of difference |
//| Parameter=Applied,ENUM_APPLIED_PRICE,PRICE_CLOSE,Prices series |
//+------------------------------------------------------------------+
// wizard description end
//+------------------------------------------------------------------+
//| Class CSignalMACD. |
//| Purpose: Class of generator of trade signals based on |
//| the 'Moving Average Convergence/Divergence' oscillator. |
//| Is derived from the CWarriorSignal class. |
//| |
//| Ported from the MQL5 standard library (Include\Expert\Signal\ |
//| SignalMACD.mqh) - m_id/m_pattern_count/m_active_pattern/ |
//| m_active_direction/ApplyPatternWeight() are this codebase's |
//| additions on top, feeding the panel and the DB pattern-ranking |
//| table exactly like CSignalMA/CSignalRSI do. |
//| |
//| This file previously existed here and was deleted when the |
//| classic-signal set was trimmed to MA+RSI; it is restored |
//| deliberately, to give the classic vote a momentum/divergence |
//| model that MA (level) and RSI (bounded oscillator) do not carry. |
//| It uses the built-in CiMACD, NOT the unified ADMovingAverage |
//| custom indicator CSignalMA drives - MACD is defined on plain |
//| EMAs, so there is nothing for MA_Type to select here. |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| CSignalMACD's side of the IOscillatorDivergenceSource view - |
//| MQL5 has no multiple inheritance, so the divergence detector is |
//| reached through this thin adapter instead of a common base. |
//+------------------------------------------------------------------+
class CSignalMACD;
class CSignalMACDDivergenceSource : public IOscillatorDivergenceSource
{
private:
CSignalMACD *m_owner;
public:
void Bind(CSignalMACD *owner) { m_owner = owner; }
virtual double DivergenceOscillatorValue(int ind) override;
virtual double DivergencePriceLow(int start, int count, int &index) override;
virtual double DivergencePriceHigh(int start, int count, int &index) override;
};
class CSignalMACD : public CWarriorSignal
{
protected:
CiMACD m_MACD; // object-oscillator
//--- adjusted parameters
int m_period_fast; // the "period of fast EMA" parameter of the oscillator
int m_period_slow; // the "period of slow EMA" parameter of the oscillator
int m_period_signal; // the "period of averaging of difference" parameter of the oscillator
ENUM_APPLIED_PRICE m_applied; // the "price series" parameter of the oscillator
//--- "weights" of market models (0-100)
int m_pattern_0; // model 0 "the oscillator has required direction"
int m_pattern_1; // model 1 "reverse of the oscillator to required direction"
int m_pattern_2; // model 2 "crossing of main and signal line"
int m_pattern_3; // model 3 "crossing of main line an the zero level"
int m_pattern_4; // model 4 "divergence of the oscillator and price"
int m_pattern_5; // model 5 "double divergence of the oscillator and price"
//--- shared extremum/divergence bit-map detector (see Signals/OscillatorDivergence.mqh)
CSignalMACDDivergenceSource m_divergenceSource;
CDivergenceDetector m_divergence;
public:
CSignalMACD(void);
~CSignalMACD(void);
//--- methods of setting adjustable parameters
void PeriodFast(int value) { m_period_fast = value; }
void PeriodSlow(int value) { m_period_slow = value; }
void PeriodSignal(int value) { m_period_signal = value; }
void Applied(ENUM_APPLIED_PRICE value) { m_applied = value; }
//--- methods of adjusting "weights" of market models
void Pattern_0(int value) { m_pattern_0 = value; }
void Pattern_1(int value) { m_pattern_1 = value; }
void Pattern_2(int value) { m_pattern_2 = value; }
void Pattern_3(int value) { m_pattern_3 = value; }
void Pattern_4(int value) { m_pattern_4 = value; }
void Pattern_5(int value) { m_pattern_5 = value; }
virtual void ApplyPatternWeight(int patternNumber, int weight);
//--- method of verification of settings
virtual bool ValidationSettings(void);
//--- method of creating the indicator and timeseries
virtual bool InitIndicators(CIndicators *indicators);
//--- methods of checking if the market models are formed
virtual int LongCondition(void);
virtual int ShortCondition(void);
//--- deep-history readiness for the meta candidate sweep - see CWarriorSignal::SweepPrepare
//--- IOscillatorDivergenceSource surface for m_divergenceSource (see OscillatorDivergence.mqh);
//--- public because the adapter is a separate object, not a subclass
double DivergenceOscillatorValue(int ind) { return(Main(ind)); }
double DivergencePriceLow(int start, int count, int &index) { return(m_low.MinValue(start, count, index)); }
double DivergencePriceHigh(int start, int count, int &index) { return(m_high.MaxValue(start, count, index)); }
protected:
//--- method of initialization of the oscillator
bool InitMACD(CIndicators *indicators);
//--- methods of getting data
double Main(int ind) { return(m_MACD.Main(ind)); }
double Signal(int ind) { return(m_MACD.Signal(ind)); }
double DiffMain(int ind) { return(Main(ind) - Main(ind + 1)); }
double State(int ind) { return(Main(ind) - Signal(ind)); }
};
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CSignalMACD::CSignalMACD(void) : m_period_fast(12),
m_period_slow(26),
m_period_signal(9),
m_applied(PRICE_CLOSE),
m_pattern_0(10),
m_pattern_1(30),
m_pattern_2(80),
m_pattern_3(50),
m_pattern_4(60),
m_pattern_5(100)
{
m_divergenceSource.Bind(GetPointer(this));
m_divergence.Bind(GetPointer(m_divergenceSource));
m_id = "MACD";
//--- Confirmation pattern(s): weight 10, never traded alone, never ranked.
m_confirm_mask = 1<<0;
m_pattern_count = 6;
//--- initialization of protected data
m_used_series = USE_SERIES_HIGH + USE_SERIES_LOW;
}
//+------------------------------------------------------------------+
//| Destructor |
//+------------------------------------------------------------------+
CSignalMACD::~CSignalMACD(void)
{
}
//+------------------------------------------------------------------+
//| Validation settings protected data. |
//+------------------------------------------------------------------+
bool CSignalMACD::ValidationSettings(void)
{
//--- validation settings of additional filters
if(!CWarriorSignal::ValidationSettings())
return(false);
//--- initial data checks
if(m_period_fast >= m_period_slow)
{
printf(__FUNCTION__ + ": slow period must be greater than fast period");
return(false);
}
//--- ok
return(true);
}
//+------------------------------------------------------------------+
//| Create indicators. |
//+------------------------------------------------------------------+
bool CSignalMACD::InitIndicators(CIndicators *indicators)
{
//--- check of pointer is performed in the method of the parent class
//---
//--- initialization of indicators and timeseries of additional filters
if(!CWarriorSignal::InitIndicators(indicators))
return(false);
//--- create and initialize MACD oscilator
if(!InitMACD(indicators))
return(false);
//--- ok
return(true);
}
//+------------------------------------------------------------------+
//| Initialize MACD oscillators. |
//+------------------------------------------------------------------+
bool CSignalMACD::InitMACD(CIndicators *indicators)
{
//--- add object to collection
if(!indicators.Add(GetPointer(m_MACD)))
{
printf(__FUNCTION__ + ": error adding object");
return(false);
}
//--- initialize object
if(!m_MACD.Create(m_symbol.Name(), m_period, m_period_fast, m_period_slow, m_period_signal, m_applied))
{
printf(__FUNCTION__ + ": error initializing object");
return(false);
}
//--- ok
return(true);
}
//+------------------------------------------------------------------+
//| CSignalMACDDivergenceSource - forwards to the owner's public |
//| IOscillatorDivergenceSource surface (see the class declaration |
//| just above CSignalMACD). |
//+------------------------------------------------------------------+
double CSignalMACDDivergenceSource::DivergenceOscillatorValue(int ind)
{
return(m_owner.DivergenceOscillatorValue(ind));
}
double CSignalMACDDivergenceSource::DivergencePriceLow(int start, int count, int &index)
{
return(m_owner.DivergencePriceLow(start, count, index));
}
double CSignalMACDDivergenceSource::DivergencePriceHigh(int start, int count, int &index)
{
return(m_owner.DivergencePriceHigh(start, count, index));
}
//+------------------------------------------------------------------+
//| "Voting" that price will grow. |
//+------------------------------------------------------------------+
int CSignalMACD::LongCondition(void)
{
int result = 0;
int idx = StartIndex();
//--- check direction of the main line
if(DiffMain(idx) > 0.0)
{
//--- the main line is directed upwards, and it confirms the possibility of price growth
if(IS_PATTERN_USAGE(0))
{
result = m_pattern_0;
m_active_pattern = "Pattern_0";
}
// "confirming" signal number 0
//--- if the model 1 is used, look for a reverse of the main line
if(IS_PATTERN_USAGE(1) && DiffMain(idx + 1) < 0.0)
{
result = m_pattern_1;
m_active_pattern = "Pattern_1";
} // signal number 1
//--- if the model 2 is used, look for an intersection of the main and signal line
if(IS_PATTERN_USAGE(2) && State(idx) > 0.0 && State(idx + 1) < 0.0)
{
result = m_pattern_2;
m_active_pattern = "Pattern_2";
} // signal number 2
//--- if the model 3 is used, look for an intersection of the main line and the zero level
if(IS_PATTERN_USAGE(3) && Main(idx) > 0.0 && Main(idx + 1) < 0.0)
{
result = m_pattern_3;
m_active_pattern = "Pattern_3";
} // signal number 3
//--- if the models 4 or 5 are used and the main line turned upwards below the zero level, look for divergences
if((IS_PATTERN_USAGE(4) || IS_PATTERN_USAGE(5)) && Main(idx) < 0.0)
{
//--- perform the extended analysis of the oscillator state
m_divergence.ExtState(idx);
//--- if the model 4 is used, look for the "divergence" signal
if(IS_PATTERN_USAGE(4) && m_divergence.CompareMaps(1, 1)) // 0000 0001b
{
result = m_pattern_4;
m_active_pattern = "Pattern_4";
} // signal number 4
//--- if the model 5 is used, look for the "double divergence" signal
if(IS_PATTERN_USAGE(5) && m_divergence.CompareMaps(0x11, 2)) // 0001 0001b
{
result = m_pattern_5;
m_active_pattern = "Pattern_5";
} // signal number 5
}
}
if(result != 0)
{
m_active_direction = "Buy";
}
//--- return the result
return(result);
}
//+------------------------------------------------------------------+
//| "Voting" that price will fall. |
//+------------------------------------------------------------------+
int CSignalMACD::ShortCondition(void)
{
int result = 0;
int idx = StartIndex();
// Check direction of the main line
if(DiffMain(idx) < 0.0)
{
// Main line is directed downwards, confirming a possibility of falling of price
if(IS_PATTERN_USAGE(0))
{
result = m_pattern_0; // "confirming" signal number 0
m_active_pattern = "Pattern_0";
}
// If the model 1 is used, look for a reverse of the main line
if(IS_PATTERN_USAGE(1) && DiffMain(idx + 1) > 0.0)
{
result = m_pattern_1; // signal number 1
m_active_pattern = "Pattern_1";
}
// If the model 2 is used, look for an intersection of the main and signal line
if(IS_PATTERN_USAGE(2) && State(idx) < 0.0 && State(idx + 1) > 0.0)
{
result = m_pattern_2; // signal number 2
m_active_pattern = "Pattern_2";
}
// If the model 3 is used, look for an intersection of the main line and the zero level
if(IS_PATTERN_USAGE(3) && Main(idx) < 0.0 && Main(idx + 1) > 0.0)
{
result = m_pattern_3; // signal number 3
m_active_pattern = "Pattern_3";
}
// If the models 4 or 5 are used and the main line turned downwards above the zero level, look for divergences
if((IS_PATTERN_USAGE(4) || IS_PATTERN_USAGE(5)) && Main(idx) > 0.0)
{
// Perform the extended analysis of the oscillator state
m_divergence.ExtState(idx);
// If the model 4 is used, look for the "divergence" signal
if(IS_PATTERN_USAGE(4) && m_divergence.CompareMaps(1, 1)) // 0000 0001b
{
result = m_pattern_4; // signal number 4
m_active_pattern = "Pattern_4";
}
// If the model 5 is used, look for the "double divergence" signal
if(IS_PATTERN_USAGE(5) && m_divergence.CompareMaps(0x11, 2)) // 0001 0001b
{
result = m_pattern_5; // signal number 5
m_active_pattern = "Pattern_5";
}
}
}
if(result != 0)
{
m_active_direction = "Sell";
}
// Return the result
return result;
}
//+------------------------------------------------------------------+
//| Set the specified pattern's weight to the specified value |
//+------------------------------------------------------------------+
void CSignalMACD::ApplyPatternWeight(int patternNumber, int weight)
{
switch(patternNumber)
{
default:
break;
case 0:
Pattern_0(weight);
break;
case 1:
Pattern_1(weight);
break;
case 2:
Pattern_2(weight);
break;
case 3:
Pattern_3(weight);
break;
case 4:
Pattern_4(weight);
break;
case 5:
Pattern_5(weight);
break;
}
}
//+------------------------------------------------------------------+