Ready to go further
Next: build the first working version
The Python trading bot page walks through the parts of a bot and a short backtest in plain Python that you can run.
What is included
- What it is, in plain words
- The formula or mechanism, with exact arithmetic
- How it hits grid, martingale and DCA bots
- A check you can run before you switch a bot on
Who it is for
- People who run or plan to run a trading bot
- Anyone reading a backtest or a bot sales page
- Readers who want the numbers, not the pitch
- Not advice, and not a signal service
Key facts
- A trading bot is a rule written precisely enough that a program can follow it. If you cannot write the rule down in full, you cannot build the bot.
- The seven steps are: write the rule, get clean data, code it, backtest it with costs, add risk rules, forward test, and paper trade. Real money comes after all seven, if at all.
- Writing the rule out is the step most people skip. In the worked example below, one rule needs an entry, an exit, a size (50 units), a stop ($100 at risk, 1 percent of a made-up $10,000 account) and a daily limit ($200).
- A backtest that looks good is where the work starts, not where it ends. See overfitting and look-ahead bias before trusting one.
- A bot needs a stop that does not depend on the bot working, and an account key that cannot withdraw.
The seven steps
1. Write the rule in full
Say exactly when to enter, when to exit, how big to trade, and when to do nothing. Each line must be something a program can check with numbers. "Buy when it looks strong" is not a rule. "Buy at the next open when the 10-bar average closes above the 30-bar average" is.
2. Get clean data
Get prices for the market you will trade, with the time zone and the bar size written down. Look for gaps, repeated bars, bad ticks and changes that are not real prices, such as a split. Bad data makes a bot do the wrong thing quickly. See survivorship bias for a data problem that hides losers.
3. Code the rule
Turn each line of the rule into code and keep it small and readable. Decide each time which bar a decision uses, and never use a bar that had not closed. A short working example in plain Python is on the Python trading bot page.
4. Backtest it, with costs
Run the rule on past data you did not tune it on, and count the spread, the fees and slippage. Look at the average result per trade, the worst fall and the number of trades, not only the total. See backtesting, overfitting and look-ahead bias.
5. Add the risk rules
Set the size of each trade from how much you are willing to lose if the stop is hit, a daily loss limit, and a maximum number of open positions. Add what happens on a lost connection, a rejected order and a restart. See position sizing and daily drawdown rules.
6. Forward test
Freeze the settings and run the bot on data that arrives after you froze them. Compare the results with the backtest. A big gap means the backtest was too kind. See forward testing.
7. Paper trade
Run the whole bot, orders and all, against a practice account or simulated fills for weeks. This tests the plumbing: order handling, timing, logging and the limits. See paper trading. A practice account fills more kindly than a real market, so it does not prove real results.
A worked example: the rules written out
This is a made-up example to show how complete a rule must be. It is not a strategy to trade. The account is a made-up $10,000 and the price is a made-up 40.00.
| Part of the rule | Written out | Number |
|---|---|---|
| Entry | At the open of the next bar after the 10-bar average closes above the 30-bar average, and only if flat | Entry 40.00 |
| Stop | A stop order placed at the same time, 2.00 below the entry | Stop 38.00 |
| Exit | At the open of the next bar after the 10-bar average closes below the 30-bar average, or at the stop | - |
| Risk per trade | 1 percent of the account | $100 |
| Size | Risk divided by stop distance: $100 / 2.00 | 50 units |
| Position value | 50 units x 40.00, which is 20 percent of the account | $2,000 |
| Loss if the stop is hit | 50 units x 2.00 (before any gap or slippage) | $100 |
| Daily loss limit | 2 percent of the account; stop trading for the day when reached | $200 |
| Stops to reach the limit | $200 / $100 | 2 |
Calculated by us on 29 Oct 2026, exact arithmetic on made-up numbers. Note what the table forces. The size comes from the stop, not from a feeling, so a wider stop means fewer units. The daily limit is reached after 2 stopped trades, which is a figure you can check against how often the rule loses. And a stop does not fix the price: a gap or a fast market can fill it worse, so the real loss can be bigger than $100. Some markets also close, which the rule must say what to do about. Rules for stops vary by broker and exchange, so check yours. See stop loss orders.
How bots break at each step
- Rule: vague wording gets filled in by the code, and the code fills it in differently from what you meant.
- Data: gaps and bad ticks create trades that never existed.
- Code: using the current bar to decide a trade at that bar's price. Test results then look far better than anything achievable.
- Backtest: tuning settings until the curve looks good fits noise. Leaving out costs removes the difference between a small edge and none.
- Risk rules: no limit on size or daily loss lets one bad day end the account. A martingale-style size rule ends this way in the long run.
- Forward test and paper trade: skipped because the backtest looked good. This is the step that finds the plumbing faults.
What this page does not tell you
It does not give a strategy, and the moving average rule above is an example of how to write a rule, not a suggestion. The numbers are made up. Nothing here says a bot built this way will make money, and many do not. Keep keys small and unable to withdraw, and keep the code out of any public place. This is education, not advice.
Keep Going
Related Tools and Guides
Keep exploring
FAQ
Frequently Asked Questions
How do I build a trading bot?
Write the rule in full, get clean data, code it, backtest with costs, add risk rules, forward test, then paper trade. Each step can send you back to an earlier one.
Do I need to know how to code to build a trading bot?
You need to be able to read and check the code, because you are responsible for what it does. A rule written in full detail makes the coding step much smaller.
How long does it take to build a trading bot?
The code can take days. Testing takes much longer, because forward testing and paper trading need weeks of new data and cannot be sped up.
What is the most common mistake when building a bot?
Trusting one good backtest. Costs left out, settings fitted to the past and decisions that use the current bar all make a backtest look better than reality.
Should I trade real money as soon as the backtest looks good?
No. The backtest is the start of the checks. Forward test and paper trade first, and if you go further, start at a very small size and compare with the tests.
Are the numbers on this page real?
Example numbers are made up to show how the maths works, and the page says so. A figure only counts as exact when the page says it was calculated on a stated date. Nothing you enter in a calculator leaves your browser.
Can I use this to decide whether to run a bot?
It can show whether a bot's numbers hold together and how much an account can lose. It cannot say what the market will do, and it is not advice. Use it to ask better questions, then read the terms of your own broker or exchange.
Is this financial advice?
No. It is general education. The example numbers are made up and nothing here is a recommendation.
Know the Risk Before You Run It
Free calculators and guides, added every week. Leave your details and we will email you when new ones go up.
Trading carries a risk of loss