How I Developed A Profitable Trading Algo From Scratch
The start-to-finish guide to develop a profitable trading algo using no advanced math or quant knowledge
In my previous post, I outlined the high level steps I took to arrive at a profitable trading algorithm, currently trading an average ~20% per month at time of writing*.
In this post, I’ll go from A-Z, start to finish, and describe in detail my experience for you, as a reference and an example of what you can do.
I chose to trade crypto futures as it allows for going long and short and it’s available 24/7.
Ok. Let’s dive right in.
1) Pick a category of trading algorithm
Towards the end of 2022 and during early parts of 2023, the price of Bitcoin (BTC) stuck around the range of $16,000 - 25,000 for a number of months.
I figured because the underlying market forces were quite balanced (i.e. it wasn’t obvious whether buyers or sellers were in control), the best thing was to look for trading strategies that works well for range bound price action.
Mean Reversion (MR) was one such category.
Once I made that decision, I dove head-first researching what other traders (who may or may not trade crypto) have published about being successful with mean reversion.
I began taking copious notes on my Obsidian app (it’s a fantastic app for storing and linking your notes together - completely free with plenty of community plug-ins).
Anyway, I took note of how Jim Simons, who was probably the world’s most successful traders of all time, whose Medallion Fund achieved a 62% annualized return (before fees), mentions that he started with a Mean Reversion algo from way back.
This gave me confidence that there exists very successful implementations of Mean Reversion, and that I only had to get the right pieces in place, and implemented correctly.
2) Research successful implementations
Traditionally, mean reversion measures the distance of the current asset price against the Moving Average. (the mean of the most recent N candles).
If it’s too far below the Moving Average, they buy (or go long).
And inversely, if the asset price is too high above the Moving Average, they sell (or go short).
There’s nothing really wrong about the generic definition. However, I wonder how if there were lesser-known (and lesser-used) ways out there I could take advantage of.
So I went and searched on YouTube to watch how other successful traders explain how they became profitable.
Turns out, one YouTuber who traded Forex successfully was showing how he was able to be highly profitable with a ridiculously simple mean reversion implementation.
He didn’t use any indicators (in the traditional sense).
Just pure price action.
So I watched more of his videos.
He described two simple ways of implementing mean reversion. For sake of simplicity, I’ll call these N-Lookback and Red-Green.
N-Lookback
The strategy is to keep a look back window of the most recent N candles’ closing prices. When the (N+1)st candle’s close is above all other closes in the window, go short. Inversely, if the (N+1)st candle’s close is below all other closes in the window, go long.
There are ways to be more sophisticated. For simplicity, I went for a fixed stop loss and a “stop and reverse” way to take profit and stay in the market as much as possible.
Red-Green
This strategy measures the number of consecutive candles in a row. If there are N consecutive red candles in a row, go long. And if there are N consecutive green candles in a row, go short.
Again, I explored ahead with a simple fixed stop loss and a stop and reverse way to take profit.
3) Simulate on TradingView
To find suitable pairs to trade, and to get an intuitive feel for some starting values, I randomly tested the different scenarios by hand.
I tested first on BTC to see if I could make it work. Then, I went after cryptos that don’t react too much to price relative to BTC.
I filtered a short list of coins to test: BTC, ETH, BNB, XRP, ADA, DOGE, FXS, and USTC.
4) Build a custom backtest environment
This part was straight forward but easy to get wrong.
First, I picked an exchange where the target pair was traded. I downloaded the pair’s historical data to a file.
I separated the data set to training data (used in discovery) and out-of-sample data (not used in discovery).
The custom backtest environment is simply create a starting balance, a loop to go through a set of candle price data (from the price data file), simulate the long or short orders, stop losses, take profits, and final balance.
And of course, I added in commissions, profit and loss (PnL), wins, losses, average win amount, average loss amount, total commissions, average and max drawdown, to give a more complete picture of backtest performance.
5) Simulate trading with past data
I opted to start with the lowest timeframe the exchange offered (1 minute) and built my own candles for any higher timeframes.
The advantage? I can create a less used timeframe even if the exchange didn’t offer it, e.g. 13 minutes.
It would matter if success depended on that timeframe.
I’m not saying there’s any magic behind doing it this way. Just saying having flexibility with reading the data is better than not having it.
It was important to me to scan a wide area at this point.
6) Look for favorable areas to explore
So for my first scan, I scanned over multiple timeframes (1m, 2m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 8h, 12h, 1d), multiple stop loss levels (0.25%, 0.5%, 0.75%, 1%, …), and of course the key variable (N look back size or N consecutive color candles).
Doing this wide scan proved to be of value, I saw some glimmer of hope with BTC, but the initial values were too good to be true.
And it was.
After finding and fixing my backtest code, I was able to find some consistency with relatively long N value.
This was good news in that I’ve now found a way to be profitable. But it wasn’t good enough as it would mean I had to wait far too long for signals to generate.
So I counted BTC as a failed case.
7) Repeat and don’t over-optimize
Having failed to see any winning scenarios from BTC, I moved on to ETH and others.
I kept going for a while, until I found favorable areas where varying N had a large neighborhood of profitable areas.
This tells me that there is a pattern which mean reversion algorithms can capture using N values around those neighborhoods.
At the end of this stage, I had two very good areas from backtesting that I felt comfortable going to live testing.
8) Build signal generating system
The signal generating system consists of subscribing to the target exchange’s Websocket API for that future pair, building to match the N value (look back window or consecutive candles), and storing that signal to a database.
I opted to use PlanetScale for their free tier MySQL. The database choice here does not matter much. It could’ve very well been Supabase or Vercel Postgres.
For logging, I chose BetterStack as I’ve used their product since they were still Logtail. Using this makes it easy to see live results and do any troubleshooting down the line.
9) Review trade signals and debug issues
This was fairly straightforward as my algo was super simple to begin with.
Using the logging solution mentioned also helped a great deal.
10) Build execution service with exchange API
Since my signal generator uses a higher timeframe, running my execution service every 5 minutes was more than sufficient.
I used Vercel’s Edge Functions and a cron job to call it every 5 minutes.
The cron job was called using AWS’s Lambda with Scheduled Events.
The exchange API was very easy to use.
I built a light wrapper so my algo code would look cleaner, and the wrapper could be reused in future (different) strategies.
11) Live test with a small amount
There’s not much to it other than making sure that my signal generator was functioning as per my strategy.
Then I check in after N periods/candles to see how it’s performing. As long as it doesn’t continuously lose money, I let it keep running.
12) Iterate to profitability
Not much behind this because the algo and its implementation worked.
I would count it as a failed attempt if it traded exactly as the strategy detailed but still did not make a profit.
In this case, it would mean that I have overfitted my algo to my training data.
Luckily for me, this wasn’t a problem.
And so, that’s a fairly good review of how I did managed to find and build a profitable trading algo, without any heavy math of quant knowledge.
What am I doing next?
Given the current market conditions at time of writing, we’re potentially at the start of a 1-2 year bull market.
The most suitable category for this environment would be trend following.
This time, I’m going to build out an entire strategy and algo in public 👇🏼
All for you to see.
Follow me 0xSmartCrypto on Twitter to see real-time updates.
For those looking to have me personally guide you through the entire trading algorithm development process, and
If you are willing to commit 5-10 hours per week over the next 4-8 weeks,
You can book a discovery call with me here.
On our call, we’ll get to know each other and see if the situation you’re in is the right fit for us to work together.
The discovery call is on Google Meet, it’s free and 100% focused on YOU.
The goal — to make a customized plan and provide paid consulting sessions where I coach you through the major steps, what to watch out for, and give you access to detailed instructions (optionally starter code) to setup your own backtest and live testing environments.
If that’s you, I’d be more than happy to help.
See you again real soon!