Creating and Running a cBot
The following diagram showcases the action flow of creating and running a new cBot.
graph TB B([Add a New cBot]) ==> C([Edit the Sample Code]); C ==> D([Save and Build Your cBot]); D ==> E([Create and Customize a cBot Instance]); E ==> F([Run a cBot Instance]);
Add a New cBot¶
Open the 'New ...' menu to the right of the search bar and select 'New cBot'. If the 'New cBot' option is already chosen, simply click on it.
cTrader Automate will automatically create a new cBot containing sample code. You should see a 'New cBot' extension added to the list of cBots.
You can rename your new bot by selecting it and pressing F2. Alternatively, right-click on the new extension and choose 'Rename'.
Edit the Sample Code¶
Click on your new bot to open the code editor window containing the following code.
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
As we have discussed previously, the cBot attribute
Robot along with its optional properties such as
AccessRights, precedes the cBot class (
OnStart() method is invoked each time an instance of your cBot is launched. In turn, the
OnTick() method is invoked on every tick. Last but not least, the
OnStop() method is invoked every time an instance of your new cBot stops operating.
Note that, as the
OnTick() method performs a certain action every tick, it is CPU-intensive. In many cases, there is also very little practical need for performing a trading operation for every tick, particularly when trading stocks or indices.
Instead, you could replace the
OnTick() method with the
OnBar() method. As implied by its name, it is only invoked at the opening of every bar, significantly reducing CPU loads.
These are far from the only methods you can use to set up the behavior of your cBot. However, they are the simplest to use if you are just starting out with C#. Please, refer to the 'References' section to learn more about the classes, methods, and properties you can use when coding cBots. In addition, you can always choose to write custom methods as shown in our introduction to C# and .NET.
Given what you have just learned and the examples provided in other parts of this documentation, edit the code of your cBot to suit your requirements.
To attain the same result, you can copy and paste the below code after the namespace declaration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Save and Build Your cBot¶
Save your code by clicking on the 'Save' button located above and to the right of the code editor window. Alternatively, press Ctrl+S. You can also right-click anywhere in the code editor and choose 'Save' from the newly opened menu.
Before you can use your code, you need to convert it into a working cBot. To build a cBot, click the 'Build' icon at the top of the code editor or in the cBot menu. The keyboard shortcut for this action is Ctrl+B.
If the build succeeds, a message confirming this will appear in the 'Build Result' viewer. If the build fails, this window will, instead, display a summary of all errors encountered when the build action was performed. Clicking on an error description in the 'Build Result' viewer will show you the exact place in your code where this error occurs.
If there are changes to your code made since the last build, you should see a red '*' sign next to the 'Build' icon. In this case, build the bot again before running it.
Create and Customize a cBot Instance¶
If you have successfully completed all of the above actions, your cBot should be ready for trading. To start using it, create a new instance by performing one of the following actions.
- Click on the '+' icon to the right of the extension name. In the menu that appears, select a symbol that you would like to trade.
- Click on the 'three dots' icon to the right of the cBot name and select 'Add Instance'. cTrader will automatically create a new instance trading the EURUSD symbol using h1 as the timeframe of the candlestick chart on which the instance is supposed to run on.
You should see a new instance located directly below your cBot.
If you have specified any customizable parameters in your code, they will be listed in the 'Parameters' tab located to the left of the 'Trade Watch' display. Before running an instance, make sure that these parameters match your requirements.
You can save your current parameter values to a locally stored
.cbotset file by clicking on the 'Save' icon in the upper-right corner of the 'Parameters' tab.
Alternatively, you can upload parameter values from an already existing
.cbotset file by pressing the 'Folder' icon.
Run a cBot Instance¶
To run the newly created instance, press the blue 'Start cBot' button to the right of the instance name. If this is your first time using cBots or indicators, you will see a warning window. Check the
"I understand how algorithmic trading works..." flag and press 'OK'. If prompted, provide your cBot with the required access rights.
To stop the newly created instance, press the red 'Stop' button. Note that you can change the symbol and timeframe for the currently chosen instance but only if it is not running. It is impossible to change the parameters of any currently running instances.