Clickalgo Knowledge Center
RETURN BACK TO WEBSITE >

How to Create a cBot Template

The correct method to create a new cBot is to first open the cTrader trading platform and then click on the Automate tab, this is where you will build and run your custom cBots and indicators if you need help with navigating your way around the cTrader platform follow the link below.

 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
 
namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
 
        protected override void OnStart()
        {
            // Put your logic is run here when the cBot starts
        }
 
        protected override void OnTick()
        {
            // Put your logic is run here when there is a symbol price change when each tick of data comes in.
        }
 
        protected override void OnStop()
        {
            // Put your logic here when the cBot stops
        }
    }
}


You can easily create a new cBot by just clicking on the NEW button, this will create a simple blank template or common code framework used with all your cBots.

 

How to create a new ctrader cbot

 

Now you will see in the code editor on the right panel the source code below.

Referenced Assemblies (Libraries)

 
Let us break the code down and explain each part to you, the using keyword you see will reference existing libraries (assemblies).
 

using System;
using System.Linq;

 
The first section of code above simply grabs existing functionality from the .NET framework so that you do not need to re-invent the wheel and create logical code blocks that already exist.
 

using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

 
The referenced assemblies above show the cTrader API libraries, it is these libraries that you will use to interface with cTrader for trading and displaying indicators.

Class Attributes

An attribute is a declarative tag that is used to convey information to runtime about the behaviours of the cBot class, the attribute used with our cBot has a few options.

 

[Robot(TimeZone = TimeZones.UTC)]

 

The attribute above is the default template, the timezone that you choose, in this case, UTC will be the server time used in the robot.

 

[Robot(TimeZone= TimeZones.UTC, AccessRights = AccessRights.FullAccess)]

 

Another attribute setting for the cBot is the access-rights, what this means is that when the cBot runs within cTrader how much access does the code within has on the user's machine.

 

An attribute is actually an object that is associated with any of these elements: Assembly, Class, Method, Delegate, Enum, Event, Field, Interface, Property and Struct.

Robot Interface Implementation

The class implements the Robot interface using the line of code below, the colon and Robot name is used. What this does is allow events that happen, some of the events are when the robot starts when the robot stops and on every price change.

 

public class NewcBot: Robot

 

It is always a good idea to change the name of your cBot and the class name to something more meaningful that has something to do with your project, if your project involved a moving average strategy, then call it Moving Average Strategy.

cBot User Defined Parameters

This is the area where you will be defining your cBot adjustable settings, you can specify only certain data type to use, in the example below it is using a double and the name of the parameter is Parameter with a default value of 0.00.


Parameter(DefaultValue = 0.0)]
public double Parameter { get; set; }

 

If you wish to create an email address entry and group your parameter settings you can use the keyword Group keyword, the example below shows a user setting for the cBot to allow an email address to be entered with a default value of your@email.com.

 

[Parameter("Email Address", DefaultValue = "your@email.com", Group = "Notifications")]
public string EmailAddress{ get; set; }

 

ctrader basic param

Custom Enums as DataTypes

It is also possible to define a dropdown list of options for adjustable cBots settings.

 

public enum ClosingStrategy
{
     StopLoss, TrailingStop, OppositeSignal
}

public class NewcBot : Robot
{
     [Parameter("Closing Strategy")]
     public ClosingStrategy ClosingStrategy { get; set; }
}

 

cBot settings enums

cBot Events

The basic template will capture 3 events that will occur with the cBot when it starts, stops and when the price changes, there are many other events now available.

  • Further Reading on cTrader cBot Events

Build The cBot

Once you are ready you just need to build your cBot by right-clicking your mouse on the cBot in the Automate screen and choosing Build.

Comments:

No comments for this article.

You need to be logged in to write comments.