VALYNOXSTRATEGY RESEARCH TECHNOLOGY
AlphaPrimeAlphaPrime
ACADEMY / Strategy generation

Day trading algorithm: how to build and test one

A day trading algorithm is four rule blocks: an entry, an exit, a time-of-day filter and a session-close exit that closes every position before the session ends, for example 5 minutes before. There is no best one. Test yours with next-bar fills, full costs and data it has never seen.

By · Published · updated · Editorial policy

Contents
  1. What is a day trading algorithm?
  2. What are the building blocks of a day trading algorithm?
  3. Which bar size should you test on, and when does the order fill?
  4. Why do trading costs hit day trading algorithms so hard?
  5. Is there a best algorithm for day trading?
  6. How do you validate a day trading algorithm before going live?
  7. Frequently asked questions
  8. References

What is a day trading algorithm?

A day trading algorithm is a set of rules that decides when to enter, when to exit, at which times of day to trade, and forces the position flat before the session closes. Traders build one rule block at a time. They backtest it with next-bar fills and full costs, then check it on unseen data. No best algorithm exists.

Day trading is opening and closing a position inside the same trading session, so no position is held overnight. Flat means holding no position. An algorithm for day trading writes the rules so precisely that a computer can follow them without asking you anything. Think of a recipe: two cooks who follow it make the same dish. A rule that needs your judgment, such as "enter when the market looks strong", is not yet part of an algorithm. It cannot be backtested. A day trading algorithm is a narrow use of algo trading: written rules as usual, with every position closed before the session ends.

A backtest is a run of the rules over historical price data that lists every trade they would have made. You can only trust the backtest if its rules are exactly the rules you would trade.

What are the building blocks of a day trading algorithm?

Most intraday strategies use four blocks: an entry, an exit, a time-of-day filter and a session-close exit. Each block answers one question, and each block changes the test result.

BlockQuestion it answersExample rule (illustrative)
EntryWhen do I open a position?Buy when price breaks above the high of the first 30 minutes
ExitWhen do I close a winning or losing trade?Stop loss at a fixed distance; profit target at twice that distance
Time-of-day filterAt which times may the algorithm open trades?No new entries in the first 15 minutes or the last 30 minutes
Session-close exitWhat happens to an open position at the end of the day?Close everything 5 minutes before the session ends

A time-of-day filter is a rule that allows or blocks new entries depending on the clock. Intraday markets do not behave the same all day. The minutes after the open often bring the widest swings and the worst fills. Midday is often quieter. With a filter, you can test whether your entry rule works better in some time windows than in others.

A session-close exit is a rule that closes any open position at a fixed time before the session ends. This rule is what makes the algorithm a day trading algorithm. Set it a few minutes before the real close, so that your exit order has time to fill.

The close time depends on the market. For example, TAIEX futures (台指期) have a regular day session from 08:45 to 13:45 Taiwan time, and the night session is a separate session. Exchanges can change session hours, so check the exchange's contract page before you set the times. The free futures market hours tool shows each session in your own time zone. Also decide which sessions your bars contain. Day-only bars and full-session bars give different results for the same rules.

Timeline of a TAIEX futures day session from 08:45 to 13:45 with example filter times: no new entries from 08:45 to 09:00 and from 13:15, entries allowed from 09:00 to 13:15, and all positions closed at 13:40, before the 13:45 session close Entries only in the middle TAIEX day session, example times 08:45 09:00 13:15 13:45 No new entries Entries allowed Close all at 13:40 Near the close, only manage trades. So: be flat before the session ends.
A day session with an example time-of-day filter and a session-close exit. The filter times are illustrative choices, not recommendations.

In AlphaPrime (Valynox), strategy generation works with these same pieces: rules and indicators, parameter ranges, entry and exit conditions, and search constraints. You can therefore make a time filter and a session-close exit part of every candidate from the first run.

Which bar size should you test on, and when does the order fill?

Test on the bar size the algorithm will actually trade, and fill every signal on the next bar, not on the bar that created it.

Data resolution is the length of each price bar in your data, for example 1-minute, 5-minute or 15-minute bars. Shorter bars give more signals and more trades. They also contain more noise, and each trade usually has less room to move, so costs take a bigger share of it. Data resolution is a setting in AlphaPrime's backtesting, so the 1-minute, 5-minute and 15-minute versions of one rule face the same fees and slippage and their cost shares can be compared.

Next-bar execution is a fill rule where a signal calculated at the close of one bar is filled at the open of the next bar. This matches what you can do in real time. You only know the signal after the bar has closed, so the earliest price you can get is the next one. A backtest that fills at the close of the same bar uses a price you could not have traded. On a daily strategy the error is small. On a 5-minute strategy with a small average trade, the error can be larger than the whole edge.

Three 5-minute candles at 10:00, 10:05 and 10:10: the signal is known at the close of the 10:05 bar, a fill at that close is crossed out, and an arrow points to the open of the 10:10 bar as the tradable fill Fill on the next bar The signal is known only at the close Signal at close Not here Fill here 10:00 10:05 10:10 That close price is already gone. So: fill at the next bar's open.
Next-bar execution: a signal known at the close of one bar can only be filled at the open of the next bar.

There is a second problem inside each bar. A bar shows only its open, high, low and close. Suppose one 5-minute bar touches both your stop and your target. The data cannot tell you which one came first. There are two fixes. You can assume the stop was hit first, which is the conservative choice. Or you can test the exits on a shorter bar size, where both levels are rarely touched in the same bar.

Why do trading costs hit day trading algorithms so hard?

Day trading algorithms trade often and make a small amount per trade, so a fixed cost per trade takes a large share of what each trade makes.

The average trade is the net result of all trades divided by the number of trades. It is the same number as expectancy per trade. The free win rate and expectancy calculator works it out from the win rate and the average win and loss. For a day trading algorithm, compare the average trade with what one trade costs:

Net average trade = average trade before costs − cost of one round trip

The gross average trade is the average result per trade before any costs. The round-trip cost is everything one complete trade costs you. It includes commission and exchange fees on the entry and on the exit, plus slippage on both sides. Slippage is the shortfall between the assumed fill price and the price the market actually gives you.

Hypothetical example

Two intraday strategies trade a contract where one tick is worth $5. Each round trip costs $4 in commission and fees, plus one tick of slippage on the entry and one on the exit ($10). The total is $14 per round trip.

Strategy A (fast)Strategy B (slower)
Trades per year500100
Gross average trade$18$70
Round-trip cost$14$14
Net average trade$4$56
Net result per year$2,000$5,600
Cost as a share of gross78%20%

Before costs, Strategy A looked better: $9,000 gross per year against $7,000. After costs, it keeps less than half of B's net result. If slippage doubles to two ticks per side, the cost becomes $24. Strategy A then loses $6 per trade, while Strategy B still makes $46.

Hypothetical example: strategy A makes 18 dollars per trade before costs, of which the 14 dollar round-trip cost takes most and 4 dollars is left; strategy B makes 70 dollars, pays the same 14 dollars and keeps 56 dollars Same cost, bigger bite Average trade, hypothetical example A: fast $14 $4 net B: slower $14 $56 net Round-trip cost Net result Costs take 78% of A's average trade. So: put costs in the first backtest.
The same $14 round-trip cost takes most of the fast strategy's average trade and a small part of the slower one's (hypothetical example).

Costs that hardly matter for a slow strategy decide the result of a fast one. That is why costs belong in the first backtest, not the last. If you rank strategies without costs, the ranking favours many small trades, and adding costs later removes most of your finalists. Commission, slippage and spread are covered in more detail in trading costs in backtests.

Is there a best algorithm for day trading?

No. There is no best day trading algorithm, because an intraday edge depends on one market, one session and one cost level, and all three change.

  • Markets change. Volatility, volume and behaviour around the open shift over the years. A breakout rule that works when daily ranges are wide can lose when ranges shrink.
  • Costs change. Commissions, exchange fees and the slippage you get all move over time. For a small average trade, a modest rise in costs can turn the net result negative, as the example above shows.
  • Rules are specific. An entry tuned on one contract's day session often behaves differently on another contract or in the night session.
  • The "best" result is usually the top of a long list. Many variants were tested on the same history, and the winner was picked afterwards.

Put enough people in a room flipping coins, and one of them will throw ten heads in a row. That person is not a skilled flipper; the room was big. Research on data snooping shows the same effect with trading rules. When many rules are tested on the same data, the best of them can look strong by chance alone. A fair test must therefore account for how many rules were tried (Sullivan, Timmermann and White, 1999; White, 2000). Bailey and coauthors (2014) show that the more variants you try, the more the best backtest tends to overstate what the strategy will do on new data.

How do you validate a day trading algorithm before going live?

Validate it on data the rules never saw, with costs included, and only then trade it small or in simulation.

Out-of-sample data is price history that was not used to build or tune the rules. It plays the role of the future inside your backtest. Use it in this order:

  1. Set aside a holdout. Before any search, keep the most recent part of the history (for example the last 20–30%) untouched.
  2. Build and tune on the rest, with next-bar execution, the session-close exit and full costs switched on.
  3. Run a walk-forward test. Tune on one window, test on the next, move forward and repeat. Walk-forward analysis explains how to choose the windows.
  4. Stress the costs. Run it again with slippage doubled.
  5. Break the result down by year, by long and short trades, and by time of day.
  6. Test the holdout once. If you change the rules after seeing it, the holdout is no longer unseen.
  7. Trade small or in simulation, and compare your real fills with the prices the backtest assumed.
CheckGood signBad sign
Out-of-sample vs in-sampleUnseen data keeps a clear part of the in-sample average tradeThe average trade collapses or turns negative on unseen data
Double slippageThe net average trade stays positiveThe net result turns negative
Neighbouring parametersNearby settings give similar resultsOnly one exact setting works
Results by yearMost years contributeOne or two years produce the whole result
Long vs shortBoth sides behave in a way you can explainOne side carries everything for no reason you can name

AlphaPrime's strategy validation offers Basic, Standard and Extensive validation levels and walk-forward analysis. You can run these checks on every candidate with the same standard, instead of judging each one by eye.

What this means if you use AlphaPrime: build intraday strategies from explicit entry, exit, time-of-day and session-close rules, and set realistic fees and slippage before the search starts. Then judge each candidate by how it holds up on data it has never seen, not by its best backtest.

Frequently asked questions

What is the best algorithm for day trading?

There is no best day trading algorithm. An intraday edge depends on the market, the session and the cost level, and all of them change over time. A strategy called "best" is often just the top result among many variants tested on the same history. Judge any algorithm by its results on unseen data after full costs.

Which bar size should a day trading algorithm use?

Use the bar size the strategy will actually trade on, and test it with next-bar fills. Shorter bars give more trades but a smaller move per trade, so costs take a larger share. If your exits are tight, check them on a shorter bar size, so that one bar rarely touches both the stop and the target.

Does a day trading algorithm have to close before the session ends?

Yes. Closing every position before the session ends is what makes it a day trading algorithm. Most traders set the session-close exit a few minutes before the official close, so the exit order has time to fill. The close time depends on the exchange and the session, so check the exchange's contract page.

How many trades does a backtest need before you trust a day trading algorithm?

There is no fixed number. You need enough trades that the result does not depend on a few unusual days. One quick check: remove the best few days and see whether the net result is still positive. Also check that most years contribute, not only one or two.

References

  • Bailey, D. H., Borwein, J., López de Prado, M., & Zhu, Q. J. (2014). Pseudo-Mathematics and Financial Charlatanism: The Effects of Backtest Overfitting on Out-of-Sample Performance. Notices of the American Mathematical Society, 61(5), 458–471.
  • Sullivan, R., Timmermann, A., & White, H. (1999). Data-Snooping, Technical Trading Rule Performance, and the Bootstrap. The Journal of Finance, 54(5), 1647–1691.
  • White, H. (2000). A Reality Check for Data Snooping. Econometrica, 68(5), 1097–1126.