I want the MACD values all at once!
Hello. This is just a little fx.
Well, as the title suggests,EA CreatorI tried making a function that grabs all at once the MACD values based on source code output by EA Creator.
Honestly, not great.
Now, without further ado, the function.
//--------------------------------------------------------------------------------------------------------+
//| Function to obtain MACD and Signal values
//| Processing: Get MACD and Signal line values for a specified timeframe, EMA periods, and signal SMA period
//| Arguments: period - Timeframe (0 uses the current chart's timeframe)
//| fastEMA - Short-term EMA period
//| slowEMA - Long-term EMA period
//| signalSMA - SMA period for the signal line
//| shift - Bar position to retrieve (0 for current bar, 1 for one bar ago, etc.)
//| Return: MACDValues structure containing MACD and Signal line values
//--------------------------------------------------------------------------------------------------------+
struct MACDValues {
double macd; // MACD value
double signal; // Signal line value
};
MACDValues getMACDValues(string symbol, int period, int fastEMA, int slowEMA, int signalSMA, int shift)
{
MACDValues macdValues;
// Retrieve MACD value
macdValues.macd = iMACD(symbol, period, fastEMA, slowEMA, signalSMA, PRICE_CLOSE, MODE_MAIN, shift);
// Retrieve Signal line value
macdValues.signal = iMACD(symbol, period, fastEMA, slowEMA, signalSMA, PRICE_CLOSE, MODE_SIGNAL, shift);
return macdValues;
}
I tried using a struct variable.
Here is how to use it.
void OnTick()
{
// Set MACD parameters
string symbol = Symbol();
int fastEMA = 12;
int slowEMA = 26;
int signalSMA = 9;
int period = PERIOD_H1; // Use the current chart's timeframe (1-hour chart)
// Retrieve MACD and Signal values
MACDValues macdValues = getMACDValues(symbol, period, fastEMA, slowEMA, signalSMA, 0);
// Print MACD and Signal values to the log
Print("MACD: ", macdValues.macd, " Signal: ", macdValues.signal);
}How about that?
Once you get used to it, it’s easy.
See you again.
File download is below.
× ![]()