//+------------------------------------------------------------------+ //| MacdSignal-Class.mqh | //| Entr04y | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Entr04y" #property link "https://www.mql5.com" input int InpMacdFast = 12; input int InpMacdSlow = 26; input int InpMacdSignal = 9; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class CMacdSignal { private: // Macd Variables int m_MacdFast; int m_MacdSlow; int m_MacdSignal; ENUM_APPLIED_PRICE m_InpAppliedPrice; public: void Init() { m_MacdFast = InpMacdFast; m_MacdSlow = InpMacdSlow; m_MacdSignal = InpMacdSignal; m_InpAppliedPrice = InpAppliedPrice; } //+------------------------------------------------------------------+ //| Init handler | //+------------------------------------------------------------------+ int IndicatorHandler(string CurrentSymbol) { return iMACD(CurrentSymbol,Period(),m_MacdFast,m_MacdSlow,m_MacdSignal,m_InpAppliedPrice); } //+------------------------------------------------------------------+ //| Custom Macd Function | //+------------------------------------------------------------------+ // Because we will have multiple handlers, we need to know which one applies to this function string GetOpenSignal(int Handler, string CurrentSymbol) { // set symbol string and indicator buffers const int StartCandle = 0; const int RequiredCandles = 3; // How many candles to store // Indicator variables and buffers const int IndexMacd = 0; // Macd line const int IndexSignal = 1; // Signal Line double BufferMacd[]; double BufferSignal[]; // Define Macd and signal lines from open candle for 3 candles and store results bool fillMacd = CopyBuffer(Handler, IndexMacd, StartCandle, RequiredCandles, BufferMacd); bool fillSignal = CopyBuffer(Handler, IndexSignal, StartCandle, RequiredCandles, BufferSignal); if(fillMacd==false || fillSignal==false) return "Buffer Not Full"; // If buffers aren't filled, return to end onTick // Find required Macd signal lines and normalize to 10 places to prevent rounding errors in x-overs double currentMacd = NormalizeDouble(BufferMacd[1], 10); double currentSignal = NormalizeDouble(BufferSignal[1], 10); double priorMacd = NormalizeDouble(BufferMacd[0], 10); double priorSignal = NormalizeDouble(BufferSignal[0], 10); // Submit Macd Long and short trades if(priorMacd <= priorSignal && currentMacd > currentSignal) { return "LONG"; } else if(priorMacd >= priorSignal && currentMacd < currentSignal) { return "SHORT"; } else { return "NO TRADE"; } } }; //+------------------------------------------------------------------+