Skip to content

How to Use Indicators in cBots

In this article and its accompanying video, we will show how you can reference and use custom indicators in your cBots. To do so, we will develop a trading algorithm that uses the output of a pre-made custom indicator to perform trading operations.

Creating a New cBot

To see all custom indicators currently installed on your machine, switch to the 'Algo' application and proceed to the 'Indicators' section.

If you see one or more custom indicators here, that means you can freely use them in any of your other algos. For this tutorial, we will be using the Sample SMA indicator and develop a cBot that trades based on simple moving average direction.

First, we will create a new cBot and rename it to something informative such as 'SMA Robot'. Our cBot will enter a buy trade when the SMA turns bullish and enter a sell trade when the SMA turns bearish. Our cBot will not hedge its positions, meaning that oppotite positions will be closed on each new signal.

Initialising an Indicator

When it comes to custom indicators, our first step is to add a reference to the custom indicator. To reference a custom indicator we need to execute the following steps.

  1. Click on the 'Manage References' button.
  2. Find the indicator that we want to reference in the newly appeared window.
  3. Tick the checkbox next to the indicator we want to reference.
  4. Click on 'Apply'.

After the indicator has been referenced, we can create an instance variable of its type.

1
SampleSMA _sma;

Note

Note that the indicator type should be the same as the indicator class name. This can be different from the file name of the indicator.

The next step is to initialise the indicator in our OnStart() method. However, before we do that, we will add a parameter to the cBot that will allow us to configure the periods used by the custom SMA indicator.

1
2
[Parameter(DefaultValue = 14)]
public int Periods { get; set; }

We can use the Indicators.GetIndicator<>() method to initialise any custom indicators we are referencing. We need to define the indicator type and pass the parameters of the indicator separated by commas.

Note

Note that the parameters need to be passed in the order declared in the indicator class.

In our case, the type is SampleSMA and we will pass the source values of this indicator, which in this case are the close prices of bars and the Period parameter.

1
_sma = Indicators.GetIndicator<SampleSMA>(Bars.ClosePrices, Periods);

Implementing Trading Logic

With our indicator initialised, we can implement our trading logic. Our custom SMA indicator saves the moving average values in the Results collection. The Results collection features two useful methods, IsRising() and IsFalling(). These methods allow us to find out if the indicator results are rising or falling.

We will use these methods to implement our trading conditions.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
protected override void OnBarClosed()
{
    if(_sma.Result.IsRising())
    {        

    }
    else if(_sma.Result.IsFalling())
    {

    }
}

We will now fill our if statements. When the moving average is rising we will close all sell positions and open a buy position. On the contrary, when the moving average is falling we will close all buy positions and open a sell position.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
protected override void OnBarClosed()
{
    if(_sma.Result.IsRising())
    {
        Close(TradeType.Sell);
        Open(TradeType.Buy);
    }
    else if(_sma.Result.IsFalling())
    {
        Close(TradeType.Buy);
        Open(TradeType.Sell);            
    }
}

In our code we have used two new methods that the API does not provide 'out of the box'. The Open() method opens new positions based on the defined direction and quantity. The Close() method closes all positions of the defined direction.

Here is how the Open() method is defined.

1
2
3
4
5
private void Open(TradeType tradeType)
{
    if(Positions.Count == 0)
        ExecuteMarketOrder(tradeType, SymbolName, Volume, InstanceId);
}

To execute an order inside the Open() method we also need to add the Volume parameter.

1
2
[Parameter(DefaultValue = 10000)]
public int Volume { get; set; }

All that is left is to define the Close() method.

1
2
3
4
5
private void Close(TradeType tradeType)
{
    foreach (var position in Positions.Where(p => p.TradeType == tradeType))
        position.Close();
}

Conducting Backtesting

As always, before we let the bot trade on our behalf, we will thoroughly backtest it. To do the same, add an instance and then switch to the 'Backtesting' tab. To learn more about backtesting, click here.

To test our bot even further, we also add the custom indicator that we have referenced to the chart to which an instance is attached and take a look at whether the position entry points matched the indicator outputs.

To learn more about using and developing cBots, feel free to browse other sections of the documentation and subscribe to our YouTube channel to be notified when we publish a new video.

Subscribe to our YouTube channel

In this article and its accompanying video, we will show how you can reference and use custom indicators in your cBots. To do so, we will develop a trading algorithm that uses the output of a pre-made custom indicator to perform trading operations.

Creating a New cBot

To see all custom indicators currently installed on your machine, switch to the Algo application and proceed to the 'Indicators'