Trading techniques explained as code
Textbook techniques people have written about for decades, each as a short Python script you can read and run, with a plain account of how it fails. 11 techniques, 16 scripts, standard library only.
Risk warning: CFDs and spread bets are complex instruments and come with a high risk of losing money rapidly due to leverage. You can lose money on these products, and each provider publishes its own loss figure under FCA rules. You should consider whether you understand how they work and whether you can afford to take the high risk of losing your money. Automated and API trading can amplify losses through bugs, connectivity failures and runaway orders.
Teaching material only. This page explains how a technique works as code and how to test an idea honestly. It is not investment advice, not a recommendation and not an invitation to trade. Every price on it is invented, so no result here says anything about any real market, and nothing here suggests that a technique makes money.
Read this one first
Every test on these pages can mislead the person running it. One page shows how, mistake by mistake, on prices that have nothing in them to find. It will change how you read all the others.
Following a trend
Rules that wait for a move to begin and then go with it.
- Channel breakoutDraw a channel from the highest high and the lowest low of recent days. The rule acts when price closes outside it. Code you can run, and an honest account of how it fails.Code run
- Momentum by rate of changeRate of change is one line of arithmetic: how far has price moved over the last so many days, in percent. Code you can run, and a result that shows why one good looking test proves nothing.Code run
- Moving average crossoverTwo averages of recent prices, one short and one long. The rule acts when the short one crosses the long one. Here it is as code you can run, with an honest account of how it fails.Code run
Measuring risk
Not rules for what to trade, but arithmetic for how much and where to stop.
- Fixed fraction position sizingPut the same share of the account at risk on every trade. This page runs one invented sequence of coin flip trades at several fractions to show what the size alone does.Code run
- Volatility stop using average true rangeAverage true range measures how far price typically moves in a day. This page computes it from scratch and uses it to set a stop that trails behind price at a distance that adapts.Code run
Reverting to an average
Rules that treat a stretched price, or a stretched gap, as something that will come back.
- Bollinger bandsAn average with a band above and below it, set by how much price has been moving. Computed from scratch, then tested against the common belief about how often price leaves the bands.Code run
- Mean reversion to a moving averageMeasure how far price has stretched from its own average and act as if it will come back. Code you can run, and an honest account of the one way this fails badly.Code run
- Pairs spreadWatch the gap between two instruments that have moved together and act when it is unusually wide. The invented data is built to work at first and then break, so you can see both.Code run
- Relative strength index (RSI)RSI turns recent gains and losses into one number between 0 and 100. Here it is computed step by step with no library, then used in a rule so you can see how one is tested.Code run
Testing honestly
How a test misleads the person running it, shown on data with nothing in it to find.
- Day of the week ruleSort daily price changes by weekday and one day will always come out on top. This page finds such a pattern in prices that were built to have none, then watches it vanish.Code run
- How a backtest liesA backtest is easy to write and easier to fool yourself with. Six common mistakes, each one demonstrated in code on invented prices, so you can see the size of the error for yourself.Code run
Families are in alphabetical order and techniques are in name order within each. The order is not a ranking and no technique here is recommended over another.
The rules every script follows
- Invented prices only. No script reads a real market, and no page shows a result from one.
- A rule decides using prices up to the day before. The one exception is the demonstration of look ahead as a mistake.
- Every script that simulates trades charges a cost on each trade and prints what simply holding did beside the result.
- Settings are example values chosen to be easy to follow. None is a suggestion.
- The code on each page is the file that was run, and the output is what that run printed.
The invented data
Save these beside a script and run it with Python from that folder. Nothing else needs to be installed.
- invented-pair.csv
- invented-prices.csv
- invented-universe.csv
- sample-candles.csv
- make_invented_data.py, the script that writes every file whose name begins with the word invented, from fixed seeds