how to show indicator when runing in MetaTrader and when debugging with MetaEditor

 

I am new to MQL5, so as far as know, there are two types of indicators, one is a built-in indicator like `iMA` function, another is the custom's indicator which should be created by the trader themselves within a separate indicator file.

My target is to show these two types of indicators onto the chart both in `live trading` mode and `debug on historical data within MetaEditor` mode, so what should i do for this four conditions:

  1. plot the built-in indicator like `iMA` onto the chart when live trading within MetaTrader
  2. plot the built-in indicator like `iMA` onto the chart when debugging code within MetaEditor
  3. plot the custom indicator from a separate file onto the chart when live trading with MetaTrader
  4. plot the custom indicator from a separate file onto the chart when debugging code within MetaEditor

For condition 3, i have tried creating a new indicator file named "IndicatorMA.mq5"with the code below, and then dragged it to the chart, the indicator is shown successfully. The code is

#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

// #property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot Upper
#property indicator_label1  "Upper"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot Lower
#property indicator_label2  "Lower"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

//--- indicator buffers
double         ma[];

int handleMa;

//--- Input variables for moving average
input ENUM_TIMEFRAMES MaTimeframe = PERIOD_H1;
input int MaPeriods = 200;
input ENUM_MA_METHOD MaMethod = MODE_SMA;
input ENUM_APPLIED_PRICE MaAppPrice = PRICE_CLOSE;

int OnInit() {
   ArraySetAsSeries(ma, true);
   SetIndexBuffer(0,ma,INDICATOR_DATA);
   
   handleMa = iMA(_Symbol, MaTimeframe, MaPeriods, 0, MaMethod, MaAppPrice);
   
   //---
   return(INIT_SUCCEEDED);
}

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[]) {
    
    CopyBuffer(handleMa, 0, 0, rates_total - prev_calculated, ma);
   
    return(rates_total);
}

and the indicator shown on the chart within MetaTrader is


For condition 4, the sample code described above triggered within MetaEditor labelled by red rectangle in the picture,

but nothing shown on the chart when debugging the code with MetaEditor.

Does the `debug mode` of MetaEditor support showing indicator on the chart, or something should be changed on the above code when adapting to the MetaEditor instead of MetaTrader ?



For the condition 1 and 2, i don' t know how to directly use the `iMA` function within an EA(in the OnTick() function as well as other order placing logic code) to draw moving average line onto the chart.


So if someone can help me understand how to plot indicators on the above four conditions and list some materials for me to learn on `drawing indicator` in EA or separate custom indicator file , i would appreciate it.

 

1. In order to plot any indicator (custom or not) on live chart use ChartIndicatorAdd in your EA code.

2. In strategy tester you get all indicators in visual mode on chart as long as you do NOT use : 

TesterHideIndicators(true)

note: In default mode all indicators are shown in strategy tester(visual mode) and you do not need  TesterHideIndicators.

 
Yashar Seyyedin #:

1. In order to plot any indicator (custom or not) on live chart use ChartIndicatorAdd in your EA code.

2. In strategy tester you get all indicators in visual mode on chart as long as you do NOT use : 

note: In default mode all indicators are shown in strategy tester(visual mode) and you do not need  TesterHideIndicators.

Thanks, Yashar Seyyedin, under your suggestion, i made it appear on my debug mode.