Algorithmic Trading for Beginners: How to Build and Backtest a Strategy With Historical Data

Algorithmic trading starts with a rule you can test repeatedly. Sort prices by time, generate signals from completed candlesticks, and measure returns from the next price at which the strategy could trade. Deduct trading costs and reserve a period you haven’t used to tune the rules for an independent test. The moving-average example below uses three consecutive opening prices so you can check the calculation by hand before writing any code.
Prepare Data That Reflects What You Knew at the Time
Choose one spot trading pair and a fixed timeframe. Retrieve timestamps, open, high, low, and close prices, and volume from the exchange’s market data API. Sort the records from oldest to newest, remove duplicate timestamps, and check whether each timestamp marks the open or close. Investigate missing hours: there may have been no trades, a trading halt, or a failed download. Filling gaps with the previous price can hide the cause. Exclude unfinished candlesticks and save the raw data and download time so you can rerun the test. Handle product, order, and fee data separately.
Turn a Moving-Average Signal Into a Testable Rule
Calculate the average closing price of 20 consecutive completed candlesticks. If the close is above the average, simulate holding the asset from the next open. If it closes at or below the average, exit at the next open. Suppose day one opens at 98 and produces a buy signal at the close. Day two opens at 100 and day three at 103. The strategy enters at 100, earning a gross return of 3% over that holding period. The move from 98 to 100 occurred before entry. If you sell one coin at 103 and pay 0.1% on each trade, the net profit is 3 − 0.100 − 0.103 = 2.797 units of the quote currency.
Decide Whether the Strategy Is Ready for Further Testing
The example assumes a single fill price at the open and omits spreads, partial fills, and automatic position closure at the end of the test. To compare withdrawable balances, deduct the cost of closing any remaining position. Split the data chronologically. Choose parameters on the earlier period, freeze them, and test on the later period. If you retune after seeing those results, you’ll need fresh data for independent validation. Compare account equity with buy-and-hold and cash benchmarks. Check maximum drawdown, turnover, and the effect of doubling costs before moving to paper trading with a bot.
Check three consecutive candlesticks by hand. The first close generates the signal, the second open establishes the position, and the third open reveals the holding-period return. Compare your calculation with the program’s positions and fees row by row. Check for charges when no trade occurred. Applying a closing signal one row too early can credit the strategy with a price move it could never have captured.
When you begin paper trading, keep both the expected orders and the execution reports. If orders repeatedly fail to fill where the backtest predicts a fill, revise the execution assumptions before judging the strategy’s returns.
Frequently Asked Questions
Why shouldn’t I shuffle historical prices?
Shuffling breaks the sequence of events and can expose the training process to information from the future.Does a candlestick touching my limit price prove my order would have filled?
No. It shows that a trade occurred at that price. It doesn’t reveal your queue position or how much volume was available.How many times are fees charged in the example?
Once on the purchase and once on the sale. The buy fee is 0.1 and the sell fee is 0.103, both in the quote currency.
Mastering the basics is the first step. Follow MGBX Academy to access more cryptocurrency educational resources and market opportunities.
The process for trading on MGBX:
Register a trading account and complete the necessary identity verification;
Deposit funds and select a trade type;
On the cryptocurrency trading page, enter the amount or quantity of the cryptocurrency you wish to purchase;
Confirm the order to complete the trade, and check that your assets have increased accordingly.