如何在MQL5中获取ADX图表的历史K线价格极值区间?
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=0refers to the currently forming (unclosed) bar. Useshift=1and above for fully closed historical bars. - Consistent Period: Ensure
TimePeriodmatches your chart's period (e.g.,PERIOD_H1for 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

