Начало беседы из 4 сообщений (1322 токенов из 2048 использовано)Angelika писал(а): В сгенерированном тобой коде есть ошибки, в частности high и low компилятор видит как undeclared identifier. Как ты можешь решить эту проблему?
Извините за недоразумение. Да, действительно, `high` и `low` в контексте функции `OnCalculate` должны использоваться через массивы, передаваемые этой функции в качестве параметров. Давайте исправим код, чтобы он корректно использовал переданные массивы цен.
Вот исправленная версия кода:
```mql5
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
}
//+------------------------------------------------------------------+
//| Function to calculate the EMA of the difference between High and Low prices |
//+------------------------------------------------------------------+
double CalculateEMA(const double &high[], const double &low[], int current_index, int period)
{
double sum = 0.0;
double multiplier = 2.0 / (period + 1);
double ema = 0.0;
// Calculate the initial SMA for the first 'period' bars
for (int i = 0; i < period; i++)
{
double hl_difference = high[current_index + i] - low[current_index + i];
sum += hl_difference;
}
ema = sum / period;
// Calculate the EMA from the 'period' bar onwards
for (int i = period; i >= 0; i--)
{
double hl_difference = high[current_index + i] - low[current_index + i];
ema = (hl_difference - ema) * multiplier + ema;
}
return ema;
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
int period = 14; // For example, use a 14-bar EMA period
if (rates_total < period)
return(0);
double ema_value = CalculateEMA(high, low, 0, period);
Print("EMA value of High-Low difference: ", ema_value);
return(rates_total);
}
```
Теперь массивы `high` и `low` используются правильно, передаваясь в функцию `CalculateEMA` как параметры. Также добавлен параметр `current_index`, который указывает текущий индекс бара для расчёта. В данном случае он задан как `0`, что означает использование последних данных.
Пожалуйста, проверьте этот код в вашей среде MetaTrader 5.