How to Create a cBot in 5 Minutes (2024)

In this article and its corresponding video, we are going to describe a quick way to create a cBot in cTrader Algo. The cBot that we will create will implement a simple trading strategy called 'Three White Soldiers and Three Black Crows'. You can learn more about this strategy in our knowledge base.

Before following our instructions, make sure you have downloaded and installed the cTrader Desktop application from the official website. Launch it, and navigate to the cTrader Algo section. You can find it in the left panel. Click on the 'Algo' tab, and the cTrader Algo section should appear.

Add a New cBot

To create a new cBot, simply click on the 'New' button. Then, select 'Blank' and confirm your choice.

You will see a new item added to the list of cBots with 'New cBot' as its name. Select this bot, and the code editor window to the right will contain a code template to help you get started.

The next step is to rename the new cBot. To do so, right-click on it and select 'Rename' or press F2. We will use 'Three White Soldiers and Three Black Crows' as the new name of our cBot. Once done, press Enter. Your new cBot has been successfully created and is now ready for you to start coding your strategy.

Add the cBot Settings

Before we start implementing a trading strategy, we will take a look at the basic cBots parameters and methods. You can also consult our documentation to familiarise yourself with what different parameters and methods do.

  • cBot parameters allow for defining configurable attributes such as volume to be traded, or stop loss/take profit distances.
  • cBot methods define how your cBot behaves when specific events occur. There are four main events you need to handle when developing a cTrader cBot; all four of them are handled by four different methods provided by cTrader. These methods are OnStart(), OnTick(), OnBar(), and OnStop(). Note that the OnBar() method is missing in the default code template.

The following table defines these four methods in detail.

Method Name Definition
OnStart() This method is triggered when a cBot instance starts operating. It is used to initialise any variables you plan to use in your cBot such as indicators, counters, event handlers, or timers.
OnTick() This method is triggered on each incoming tick on the trading chart on which a cBot instance is running. Inside the OnTick() method, you can program custom entry and exit conditions as well as any other auxiliary function you need to run when a new tick arrives.
OnBar() This method is similar to OnTick(); however, it is triggered only when a new bar is drawn on the chart on which your cBot is running. Analogously to OnTick(), you can use it to program custom entry and exit conditions or any other functions that need to run on the formation of each new bar.
OnStop() This method is triggered when a cBot instance stops. It is used to perform final operations such as closing positions.

Add the Trading Logic

We will now add the code for our trading strategy. Our cBot should open a 'Buy' position when three green bars are formed on the chart and a 'Sell' position when there are three consecutive red bars.

First, we need to define the parameters of our cBot. These parameters will be fully customisable from the cTrader UI. We will define three parameters in our cBot.

  1. The volume of each trade.
  2. The stop loss in pips.
  3. The take profit in pips.

Paste the following code into your code editor just below the curly bracers after the class declaration (public class NewcBot : Robot)

12345678
[Parameter(DefaultValue = 1000)]public double Volume { get; set; }[Parameter(DefaultValue = 10)]public double TakeProfit { get; set; }[Parameter(DefaultValue = 10)]public double StopLoss { get; set; }

We now need to implement the logic behind our trading strategy. Paste the following code in your OnBar() method. Remember that this method is called every time a new bar is formed.

 1 2 3 4 5 6 7 8 9101112131415
//Three White Soldiersif(Bars.ClosePrices.Last(1) > Bars.OpenPrices.Last(1)&& Bars.ClosePrices.Last(2) > Bars.OpenPrices.Last(2)&& Bars.ClosePrices.Last(3) > Bars.OpenPrices.Last(3)){ ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "", StopLoss, TakeProfit);}//Three Black Crowsif(Bars.ClosePrices.Last(1) < Bars.OpenPrices.Last(1)&& Bars.ClosePrices.Last(2) < Bars.OpenPrices.Last(2)&& Bars.ClosePrices.Last(3) < Bars.OpenPrices.Last(3)){ ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, "", StopLoss, TakeProfit);}

Now we will examine the code more closely. cTrader gives you access to the chart historical bars, so we can check whether the last three bars are all green or all red, respectively. In the OnBar() method above, we first check whether the last three bars were all green. We then evaluate whether these bars were all red.

If one of the two conditions is true, we place a market order via the ExecuteMarketOrder() method. It takes several inputs (arguments), with the most important of them being the direction of the trade, the symbol, the traded volume, the stop loss, and the take profit.

In our case, the order direction (TradeType.Buy and TradeType.Sell) is defined by the trading strategy conditions. The symbol name is taken directly from the chart that our cBot is running on (SymbolName). The trading volume (Volume), stop loss (Stop Loss), and take profit (TakeProfit) are all taken from the cBot parameters we have defined.

Build and Run the cBot

We now need to check whether our cBot builds successfully. By default, the build result window is located directly beneath the code editor. If you do not see it, click on the 'Layout' button in the top-most bar of the cTrader UI and select 'Build Result'. The keyboard shortcut for this action is Ctrl + W

Afterward, press the 'Build' button located in the topmost bar of the cTrader UI. Alternatively, right-click on your cBot and click on 'Build' in the newly appeared menu, or simply press Ctrl + B.

How to Create a cBot in 5 Minutes (1)

If the build is successful, you will see a green message in the 'Build Result' area at the bottom of the code editor.

How to Create a cBot in 5 Minutes (2)

Alternatively, if there are issues with your code, you will see a red message and a detailed summary of all build errors.

How to Create a cBot in 5 Minutes (3)

After your cBot has been built successfully, you can start using it. All you need to do is add an instance on a trading chart. To do so, simply click on the 'plus' icon to the right of the name of your cBot. You will see a list of all symbols on which you can run your cBot.

How to Create a cBot in 5 Minutes (4)

We are going to select EURUSD. After you choose a symbol, a new cBot instance will be added. You will see a new item in the list of cBots with the symbol name and the timeframe you would like to trade.

You should also see the parameters you have coded in the 'Parameters' tab directly beneath the trading chart.

How to Create a cBot in 5 Minutes (5)

You can change these parameters however you want or simply leave them at their default values.

Additionally, you will see a 'Play' button at the top of the EURUSD chart.

How to Create a cBot in 5 Minutes (6)

To launch your instance, press this button. Alternatively, a copy of this button is also located to the right of your instance in the list of cBots to the left. Either one of these buttons will start the cBot.

After clicking on either button, you should see it turn orange, meaning that an instance of our cBot is now up and running. the 'Log' tab, you should also see a new line stating that the cBot has now started.

Backtest the cBot

Our cBot may take a long time to open a position on a real trading chart. We are going to run a quick backtest to see it in action on historical data. To do this, open the 'Backtest' tab.

How to Create a cBot in 5 Minutes (7)

Use the calendar slider closer to the top of the screen to select the date range that you would like to use for backtesting. Enable the 'Visual Mode' flag to track the results of backtesting as they occur.

We are going to move the slider back one month. Click on the 'Play' button now to launch the cBot on the chosen historical data.

Afterward, you will be able to see new positions being opened and/or closed in the tab immediately below the trading chart and to the right of the 'Parameters' section. Specifically, after detecting either of the two patterns we have coded, the cBot will place a 'Buy' order or a 'Sell' order.

Running a backtest is a useful way to analyse whether your cBot is working correctly.

Summary

We hope that this article has been helpful in demonstrating how you can create an automated trading robot using cTrader. To learn more, consult our extensive documentation or post a question on our forums.

You can also subscribe to your YouTube channel to be alerted when we post a new video.

Subscribe to our YouTube channel

How to Create a cBot in 5 Minutes (2024)
Top Articles
The Value of an AWS Certification
How to Find Out the Maximum RAM Capacity for Your Computer
Andi Eskin
Craigslist Lake Of Ozarks Mo
FTC challenge of biggest grocery deal ever captures Albertsons exec's surprise: 'You are basically creating a monopoly in grocery with the merger'
Active Inmates Ashland County
What Does “Turkey Trot” Mean? The History and Traditions Behind This Thanksgiving Tradition – THEKITCHENTODAY
Adan4Adam Com
About Us! - Boys & Girls Clubs of Southcentral Alaska
Homewav Pending Connection
Dkm Match Pairs
National Weather Service
List of Amazon Fulfillment Center Locations [2024 Updated]
Key takeaways from the Fed’s decision to deliver a jumbo-sized interest rate cut | CNN Business
Imbigswoo
Section 102 Allstate Arena
Methstreams Boxing Stream
Kwabena Agyei-Agyemang on LinkedIn: #youtube #work #gaming
Cranston Sewer Tax
8er Reihe Einmaleins - Kostenlose Arbeitsblätter
Craigslist Portland Cars And Trucks By Owner
Drunk Farmer Morning Routine
Wild West 2013-12 - PDF Free Download
Creed 3 Showtimes Near Southeast Cinemas Alamance Crossing Stadium 16
Houses For Sale 180 000
R Guildwars2
How To Write an Email To Supplier For a Purchase Order
Big Meech Childhood Home
Joy Ride 2023 Showtimes Near Marcus Oakdale Cinema
Costco Gas Prices Macomb Mi
Itslunarliv Leaked Video
Rettungshund Ruby bei Netflix: Alles zu Start, Besetzung und Handlung
Violent Night Showtimes Near Century 14 Vallejo
Hotels Near 9300 Sw 72Nd St Miami Fl 33173
라이키 유출
24Hrs Mcdonalds Near Me
Halfabob [110 Berserker] - Maj'Dul - EQ2U
Google Flights Calgary
Corpus Christi Busted Newspaper
Wall Street Institute
Myusu Canvas
Appian Community
Theobromine: Benefits and Side Effects of Cocoa’s Interesting Phytochemical
Elanco Rebates.com 2022
Hmnu Stocktwits
Ltlv Las Vegas
Ffxiv Shelfeye Reaver
Seat Number Usana Seating Chart With Rows
Violent Night Showtimes Near Mjr Universal Grand Cinema 16
R/Clashroyale
The Star Beacon Obituaries
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 5718

Rating: 4.7 / 5 (47 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.