ent-testing/indicator_includes/MacdSignal.mqh
super.admin 1a8f6f22b7 convert
2025-05-30 14:53:02 +02:00

65 lines
No EOL
2.7 KiB
MQL5

//+------------------------------------------------------------------+
//| MacdSignal.mqh |
//| Entr04y |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Entr04y"
#property link "https://www.mql5.com"
// Macd Variables
int MacdFast = 12;
int MacdSlow = 26;
int MacdSignal = 9;
//+------------------------------------------------------------------+
//| Init handler |
//+------------------------------------------------------------------+
int MacdIndicatorHandler()
{
return iMACD(Symbol(),Period(),MacdFast,MacdSlow,MacdSignal,InpAppliedPrice);
}
//+------------------------------------------------------------------+
//| Custom Macd Function |
//+------------------------------------------------------------------+
// Because we will have multiple handlers, we need to know which one applies to this function
string GetMacdOpenSignal(int Handler)
{
// set symbol string and indicator buffers
string CurrentSymbol = Symbol();
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";
}
}
//+------------------------------------------------------------------+