Testing investment rules honestly, without real money
A small tool to test simple investment rules on past market data — built so that it can't cheat, and can't place a real order.
The need
Tests of investment strategies on past data often look brilliant because, without saying so, they use information nobody had at the time.
The idea
Build the lab around the pitfall rather than around the strategies.
What I built
- A rule computed on one day's closing price only applies to the next day's return — never the same day.
- Every change of position costs 0.05% in fees.
- Every rule is compared with “buy at the start and do nothing”: a strategy that makes 8% while the market makes 20% has lost value.
- Two safety locks: the trading address is hard-wired to the sandbox, and the code can only read — it has no way to place an order.
The result
- 3Python modules
- 453lines of code
- 11indicators computed per test
- 0orders it can place — by design
No automated tests yet, and no run with real paper-trading keys that I can show. It is a tool to understand a mechanism, not to decide an investment.
In pictures
What I learned
Try enough combinations on the past and one will always look like it worked. Knowing the limits of a test is part of the result.
Technical details Stack, architecture and decisions — for technical readers
Stack
- Python
- Streamlit
- pandas
- NumPy
- requests
- Alpaca API (paper trading)
How it works
- Signals computed at the close of day D apply to day D+1 returns; 0.05% cost per position change; systematic buy-and-hold benchmark.
- Two testable rules — moving-average crossover and breakout above a previous high — on one to five years of daily US stock prices.
- Trading API base URL hard-coded to Alpaca's paper-api sandbox (not configurable); read-only client with no order function; keys read from environment variables only.
- When data is missing (no keys, 401/403, rate limit), the Streamlit interface shows the reason instead of a default number.
- Known bias: entries are priced at the close that produced the signal, not at the next open, so the overnight gap is included.
Still missing
- No automated tests yet
- Not an investment tool