Do not forget the rules:create the indicator handle ONCE in OnInit,
//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
//--- create handle of the indicator iRSI
handle_iRSI=iRSI(Symbol(),Period(),Inp_RSI_ma_period,Inp_RSI_applied_price);
//--- if the handle is not createdif(handle_iRSI==INVALID_HANDLE)
{
//--- tell about the failure and output the error codePrintFormat("Failed to create handle of the iRSI indicator for the symbol %s/%s, error code %d",
Symbol(),
EnumToString(Period()),
GetLastError());
//--- the indicator is stopped earlyreturn(INIT_FAILED);
}//---return(INIT_SUCCEEDED);
}
An example of get values from the iBands indicator
Code (Expert): iBands values on a chart.mq5
Do not forget the rules:create the indicator handle ONCE in OnInit,
//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
//--- create handle of the indicator iBands
handle_iBands=iBands(Symbol(),Period(),Inp_Bands_bands_period,
Inp_Bands_bands_shift,Inp_Bands_deviation,Inp_Bands_applied_price);
//--- if the handle is not createdif(handle_iBands==INVALID_HANDLE)
{
//--- tell about the failure and output the error codePrintFormat("Failed to create handle of the iBands indicator for the symbol %s/%s, error code %d",
Symbol(),
EnumToString(Period()),
GetLastError());
//--- the indicator is stopped earlyreturn(INIT_FAILED);
}
//---return(INIT_SUCCEEDED);
}
Our indicator ('xSuperTrend') is equipped with iATR - which means we need one more buffer. Add a buffer.
It was:
#property indicator_chart_window#property indicator_buffers3#property indicator_plots3
***
//--- indicator buffersdouble UpTrendBuffer[];
double SuperTrendBuffer[];
double DownTrendBuffer[];
//---int handle_iATR; // variable for storing the handle of the iATR indicator
It became:
#property indicator_chart_window#property indicator_buffers4#property indicator_plots3
***
//--- indicator buffersdouble UpTrendBuffer[];
double SuperTrendBuffer[];
double DownTrendBuffer[];
double iATRBuffer[];//---int handle_iATR; // variable for storing the handle of the iATR indicator
We get the handle of the indicator (do not forget: the handle needs to be received ONE time in OnInit)
//+------------------------------------------------------------------+//| Custom indicator initialization function |//+------------------------------------------------------------------+intOnInit()
{
//--- indicator buffers mappingSetIndexBuffer(0,UpTrendBuffer,INDICATOR_DATA);
SetIndexBuffer(1,SuperTrendBuffer,INDICATOR_DATA);
SetIndexBuffer(2,DownTrendBuffer,INDICATOR_DATA);
SetIndexBuffer(3,iATRBuffer,INDICATOR_CALCULATIONS);
//--- create handle of the indicator iATR
handle_iATR=iATR(Symbol(),Period(),Inp_ma_period);
//--- if the handle is not createdif(handle_iATR==INVALID_HANDLE)
{
//--- tell about the failure and output the error codePrintFormat("Failed to create handle of the iATR indicator for the symbol %s/%s, error code %d",
Symbol(),
EnumToString(Period()),
GetLastError());
//--- the indicator is stopped earlyreturn(INIT_FAILED);
}//---return(INIT_SUCCEEDED);
}
//| Demo_iATR.mq5 | //| Copyright 2011, MetaQuotes Software Corp. | //| https://www.mql5.com | "The method of creation of the handle is set through the 'type' parameter (function type...
First, at the "top of the program", add a variable 'bars_calculated' - it stores the number of barors that the iATR indicator has counted.
int handle_iATR; // variable for storing the handle of the iATR indicatorint bars_calculated=0; // we will keep the number of values in the Average True Range indicator//+------------------------------------------------------------------+//| Custom indicator initialization function |//+------------------------------------------------------------------+intOnInit()
We almost completely insert the code from the help (In the inserted code, replace 'handle' with 'handle_iATR')
//+------------------------------------------------------------------+//| Custom indicator iteration function |//+------------------------------------------------------------------+intOnCalculate(constint rates_total,
constint prev_calculated,
constdatetime &time[],
constdouble &open[],
constdouble &high[],
constdouble &low[],
constdouble &close[],
constlong &tick_volume[],
constlong &volume[],
constint &spread[])
{
//---int values_to_copy;
//--- determine the number of values calculated in the indicatorint calculated=BarsCalculated(handle_iATR);
if(calculated<=0)
{
PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());
return(0);
}
//--- if it is the first start of calculation of the indicator or if the number of values in the iATR indicator changed//---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history)if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1)
{
//--- if the iATRBuffer array is greater than the number of values in the iATR indicator for symbol/period, then we don't copy everything//--- otherwise, we copy less than the size of indicator buffersif(calculated>rates_total)
values_to_copy=rates_total;
else
values_to_copy=calculated;
}
else
{
//--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate()//--- for calculation not more than one bar is added
values_to_copy=(rates_total-prev_calculated)+1;
}
//--- fill the iATRBuffer array with values of the Average True Range indicator//--- if FillArrayFromBuffer returns false, it means the information is nor ready yet, quit operationif(!FillArrayFromBuffer(iATRBuffer,handle_iATR,values_to_copy))
return(0);
//--- memorize the number of values in the Average True Range indicator
bars_calculated=calculated;//--- return value of prev_calculated for next callreturn(rates_total);
}
//+------------------------------------------------------------------+//| Filling indicator buffers from the iATR indicator |//+------------------------------------------------------------------+bool FillArrayFromBuffer(double &values[], // indicator buffer for ATR valuesint ind_handle, // handle of the iATR indicatorint amount // number of copied values
)
{
//--- reset error codeResetLastError();
//--- fill a part of the iATRBuffer array with values from the indicator buffer that has 0 indexif(CopyBuffer(ind_handle,0,0,amount,values)<0)
{
//--- if the copying fails, tell the error codePrintFormat("Failed to copy data from the iATR indicator, error code %d",GetLastError());
//--- quit with zero result - it means that the indicator is considered as not calculatedreturn(false);
}
//--- everything is finereturn(true);
}
//+------------------------------------------------------------------+//| Indicator deinitialization function |//+------------------------------------------------------------------+voidOnDeinit(constint reason)
{
if(handle_iATR!=INVALID_HANDLE)
IndicatorRelease(handle_iATR);
Comment("");
}//+------------------------------------------------------------------+
//| Demo_iATR.mq5 | //| Copyright 2011, MetaQuotes Software Corp. | //| https://www.mql5.com | "The method of creation of the handle is set through the 'type' parameter (function type...
Example of creating an indicator based on iATR. Part 5.
code: xSuperTrend.mq5
Until this moment, we performed preparatory operations. Now it is time to write the indicator logic.
To avoid unnecessary calculations, we always work in the 'prev_calculated' variable
//--- memorize the number of values in the Average True Range indicator
bars_calculated=calculated;
//--- main loopint limit=prev_calculated-1;
if(prev_calculated==0)
limit=1;
for(int i=limit; i<rates_total; i++)
{
}//--- return value of prev_calculated for next callreturn(rates_total);
Remember that the indicator already has all arrays by the current character (ope, high, low, close, and others) - they are transferred through OnCalculate.
bars_calculated=calculated;
//--- main loopint limit=prev_calculated-1;
if(prev_calculated==0)
limit=1;
for(int i=limit; i<rates_total; i++)
{
//--- сalculate SuperTrend
UpTrendBuffer[i] = (high[i-1]+low[i-1])/2.0+Inp_multiplier*iATRBuffer[i];
DownTrendBuffer[i]= (high[i-1]+low[i-1])/2.0-Inp_multiplier*iATRBuffer[i];
SuperTrendBuffer[i]=SuperTrendBuffer[i-1];
//--- set SuperTrend levelsif(close[i]>SuperTrendBuffer[i-1] && close[i-1]<=SuperTrendBuffer[i-1])
{
SuperTrendBuffer[i]=DownTrendBuffer[i];
}
elseif(close[i]<SuperTrendBuffer[i-1] && close[i-1]>=SuperTrendBuffer[i-1])
{
SuperTrendBuffer[i]=UpTrendBuffer[i];
}
elseif(SuperTrendBuffer[i-1]<DownTrendBuffer[i])
SuperTrendBuffer[i]=DownTrendBuffer[i];
elseif(SuperTrendBuffer[i-1]>UpTrendBuffer[i])
SuperTrendBuffer[i]=UpTrendBuffer[i];
else
SuperTrendBuffer[i]=SuperTrendBuffer[i-1];
}//--- return value of prev_calculated for next callreturn(rates_total);
}
It should be remembered that in MQL5 the indicator handle must be received once (in OnInit). Objective: there are three timeframes and their own parameters for RSI indicators. You need to get values from these indicators.
Algorithm of actions: at the global program level (in the "header" of the Expert Advisor), declare the 'arr_handle_iRSI' array - this array will store the handles of RSI indicators from different time frames.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
An example of get values from the iRSI indicator
Code: iRSI get value.mq5
Do not forget the rules: create the indicator handle ONCE in OnInit,
use CopyBuffer to get the data:
Result:
Рис. 1. iRSI get value
An example of get values from the iBands indicator
Code (Expert): iBands values on a chart.mq5
Do not forget the rules: create the indicator handle ONCE in OnInit,
use CopyBuffer to get the data:
Result:
Рис. 1. iBands values on a chart
Example of creating an indicator based on iATR
code: xSuperTrend.mq5
Step 1: In the MetaEditor, go to the folder with indicators (in my example, this is the "MyInd" folder). Next click the right mouse button:
Pic. 1. Step 1
Step 2: Choose the type of program: Custom Indicator
Pic. 2. Step 2
Step 3: Enter the name of the indicator
Pic. 3. Step 3
Step 4. We connect the full form for OnCalculate
Pic. 4. Step 4
Step 5. We add three graphical constructions to the indicator. We give them names and colors.
Pic. 5. Step 5
As a result, we get a blank indicator. Denote the version with the number '1.000'
To be continued ...
Example of creating an indicator based on iATR. Part 2.
code: xSuperTrend.mq5
Let's start filling our indicator: add input parameters, add the variable "handle_iATR" - the handle of the iATR indicator will be stored in it.
Note: the variable 'handle_iATR' is declared at the global program level (in the "header" of the program).
Save the version number 1.001
To be continued ...
Example of creating an indicator based on iATR. Part 3.
code: xSuperTrend.mq5
Works with help ( iATR )
Our indicator ('xSuperTrend') is equipped with iATR - which means we need one more buffer. Add a buffer.
It was:
It became:
We get the handle of the indicator (do not forget: the handle needs to be received ONE time in OnInit)
Save the version number 1.002
To be continued ...
Example of creating an indicator based on iATR. Part 4.
code: xSuperTrend.mq5
Works with help ( iATR ) - Copy paste :)
First, at the "top of the program", add a variable 'bars_calculated' - it stores the number of barors that the iATR indicator has counted.
We almost completely insert the code from the help (In the inserted code, replace 'handle' with 'handle_iATR')
Save the version number 1.003
To be continued ...
Example of creating an indicator based on iATR. Part 5.
code: xSuperTrend.mq5
Until this moment, we performed preparatory operations. Now it is time to write the indicator logic.
To avoid unnecessary calculations, we always work in the 'prev_calculated' variable
Remember that the indicator already has all arrays by the current character (ope, high, low, close, and others) - they are transferred through OnCalculate.
Save the version number 1.005
The end.
Step-by-step instruction: the indicator displays two iCCIs in one window.
So, step by step:
Fig. 1. Step one
Full text and code: CCI Dual:
Simple example One position
Code: Simple example One position.mq5
The EA holds one position in the market (of a given type) - as soon as you close this position, the EA will open one position again.
Example MultiTimeFrame iRSI get value
Code: Example MultiTimeFrame iRSI get value.mq5
It should be remembered that in MQL5 the indicator handle must be received once (in OnInit). Objective: there are three timeframes and their own parameters for RSI indicators. You need to get values from these indicators.
Algorithm of actions: at the global program level (in the "header" of the Expert Advisor), declare the 'arr_handle_iRSI' array - this array will store the handles of RSI indicators from different time frames.
Code:
Result of work: