Crypto algorithmic trading means trading by rules written so precisely that a program can follow them without you. A computer never gets tired or panics, but it won't think for you either: it will repeat a good idea and a mistake baked into that idea with the same diligence. In this lesson, we'll look at the building blocks of a trading algorithm, how to test a strategy on historical data without fooling yourself, and what to do before you trust a program with real money.
- An algorithm is a strategy translated into unambiguous rules: when to enter, how much to buy, when to exit.
- A good backtest accounts for fees and slippage and is checked on data that wasn't used during tuning.
- Between the backtest and real money you need a paper trading stage and a launch with minimal size.
- Code bugs, connection failures and leaked API keys are just as dangerous as market risks.
What a trading algorithm is
A rule like "I buy when the price seems cheap" won't work for an algorithm: a computer has no idea what "seems" means. You need rules with zero ambiguity, for example: if the daily candle closes above the 50-day moving average and RSI is below a set level, buy an amount sized so that the risk to your stop doesn't exceed your chosen share of the account.
That precision is both the strength and the weakness of the approach. On the one hand, emotions are out of the picture, and you can test an idea against years of history in minutes. On the other, markets change, and an algorithm stubbornly does exactly what it was told, even when the conditions it was written for are long gone.
Ready-made tools you set up in a few clicks, such as grid bots and copy trading services, were covered in the lesson on trading bots and automation. Here we'll look at how an algorithm works under the hood and how it's built and tested.
Testing a strategy on historical data: the program "replays" the past candle by candle and records which trades would have been opened and how they would have turned out.
The main types of strategies
Trend-following strategies try to get into a sustained move and stay in it until it breaks: they lose a little at a time in sideways markets and make their money on rare strong trends. Mean reversion strategies, on the other hand, bet that after a sharp deviation the price will return to its usual range, so they do well in a flat market but suffer in a strong trend.
There are other classes too. Arbitrage algorithms look for price differences in the same asset across platforms; there's more on that in the lesson on crypto arbitrage. Market making profits from the spread between buying and selling and requires very fast infrastructure. Execution algorithms aren't chasing profit at all: their job is to buy a large amount in pieces without moving the price.
For a retail trader, the most realistic place to start is trend and range strategies on hourly and daily charts. In high-speed strategies, you have almost no chance against professionals whose servers sit right next to the exchange.
Most signals in these strategies are built on indicators like moving averages, RSI and MACD, as well as on price levels and volume. If you can't yet read these tools confidently by hand, it's too early to automate them: first you need to understand why an indicator gives a signal and under what conditions it lies.
What an algorithm is made of
Even a simple algorithm is easier to work with when you split it into independent blocks. That makes bugs easier to find, and you can improve each part on its own without breaking the rest.
To connect to exchanges, developers often use the open-source CCXT library, which gives you a single interface to the APIs of dozens of platforms. For real-time prices, a WebSocket feed beats polling the exchange every second: APIs have rate limits, and if you go over them, the exchange temporarily blocks your access.
Give some thought to where the program will run. A home computer can go to sleep, restart for an update or drop its connection. That's why algorithms usually run on a rented server with an always-on connection, and the program should be able to rebuild the state of its positions from exchange data, not just from its own memory.
- Data: candles, order book, trades, funding. It needs to be checked for gaps and anomalies.
- Signal: entry and exit rules, built for example on indicators or price levels.
- Risk module: position size, stop-loss, caps on total risk and on daily loss.
- Execution: sending orders, handling partial fills, rejections and API errors.
- Monitoring: an activity log, alerts and a kill switch that closes everything with one command.
Your algorithm needs access to trading, but never to withdrawals. Lock the API key to your server's IP address and keep the secret outside your code. More on this in the lesson on crypto exchange security.
Backtesting traps
For a beginner algo trader, nothing is more dangerous than a beautiful equity curve on historical data. It's almost always at least partly the product of testing mistakes. Here are the most common ones.
The sneakiest is overfitting. Try hundreds of parameter combinations, and one of them will post a great result on history purely by chance. The warning sign: tweak a parameter slightly, say the moving average period from 20 to 22, and the results fall apart. A robust strategy performs acceptably across a wide range of settings.
Data quality can let you down too. Historical candles contain gaps, duplicates and abnormal price spikes at levels where nothing could actually have been filled in the real market. A single candle like that can paint a profitable trade in your backtest that could never have happened, so check your data before testing as carefully as you check your code.
| Trap | How it shows up | How to protect yourself |
|---|---|---|
| Look-ahead bias | The signal uses data that didn't exist yet at that moment, such as the close of a candle that hasn't closed | Make decisions only on closed candles and check for data shifts |
| Survivorship bias | The test only includes coins that trade today, and the ones that disappeared drop out | Use historical asset lists that include delistings |
| Overfitting | Parameters are perfectly fitted to the past and stop working going forward | Use an out-of-sample period and test on other periods and coins |
| Zero costs | The test ignores fees, spread, slippage and funding | Build in costs with a margin of safety, especially for frequent trading |
From idea to launch: step by step
Take your time on the road from idea to live trading. Each stage weeds out some strategies, and that's how it should be: dropping an idea at the backtest stage is cheaper than dropping it after a month of losses.
Compare your live results with the backtest for the same period. A noticeable gap means the model is missing something: delays, slippage or how orders actually behave in a real order book.
- State the idea in one sentence and explain why it should work, that is, whose mistake or need it takes advantage of.
- Turn the idea into unambiguous rules for entry, exit and position size.
- Run a backtest with realistic costs on one part of the history.
- Test the strategy on an out-of-sample period that wasn't used for tuning.
- Run paper trading without real money for a few weeks.
- Move to a real account with minimal size and a strict daily loss limit.
- Only increase size if live results look like the test results.
A strategy makes dozens of trades a day and, on average, earns slightly more on each one than the exchange fee. In a backtest without fees it looks profitable, but once you add fees and slippage it turns into a steady loss.
Realistic expectations
An algorithm doesn't turn a weak idea into a strong one; it just executes it faster and without emotion. The market is competitive: simple patterns that are easy to find are usually already being exploited by others, and over time they stop working. That's why you need to recheck your strategies regularly and calmly switch them off when they behave very differently from what you expected.
Then there's technical risk. A bug in a single line of code can open a position ten times bigger than intended, and a dropped internet connection can leave it unattended. Place protective stop orders on the exchange side, not just in your program, and check how the program behaves after a restart. If you trade with leverage, keep in mind the rules from the lesson on margin trading.
It helps to write down in advance the conditions under which you'll shut a strategy off: for example, a drawdown deeper than anything in the backtest, or a losing streak longer than usual. A written rule protects you from the temptation to "give it a little longer" when the algorithm has clearly stopped working.
Take one of your trading ideas and write it down as rules for a program: the exact entry condition, the exit condition, where the stop goes and how the size is calculated, with no "seems" or "roughly". Then scroll the daily BTC/USDT chart back a few months, for example in the BuyCrypt app, and walk through the candles by hand, looking only at candles that have already closed and marking every trade your rules would have produced. If you had to decide "by feel" even once, your rules aren't ready to be automated yet.
Entry is free and you trade on a demo account, so you need no money of your own. Free tournaments pay out Tour Passes, and those get you into tournaments with USDT prizes. The best go further — to a $5,000 funded account.
FAQ
Do I need to know how to code for algorithmic trading?
For your own algorithms, yes; Python is the most common choice. Without coding you can use ready-made bots and strategy builders, but then you're limited to what they can do and need to check your settings especially carefully.
Why is a strategy profitable in a backtest but losing in real trading?
The most common reasons are overfitting to history, unaccounted fees and slippage, look-ahead bias in the code, and changes in the market. Out-of-sample testing and paper trading help catch most of these problems before real money is involved.
How much money do I need to launch a trading algorithm?
For the first launch on a real account, the minimum size the exchange allows is enough: at this stage the goal is to make sure the program works, not to make money. It only makes sense to increase the amount once live results match your test results.
Discussion
Comments from BuyCrypt Live. Sign in with Google or Apple — we create your BuyCrypt account on the way, the same one as in the app.