How to use indicators in cBots¶
In this article and its corresponding video, we explain how you can use built-in indicators in your cBots. Using indicators in a cBot is typically done for a variety of purposes, most notably making cBots take trading decisions depending on indicator outputs. To demonstrate this, we will develop a simple automated trading strategy based on the Relative Strength Index (RSI).
Create a new cBot¶
To create a new cBot, switch to the Algo tab and click New cBot.
We will rename our cBot to "RSI cBot". Here is how our automated trading strategy will function.
- The bot will enter buy positions when the RSI indicator moves below a predefined threshold.
- The bot will enter sell positions when the RSI indicator moves above a predefined threshold.
Define and initialise the indicator¶
The first thing we need to do is define the RSI indicator in our cBot class. To do so, we declare a new field.
1 |
|
Using private class members
We can safely make the rsi
field private as it will only be used in our cBot.
All cTrader indicators need to be initialised before we can use them. This is best done in the body of the OnStart()
method so that the cBot has access to the indicator output after it begins running. cTrader offers a handy class that allows you to easily initialise built-in indicators using ready-made constructors. Before we can see how this is done, we will add two parameters necessary for initialising the RSI indicator.
1 2 3 4 5 |
|
Now that we have added our parameters, we are ready to initialise the indicator.
1 2 3 4 |
|
Implement trading logic¶
As we have already initialised the indicator, we can call it and use it in our trading logic. In the case of the RSI indicator, we can read the outputs using the Results
collection.
To do so, we will use the OnBar()
method and implement the following conditions.
- If the current RSI value is below the RSI buy threshold, enter a new buy position.
- If the current RSI value is above the RSI sell threshold, enter a new sell position.
Because we rely on predefined thresholds to execute our trading logic, we will add two more parameters to our cBot.
1 2 3 4 5 |
|
Next, we will implement our conditions in the cBot code.
1 2 3 4 5 6 7 8 9 10 11 |
|
It is time to implement auxiliary methods for executing trading orders. This is done to create an additional level of abstraction in the code, improving readability and reusability.
We start by adding one more cBot parameter that allows you to set the traded volume.
1 2 |
|
Afterwards, we will add the Open()
and Close()
methods for entering a new position and closing all positions opened by the cBot in a given direction.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
We can use our auxiliary methods to complete the automated trading strategy.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Backtest the cBot¶
After we finish coding the cBot, we can build it and switch to the Backtesting tab to see how it performs on historical data. Click here to learn more about backtesting.
We can also attach the RSI indicator to the current chart to see whether the trades executed by the cBot match the intended entry points.