Indicator with multiple calculations
Last updated
Last updated
Blocks have a limitation in TSLab, a block can only produce one value. There are situations when one indicator must calculate several values and all of them must be used in the script.
For example, we want to write a Bollinger indicator, and in the script we want to use the upper line, lower line and channel width. To calculate these values, it is enough to calculate the average SMA line and the standard deviation StDev.
Let's write a universal MyBollinger block, which, depending on the parameters, can issue the top line, bottom line, or the width of the Bollinger. The block will have the following parameters:
Period is the indicator calculation period.
Coef is the indicator calculation coefficient
Type is an enumeration type that specifies how the indicator is calculated. If Top, then displays the top line. If Bottom, then displays the bottom line. If Width, then displays the width of the channel.
The formulas are: Upper line: SMA + StDev * Coef. Bottom line: SMA - StDev * Coef. Channel width: top line - bottom line.
We will calculate the SMA and StDev indicators through caching (Context.GetData). This will reduce the number of identical calculations. For example, let's add one block for the top line, one block for the bottom line, and one block for the channel width to the script. When the script is executed, the first block will calculate the SMA, StDev values and write them to the cache. Then the second and third blocks will no longer calculate these values, they will simply take them from the cache.
Important: you need to cache indicators with a full chain of dependent parameters (more details ). For this, the INeedVariableParentNames interface has been added, it has one VariableParentNames property, which contains the names of parent blocks with parameters. We will add this property to all caches of calculated indicators in order to accurately identify the incoming IList<double> values.