write me a code for a andicator for metatrader5 that shows high and low in past 8 candles before current candle and high and low of 25 candles before current candle
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Creating a custom indicator for MetaTrader 5 requires the use of MQL5 (MetaQuotes Language 5). Below is an example of how you can write an indicator that shows the highest and lowest values from both the last 8 candles and the last 25 candles before the current candle.
mql5
//+------------------------------------------------------------------+
//| HighLowInd.mq5 |
//| Copyright 2023, MetaQuotes Software Corp. |
//| Created by OpenAI. |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 DodgerBlue
#property indicator_color2 DarkOrange
// Indicator buffers
double HighLow8Buffer[];
double HighLow25Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Indicator buffers mapping
SetIndexBuffer(0,HighLow8Buffer);
SetIndexBuffer(1,HighLow25Buffer);
// Set buffer styles
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
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[])
{
// Check for enough history
if (rates_total < 25) return(0);
// Calculation of the indicator
for (int i = 0; i < rates_total; i++)
{
// Find the highest and lowest value over the past 8 candles
int limit8 = i + 8;
if (limit8 > rates_total - 1) limit8 = rates_total - 1;
double highest8 = high[i];
double lowest8 = low[i];
for (int j = i + 1; j <= limit8; j++)
{
if (high[j] > highest8) highest8 = high[j];
if (low[j] < lowest8) lowest8 = low[j];
}
HighLow8Buffer[i] = highest8;
HighLow8Buffer[limit8] = lowest8;
// Find the highest and lowest value over the past 25 candles
int limit25 = i + 25;
if (limit25 > rates_total - 1) limit25 = rates_total - 1;
double highest25 = high[i];
double lowest25 = low[i];
for (int j = i + 1; j <= limit25; j++)
{
if (high[j] > highest25) highest25 = high[j];
if (low[j] < lowest25) lowest25 = low[j];
}
HighLow25Buffer[i] = highest25;
HighLow25Buffer[limit25] = lowest25;
}
return(rates_total);
}
//+------------------------------------------------------------------+
Explanation:
Indicator Properties:
The indicator is set to draw in the chart window.
Two buffers are created to hold the high-low values for both the 8-candle and 25-candle lookback periods.
Buffers and Colors:
HighLow8Buffer and HighLow25Buffer are defined to store these extreme values.
Colors DodgerBlue and DarkOrange are used to differentiate the two lines drawn by the indicator.
OnInit Function:
Initializes and maps the indicator buffers.
Sets the visual style of the lines.
OnCalculate Function:
Runs on every tick to update the indicator.
Ensures enough data is available before calculating the high and low for the respective periods.
The loop iterates over the chart data, recalculating the highest and lowest points based on the specified lookbacks.
Adjust and Compile:
Save this code into a file named HighLowInd.mq5 inside your Indicators directory in the MetaTrader 5 platform.
Compile it using the MetaEditor before applying it to a chart.
This indicator will provide visual cues for traders seeking to assess recent market highs and lows over specified time frames. Adjust any parameters as necessary to better fit specific needs.