//+------------------------------------------------------------------+ // ATR.mq5 //…
- User
//+------------------------------------------------------------------+ //| ATR.mq5 | //| Copyright 2024, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2024, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #property indicator_chart_window #property indicator_buffers 0 #property indicator_plots 0
//— input parameters input int InpAtrPeriod = 10; // ATR period
//— indicator buffers
//±-----------------------------------------------------------------+ //| Custom indicator initialization function | //±-----------------------------------------------------------------+ int OnInit() { return (INIT_SUCCEEDED); }
//±-----------------------------------------------------------------+ //| 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[]) {
double H_L[10]; int m = 0; double openATR[10];
// Calculate moving average and MADiff for(int i = rates_total-1; i > rates_total-InpAtrPeriod-1; i--) { double maxHigh = high[i]; double minLow = low[i]; openATR[m] = open[i]; H_L[m] = NormalizeDouble(maxHigh - minLow,_Digits); m++; }
double ATR = 0; for(int i=0;i<9;i++) { double sum = H_L[i]+H_L[i+1]; ATR += sum/2/openATR[i+1]*100; }
double ATRavg = NormalizeDouble(ATR/9,2);
double priceHigh = openATR[0] + (openATR[0]*ATRavg/100); double priceLow = openATR[0] - (openATR[0]*ATRavg/100); double priceHigh_2 = openATR[0] + (openATR[0]*ATRavg/200); double priceLow_2 = openATR[0] - (openATR[0]*ATRavg/200);
datetime time_end = iTime(NULL, 0, 0);
// Вычисляем время на 10 свечей назад от текущего времени datetime time_start = iTime(NULL, 0, 10);
ObjectCreate(0, "priceHigh", OBJ_TREND, 0, time_start, priceHigh, time_end, priceHigh); ObjectCreate(0, "priceLow", OBJ_TREND, 0, time_start, priceLow, time_end, priceLow); ObjectCreate(0, "priceHigh_0.5", OBJ_TREND, 0, time_start, priceHigh_2, time_end, priceHigh_2); ObjectCreate(0, "priceLow_0.5", OBJ_TREND, 0, time_start, priceLow_2, time_end, priceLow_2);
ObjectSetInteger(0, "priceHigh", OBJPROP_COLOR, clrCrimson); ObjectSetInteger(0, "priceLow", OBJPROP_COLOR, clrCrimson); ObjectSetInteger(0, "priceHigh_0.5", OBJPROP_COLOR, clrBlue); ObjectSetInteger(0, "priceLow_0.5", OBJPROP_COLOR, clrBlue);
ObjectSetInteger(0, "priceHigh", OBJPROP_WIDTH, 2); ObjectSetInteger(0, "priceLow", OBJPROP_WIDTH, 2); ObjectSetInteger(0, "priceHigh_0.5", OBJPROP_WIDTH, 2); ObjectSetInteger(0, "priceLow_0.5", OBJPROP_WIDTH, 2);
return(rates_total);
} //±-----------------------------------------------------------------+ нужно что бы на каком бы я таймфрейме не находился он считал как будто я на ммесячном
- Assistant
Чтобы ваш индикатор рассчитывал значения как если бы он был на месячном таймфрейме, независимо от текущего таймфрейма графика, ваш код должен использовать данные месячного таймфрейма. В MQL5 для получения данных абсолютно другого таймфрейма можно использовать функции
iHigh,iLow,iOpen, и т.д. с указанием таймфреймаPERIOD_MN1(для месячных данных).Вот обновленный код для индикатора:
//+------------------------------------------------------------------+ //| ATR.mq5 | //| Copyright 2024, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2024, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #property indicator_chart_window #property indicator_buffers 0 #property indicator_plots 0 //— input parameters input int InpAtrPeriod = 10; // ATR period //— indicator buffers //±-----------------------------------------------------------------+ //| Custom indicator initialization function | //±-----------------------------------------------------------------+ int OnInit() { return (INIT_SUCCEEDED); } //±-----------------------------------------------------------------+ //| 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[]) { double H_L[10]; int m = 0; double openATR[10]; // Получаем данные для месячного таймфрейма for(int i = 0; i < InpAtrPeriod; i++) { int shift = i + 1; double maxHigh = iHigh(NULL, PERIOD_MN1, shift); double minLow = iLow(NULL, PERIOD_MN1, shift); openATR[m] = iOpen(NULL, PERIOD_MN1, shift); H_L[m] = NormalizeDouble(maxHigh - minLow, _Digits); m++; } double ATR = 0; for(int i = 0; i < InpAtrPeriod - 1; i++) { double sum = H_L[i] + H_L[i + 1]; ATR += sum / 2 / openATR[i + 1] * 100; } double ATRavg = NormalizeDouble(ATR / (InpAtrPeriod - 1), 2); double priceHigh = openATR[0] + (openATR[0] * ATRavg / 100); double priceLow = openATR[0] - (openATR[0] * ATRavg / 100); double priceHigh_2 = openATR[0] + (openATR[0] * ATRavg / 200); double priceLow_2 = openATR[0] - (openATR[0] * ATRavg / 200); datetime time_end = TimeCurrent(); datetime time_start = time_end - PeriodSeconds(PERIOD_MN1) * InpAtrPeriod; // Создаем трендовые линии на графике ObjectCreate(0, "priceHigh", OBJ_TREND, 0, time_start, priceHigh, time_end, priceHigh); ObjectCreate(0, "priceLow", OBJ_TREND, 0, time_start, priceLow, time_end, priceLow); ObjectCreate(0, "priceHigh_0.5", OBJ_TREND, 0, time_start, priceHigh_2, time_end, priceHigh_2); ObjectCreate(0, "priceLow_0.5", OBJ_TREND, 0, time_start, priceLow_2, time_end, priceLow_2); ObjectSetInteger(0, "priceHigh", OBJPROP_COLOR, clrCrimson); ObjectSetInteger(0, "priceLow", OBJPROP_COLOR, clrCrimson); ObjectSetInteger(0, "priceHigh_0.5", OBJPROP_COLOR, clrBlue); ObjectSetInteger(0, "priceLow_0.5", OBJPROP_COLOR, clrBlue); ObjectSetInteger(0, "priceHigh", OBJPROP_WIDTH, 2); ObjectSetInteger(0, "priceLow", OBJPROP_WIDTH, 2); ObjectSetInteger(0, "priceHigh_0.5", OBJPROP_WIDTH, 2); ObjectSetInteger(0, "priceLow_0.5", OBJPROP_WIDTH, 2); return rates_total; } //±-----------------------------------------------------------------+В этом измененном коде используется доступ к данным месячного таймфрейма через функции
iHigh(),iLow(),iOpen(), и т.д., что позволяет вам рассчитывать значения индикатора как будто бы график стоит на месячном таймфрейме.