Creating a custom indicator for MetaTrader 5 requires familiarity with the MQL5 programming language. Below is a simple example of how you can create an indicator that shows the highest high and lowest low for the past 8 candles and the past 25 candles before the current candle.
//+------------------------------------------------------------------+
//| HighLow.mq5 |
//| Copyright 2023, Your Name |
//| http://www.yoururl.com|
//+------------------------------------------------------------------+
#property copyright "Your Name"
#property link "http://www.yoururl.com"
#property version "1.00"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Red
#property indicator_color2 Red
#property indicator_color3 Blue
#property indicator_color4 Blue
// Indicator buffers
double High8Buffer[];
double Low8Buffer[];
double High25Buffer[];
double Low25Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Bind buffers
SetIndexBuffer(0, High8Buffer);
SetIndexBuffer(1, Low8Buffer);
SetIndexBuffer(2, High25Buffer);
SetIndexBuffer(3, Low25Buffer);
// Set buffer styles
SetIndexStyle(0, DRAW_LINE);
SetIndexStyle(1, DRAW_LINE);
SetIndexStyle(2, DRAW_LINE);
SetIndexStyle(3, DRAW_LINE);
// Set indicator labels
IndicatorSetString(INDICATOR_SHORTNAME, "High Low Levels");
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[])
{
int start = 0;
if(prev_calculated == 0)
start = 25; // Start calculation after first 25 candles to avoid array out of range
else
start = prev_calculated - 1;
for (int i = start; i < rates_total; i++)
{
// Calculate highest high and lowest low for past 8 candles
if (i >= 8)
{
High8Buffer[i] = iHigh(NULL,0,ArrayMaximum(high, i-8, 8));
Low8Buffer[i] = iLow(NULL,0,ArrayMinimum(low, i-8, 8));
}
// Calculate highest high and lowest low for past 25 candles
if (i >= 25)
{
High25Buffer[i] = iHigh(NULL,0,ArrayMaximum(high, i-25, 25));
Low25Buffer[i] = iLow(NULL,0,ArrayMinimum(low, i-25, 25));
}
}
return(rates_total);
}
//+------------------------------------------------------------------+
Instructions for Installation and Use:
-
Open MetaEditor:
- Open MetaTrader 5.
- Go to
Tools -> MetaQuotes Language Editor or press F4 to open MetaEditor.
-
Create a New Indicator:
- Click on
File -> New in MetaEditor.
- Select
Custom Indicator and click Next.
- Name it (e.g.,
HighLowLevels) and proceed.
- Finish the default setup.
-
Replace Code:
- Delete the default code in the editor that appears for the new indicator.
- Copy and paste the provided code into the editor.
-
Compile the Indicator:
- Click
Compile or press F7 to compile the code.
- Ensure there are no errors in the compilation.
-
Apply the Indicator in MetaTrader 5:
- In MetaTrader 5, go to the
Navigator pane (press Ctrl+N if it's not open).
- Under
Indicators, locate your newly created HighLowLevels.
- Drag and drop it onto your desired chart.
This code will plot the highest and lowest levels of the past 8 and 25 candles on your chart. You can adjust the number of periods by changing the values in the code as necessary.