You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MQL5中获取ADX图表的历史K线价格极值区间?

Fixing Historical Bar Price Extremes in MQL4/MT5

Got it, let's sort this out for you! The problem with using ChartGetDouble() here is that functions like CHART_PRICE_MAX and CHART_PRICE_MIN return chart-wide or current session-level extremes, not the high/low values of specific historical bars. To target individual or ranges of past candles, you'll want to use MQL's dedicated historical price functions instead.

Step 1: Fetch Individual Historical Bar High/Low

Use iHigh() and iLow() to directly get the high or low of any specific historical bar by its "shift" position (how far back it is from the current bar).

Example code:

// Define which historical bar to target: shift=0 = current unclosed bar, shift=1 = last closed bar, shift=5 = 5 bars back
int targetShift = 5;

// Retrieve high and low for the target bar
double barHigh = iHigh(_Symbol, TimePeriod, targetShift);
double barLow = iLow(_Symbol, TimePeriod, targetShift);

// Print results
Print("Historical Bar ", targetShift, " High: ", barHigh);
Print("Historical Bar ", targetShift, " Low: ", barLow);

Step 2: Calculate Extremes for a Range of Historical Bars

If you need the overall high and low across a set of candles (like the range tied to your Candles_for_adx value), loop through the bars and track the extremes:

int lookbackPeriod = Candles_for_adx; // Reuse your ADX candle count variable
double rangeHigh = 0.0;
double rangeLow = DBL_MAX; // Initialize with a very high starting value

// Loop through each bar in the lookback range
for(int i = 0; i < lookbackPeriod; i++){
    double currentBarHigh = iHigh(_Symbol, TimePeriod, i);
    double currentBarLow = iLow(_Symbol, TimePeriod, i);
    
    // Update range high if current bar is higher
    if(currentBarHigh > rangeHigh){
        rangeHigh = currentBarHigh;
    }
    // Update range low if current bar is lower
    if(currentBarLow < rangeLow){
        rangeLow = currentBarLow;
    }
}

// Print the full range extremes
Print("Range High (last ", lookbackPeriod, " bars): ", rangeHigh);
Print("Range Low (last ", lookbackPeriod, " bars): ", rangeLow);

Key Tips

  • Shift Parameter: shift=0 refers to the currently forming (unclosed) bar. Use shift=1 and above for fully closed historical bars.
  • Consistent Period: Ensure TimePeriod matches your chart's period (e.g., PERIOD_H1 for 1-hour bars) to avoid mismatched data.
  • ADX Code: Your original iADX() call works fine—this fix only replaces the price extreme retrieval logic.

内容的提问来源于stack exchange,提问作者Jaffer Wilson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:12:01