How do I build my first trading algorithm?
A first trading algorithm is a short list of rules a computer can follow without you. The rules say when to buy, when to sell, and how much to commit. The example used on this page is the simplest one with a name: buy a fund when its 14-day RSI falls below 30, sell when RSI climbs back above 70. It is the RSI(14) mean reversion template, and DeployQuant ran it on 59 ETFs from 2021-01-04 to 2026-10-02 with $10,000 of starting capital.
The result is useful because it is ordinary. The median ETF made a 2.98% CAGR, against a median max drawdown of 24.63%. The rule beat buy-and-hold on 25 of the 59 funds. That is a first algorithm built correctly that is still not clearly better than doing nothing.
The steps run in order: pick one rule, write it down, backtest it, read the trades, compare against buy-and-hold, check how fragile the settings are, paper trade, and only then put in small money. Each step uses a number from the RSI test or from a plain buy-and-hold run on a well-known fund.
RSI(14) Mean Reversion across 59 ETFs
Median CAGR 3.0%, median max drawdown −24.6%, ahead of buy-and-hold on 25 of 59 (2021-01-04 to 2026-10-02).
| ETF | CAGR | Buy & hold | Max DD | Round trips |
|---|---|---|---|---|
| TECL | 23.9% | 38.2% | −64.1% | 18 |
| FAS | 22.3% | 18.3% | −50.1% | 20 |
| SOXL | 14.4% | 33.3% | −81.0% | 14 |
| SOXX | 12.8% | 31.1% | −35.0% | 14 |
| ROM | 12.8% | 30.2% | −52.0% | 16 |
| SQQQ | −23.6% | −42.3% | −85.2% | 16 |
| TMF | −25.0% | −31.2% | −81.1% | 13 |
| TECS | −29.1% | −46.7% | −89.5% | 18 |
| UVXY | −48.5% | −48.7% | −98.0% | 12 |
| SOXS | −53.7% | −48.3% | −99.5% | 15 |
The five strongest and five weakest results. All 59.
What a first RSI rule did across 59 ETFs
The median CAGR was 2.98%. By category the spread is wide. Leveraged ETFs had a median CAGR of 12.08% against 23.55% for buy-and-hold, and beat buy-and-hold on 3 of 10. Broad index ETFs had a median of 6.46% against 14.42%, and beat buy-and-hold on 1 of 12. Sector ETFs had 8.74% against 12.58%, with 2 of 6 ahead. Bond ETFs, which fell in the test window, had a median of -0.23% against -0.78%, and the rule was ahead on 5 of 7.
Read the pattern. Where the fund rose strongly and steadily, sitting out most of the time cost return. Where the fund fell, sitting out most of the time helped. The inverse ETFs show the same thing: a median of -11.04% against -21.57% for buy-and-hold, with the rule ahead on 9 of 11. Both numbers are negative, so ahead here means lost less.
The shallower-drawdown count is where the rule looks best. It had a shallower max drawdown than buy-and-hold on 58 of the 59 funds. That follows from being invested only 37.2% of the time. Lower exposure means smaller falls and smaller gains. A beginner who checks only drawdown would call the rule a success, and one who checks only CAGR would call it a failure. Both numbers belong in the same table.
The strongest and weakest funds are in the table above. FAS made 22.26% against 18.34% for buy-and-hold, with 20 round trips and an 85% win rate. SOXX made 12.78% while buy-and-hold made 31.11%, with a win rate of 86%. A high win rate and a loss against buy-and-hold can sit together: the rule won most of its trades and still missed the larger move.
Step 1: choose one rule you can say out loud
A rule is ready to build when you can say it in one breath and a stranger could follow it. "Buy when the 14-day RSI is below 30, sell when it is above 70" passes. "Buy when things look cheap" does not, because a computer cannot tell what cheap means.
The RSI template has two rules. When the market opens, if the strategy is not invested and RSI(14) is below 30, it buys with 98% of the sleeve. When the market opens, if it is invested and RSI(14) is above 70, it sells the whole position. There is no profit target and no stop. The only way out is RSI recovering.
Two features make this a good first rule. It has two numbers, 30 and 70, so there is little to adjust. It also decides once a day, so every trade can be read afterwards as a plain sentence: on this date RSI was this value, so the strategy bought at the next open.
A rule also needs an asset. The asset matters as much as the rule. The same two sentences produced a 23.89% CAGR on TECL and a loss of 53.71% a year on SOXS. Picking the fund is half of the design.
Step 2: express it as blocks, plain English or code
DeployQuant accepts a strategy three ways. You can describe it in plain English and the AI builder turns the description into rules you can read and edit. You can assemble visual blocks that read as sentences, such as WHEN the market opens, IF RSI(14) < 30, THEN buy. Or you can write Python in the industry-standard algorithm format. Every strategy is one set of rules with two views, Blocks and Code, so you can start in one and switch.
For a first rule, English or blocks is usually quicker. The builder costs one token per successful build, and a build that fails costs nothing. New accounts start with 5 free tokens. Unlimited strategies and backtests are included on the free plan, so building and testing a rule does not need a paid plan. The page on automating trading without coding covers what the no-code route gives up and what it keeps.
Whichever way you write it, read the result back as a sentence. If the blocks do not say what you meant, fix them before running anything. A rule that is slightly different from the one in your head produces a backtest of a strategy you did not intend.
Step 3: backtest over enough history and read every trade
Backtesting replays your rules against past prices. The window here runs 5.74 years of minute-resolution data, from 2021-01-04 to 2026-10-02. That is 1,444 sessions. It contains a bull year, a bear year and a sharp spring 2025 drop, which is the minimum variety a first test should have.
The headline run has no fees and no slippage, and orders fill on minute bars after the signal. A cost run is the way to check that assumption, and the strategy pages show 5 and 10 basis point versions. Read those before trusting any headline number.
The headline return hides most of what you need. Open the trade list. On the RSI test the median fund made 15 round trips in about six years and was invested 37.2% of the time. Fifteen trades is a small sample. A rule with a good CAGR built on a handful of trades has not proved much, and the trade list shows you how many it was.
Look for the clustering as well. The rule buys after falls. On a fund in a long decline, it buys, the fund keeps falling, RSI stays low, and the position sits in a loss for weeks. TMF, a leveraged bond fund, shows it: a 13 round trip record, a win rate of 46%, and a max drawdown of 81.12%. You only see that by reading trades and drawdowns, not the CAGR.
Step 4: compare against buy and hold on the same asset
Every strategy result needs a baseline, and the cheapest one is buying the fund on day one and doing nothing. The baseline in these tests puts 98% of the account in at the first open, as the templates do.
Here are some baselines from the same window. SPY made a 14.56% CAGR with a max drawdown of 23.99%, and ended at $21,820 from $10,000. QQQ made 16.70% with a drawdown of 34.23%. IWM made 7.50% with a drawdown of 31.34%. TLT lost 8.23% a year and fell 42.77% from its January 2021 peak without recovering by the end of the data. TQQQ made 25.35% with a drawdown of 80.77%. UVXY lost 48.74% a year and ended near $216.
Those numbers set what a rule has to beat. On SPY, an RSI rule that made a 6% CAGR with a smaller drawdown is a different thing from one that made 6% on a fund where buy-and-hold lost money. The first gave up return to cut risk. The second added value. The comparison tells you which.
The same logic applies to the strategy's own median. The median ETF in the RSI test returned 2.98% a year, and the median buy-and-hold figure in the broad index group was 14.42%. A beginner who sees "3% a year" and compares it to a savings rate has used the wrong baseline. The right one is the fund, held, over the same dates.
Why the rule behaves differently on each fund
The reason RSI(14) below 30 works on some funds and not others is in the fund's own behaviour. On SPY, the 14-day RSI fell below 30 on 18 sessions in the window. The median 5-day forward return after those sessions was 3.74%, against a baseline 5-day median of 0.42% for all sessions. For 20 days the median was 2.86% against 1.73%. On QQQ the 5-day figure was 3.69% on 15 sessions.
That is the pattern the rule is built on: short-term washouts in an uptrending index tended to rebound. Eighteen sessions is a small count, though, and they cluster in a few episodes. It shows the idea has support on SPY and QQQ. It does not show a reliable edge.
TQQQ pushed the same pattern further. After 18 sessions with RSI(14) below 30, the median 5-day return was 10.30% against a baseline of 1.24%. Even so, the strategy on TQQQ made 12.08% while buy-and-hold made 25.35%, with a max drawdown of 66.58%. The rebounds were large, and the rule still spent time out of the market during the fund's biggest gains. TQQQ's 2023 return was 198.32% and its 2022 return was -79.07%, so sitting out any part of 2023 was expensive.
TLT is the opposite case. RSI(14) was below 30 on 61 sessions, and the median 5-day forward return was 0.02% and the 20-day figure was -1.63%. The fund had been in one long decline, and oversold readings kept happening because the price kept falling. UVXY is more extreme: 116 sessions below 30, with a median 20-day return of -5.82%. A mean reversion rule assumes the price comes back, and that assumption has to be true of the asset.
Step 5: check how fragile the settings are
Once a rule backtests, the temptation is to adjust 30 and 70 until the result improves. Resist it for a different reason than discipline: the test shows how much a small change moves the answer, and that is information about the rule.
The template was run with four alternatives. RSI below 25 with exit above 70 gave a median CAGR of 1.32%, a median drawdown of 21.51% and 11 round trips. RSI below 35 with exit above 70 gave 3.77%, 26.10% and 18 round trips. RSI below 30 with exit above 65 gave 2.59%, 20.37% and 17 round trips. RSI below 30 with exit above 75 gave 3.72%, 23.33% and 12 round trips. The base setting gave 2.98%, 24.63% and 15.
Two things show here. Buying earlier, at 35, raised CAGR and drawdown together, and buying later, at 25, lowered both and cut the trade count. The settings trade return against risk and against sample size, and none of them changed the character of the rule. All five medians sit within a few points of each other.
That is the result you want from a parameter check. A rule whose result flips from good to bad when 30 becomes 35 is overfit to the history it was tuned on. A rule whose medians move gently has a stable shape. The cost is that a stable rule might be stably mediocre, which is what this one is.
Pick settings before looking at results, write them down, and treat any later change as a new rule that needs its own test.
Step 6: split the result by year
A single multi-year number can hide one lucky year. The year-by-year medians for the RSI rule across the 59 funds are below, with buy-and-hold in brackets: 2021 at 2.5% (4.1%), 2022 at -4.0% (-12.7%), 2023 at 2.9% (8.9%), 2024 at 5.3% (9.7%), 2025 at 0.0% (11.1%), and 2026 at 2.1% (3.7%, a partial year).
The rule was ahead of the median fund in only one year, 2022, and by being invested less while prices fell. In 2025 the median result was flat while the median buy-and-hold fund returned 11.1%. The number of funds with a positive return that year was 29 of 59, against 48 in 2024.
Check this on your own rule. If one year produces most of the gain, the rule depends on that kind of year. For a mean reversion rule, the year that helps is one with sharp drops followed by rebounds. A year of steady drift upward gives it few entries, and the median fund in 2024 and 2025 had just that.
Compare it with the market years on the example funds. SPY returned 30.46% in 2021, -18.16% in 2022, 26.18% in 2023, 24.86% in 2024 and 17.72% in 2025. A rule that is invested 37.2% of the time cannot match those years, and was not expected to. Its job in the table is to show what the exposure traded away.
Step 7: paper trade before real money
A backtest uses data you already have. Paper trading runs the rules on the live feed with simulated money, so the signals, the prices and the orders all happen in real time. On DeployQuant it needs no broker. It checks the whole loop: the data arrives, the rule fires, the order is formed.
Paper results will differ from the backtest even with the same rules. Fills happen at live prices, the day you start is not in the backtest window, and a few weeks of data is far too short to judge performance. What paper trading tests is the machinery. Did the rule buy when you expected, did the sizes make sense, did it hold when it should. It does not tell you whether the strategy makes money.
For an RSI rule that made 15 round trips in 5.74 years on the median fund, a few weeks of paper trading may include no trade at all. That is a limit of the method. Pick a rule that trades often enough for the paper period to show something, or accept that the paper run only confirms the connection.
Alpaca offers a paper account as well. Webull and Schwab connections are live-money only, so on those brokers paper trading happens on DeployQuant's own paper mode instead.
Step 8: go live with a small allocation
The last step is small money. DeployQuant runs a deployed strategy in its own sleeve with its own cash, positions and profit and loss, so a first algorithm does not touch the rest of the account. Real money moves only after you type an explicit confirmation on a live deployment. Per-order and per-position dollar caps and price bands on limit orders are available, and caps never block an order that reduces exposure. Dry-run mode runs the full order flow without sending anything to the broker.
Supported brokers are Alpaca, Webull and Charles Schwab. Broker connections have trade and read permissions only, so they cannot withdraw or transfer money.
Size is the open question. The page on how much money you need to start covers that. The facts from this test are about drawdown: the median max drawdown of the RSI rule was 24.63%, and on leveraged funds it ran to 64.11% on TECL and 81.01% on SOXL. Size a first allocation so the drawdown the backtest showed would not force you to stop the strategy.
What this test does not show
The test is one window of 5.74 years, from 2021-01-04 to 2026-10-02, that includes a single major bear market. Starting capital was $10,000, with no margin, and the headline run has no fees or slippage. Rules decide once a day. Results are hypothetical.
The 59 funds are not independent. Many are leveraged or inverse versions of the same few indexes, so the sample is smaller than 59 suggests. Eighteen sessions of RSI below 30 on SPY is a handful of events. The medians across the table describe this window and nothing else.
Other templates are on the same data if you want a second rule to compare. RSI(2) snapback had a median CAGR of 4.74% with a median of 166 round trips, and beat buy-and-hold on 34 of 59. The weekly 7% target had a median of 6.61% with 106 round trips and also beat buy-and-hold on 34. The golden cross had 2.05% with a median of 3 round trips. More trades give a result you can trust more, which is a reason to check the round trip count before the CAGR.
Frequently asked questions
What is the simplest first trading algorithm to build?
One rule with two numbers, such as buy when the 14-day RSI is below 30 and sell when it is above 70. It decides once a day and every trade can be read as a sentence. DeployQuant ran it on 59 ETFs from 2021-01-04 to 2026-10-02, and the median result was a 2.98% CAGR.
Do I need to know how to code to build a trading algorithm?
No. DeployQuant takes a plain-English description or visual blocks, and each strategy also has a Python view you can switch to. The builder costs one token per successful build, and new accounts start with 5. The [no-code answer](/learn/answers/automate-trading-without-coding/) covers the trade-offs.
How long should I backtest a first algorithm?
Long enough to include more than one kind of market. The test here covers 5.74 years with a bull year, a bear year in 2022 and an April 2025 drop. A longer window is better where the data exists, and the trade count matters as much as the years. The median fund here made 15 round trips.
Why compare the algorithm with buy-and-hold?
Buy-and-hold on the same fund is the baseline the rule has to justify itself against. SPY buy-and-hold made a 14.56% CAGR in this window. The RSI rule beat buy-and-hold on 25 of 59 funds, and had a shallower drawdown on 58. Both comparisons are needed to see what the rule traded away.
What is overfitting, and how do I avoid it?
[Overfitting](/learn/glossary/overfitting/) is tuning a rule until it fits past noise instead of a real pattern. Choose settings before looking at results and test small changes. Here, moving the buy level from 30 to 25 or 35 moved the median CAGR to 1.32% or 3.77%, a gradual change that suggests the rule is not tuned to one setting.
How long should I paper trade before using real money?
The brief answer is a few weeks. What matters is whether the rule has had a chance to trade. A rule with 15 round trips over almost six years may not fire in that time, so paper trading mainly confirms the data feed, the signals and the orders. Read the [paper trading](/learn/glossary/paper-trading/) entry for what it can and cannot show.
Real results across 59 ETFs, 5.7 years of minute data.
Terms used here
Related questions
Backtests are hypothetical, computed by DeployQuant's engine on minute-resolution consolidated US market data (2021-01-04 to 2026-10-02, $10,000 starting capital, no margin, no fees or slippage in the headline run; buy-and-hold puts 98% of the account in at the first open, as the templates do) and do not guarantee future results. Nothing on this page is investment advice. Live trading involves risk of loss.