How to Build Your First cBot: From Idea to Demo Account
A beginner's path to a first trading bot: pick one simple idea, write exact rules, understand the cBot structure, backtest honestly and run it on a demo account.
A first trading bot should be boring. Its job is not to make money - it is to teach you the full path from an idea to a program that runs unattended, and to show you every place where that path can go wrong. Once you have done it once with a simple strategy, more ambitious bots become much easier.
Step 1: choose one simple idea
Pick an idea you can explain in one sentence and test without debate. Good first candidates:
- Moving-average trend filter: trade only in the direction of a long average, enter on a cross of a short one.
- Breakout: enter when price closes above the highest high of the last N bars.
- Session range: trade the break of the Asian range during the London session.
Avoid grids, martingales and multi-indicator systems for a first bot. They hide mistakes behind complexity.
Step 2: write exact rules
Turn the idea into rules a program can follow:
- Symbol and timeframe: EURUSD, H1.
- Entry: when the 20-period EMA crosses above the 50-period EMA on a closed bar and price is above the 200-period EMA, buy. Mirror for sells.
- Exit: stop loss 1.5 × ATR(14); take profit 2.5 × ATR(14).
- Risk: 1% of equity per trade, at most one position.
- Session: no new trades between 21:00 and 23:00 server time (rollover).
Every word you cannot turn into a condition is a decision the program will make for you.
Step 3: understand the bot’s structure
Most trading platforms organise a bot around events. In cTrader, a cBot is a class with a few methods the platform calls at the right moments:
| Method | When it runs | Typical use |
|---|---|---|
OnStart() |
When the bot starts | Create indicators, check settings |
OnBar() |
When a new bar opens | Evaluate signals on the bar that just closed |
OnTick() |
On every price change | Trailing stops, very short-term logic |
OnStop() |
When the bot stops | Clean-up, final log message |
For the rules above, all the logic lives in OnBar(): read the indicator values of the last closed bar, check the conditions, and send an order with stop and target attached. Parameters such as EMA lengths and risk percentage are exposed as inputs, so you can change and optimise them without editing code.
Step 4: add the safety basics
Even a first bot needs a few protections:
- Position check - do not open a new trade if one is already open for this bot (use a label to identify the bot’s positions).
- Stop loss on every order, sent with the order, not afterwards.
- Volume checks - round to the symbol’s volume step and respect minimum and maximum volume.
- Logging - print every signal and every order result, so you can reconstruct what happened.
Step 5: backtest honestly
Run the bot on several years of data with realistic commission and spread. Then:
- Watch it visually for the first few dozen trades. Are entries on the right bars? Are stops on the correct side?
- Read the report - number of trades, expectancy, maximum equity drawdown, results by year.
- Stress it - higher spread, entries one bar later.
Expect your first result to be mediocre. That is normal. The point is that the bot does exactly what the rules say.
See how backtesting works and common mistakes.
Step 6: run it on a demo account
Start the bot on a demo account with the same settings and let it run for several weeks. Check every day at first:
- Did it trade when the backtest says it should have?
- Are spreads and fills close to what you assumed?
- Does it survive weekends, restarts and disconnections without duplicate orders?
On cTrader you can run a bot locally in the desktop app or as a cloud instance that keeps running when your computer is off. Check that the features you use (for example, sending notifications to the internet) are available in the environment you choose.
Step 7: decide what to improve
After the demo period, improve one thing at a time and re-test: an exit rule, a session filter, a volatility filter. Keep a log of versions. Resist the urge to add many filters at once - that is how first bots turn into overfitted ones.
A realistic timeline
| Phase | Typical time |
|---|---|
| Idea and rules | 1 day |
| Code and debugging | 1-3 days |
| Backtests and stress tests | 2-4 days |
| Demo forward test | 3-6 weeks |
| Small live test | 1-3 months |
Key takeaways
- Start with one simple, testable idea and write exact rules.
- Put signal logic in a bar-close event unless you truly need ticks.
- Add basic protections from the first version: stops, position checks, logging.
- Backtest, watch, forward test - and change one thing at a time.
Next: the cTrader Algo guide and what a trading bot is.
FAQ
Do I need to be a programmer to build a cBot?
Basic programming helps a lot, because cBots are written in C# (or Python in newer cTrader versions). A first bot with a moving-average rule needs only a few dozen lines; most of the work is writing clear rules and testing them.
Should my first bot trade on every tick?
Usually not. Acting once per closed bar is simpler, faster to backtest and closer to how most strategies are designed. Tick logic is for specific needs such as trailing stops.
When can my first bot trade real money?
After a realistic backtest, an out-of-sample check and a forward test on a demo account with behaviour close to the backtest. Then start with very small size.
Sources and further reading
- cTrader Help: Create a cBot - help.ctrader.com
- cTrader Help: cBot lifecycle - help.ctrader.com
- cTrader Help: Backtest a cBot - help.ctrader.com