Skip to content

Managing Chart Indicators

Traders often a rely on some conmbination of manual trading and automated trading 'helpers', especially custom indicators. However, in many cases, these 'helpers' are completelyt unaware of each other and cannot react to one another's actions and outputs.

Fortunately, cTrader offers a convenient means of managing chart indicators programmatically. Using this feature, you can build dynamic 'assistants' that read data from indicators users add to a chart.

You can also make it so an algo adds, removes, or changes indicator settings based on some conditions, allowing you to offer even more value to your users and open up new avenues for monetisation.

This API guide explains how you can manage chart indicators programmatically in different conditions.

Chart Indicator Management in One Minute!

  • By managing chart indicators programmatically, you can adjust indicator settings (e.g., line thickness) depending on certain conditions.
  • cBots can access any indicators you manually attach to a chart, read their data and trade accordingly!
  • Your algos can add or remove indicators to and from charts automatically, saving you time.
  • Managing chart indicators programmatically is possible for all custom indicators.

Adding and Removing Indicators Programmatically

You can easily add any indicator that you have access to by calling the following method.

1
ChartIndicator ChartIndicators.Add(string name, params object[] parameterValues)

The method takes the indicator name as the first parameter and then takes all parameters of said indicator in sequence (e.g., "Simple Moving Average", "High", 25).

Note

The method will accept the name of any indicator that you have access to.

Note

Any DataSeries parameters have to be specified as strings in the method arguments.

Note

If some parameters are not specified, the method will use default values. If some parameters are invalid, the method will invoke the OnException handler for your algo.

After the method is executed, the requested indicator will immediately be added to a chart and you will be able to manage it using the value returned by the method. For example, we can remove it by calling the ChartIndicators.Remove() method.

1
2
3
var newIndicator = ChartIndicators.Add("Simple Moving Average", "High", 25);
...
ChartIndicators.Remove(newIndicator);

Here is a simple cBot that uses both methods to add a custom Bollinger Bands indicator to a chart whenever the Average True Range (ATR) value crosses a specific threshold. If the ATR value falls below the threshold, the indicator is removed.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class ChartIndicatorsTest : Robot
    {

        private AverageTrueRange _aTR;
        private ChartIndicator _customBollingerBands;

        protected override void OnStart()
        {
            _aTR = Indicators.AverageTrueRange(14, MovingAverageType.Exponential);
            ChartIndicators.Add("Average True Range");
        }

        protected override void OnBar()
        {
            if (_aTR.Result[Bars.Count - 1] >= 0.0002) 
            {
                _bollingerBands = ChartIndicators.Add("Custom BB");
            } else if (_bollingerBands != null)
            {
                ChartIndicators.Remove(_bollingerBands);
            }
        }

    }
}

Modifying the Settings of Indicators

The ChartIndicator interface provides direct access to all major indicator settings via the following properties.

  • ChartIndicator.Type, which gets the type of an indicator.
  • ChartIndicator.Parameters, which gets a list of all indicator parameters.
  • Lines, which gets a list of all lines outputted by an indicator.

As indicators are visual aids, you can enhance them by modifying their settings depending on market conditions. The following indicator tries to do exactly that.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Indicator(AccessRights = AccessRights.None, IsOverlay = true)]
    public class ColourMovingAverage : Indicator
    {

        private MovingAverage _fastMA;
        private ChartIndicator _chartMA;
        private const int __lineThickness = 4;


        protected override void Initialize()
        {
            _fastMA = Indicators.MovingAverage(Bars.OpenPrices, 10, MovingAverageType.Exponential);
            _chartMA = ChartIndicators.Add("Exponential Moving Average", "Open", 10);
        }

        public override void Calculate(int index)
        {
            var line = _chartMA.Lines[0];

            if (Symbol.Bid > _fastMA.Result[index]) 
            {
                line.Color = Color.Green;
                line.Thickness = __lineThickness;
            } else 
            {
                line.Color = Color.Red;
                line.Thickness = __lineThickness;
            }
        }
    }
}

Depending on whether the current price of a symbol is above or below the exponential moving average, the indicator line changes colour and thickness. In some cases (e.g., quickly reacting to changing trends), additional visual help can make all the difference.

Handling Additional Events

The ChartsIndicators interface also exposes several events that you can handle to programmatically manage chart indicators.

  • IndicatorAdded, which is triggered every time a new indicator is added to a chart.
  • IndicatorRemoved, which is triggered every time an indicator is removed from a chart.
  • IndicatorModified, which is triggered every time the settings of an indicator are adjusted.

These events are valuable for the developers of plugins. Via plugins, you can create new elements of the cTrader UI that change their contents depending on whether a user adds, removes, or modifies indicators.

Indicator Management in Different Modes

Here is how indicator management works across different cTrader modes and tools.

Mode/Tool How it works
Non-visual backtesting All indicators added via ChartIndicators will be shown on the chart when backtesting finishes.
Visual backtesting and 'Market Replay' Everything works in real time as intended.
Optimisation All operations will work but no changes to the chart will be made when backtesting finishes.
cTrader CLI All operations work as intended but no changes are made to any charts.

Summary

Managing chart indicators programmatically is perfect for any developer who wants to offer algos that somehow enhance or simplify working with indicators. By leveraging this feature, you can deliver high-quality and valuable products to your users.