Predicting the future by looking at the past.

An analysis of trading strategies with backtesting.

Keno Leon
Keno Leon
Jun 7 · 10 min read

The Problem:

* Note: I've never been a fan of Technical Analysis but to each its own, my main beef is that it lacks a proven track record and hinges on a mostly absolute certainty that the past will repeat itself; and that there are rules governing behaviors across markets, exchanges and participants we can describe, yet the basis for these rules is usually not explained, a fertile ground for snake oil.

The Solution:

  • It’s cheap : Day trading can chew your principal capital via commissions and taxes.
  • It’s low maintenance: You literally have to do nothing for years on end but provide capital, XYZCorp then uses your capital to generate monetary value and you get a cut via your stock price and/or dividends.
  • It’s less risky: The risk here comes from missing out on the big swings that can happen in a matter of minutes, being on the wrong side of a trade during a swing is usually devastating.
This post is not about financial advice, but for an overview that expands on the risks of day trading and other financial follies I can recommend The Investment Answer (Goldie, Murray) 

A reasonable alternative ?

You could argue that all of Investing is gambling and you wouldn’t be entirely wrong, even guaranteed investments like government bonds fail from time to time, so what is important to note is the risk amount which in turn is correlated to how uncertain we are of the outcome, in other words, how confident are you that XYZCorps price will behave in such and such way in the following days.

Confronted with this seemingly impossible problem ( uncertainty ) we usually turn to the past, not because the past is a perfect or even good predictor of the future, but rather ( humbly ) because we don’t have anything else to look at for clues about the future, and that’s unfortunately all we get, clues…

Backtesting as its name implies is simulating or testing a certain action with past data and confronting the result with the present, it also gives us valuable insights about future strategies, if you have ever thought about your life in terms of the choices you didn't take ( what if I had pursued a medical profession ) and then confronted that with present data ( Jim is a doctor and he’s wealthy but overworked ) you are backtesting. When your own son or daughter then asks you what to study, you can tell them: from what I understand you have a good chance of becoming wealthy and overworked, the certainty /risk is left out, but it depends on many factors like your sample size, data quality and external or intrinsic factors, if medical robots suddenly become the norm your offspring might not fare that well, ( but you had no way of knowing ), as I said the past gives us clues, uncertain ones, nothing more…

To the Past !

Note: from here on, I’ll use real data, but momentarily obfuscate the identity of the investment, so for now XYZCorp it is, I'll also simplify considerably our analysis by only using close prices and zero commissions, in reality you'd need to take into consideration Bid/Offer spreads and other subtleties.

The data itself is simply a YTD time series of the closing price, here in chart form along with some basic volatility measures:

Our baseline strategy then would be the profit or loss during this period Year to Date Profit/Loss, any other trading strategy will be measured on top, in excess or loss against this baseline:

YTD P/L = (( Last Price / First Price ) - 1) * 100YTD P/L = (( 271.77 / 140.82 ) - 1) * 100YTD P/L = 93% If you had invested $100 you would have around $193 now, not bad.

Backtesting a staggered buy strategy.

If you had perfect knowledge of the future, you would save your money and buy late in a downward market, likewise you would hold onto your investment in an upward market and sell, the issue here is that we don’t know where the trend is going, so this strategy serves as a way to invest when there is a lot of uncertainty in this regard, the fact that we usually have savings available on a monthly or biweekly basis also makes it a popular strategy…

So let’s start there, let’s say you got paid twice a month, that would roughly translate to 10 buy periods of 10$ each. To backtest this strategy we simply divide our data and capital into 10 periods and create imaginary buy operations, it is easier to grasp in table form:

Date        |    Close   |   Amt  | Buy   Qty |  cumSumQty |   #
----------- +------------+--------+-----------+------------+-----
 2019-01-14 |    $129.07 |    $10 | 0.0774773 |   0.077477 |   1
 2019-01-27 |    $113.41 |    $10 | 0.0881756 |   0.16565  |   2
 2019-02-10 |    $124.81 |    $10 | 0.0801218 |   0.24577  |   3
 2019-02-23 |    $158.8  |    $10 | 0.0629723 |   0.30874  |   4
 2019-03-08 |    $135.13 |    $10 | 0.0740028 |   0.38275  |   5
 2019-03-22 |    $137.18 |    $10 | 0.0728969 |   0.45564  |   6
 2019-04-04 |    $158.05 |    $10 | 0.0632711 |   0.51891  |   7
 2019-04-17 |    $166.95 |    $10 | 0.0598982 |   0.57881  |   8
 2019-05-01 |    $160.82 |    $10 | 0.0621813 |   0.64099  |   9
 2019-05-14 |    $217.15 |    $10 | 0.0460511 |   0.68704  |  10

We can get our P/L by deriving it from our final quantity bought:

P/L = ((( SumQty * Last price )/ Amount) - 1) * 100P/L = (((0.68704 * 271.77)/100)-1) * 100P/L = 86.71%After investing $100 you would have around $186 now, still not too shabby.

We can also ask the very valid question, what would have happened if instead of 10 periods we bought in 2, 4 , 20 etc, well we can answer that:

The first thing to notice is that there is not much difference to our baseline strategy of buying at the start of the year, even though we won’t be able to capture a better return, at least we are not that much worse. The other thing to notice is the worst and best performing periods 2 and 3 , here it is implied that you are better off buying in a few periods, but not too few.

Backtesting Day Trading Strategies

What backtesting needs to provide here is a simulation of a trading algorithm along with a trading schedule of operations, we can then tally our P/L and compare it to our baseline P/L…

Let’s start with a trading algorithm that buys on the first of the year, sells when the price goes UP by 8% or more and rebuys the day after, here’s each one of those trades:

Date        |   CloseP  |   chg1D | chg1D% |   BuyP  |    P_L
----------- +-----------+---------+--------+---------+-----------
 2019-01-02 |    155.05 |  0.1011 | 10%    |  149.14 |  1.03963
 2019-01-14 |    129.07 |  0.1041 | 10%    |  122.03 |  1.0996
 2019-02-08 |    119.27 |  0.1409 | 14%    |  119.43 |  1.09813
 2019-02-17 |    133.6  |  0.0839 | 8%     |  146.1  |  1.00418
 2019-02-18 |    146.1  |  0.0936 | 9%     |  145.35 |  1.00936
 2019-04-02 |    163.96 |  0.156  | 15%    |  161.46 |  1.02499
 2019-05-11 |    194.3  |  0.1222 | 12%    |  187.33 |  1.06312
 2019-05-14 |    217.15 |  0.1031 | 10%    |  247.15 |  0.934078
 2019-05-15 |    247.15 |  0.1382 | 13%    |  264.01 |  0.874427
 2019-05-19 |    261.29 |  0.1138 | 11%    |  251.86 |  0.90716P_L in excess/loss : -9.28%
Accumulated P/L    : 75.08%Note that here we are calculating our P_L just for the relevant trades, additionally you need to also take into consideration the Start/End prices to properly compare this backtest with the baseline, this is the Accumulated P/L. 

As you can see everything was going well until mid May where we had 2 consecutive days where the price rose more than 8% and our P/L turned negative, bummer.

Technical Notes: The code I am using here to backtest is unfortunately part of a side project that is not ready for release, but if you want to roll your own, you can start like I did, with excel, then move onto python data frames to expedite the process, if you have any specific questions let me know.

More Tests, way more tests :

❗️Note that unlike our first trading strategy ( 1D Change -> Sell -> reBuy ), here, unless the reBuy period is 1 day, we can't do consecutive operations since we are out of stock to sell, if you don't incorporate this small but crucial detail, your backtest will be incorrect.

Dissecting these strategies, out of 49 only 5 return a profit, and all of them require we re buy our stock within one day, it makes sense when confronted with the price chart due to the up trend.

More tests that you might want to try:- Make the Day change variable, so if the stock goes up X percent or more in a period of 2,3,4, etc days -> Sell -> Rebuy after Y days, for a period of 7 days you are looking at 343 backtests. - Try the opposite strategy, start long cash, then buy when the stock goes DOWN x percent or more and resell y days after (another 343 backtests).- Mix and match, combine both strategies. 

Who tests the backtests ?

Well, a good strategy is to test our previous group of tests on a different time period and see how well they hold up, for instance we could ask what if we tried our last set of strategies in the previous 6 months ?

For period June- Dec 2018

Would you look at that, our ugly duckling of a strategy suddenly turned into a mostly positive swan, you’d also be more impressed if I mentioned that our baseline P/L is a negative -76.5%, what’s happening here ?

If you look at the price chart things start to make more sense:

Selling and then rebuying afterwards on a downward market can be quite lucrative, the only gotcha is that you need to be sure the trend is going your way.

What have we learned ?

Conclusions ?

Closing Notes:More specifically I would interpret these backtests as follows: -If I had $100 to invest passively for this stock I would divide them in 3-4 periods spread out during a few months.- When considering day trading strategies, the market trend needs to be taken into consideration.-Hundreds if not thousands of backtests need to be performed in order to gain further insights that are suited to an individual investors risk profile, and even then, they are no guarantee of a repeat performance but can save you costly mistakes.
THE DATA: Close prices of Ethereum ETH from June 2018 to May 2019.

Thanks for reading !

About the Author:

Born Eugenio Noyola Leon (Keno) I am a Designer, Software Developer & Artist currently living in Mexico City, you can find me at

Keno Leon

Written by

Keno Leon

AI, Software Developer, Designer :