// Set colors and widths for each EMA line
SetEMAColor(EMA1_PERIOD, COLOR_EMA1);
SetEMAColor(EMA2_PERIOD, COLOR_EMA2);
SetEMAColor(EMA3_PERIOD, COLOR_EMA3);
}
//+------------------------------------------------------------------+
//| Set color and width of EMA indicator object on the chart |
//+------------------------------------------------------------------+
void SetEMAColor(int period, color clr)
{
string period_str = "(" + IntegerToString(period) + ")";
Assistant
User
if i want add this to chart ask me to remove another ea
Assistant
User
pleased Convert code into a Custom Indicator
Assistant
User
'PLOT_SHOW' - undeclared identifier BSW.mq5 92 30
'PlotIndexSetInteger' - no one of the overloads can be applied to the function call BSW.mq5 92 7
Assistant
The errors you encounter relate to the fact that the enumeration value PLOT_SHOW does not exist in MQL5 for PlotIndexSetInteger(). Also, the way to hide/show plots is different.
Explanation & Fix:
PLOT_SHOW is not a valid property.
To control the visibility of indicator plots, you actually use:
PlotIndexSetInteger(plot_index, PLOT_DRAW_TYPE, DRAW_NONE) to hide plot
or
PlotIndexSetInteger(plot_index, PLOT_DRAW_TYPE, DRAW_LINE) (or other draw types) to show plot.
How to fix your code:
User
full code
Assistant
User
what is the order for label buy
Assistant
The "Buy only" label appears when the EMAs are in the following strict order:
EMA(50) > EMA(100) > EMA(200)
That is:
The shortest period EMA (50) is above the medium period EMA (100),
And the medium period EMA (100) is above the longest period EMA (200).
Why this order indicates "Buy only"?
This alignment suggests a strong uptrend because:
The fast EMA (50) is above the slower EMAs, showing recent prices are higher,
The EMAs are stacked in ascending order, confirming trend strength.
Similarly,
The "Sell only" label appears when:
EMA(50) < EMA(100) < EMA(200)
indicating a strong downtrend.
Otherwise,
If EMAs are mixed or not strictly ordered, the status label shows "Wait".
From the code snippet in UpdateStatusLabel function:
if (ema1 > ema2 && ema2 > ema3)
status = "Buy only";
else if (ema1 < ema2 && ema2 < ema3)
status = "Sell only";
else
status = "Wait";
User
i got ema2>ema1>ema3 and sey buy
Assistant
Thanks for pointing that out!
The logic as coded is:
if (ema1 > ema2 && ema2 > ema3)
status = "Buy only";
else if (ema1 < ema2 && ema2 < ema3)
status = "Sell only";
else
status = "Wait";
User
may be this the cause : 2025.05.23 08:11:37.565 BSW (XAUUSD.p,M1) array out of range in 'BSW.mq5' (174,27)
Assistant
The error message:
2025.05.23 08:11:37.565 BSW (XAUUSD.p,M1) array out of range in 'BSW.mq5' (174,27)
User
need full code updated
Assistant
User
label is not corect i got now ema1<ema2<ema3 and say buy only
Assistant
User
full fixed code please
Assistant
User
2025.05.23 08:32:08.510 BSW (XAUUSD.p,M15) Label set to: Buy only (EMA50=1333.86784 EMA100=1333.72535 EMA200=1333.65303)
Assistant
Thanks for sharing your log! Let's analyze the relevant part:
Label set to: Buy only (EMA50=1333.86784 EMA100=1333.72535 EMA200=1333.65303)
if (ema1 > ema2 && ema2 > ema3)
status = "Buy only";
where ema1=EMA50, ema2=EMA100, ema3=EMA200.
Since the EMA values satisfy this (1333.87 > 1333.73 > 1333.65), the indicator is behaving correctly.
Your initial concern was:
int total = ObjectsTotal(0);
for (int i = 0; i < total; i++)
{
string name = ObjectName(0, i);
// Check name starts with "EMA" and contains period string
if (StringFind(name, "EMA") == 0 && StringFind(name, period_str) >= 0)
{
// Optionally skip type check, since OBJ_INDICATOR is undefined in MQL5
// Just set properties by name:
ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
ObjectSetInteger(0, name, OBJPROP_WIDTH, 2);
break; // Found and set, exit loop
}
}
if (Show_EMAs)
{
if (added_ema1) ChartIndicatorDelete(0, 0, ema1_handle);
if (added_ema2) ChartIndicatorDelete(0, 0, ema2_handle);
if (added_ema3) ChartIndicatorDelete(0, 0, ema3_handle);
}
// Release indicator handles
if (ema1_handle != INVALID_HANDLE) IndicatorRelease(ema1_handle);
if (ema2_handle != INVALID_HANDLE) IndicatorRelease(ema2_handle);
if (ema3_handle != INVALID_HANDLE) IndicatorRelease(ema3_handle);
}
//+------------------------------------------------------------------+
//| Main tick processing |
//+------------------------------------------------------------------+
void OnTick()
{
double ema1_buf[], ema2_buf[], ema3_buf[];
// Get EMA values for last closed bar (index=1) - safer than current forming bar (0)
if (CopyBuffer(ema1_handle, 0, 1, 1, ema1_buf) <= 0) return;
if (CopyBuffer(ema2_handle, 0, 1, 1, ema2_buf) <= 0) return;
if (CopyBuffer(ema3_handle, 0, 1, 1, ema3_buf) <= 0) return;
The provided MQL5 expert demonstrates how to calculate 3 EMAs on a selected timeframe, optionally display them on the chart, and show a status label indicating a "Buy only", "Sell only" or "Wait" signal depending on the relative EMA order.
Quick review and suggestions:
Overall code logic looks correct and in line with MQL5 best practices.
Using EMA handles via iMA() with MODE_EMA is good.
Reading values from the previous closed bar (CopyBuffer(..., 1, 1, ...)) is safer than bar 0.
The label is created once on init and updated every tick.
The indicator lines are added via ChartIndicatorAdd() when Show_EMAs is true, and deleted on deinit.
Color setting is done by searching object names containing the EMA period in parentheses.
Handles released on deinit.
A few practical improvements / fixes:
1. SetEMAColor() Approach
The code searches for objects whose names start with "EMA" and contain the period in parentheses.
But the built-in EMA indicator object names added to the chart may have different naming conventions depending on platform language and version.
Also, the loop may find unrelated objects accidentally.
Alternative: Use the ChartIndicatorAdd() approach but immediately after adding, use ChartIndicatorGet() or related function to get the actual indicator object and set its properties directly if supported.
Unfortunately, MQL5 has no direct method to get subobjects by handle. So your approach is pragmatic.
Still, a better approach might be:
After you add the indicator with ChartIndicatorAdd(), retrieve the indicator name from iMA() call or from the list of indicators on the chart, then modify it.
Or else just create your own custom EMAs as separate graphical objects to have full control.
2. Handling Indicator Window/Subwindow
Your code assumes the EMAs are always added to the main chart window (subwindow=0).
This is standard for EMAs, but if you want to keep things flexible, add validation on ChartIndicatorAdd() return or check if any error happens.
3. Performance optimization for OnTick()
EMA buffers are small (1 value), so it's fine.
But consider adding a check to update status only when new bar appears on the SelectedTimeframe, to avoid redundant updates.
Example:
static datetime last_bar_time = 0;
MqlRates rates[];
if (CopyRates(_Symbol, SelectedTimeframe, 1, 1, rates) > 0)
{
if (rates[0].time == last_bar_time) return; // no new bar => no update
last_bar_time = rates[0].time;
}
This will limit label text updates and avoid excessive redraws on every tick.
4. Using string constants instead of literals for label status
Make status strings constants for easier future localization or changes.
5. Adding option to change label position and font size via inputs can improve usability.
Minimal Modified Code Version for your OnTick:
void OnTick()
{
static datetime last_bar_time = 0;
MqlRates rates[];
if (CopyRates(_Symbol, SelectedTimeframe, 1, 1, rates) <= 0) return;
if (rates[0].time == last_bar_time)
return; // No new completed bar on selected TF
last_bar_time = rates[0].time;
double ema1_buf[], ema2_buf[], ema3_buf[];
if (CopyBuffer(ema1_handle, 0, 1, 1, ema1_buf) <= 0) return;
if (CopyBuffer(ema2_handle, 0, 1, 1, ema2_buf) <= 0) return;
if (CopyBuffer(ema3_handle, 0, 1, 1, ema3_buf) <= 0) return;
double ema1 = ema1_buf[0];
double ema2 = ema2_buf[0];
double ema3 = ema3_buf[0];
string status = "Wait";
if (ema1 > ema2 && ema2 > ema3)
status = "Buy only";
else if (ema1 < ema2 && ema2 < ema3)
status = "Sell only";
ObjectSetString(0, label_name, OBJPROP_TEXT, status);
}
Summary
Your code is functionally sound. The main things to watch for:
Make sure the SetEMAColor() correctly finds the line objects.
Consider updating OnTick only on new bar on your selected timeframe.
Optionally add code comments/documentation for future maintainers.
Check your platform language—names of chart objects might differ.
If you want, I can help with a fully modified version or explain any specific part further. Just let me know!
When you try to attach an Expert Advisor (EA) to a chart in MetaTrader 5 that already has another EA running, the platform generally prompts you with a message like:
"You have already attached an Expert Advisor to this chart. Do you want to replace it?"
This is default MT5 behavior and cannot be avoided directly because MT5 allows running only one EA per chart at a time.
What does this mean for your situation?
Your EMA_Status EA is an Expert Advisor and will trigger this dialog if you try to add it to a chart already running another EA.
This dialog is triggered by the MT5 terminal itself — your code does not cause it.
This limitation is by design and applies to all EAs as MT5 does not support running multiple EAs on the same chart concurrently.
Possible workarounds
Use Indicator Instead of EA
Convert your code into a Custom Indicator (.mq5 indicator file).
Indicators can run alongside other EAs and indicators on the same chart without conflict.
Your logic outputs only display info, no trading is performed — so an indicator is more appropriate.
You can draw the label and optionally plot the EMAs inside the indicator.
Use SetIndexBuffer() and PlotIndexSetDouble() functions to plot EMAs.
Use ObjectsCreate() to create and update text label.
This way no EA replacement prompts appear.
Use Separate Charts for Each EA
Each EA must run on its own chart window.
Spread out your workspace to avoid conflicts.
Combine all your logic into a Single EA
If you must run multiple EA functions, put all functions inside a single EA so it manages all tasks.
But this increases complexity.
How to Convert your code to an Indicator
If you want, here is a brief overview:
Change .mq5 from Expert to Indicator:
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 3
// Define three indicator buffers for EMAs
double ema1_buf[];
double ema2_buf[];
double ema3_buf[];
// Indicator parameters and initialization like before...
In OnInit(), create EMA handles and set buffers.
In OnCalculate(), copy values from EMA handles to your buffers.
Draw the label with ObjectCreate() and update its text inside OnCalculate().
Provide an input for the timeframe and whether to show EMAs.
Let MT5 handle EMA display using plot functions.
Summary
MT5 allows only one EA per chart, so you cannot avoid the prompt to replace one EA with another.
But you can convert your EA to a custom indicator if you want it to coexist with other EAs on the same chart without prompts.
If you want, I can help you convert your EA code into an indicator code with the same features — just ask!
Certainly! Below is your converted MQL5 Custom Indicator code based on your EA, which:
Calculates 3 EMAs on the selected timeframe
Optionally plots these EMAs on the main chart
Displays a label with status "Buy only", "Sell only", or "Wait" based on EMA relative positions
Avoids any EA conflicts (because it’s an indicator)
EMA_Status Indicator (EMA_Status.mq5)
//+------------------------------------------------------------------+
//| EMA_Status.mq5 |
//| Version 1.0, converted by ChatGPT |
//+------------------------------------------------------------------+
#property copyright "ChatGPT"
#property version "1.0"
#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 3
// EMA plot settings
#property indicator_label1 "EMA 50"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrGreen
#property indicator_width1 2
#property indicator_label2 "EMA 100"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrBlue
#property indicator_width2 2
#property indicator_label3 "EMA 200"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrRed
#property indicator_width3 2
//--- input parameters
input ENUM_TIMEFRAMES SelectedTimeframe = PERIOD_M15; // Timeframe for EMAs
input bool Show_EMAs = true; // Show/Hide EMAs on chart
//--- EMA periods
#define EMA1_PERIOD 50
#define EMA2_PERIOD 100
#define EMA3_PERIOD 200
//--- indicator buffers
double ema1_buf[];
double ema2_buf[];
double ema3_buf[];
string label_name = "EMA_Status_Label";
// Handles for EMA indicators on selected timeframe
int ema1_handle = INVALID_HANDLE;
int ema2_handle = INVALID_HANDLE;
int ema3_handle = INVALID_HANDLE;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Create EMA indicator handles for selected timeframe
ema1_handle = iMA(_Symbol, SelectedTimeframe, EMA1_PERIOD, 0, MODE_EMA, PRICE_CLOSE);
if (ema1_handle == INVALID_HANDLE)
{
Print("Failed to create EMA1 handle");
return(INIT_FAILED);
}
ema2_handle = iMA(_Symbol, SelectedTimeframe, EMA2_PERIOD, 0, MODE_EMA, PRICE_CLOSE);
if (ema2_handle == INVALID_HANDLE)
{
Print("Failed to create EMA2 handle");
IndicatorRelease(ema1_handle);
return(INIT_FAILED);
}
ema3_handle = iMA(_Symbol, SelectedTimeframe, EMA3_PERIOD, 0, MODE_EMA, PRICE_CLOSE);
if (ema3_handle == INVALID_HANDLE)
{
Print("Failed to create EMA3 handle");
IndicatorRelease(ema1_handle);
IndicatorRelease(ema2_handle);
return(INIT_FAILED);
}
// Set buffers to indicator
SetIndexBuffer(0, ema1_buf, INDICATOR_DATA);
SetIndexBuffer(1, ema2_buf, INDICATOR_DATA);
SetIndexBuffer(2, ema3_buf, INDICATOR_DATA);
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, EMA1_PERIOD);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, EMA2_PERIOD);
PlotIndexSetInteger(2, PLOT_DRAW_BEGIN, EMA3_PERIOD);
// Show or hide EMAs plots
for (int i=0; i < 3; i++)
PlotIndexSetInteger(i, PLOT_SHOW, Show_EMAs);
CreateStatusLabel();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
IndicatorRelease(ema1_handle);
IndicatorRelease(ema2_handle);
IndicatorRelease(ema3_handle);
ObjectDelete(0, label_name);
}
//+------------------------------------------------------------------+
//| Create status label on chart |
//+------------------------------------------------------------------+
void CreateStatusLabel()
{
if (ObjectFind(0, label_name) == -1)
{
ObjectCreate(0, label_name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, label_name, OBJPROP_CORNER, CORNER_RIGHT_UPPER);
ObjectSetInteger(0, label_name, OBJPROP_XDISTANCE, 100);
ObjectSetInteger(0, label_name, OBJPROP_YDISTANCE, 10);
ObjectSetInteger(0, label_name, OBJPROP_BACK, true);
ObjectSetInteger(0, label_name, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, label_name, OBJPROP_SELECTED, false);
ObjectSetInteger(0, label_name, OBJPROP_FONTSIZE, 14);
ObjectSetString(0, label_name, OBJPROP_FONT, "Arial");
ObjectSetInteger(0, label_name, OBJPROP_COLOR, clrWhite);
ObjectSetString(0, label_name, OBJPROP_TEXT, "Wait");
}
}
//+------------------------------------------------------------------+
//| Update status label text based on EMA order |
//+------------------------------------------------------------------+
void UpdateStatusLabel(double ema1, double ema2, double ema3)
{
string status = "Wait";
if (ema1 > ema2 && ema2 > ema3)
status = "Buy only";
else if (ema1 < ema2 && ema2 < ema3)
status = "Sell only";
ObjectSetString(0, label_name, OBJPROP_TEXT, status);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &price[])
{
// Safety check to ensure enough bars for longest EMA
if(rates_total < EMA3_PERIOD)
return(0);
int to_copy = rates_total;
double temp1[], temp2[], temp3[];
// Copy EMA buffers from indicator handles
if(CopyBuffer(ema1_handle, 0, 0, to_copy, temp1) <= 0)
return(prev_calculated);
if(CopyBuffer(ema2_handle, 0, 0, to_copy, temp2) <= 0)
return(prev_calculated);
if(CopyBuffer(ema3_handle, 0, 0, to_copy, temp3) <= 0)
return(prev_calculated);
// Copy to indicator buffers for plotting
for(int i=0; i<to_copy; i++)
{
ema1_buf[i] = temp1[i];
ema2_buf[i] = temp2[i];
ema3_buf[i] = temp3[i];
}
// Update the label only on the last fully formed bar
// index 1 is last complete bar (index 0 is current forming bar)
if(to_copy > 1)
{
double last_ema1 = temp1[1];
double last_ema2 = temp2[1];
double last_ema3 = temp3[1];
UpdateStatusLabel(last_ema1, last_ema2, last_ema3);
}
return(rates_total);
}
//+------------------------------------------------------------------+
How to use:
Save the above as EMA_Status.mq5 in your MQL5/Indicators folder.
Compile it in MetaEditor.
Attach it to any chart.
It will compute EMAs on the selected timeframe (default M15).
The EMAs are plotted if you set Show_EMAs to true.
You’ll see a status label top-right corner showing "Buy only", "Sell only", or "Wait" depending on EMAs.
Used indicator buffers for EMA lines so they plot natively.
The EMAs use EMA indicator handles on the user-selected timeframe.
Draw and update the label from OnCalculate().
No trade logic or EA-specific functions, so no the “replace EA” issue.
If you want me to also add or explain:
How to change label position or color dynamically,
Additional timeframe options,
Any other features,
just ask!
Instead of using:
PlotIndexSetInteger(i, PLOT_SHOW, Show_EMAs);
Do this in OnInit() after setting plot properties:
for (int i = 0; i < 3; i++)
{
if(Show_EMAs)
PlotIndexSetInteger(i, PLOT_DRAW_TYPE, DRAW_LINE);
else
PlotIndexSetInteger(i, PLOT_DRAW_TYPE, DRAW_NONE);
}
Complete fixed snippet around line 90:
// Set draw begin offset for EMAs
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, EMA1_PERIOD);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, EMA2_PERIOD);
PlotIndexSetInteger(2, PLOT_DRAW_BEGIN, EMA3_PERIOD);
// Show or hide EMAs plots
for (int i=0; i < 3; i++)
{
if(Show_EMAs)
PlotIndexSetInteger(i, PLOT_DRAW_TYPE, DRAW_LINE);
else
PlotIndexSetInteger(i, PLOT_DRAW_TYPE, DRAW_NONE);
}
Summary
Remove use of PLOT_SHOW (not defined).
Use PlotIndexSetInteger(..., PLOT_DRAW_TYPE, ...) with DRAW_LINE or DRAW_NONE to show/hide your plots.
If you want, I can send you a fixed full indicator code snippet with this correction applied! Would you like that?
Certainly! Here is the full corrected indicator code with the PLOT_DRAW_TYPE fix to show/hide plots correctly:
//+------------------------------------------------------------------+
//| EMA_Status.mq5 |
//| Version 1.1, converted by ChatGPT |
//+------------------------------------------------------------------+
#property copyright "ChatGPT"
#property version "1.1"
#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 3
// EMA plot settings
#property indicator_label1 "EMA 50"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrGreen
#property indicator_width1 2
#property indicator_label2 "EMA 100"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrBlue
#property indicator_width2 2
#property indicator_label3 "EMA 200"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrRed
#property indicator_width3 2
//--- input parameters
input ENUM_TIMEFRAMES SelectedTimeframe = PERIOD_M15; // Timeframe for EMAs
input bool Show_EMAs = true; // Show/Hide EMAs on chart
//--- EMA periods
#define EMA1_PERIOD 50
#define EMA2_PERIOD 100
#define EMA3_PERIOD 200
//--- indicator buffers
double ema1_buf[];
double ema2_buf[];
double ema3_buf[];
string label_name = "EMA_Status_Label";
// Handles for EMA indicators on selected timeframe
int ema1_handle = INVALID_HANDLE;
int ema2_handle = INVALID_HANDLE;
int ema3_handle = INVALID_HANDLE;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Create EMA indicator handles for selected timeframe
ema1_handle = iMA(_Symbol, SelectedTimeframe, EMA1_PERIOD, 0, MODE_EMA, PRICE_CLOSE);
if (ema1_handle == INVALID_HANDLE)
{
Print("Failed to create EMA1 handle");
return(INIT_FAILED);
}
ema2_handle = iMA(_Symbol, SelectedTimeframe, EMA2_PERIOD, 0, MODE_EMA, PRICE_CLOSE);
if (ema2_handle == INVALID_HANDLE)
{
Print("Failed to create EMA2 handle");
IndicatorRelease(ema1_handle);
return(INIT_FAILED);
}
ema3_handle = iMA(_Symbol, SelectedTimeframe, EMA3_PERIOD, 0, MODE_EMA, PRICE_CLOSE);
if (ema3_handle == INVALID_HANDLE)
{
Print("Failed to create EMA3 handle");
IndicatorRelease(ema1_handle);
IndicatorRelease(ema2_handle);
return(INIT_FAILED);
}
// Set buffers to indicator
SetIndexBuffer(0, ema1_buf, INDICATOR_DATA);
SetIndexBuffer(1, ema2_buf, INDICATOR_DATA);
SetIndexBuffer(2, ema3_buf, INDICATOR_DATA);
// Set drawing start offsets so the indicator starts plotting after enough bars
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, EMA1_PERIOD);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, EMA2_PERIOD);
PlotIndexSetInteger(2, PLOT_DRAW_BEGIN, EMA3_PERIOD);
// Show or hide EMAs plots using DRAW_LINE or DRAW_NONE
for (int i = 0; i < 3; i++)
{
if (Show_EMAs)
PlotIndexSetInteger(i, PLOT_DRAW_TYPE, DRAW_LINE);
else
PlotIndexSetInteger(i, PLOT_DRAW_TYPE, DRAW_NONE);
}
CreateStatusLabel();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
IndicatorRelease(ema1_handle);
IndicatorRelease(ema2_handle);
IndicatorRelease(ema3_handle);
ObjectDelete(0, label_name);
}
//+------------------------------------------------------------------+
//| Create status label on chart |
//+------------------------------------------------------------------+
void CreateStatusLabel()
{
if (ObjectFind(0, label_name) == -1)
{
ObjectCreate(0, label_name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, label_name, OBJPROP_CORNER, CORNER_RIGHT_UPPER);
ObjectSetInteger(0, label_name, OBJPROP_XDISTANCE, 100);
ObjectSetInteger(0, label_name, OBJPROP_YDISTANCE, 10);
ObjectSetInteger(0, label_name, OBJPROP_BACK, true);
ObjectSetInteger(0, label_name, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, label_name, OBJPROP_SELECTED, false);
ObjectSetInteger(0, label_name, OBJPROP_FONTSIZE, 14);
ObjectSetString(0, label_name, OBJPROP_FONT, "Arial");
ObjectSetInteger(0, label_name, OBJPROP_COLOR, clrWhite);
ObjectSetString(0, label_name, OBJPROP_TEXT, "Wait");
}
}
//+------------------------------------------------------------------+
//| Update status label text based on EMA order |
//+------------------------------------------------------------------+
void UpdateStatusLabel(double ema1, double ema2, double ema3)
{
string status = "Wait";
if (ema1 > ema2 && ema2 > ema3)
status = "Buy only";
else if (ema1 < ema2 && ema2 < ema3)
status = "Sell only";
ObjectSetString(0, label_name, OBJPROP_TEXT, status);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &price[])
{
// Safety check to ensure enough bars for longest EMA
if(rates_total < EMA3_PERIOD)
return(0);
int to_copy = rates_total;
double temp1[], temp2[], temp3[];
// Copy EMA buffers from indicator handles
if(CopyBuffer(ema1_handle, 0, 0, to_copy, temp1) <= 0)
return(prev_calculated);
if(CopyBuffer(ema2_handle, 0, 0, to_copy, temp2) <= 0)
return(prev_calculated);
if(CopyBuffer(ema3_handle, 0, 0, to_copy, temp3) <= 0)
return(prev_calculated);
// Copy to indicator buffers for plotting
for(int i=0; i<to_copy; i++)
{
ema1_buf[i] = temp1[i];
ema2_buf[i] = temp2[i];
ema3_buf[i] = temp3[i];
}
// Update the label only on the last fully formed bar (index 1)
if(to_copy > 1)
{
double last_ema1 = temp1[1];
double last_ema2 = temp2[1];
double last_ema3 = temp3[1];
UpdateStatusLabel(last_ema1, last_ema2, last_ema3);
}
return(rates_total);
}
//+------------------------------------------------------------------+
How to compile and use:
Copy and save this as EMA_Status.mq5 in your MQL5/Indicators folder.
Open MetaEditor, compile.
Attach it to any chart.
You can change the SelectedTimeframe and Show_EMAs inputs.
You will see 3 EMAs plotted (if enabled) and the status label top-right corner.
If you want enhancements (like dynamically changing label position, color, font size, or adding alerts), just ask!
where ema1 = EMA(50), ema2 = EMA(100), ema3 = EMA(200).
where ema1 = EMA(50) ema2 = EMA(100) ema3 = EMA(200)
Your case: ema2 > ema1 > ema3
So EMA(100) > EMA(50) > EMA(200).
Here ema1 > ema2 condition is false since ema1 < ema2.
So it should not trigger "Buy only" according to the strict condition.
But you said it shows "Buy only", which means something else could be wrong.
Possible reasons:
Are you sure the right EMA buffer values are assigned correctly? In your code ema1 is EMA(50), ema2 is EMA(100), etc.
Could indexing be mixed?
Suggest verifying the EMA variables assignment in UpdateStatusLabel call inside OnCalculate:
Check in Experts tab what values show and ensure correspondences.
If the values are actually swapped, fix by swapping buffer assignments or variable names.
Correct logic:
If you want "Buy only" status only when EMA(50) > EMA(100) > EMA(200) (short-term above medium-term above long-term), AND you want to avoid false positives like your case:
Make sure the indicator buffers and EMA handles correspond exactly.
Final note:
If you want to also consider the >= condition for safe margin (if you want), change:
if (ema1 >= ema2 && ema2 >= ema3)
but standard strict ordering is usually better.
Summary checklist:
Confirm ema1 = EMA(50) buffer
Confirm ema2 = EMA(100) buffer
Confirm ema3 = EMA(200) buffer
Confirm values passed into UpdateStatusLabel are in correct order
Use Print() debugging to verify
If you want me to review your exact current code snippet for that part or add debugging code, just tell me!
indicates that in your code file BSW.mq5, at line 174, column 27, you are accessing an array out of its valid bounds.
Why is this important?
When you get "array out of range", it means you tried to access an element at an index that doesn't exist, like:
double arr[5];
// Access arr[5] or arr[-1] --> Out of range
Since your EMA buffer arrays are filled with CopyBuffer() for to_copy bars, but you may be accessing index 1 or some other index without checking if enough bars exist, this error can happen.
If to_copy == 1 or less, accessing temp1[1] causes out of range error.
Fix:
Make sure the number of bars you request and check is enough before accessing index 1.
Here is a robust way to do it:
int to_copy = rates_total;
// Safety: you need at least 2 bars to read the previous closed bar at index 1
if (to_copy < 2)
return prev_calculated;
// Copy EMA buffers
if(CopyBuffer(ema1_handle, 0, 0, to_copy, temp1) <= 0)
return prev_calculated;
if(CopyBuffer(ema2_handle, 0, 0, to_copy, temp2) <= 0)
return prev_calculated;
if(CopyBuffer(ema3_handle, 0, 0, to_copy, temp3) <= 0)
return prev_calculated;
// Now safe to read temp arrays at index 1:
double last_ema1 = temp1[1];
double last_ema2 = temp2[1];
double last_ema3 = temp3[1];
UpdateStatusLabel(last_ema1, last_ema2, last_ema3);
Also, in the earlier code, change this:
if(rates_total < EMA3_PERIOD)
return(0);
to something safer, but keep in mind the minimal number of bars for EMA calculation and for accessing your buffers at index 1.
Summary:
Always check array length before accessing indices.
When you want to use value from previous closed candle, index 1 must exist.
So rates_total (bars on chart) >= 2 is minimal requirement.
Use CopyBuffer() with to_copy >= rates_total and use returned bar counts carefully.
If you want I can review your full OnCalculate() code to add correct boundary checks and add debug prints for arrays, just let me know!
Certainly! Here's the updated full indicator code with robust checks to avoid "array out of range" errors and proper EMA status logic:
//+------------------------------------------------------------------+
//| EMA_Status.mq5 |
//| Version 1.2, updated by ChatGPT |
//+------------------------------------------------------------------+
#property copyright "ChatGPT"
#property version "1.2"
#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 3
// EMA plot settings
#property indicator_label1 "EMA 50"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrGreen
#property indicator_width1 2
#property indicator_label2 "EMA 100"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrBlue
#property indicator_width2 2
#property indicator_label3 "EMA 200"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrRed
#property indicator_width3 2
//--- input parameters
input ENUM_TIMEFRAMES SelectedTimeframe = PERIOD_M15; // Timeframe for EMAs
input bool Show_EMAs = true; // Show/Hide EMAs on chart
//--- EMA periods
#define EMA1_PERIOD 50
#define EMA2_PERIOD 100
#define EMA3_PERIOD 200
//--- indicator buffers
double ema1_buf[];
double ema2_buf[];
double ema3_buf[];
string label_name = "EMA_Status_Label";
// Handles for EMA indicators on selected timeframe
int ema1_handle = INVALID_HANDLE;
int ema2_handle = INVALID_HANDLE;
int ema3_handle = INVALID_HANDLE;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Create EMA indicator handles for selected timeframe
ema1_handle = iMA(_Symbol, SelectedTimeframe, EMA1_PERIOD, 0, MODE_EMA, PRICE_CLOSE);
if (ema1_handle == INVALID_HANDLE)
{
Print("Failed to create EMA1 handle");
return(INIT_FAILED);
}
ema2_handle = iMA(_Symbol, SelectedTimeframe, EMA2_PERIOD, 0, MODE_EMA, PRICE_CLOSE);
if (ema2_handle == INVALID_HANDLE)
{
Print("Failed to create EMA2 handle");
IndicatorRelease(ema1_handle);
return(INIT_FAILED);
}
ema3_handle = iMA(_Symbol, SelectedTimeframe, EMA3_PERIOD, 0, MODE_EMA, PRICE_CLOSE);
if (ema3_handle == INVALID_HANDLE)
{
Print("Failed to create EMA3 handle");
IndicatorRelease(ema1_handle);
IndicatorRelease(ema2_handle);
return(INIT_FAILED);
}
// Set buffers to indicator
SetIndexBuffer(0, ema1_buf, INDICATOR_DATA);
SetIndexBuffer(1, ema2_buf, INDICATOR_DATA);
SetIndexBuffer(2, ema3_buf, INDICATOR_DATA);
// Set drawing start offsets so the indicator starts plotting after enough bars
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, EMA1_PERIOD);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, EMA2_PERIOD);
PlotIndexSetInteger(2, PLOT_DRAW_BEGIN, EMA3_PERIOD);
// Show or hide EMAs plots using DRAW_LINE or DRAW_NONE
for (int i = 0; i < 3; i++)
{
if (Show_EMAs)
PlotIndexSetInteger(i, PLOT_DRAW_TYPE, DRAW_LINE);
else
PlotIndexSetInteger(i, PLOT_DRAW_TYPE, DRAW_NONE);
}
CreateStatusLabel();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
IndicatorRelease(ema1_handle);
IndicatorRelease(ema2_handle);
IndicatorRelease(ema3_handle);
ObjectDelete(0, label_name);
}
//+------------------------------------------------------------------+
//| Create status label on chart |
//+------------------------------------------------------------------+
void CreateStatusLabel()
{
if (ObjectFind(0, label_name) == -1)
{
ObjectCreate(0, label_name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, label_name, OBJPROP_CORNER, CORNER_RIGHT_UPPER);
ObjectSetInteger(0, label_name, OBJPROP_XDISTANCE, 100);
ObjectSetInteger(0, label_name, OBJPROP_YDISTANCE, 10);
ObjectSetInteger(0, label_name, OBJPROP_BACK, true);
ObjectSetInteger(0, label_name, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, label_name, OBJPROP_SELECTED, false);
ObjectSetInteger(0, label_name, OBJPROP_FONTSIZE, 14);
ObjectSetString(0, label_name, OBJPROP_FONT, "Arial");
ObjectSetInteger(0, label_name, OBJPROP_COLOR, clrWhite);
ObjectSetString(0, label_name, OBJPROP_TEXT, "Wait");
}
}
//+------------------------------------------------------------------+
//| Update status label text based on EMA order |
//+------------------------------------------------------------------+
void UpdateStatusLabel(double ema1, double ema2, double ema3)
{
string status = "Wait";
if (ema1 > ema2 && ema2 > ema3)
status = "Buy only";
else if (ema1 < ema2 && ema2 < ema3)
status = "Sell only";
ObjectSetString(0, label_name, OBJPROP_TEXT, status);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &price[])
{
// We need enough bars for longest EMA + 2 bars to access index 1 safely
int min_bars_needed = EMA3_PERIOD + 2;
if (rates_total < min_bars_needed)
return(prev_calculated);
// We copy all bars available to buffers for plotting
int to_copy = rates_total;
double temp1[], temp2[], temp3[];
if (CopyBuffer(ema1_handle, 0, 0, to_copy, temp1) <= 0)
return(prev_calculated);
if (CopyBuffer(ema2_handle, 0, 0, to_copy, temp2) <= 0)
return(prev_calculated);
if (CopyBuffer(ema3_handle, 0, 0, to_copy, temp3) <= 0)
return(prev_calculated);
// Fill indicator buffers for plotting
for(int i=0; i<to_copy; i++)
{
ema1_buf[i] = temp1[i];
ema2_buf[i] = temp2[i];
ema3_buf[i] = temp3[i];
}
// Check again to be sure index 1 exists before accessing it
if(to_copy > 1)
{
double last_ema1 = temp1[1];
double last_ema2 = temp2[1];
double last_ema3 = temp3[1];
UpdateStatusLabel(last_ema1, last_ema2, last_ema3);
}
return(rates_total);
}
//+------------------------------------------------------------------+
Key fixes:
Added checks to ensure rates_total >= EMA3_PERIOD + 2 so index 1 is always safe to access.
Returned prev_calculated in case of failure to CopyBuffer.
The previous closed bar EMA values (index 1) are used for status to avoid using incomplete “forming” bar values.
Robust and safe copying of buffers.
Basic status logic as before.
How to test:
Compile the indicator.
Attach to chart with enough bars loaded (preferably > 200 bars to be safe).
Check the top-right label for status.
Open the Experts tab to verify no runtime errors appear.
If you want me to help you add debug printing or other improvements, just ask!
Thanks for the feedback! If your label says "Buy only" when the condition is actually ema1 < ema2 < ema3 (which should indicate a "Sell only"), it most likely means the EMA variables (ema1, ema2, ema3) are not assigned or interpreted in the order you expect.
Remember:
ema1 = EMA with period 50 (fastest)
ema2 = EMA 100 (medium)
ema3 = EMA 200 (slowest)
You want:
Buy:ema1 > ema2 > ema3
Sell:ema1 < ema2 < ema3
But if your values are not mapped correctly (like EMA50 value assigned to ema2 instead of ema1), the logic flips.
How to fix this — Diagnose and Adjust
Step 1: Add debugging print to verify which EMA is which value
Add the following Print statement before calling UpdateStatusLabel in OnCalculate:
Step 3: If order is reversed in actual price levels, fix your Label update logic:
If debugging reveal, say, that last_ema1 is actually smaller than last_ema2 and last_ema3 (meaning EMA50 < EMA100 < EMA200) but label says "Buy only", then maybe the UpdateStatusLabel comparison is accidentally reversed.
Currently your code is:
void UpdateStatusLabel(double ema1, double ema2, double ema3)
{
string status = "Wait";
if (ema1 > ema2 && ema2 > ema3)
status = "Buy only";
else if (ema1 < ema2 && ema2 < ema3)
status = "Sell only";
ObjectSetString(0, label_name, OBJPROP_TEXT, status);
}
This is logically correct.
Step 4: Possible Explanation — Cross Symbol or timeframe mismatch
If you compute EMA on a timeframe that is much different than chart timeframe or a different symbol (broken reference), the EMA values may be inconsistent.
Make sure:
Indicator input SelectedTimeframe matches your intended timeframe.
_Symbol used in iMA() represents your current chart symbol.
Step 5: To make extra sure, print the label status you assign
Add a Print at the end of UpdateStatusLabel:
PrintFormat("Label set to: %s (EMA50=%.5f EMA100=%.5f EMA200=%.5f)", status, ema1, ema2, ema3);
Summary of what to do now:
Add debugging prints (especially PrintFormat() in OnCalculate() and inside UpdateStatusLabel())
Verify curves (EMA50, EMA100, EMA200) correspond to their buffers
Verify EMA values order in printed logs matches expectation
Confirm no mixing of indexes / symbols / timeframes
If you want, here is an updated OnCalculate() snippet with debug prints:
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &price[])
{
int min_bars_needed = EMA3_PERIOD + 2;
if (rates_total < min_bars_needed)
return(prev_calculated);
int to_copy = rates_total;
double temp1[], temp2[], temp3[];
if (CopyBuffer(ema1_handle, 0, 0, to_copy, temp1) <= 0)
return(prev_calculated);
if (CopyBuffer(ema2_handle, 0, 0, to_copy, temp2) <= 0)
return(prev_calculated);
if (CopyBuffer(ema3_handle, 0, 0, to_copy, temp3) <= 0)
return(prev_calculated);
// Copy buffers
for(int i=0; i<to_copy; i++)
{
ema1_buf[i] = temp1[i];
ema2_buf[i] = temp2[i];
ema3_buf[i] = temp3[i];
}
if (to_copy > 1)
{
double last_ema1 = temp1[1];
double last_ema2 = temp2[1];
double last_ema3 = temp3[1];
PrintFormat("EMA50=%.5f EMA100=%.5f EMA200=%.5f", last_ema1, last_ema2, last_ema3);
UpdateStatusLabel(last_ema1, last_ema2, last_ema3);
}
return(rates_total);
}
If after checking you still have the issue, post your debug output here, and I can guide you what is flipped.
Let me know if you want me to help you rewrite UpdateStatusLabel() with extra guards or force the label to match the logical price ordering!
Certainly! Below is the full fixed version of your EMA_Status indicator including debug prints to verify EMA values, solid array bounds checking, and the correct logic that maps EMA50, EMA100, and EMA200 properly.
//+------------------------------------------------------------------+
//| EMA_Status.mq5 |
//| Version 1.3, fixed and debug by ChatGPT |
//+------------------------------------------------------------------+
#property copyright "ChatGPT"
#property version "1.3"
#property strict
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots 3
// EMA plot settings
#property indicator_label1 "EMA 50"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrGreen
#property indicator_width1 2
#property indicator_label2 "EMA 100"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrBlue
#property indicator_width2 2
#property indicator_label3 "EMA 200"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrRed
#property indicator_width3 2
//--- input parameters
input ENUM_TIMEFRAMES SelectedTimeframe = PERIOD_M15; // Timeframe for EMAs
input bool Show_EMAs = true; // Show/Hide EMAs on chart
//--- EMA periods
#define EMA1_PERIOD 50
#define EMA2_PERIOD 100
#define EMA3_PERIOD 200
//--- indicator buffers
double ema1_buf[]; // EMA 50
double ema2_buf[]; // EMA 100
double ema3_buf[]; // EMA 200
string label_name = "EMA_Status_Label";
// Handles for EMA indicators on selected timeframe
int ema1_handle = INVALID_HANDLE;
int ema2_handle = INVALID_HANDLE;
int ema3_handle = INVALID_HANDLE;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
// Create EMA indicator handles for selected timeframe
ema1_handle = iMA(_Symbol, SelectedTimeframe, EMA1_PERIOD, 0, MODE_EMA, PRICE_CLOSE);
if (ema1_handle == INVALID_HANDLE)
{
Print("Failed to create EMA1 handle");
return(INIT_FAILED);
}
ema2_handle = iMA(_Symbol, SelectedTimeframe, EMA2_PERIOD, 0, MODE_EMA, PRICE_CLOSE);
if (ema2_handle == INVALID_HANDLE)
{
Print("Failed to create EMA2 handle");
IndicatorRelease(ema1_handle);
return(INIT_FAILED);
}
ema3_handle = iMA(_Symbol, SelectedTimeframe, EMA3_PERIOD, 0, MODE_EMA, PRICE_CLOSE);
if (ema3_handle == INVALID_HANDLE)
{
Print("Failed to create EMA3 handle");
IndicatorRelease(ema1_handle);
IndicatorRelease(ema2_handle);
return(INIT_FAILED);
}
// Set buffers to indicator
SetIndexBuffer(0, ema1_buf, INDICATOR_DATA);
SetIndexBuffer(1, ema2_buf, INDICATOR_DATA);
SetIndexBuffer(2, ema3_buf, INDICATOR_DATA);
// Set drawing start offsets so the indicator starts plotting after enough bars
PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, EMA1_PERIOD);
PlotIndexSetInteger(1, PLOT_DRAW_BEGIN, EMA2_PERIOD);
PlotIndexSetInteger(2, PLOT_DRAW_BEGIN, EMA3_PERIOD);
// Show or hide EMAs plots using DRAW_LINE or DRAW_NONE
for (int i = 0; i < 3; i++)
{
if (Show_EMAs)
PlotIndexSetInteger(i, PLOT_DRAW_TYPE, DRAW_LINE);
else
PlotIndexSetInteger(i, PLOT_DRAW_TYPE, DRAW_NONE);
}
CreateStatusLabel();
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
IndicatorRelease(ema1_handle);
IndicatorRelease(ema2_handle);
IndicatorRelease(ema3_handle);
ObjectDelete(0, label_name);
}
//+------------------------------------------------------------------+
//| Create status label on chart |
//+------------------------------------------------------------------+
void CreateStatusLabel()
{
if (ObjectFind(0, label_name) == -1)
{
ObjectCreate(0, label_name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0, label_name, OBJPROP_CORNER, CORNER_RIGHT_UPPER);
ObjectSetInteger(0, label_name, OBJPROP_XDISTANCE, 100);
ObjectSetInteger(0, label_name, OBJPROP_YDISTANCE, 10);
ObjectSetInteger(0, label_name, OBJPROP_BACK, true);
ObjectSetInteger(0, label_name, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, label_name, OBJPROP_SELECTED, false);
ObjectSetInteger(0, label_name, OBJPROP_FONTSIZE, 14);
ObjectSetString(0, label_name, OBJPROP_FONT, "Arial");
ObjectSetInteger(0, label_name, OBJPROP_COLOR, clrWhite);
ObjectSetString(0, label_name, OBJPROP_TEXT, "Wait");
}
}
//+------------------------------------------------------------------+
//| Update status label text based on EMA order |
//+------------------------------------------------------------------+
void UpdateStatusLabel(double ema1, double ema2, double ema3)
{
string status = "Wait";
if (ema1 > ema2 && ema2 > ema3)
status = "Buy only";
else if (ema1 < ema2 && ema2 < ema3)
status = "Sell only";
ObjectSetString(0, label_name, OBJPROP_TEXT, status);
PrintFormat("Label set to: %s (EMA50=%.5f EMA100=%.5f EMA200=%.5f)", status, ema1, ema2, ema3);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const int begin,
const double &price[])
{
// We need enough bars for longest EMA + 2 bars (for safely reading index 1)
int min_bars_needed = EMA3_PERIOD + 2;
if (rates_total < min_bars_needed)
return(prev_calculated);
// We copy all bars available for plotting
int to_copy = rates_total;
double temp1[], temp2[], temp3[];
// Copy EMA buffers, return prev_calculated on failure
if (CopyBuffer(ema1_handle, 0, 0, to_copy, temp1) <= 0)
return(prev_calculated);
if (CopyBuffer(ema2_handle, 0, 0, to_copy, temp2) <= 0)
return(prev_calculated);
if (CopyBuffer(ema3_handle, 0, 0, to_copy, temp3) <= 0)
return(prev_calculated);
// Fill indicator buffers for plot
for(int i=0; i<to_copy; i++)
{
ema1_buf[i] = temp1[i];
ema2_buf[i] = temp2[i];
ema3_buf[i] = temp3[i];
}
// Only proceed if we have at least 2 bars for index 1
if(to_copy > 1)
{
double last_ema1 = temp1[1]; // EMA50
double last_ema2 = temp2[1]; // EMA100
double last_ema3 = temp3[1]; // EMA200
UpdateStatusLabel(last_ema1, last_ema2, last_ema3);
}
return(rates_total);
}
//+------------------------------------------------------------------+
What’s new/fixed:
Added PrintFormat() in UpdateStatusLabel() to output current EMAs and label status—helps debug in Experts tab.
Added explicit comments clarifying buffer and EMA correspondences.
Confirmed ema1 is EMA50, ema2 EMA100, ema3 EMA200 everywhere.
Safe array access with checks before indexing.
Clear and robust.
How to proceed:
Compile this code in MetaEditor.
Attach the indicator to your chart.
Open the "Experts" tab in Terminal window.
Watch the printed values for EMA50, EMA100, EMA200 and the label status.
Verify that the label matches the logical EMA order.
If you see mismatched behavior, please copy the log printed by PrintFormat() here and I can help interpret it!
Feel free to ask if you want me to add:
Alerts,
Input to disable debugging print,
Label customization,
Or anything else.
"I got ema2 > ema1 > ema3 and it says buy"
Now, your print shows EMA50 > EMA100 > EMA200 — which is notema2 > ema1 > ema3.
Possible reasons for confusion before:
Maybe the values you saw initially were from current forming bar (index 0) or other place.
This version always uses the last closed bar (index 1 from buffering), which is better for consistent signals.
Summary:
Your label is consistent with data.
"Buy only" means EMA50 > EMA100 > EMA200, confirmed by your printout.
The indicator logic and code are working as intended.
Tips:
Always watch debug prints to check exact EMA values.
If you want to confirm signals with chart visuals, enable Show_EMAs = true.
The label text only reflects last closed bar EMA relationship.