Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
If you’ve ever looked at a chart and thought, “I wish I had an indicator that did exactly what I want”, then learning about creating custom indicators in mt4 mql4 is a game changer. Instead of relying only on built-in tools, you can turn your own trading ideas into real, visual indicators on your MetaTrader 4 charts.
In this guide, we’ll walk through the basics of MT4 custom indicators, how MQL4 code is structured, and how to build, test, and improve your own tools step by step. You don’t need to be a professional programmer. With patience, a bit of practice, and this roadmap, you can create indicators that match your strategy perfectly.
Custom indicators in MetaTrader 4 (MT4) are small programs written in the MQL4 language that analyze price data and display calculated information on charts. They can show lines, histograms, arrows, or any other visual help you need to make trading decisions.
At a simple level, an indicator:
It’s easy to confuse indicators, Expert Advisors (EAs), and scripts, but they have different roles:
So, think of custom indicators as eyes and brain that read the market. EAs are the hands that execute trades.
Traders build custom indicators because:
When you learn creating custom indicators in mt4 mql4, you gain the power to express your trading logic exactly how you imagine it.
Before you write your first line of MQL4, you need the right environment and basic understanding of how indicator files are structured.
MetaEditor is the code editor for MQL4. It comes bundled with MT4.
To open it:
Inside MetaEditor, you can:
Every indicator file has the extension .mq4. When compiled, it produces an .ex4 file that MT4 actually runs.
An indicator file typically contains:
#property indicator_separate_window).OnInit() – runs once when the indicator is loaded.OnDeinit() – runs when the indicator is removed.OnCalculate() – runs whenever new data arrives and does the main work.#property, OnInit, and OnCalculate#property lines tell MT4 how to display the indicator:
OnInit() is used to:
OnCalculate() is the heart of your indicator:
To create powerful and stable indicators, you must understand a few key building blocks.
An indicator buffer is a special array that stores values for each bar. MT4 reads these buffer values and draws them on the chart.
SetIndexBuffer() in OnInit().Example idea:
double MA_Buffer[]; – an array that will hold moving average values for each bar.MQL4 gives you built-in arrays and functions to access market data:
Close[] – closing prices.Open[] – opening prices.High[], Low[].iMA() – moving average.iRSI() – Relative Strength Index.iMACD(), iCCI(), and more.You can also access data from other timeframes and symbols using these functions by passing different parameters.
Input parameters let users customize indicator behavior without editing the code.
Example:
input int Period = 14;
input int Shift = 0;
These appear in the indicator’s settings dialog, allowing traders to change values easily.
Now let’s walk through the process in a simple, ordered way.
My_First_Indicator).MetaEditor creates a template with OnInit() and OnCalculate() functions already in place.
At the top of your file:
Example:
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
double MyBuffer[];
input int Period = 14;
Later in OnInit():
int OnInit()
{
SetIndexBuffer(0, MyBuffer);
SetIndexStyle(0, DRAW_LINE);
IndicatorShortName("My First Indicator");
return(INIT_SUCCEEDED);
}
OnCalculateOnCalculate() receives information like how many bars are calculated and where to start. Inside, you write a loop to process each bar.
Very simplified pattern:
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 = prev_calculated;
if(start == 0) start = Period;
for(int i = start; i < rates_total; i++)
{
// your calculation logic here
// e.g. MyBuffer[i] = close[i];
}
return(rates_total);
}
This function fills MyBuffer[] with values, and MT4 draws them.
Let’s build a simple Moving Average (MA) indicator from scratch. Yes, MT4 already has one, but this is perfect for learning the structure.
You might want users to control:
Example inputs:
input int InpMAPeriod = 14;
input int InpMAMethod = MODE_SMA;
input int InpAppliedPrice = PRICE_CLOSE;
We’ll use the built-in iMA() function in our indicator for simplicity.
Outline inside OnCalculate():
iMA() for the current symbol and timeframe.Example snippet:
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 = InpMAPeriod;
if(prev_calculated > start)
start = prev_calculated - 1;
for(int i = start; i < rates_total; i++)
{
MyBuffer[i] = iMA(NULL, 0, InpMAPeriod, 0, InpMAMethod, InpAppliedPrice, i);
}
return(rates_total);
}
Once compiled and added to the chart:
This simple project lays the foundation for more advanced tools you’ll build after practicing creating custom indicators in mt4 mql4 a bit more.
Coding rarely works perfectly on the first try, and that’s normal. Learning how to read and fix errors is part of the process.
When you compile:
;Tips:
“Array out of range” is very common in MQL4. It means you tried to access array[index] where index is outside valid limits.
To avoid it:
i < rates_total, not <=.Period or more).Print() if you’re unsure what’s going on.If your indicator shows strange values:
A heavy indicator can slow down MT4, especially on multiple charts or with many symbols.
To keep things smooth:
prev_calculated to only process newly added bars.For example:
int start = prev_calculated;
if(start == 0) start = Period;
This ensures you only recalc what’s needed.
Good performance makes your indicators feel professional and reliable.
Good habits early will save you hours later.
FastMAPeriod instead of a.SignalBuffer instead of buf1.Example:
// Start from the first bar where we have enough data
int start = InpMAPeriod;
Group related code into blocks and keep your file tidy.
MyIndicator_v1_1.This makes it easier to maintain and improve your tools over time.
Once you’re comfortable with basic indicators, you can add more advanced features.
You can read data from higher timeframes using built-in functions like iMA() with a different timeframe parameter.
Example:
double maH1 = iMA(NULL, PERIOD_H1, 14, 0, MODE_SMA, PRICE_CLOSE, shift);
This lets you show higher timeframe signals on a lower timeframe chart.
You can trigger alerts when conditions are met:
if(buySignalCondition)
{
Alert("Buy signal on ", Symbol(), " at ", TimeToString(TimeCurrent()));
}
You can also use SendNotification() or SendMail() for mobile or email alerts (after configuring MT4).
Many traders:
This keeps your strategy structure clean: indicator for logic, EA for execution.
An indicator is never “done” after the first version. Testing and refinement are key.
Even though indicators don’t trade, you can:
This helps you spot:
When you tweak logic:
You can also study more about MQL4 from official documentation and community resources like the MQL5 community site (searching there is a good next step for deeper technical details and examples).
Q1. Do I need to be a professional programmer to start creating custom indicators in mt4 mql4?
No. Basic programming logic helps, but you can start with simple examples, copy templates, and learn step by step. Over time you’ll become more confident.
Q2. Where are my custom indicators saved in MT4?
They’re saved as .mq4 source files (and compiled .ex4 files) inside the MQL4/Indicators folder of your MT4 data directory. You can access this via File → Open Data Folder in MT4.
Q3. Can a custom indicator open trades automatically?
Directly, no. Indicators are meant for analysis and display. To open trades automatically, you should use an Expert Advisor that reads the indicator’s buffers and then places trades.
Q4. Why is my indicator not showing anything on the chart?
Common reasons:
SetIndexBuffer().OnCalculate() loop starts too late or ends too early.Q5. Can I share or sell my custom indicators?
Yes. You can share .ex4 or .mq4 files with others. Many platforms and marketplaces allow you to distribute or sell indicators. Just be sure to respect any licensing or broker rules.
Q6. How can I learn more advanced MQL4 techniques?
You can study the official MQL4 documentation and community articles, for example on the MetaQuotes community website (MQL4/MQL5 docs and forums) which provide tutorials, samples, and reference material.
Learning creating custom indicators in mt4 mql4 is like learning a new language for your trading brain. Instead of bending your strategies to fit existing tools, you can make tools that fit your strategy.
By understanding:
OnCalculate() work,you can steadily move from simple moving averages to powerful, custom-made signal engines.
Start small, experiment often, and keep your code organized. With each indicator you build, your skills grow—and so does your ability to turn trading ideas into real, testable tools on your MT4 charts.