Files
xMQL4/Docs/Samples/Chart.Indicator.Tutorial.md
2024-01-25 04:03:36 +03:30

3.8 KiB

Chart Indicator Tutorial

Create Indicator

open meta editor and create a new custom indicator.

Inputs

we have to show 2 lines in chart as my indicator, so in creation time, i specified these on input window:

line 1 => Line => Red line 2 => Line => Yellow

in this section if you check the Indicator in separate window, you tell the MT4 which this is an Oscillator.

after finish this section and source code will opened in meta editor. you can see how it defined as source code ...

i will complete all next works on the source code, and comment it ...

Source Code

//
// these line are comment lines and no necessary ...
//+------------------------------------------------------------------+
//|                                  x-saherelm-custom-indicator.mq4 |
//|                              Copyright 2023, SaherElm IT Center. |
//|                                          https://www.saherelm.ir |
//+------------------------------------------------------------------+

//
// all compiler options defines using # sign ...

//
// here define some global properties of indicator ...
// these are not necessary, but provides some usefull info about indicator itself ...
#property copyright "Copyright 2023, SaherElm IT Center."
#property link      "https://www.saherelm.ir"
#property version   "1.00"

//
// use for improvement compiling ...
#property strict

//
// here we tell this is usually indicator which
// represent's on chart itself ...
#property indicator_chart_window

//
// define which buffers processed using this indicator ...
#property indicator_buffers 2

//
// define which plots represent using this indicator ...
#property indicator_plots   2

//
// here we declare plot line1 to system ...
#property indicator_label1  "line1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrLime
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1

//
// here we declare plot line2 to system ...
#property indicator_label2  "line2"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrRed
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1

//
// here we declare indicator buffers ...
double         line1Buffer[];
double         line2Buffer[];

//
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+

//
// this event handle used to initialize what we want to use and what we 
// can do on indicator ...
int OnInit() {
    //
    // indicator buffers mapping ...
    // we must assign which buffer must used to drawn which plot ... 
    SetIndexBuffer(0,line1Buffer);
    SetIndexBuffer(1,line2Buffer);
   
    //
    // this means initialization end's successfully ...
    return(INIT_SUCCEEDED);
}

//
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+

//
// this event handler calls with system every time a change happens on price ( new tick ) ...
// all of calculatings must be done in this section ...
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[]
) {
    //
    // here we must do all of indicator related calculations and fill buffers for drawing plots on chart ...
   
    //
    // return value of prev_calculated for next call
    return(rates_total);
}

//
//+------------------------------------------------------------------+