The high minus low indicator calculates the difference between the high and low prices of the current bar and displays it in an output series. The series is drawn on the chart as a line connecting the resulting values in each bar.
The [Indicator...] declaration includes several parameters which are defined as follows.
IsOverlay. A boolean that defines whether the line will be overlayed on the chart or displayed in a separate UI panel.
TimeZone. A TimeZones class field that specifies the timezone of the indicator data and the server time.
AccessRights. An 'AccessRights' class field that determines the access rights allocated to your indicator.
ScalePrecision. An int setting up the scale precision of the indicator output.
As stated previously, the Output attribute is declared to mark a property as an indicator output. This property should be public so that it can be referenced by other classes.
Indicator output should always be of the IndicatorDataSeries data type which is a list of doubles that can be indexed like an array. Therefore, the value at each [index] in the list Result can be assigned in the Calculate method as follows.
In most cases, the output of indicators can vary depending on user input. The way customisable parameters are set up for indicators is similar to how this is done for cBots.
The below simple moving average indicator is designed to accept a price source and a period as customisable parameters. Such parameters (Source and Periods in this particular example) must be preceded by the Parameter attribute.
Similarly to the [Indicator()] and [Output()] attributes discussed earlier, the [Parameter()] attribute can define certain characteristics applied to the user input.
Nested indicators are defined as indicators the value of which depends on the results of calculations performed by other indicators. They are useful when writing certain types of extensions such as the DeMark 9 indicator. Consider the following sample code.
To simplify working with nested indicators, IntelliSense automatically populates the list of all built-in indicators when you type Indicators followed by a dot into the code editor. It will also display all related input parameters once you select a certain indicator from this list.
cTrader Algo uses lazy loading when you use referenced indicators. The values supplied by the referenced indicator are not calculated until your code starts actively using them.
If you access the Outputs data of a referenced indicator, cTrader will start loading the indicator data by calling its Calculate() method on past and future bars. In any other case, the referenced indicator will remain idle and, therefore, will not consume any system resources.
This also means that if your indicator does not have any Output or if you have tried to access any of its public properties, you will get the default value of the property in question. To resolve this issue, call the Calculate() method of your custom indicator from the Calculate() method of the current indicator.
The term 'oscillators' encompasses all indicators that oscillate around a certain constant variable.
When creating oscillators, it is useful to first draw a horizontal or 'level' line at that constant value; the indicator will then oscillate around this line. In many cases, the constant value equals zero.
In the below example, we define a momentum oscillator. It typically oscillates around the value of 100. We add a level line at this value using the Levels attribute which is declared prior to the indicator attributes.
Note that the Levels attribute can only be used if the indicator is not overlayed on the chart, meaning that the IsOverlay property is not set to true. By default, the value of IsOverlay is false. If this attribute is omitted from your code, Levels should work properly.
If you need to establish multiple 'Level' lines, add a comma-separated list of values within the parenthesis as displayed below.
In some cases, you may want to create an indicator that has to be calculated only for the last bar in the trading chart. Simplifying this, the IsLastBar property can be used to check whether the index parameter of the Calculate() method is that of the last bar.
The below indicator is based on UTC time; however, it can display the last open time in New York and Tokyo.
1 2 3 4 5 6 7 8 91011121314151617181920
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC)]publicclassTimeInDifferentParts:Indicator{publicoverridevoidCalculate(intindex){if(IsLastBar)DisplayTime(index);}protectedvoidDisplayTime(intindex){DateTimenyDateTime=Bars.OpenTimes[index].AddHours(-5);DateTimetokyoDateTime=Bars.OpenTimes[index].AddHours(7);stringnyTime=nyDateTime.ToShortTimeString();stringtokyoTime=tokyoDateTime.ToShortTimeString();Chart.DrawStaticText("Title","Last Bar OpenTime \n NY "+nyTime+"\n"+"Tokyo "+tokyoTime,VerticalAlignment.Top,HorizontalAlignment.Left,Color.Lime);}}