- Handles and counters of indicator owners
- A simple way to create indicator instances: iCustom
- Checking the number of calculated bars: BarsCalculated
- Getting timeseries data from an indicator: CopyBuffer
- Support for multiple symbols and timeframes
- Overview of built-in indicators
- Using built-in indicators
- Advanced way to create indicators: IndicatorCreate
- Flexible creation of indicators with IndicatorCreate
- Overview of functions managing indicators on the chart
- Combining output to main and auxiliary windows
- Reading data from charts that have a shift
- Deleting indicator instances: IndicatorRelease
- Getting indicator settings by its handle
- Defining data source for an indicator
Checking the number of calculated bars: BarsCalculated
When we create a third-party indicator by calling iCustom or other functions that we will see later in this chapter, it takes some time to calculate. As we know, the main measure of indicator data readiness is the number of calculated bars, which it returns from its OnCalculate function. Having the handle of the indicator, we can find out this number.
int BarsCalculated(int handle)
The function returns the number of bars for which data is calculated in the indicator specified by handle. In case of an error, we get -1.
While the data has not yet been calculated, the result is 0. Later this number should be compared with the size of the timeseries (for example, with rates_total if the calling indicator checks BarsCalculated in the context of its own OnCalculate function) to analyze the processing of new bars by the indicator.
In the UseWPR2.mq5 indicator, we will try to create IndWPR while changing the WPR period in the input argument.
input int WPRPeriod = 0; |
Its default value is 0, which is an invalid value. It is proposed intentionally to demonstrate an abnormal situation. Recall that in the source IndWPR.mq5 code there are checks in OnInit and in OnCalculate.
// IndWPR.mq5
|
Thus, at zero period, we should receive an error message, and BarsCalculated should always return 0. After we enter a positive value for the period, the auxiliary indicator should start calculating normally (and given the ease of calculating WPR, almost immediately), and BarsCalculated should return the total number of bars.
Now let's present the source code for creating a handle in UseWPR2.mq5.
// UseWPR2.mq5
|
In OnCalculate we just log the values BarsCalculated and rates_total.
int OnCalculate(const int rates_total,
|
Compile and run UseWPR2, first with the parameter 0, and then with some valid value, for example, 21. Here are the log entries for period zero.
iCustom(_Symbol,_Period,IndWPR,WPRPeriod)=10 / ok
|
Immediately after the creation of the handle, the data is not yet available, so the INDICATOR_DATA_NOT_FOUND(4806) error is shown, and the result BarsCalculated equals -1. This is followed by a notification about an incorrect input parameter, which confirms the successful loading and launch of the indicator IndWPR. In the following segment, we get the BarsCalculated value equal to 0.
In order for the indicator to be calculated, we will enter the correct input parameter. In this case, BarsCalculated equals rates_total.
iCustom(_Symbol,_Period,IndWPR,WPRPeriod)=10 / ok
|
After we have mastered checking the readiness of a slave indicator, we can start reading its data. Let's do this in the next example UseWPR3.mq5, where we will get acquainted with the function CopyBuffer.