Older you bring a sheet of stock prices. How rich can you get? Really? This is why time travel was not invented!

Grumpy, future old you, dumps into your lap some closing prices. How rich can you get? The answer might surprise you!

Grumpy, future old you, dumps into your lap some closing prices. How rich can you get? The answer might surprise you!

Being reality and all, we have only a section of this data. We have only Open-High-Low-Close prices for a limited time window. For simplicity, in this blog post we will process only Open-Close prices, assuming that these are guaranteed to know when they will happen and that we will have enough volume to trade. It would be nice to buy on lowest price for the bar and sell on highest, but which one will happen first? What if selling short is not an option? Or is it costly? So we simplify things, focusing only on Open and Close prices. We don;t do any action, except when “entering” the bar and “exiting” the bar! In this way, it is guaranteed that our price will be met! Because we know for sure, that there was at least one transaction with that price, at the beginning of the bar.

We also ignore slippage, volume issues and market movements caused by our actions.

The three stocks that made it into our lap, from the future, 150 bars, on quiet market.

Note to more pedantic readers: This is a fabricated sample. We started from some intraday data, amplified some intra-bar changes and we removed the global trend so the hodl strategy yields zero. There are no sudden jumps at end of day, no stock splitting, no dividends to be factored in, no future contracts expiring, no distractions. And we are forced to act. So at least tow orders will be placed, one BUY and one SELL. There is no fiat option, again, for simplicity.

Ok, the stock sheet is deciphered, let’s see how we can beat the market!

Case 1. There is no fee!

That’s quite trivial, right? At each end of the bar sell and enter long on the stock that we know will be the highest gainer for the next interval. Rinse and repeat! Of course, if the best performer is our current stock, do nothing! Let’s assume we don’t have the option to stay on cash.

Best course of action to maximize the profits! Start on Stock 2, move to Stock 1, move to Stock 3, exit and party hard! Strategy is simple: Always bet on the winner!

We start with 100 units of capital. We select the strongest stock at each bar and we move to it. Note how the profit compounds! Comparing the winning path (strong stroke) with the less profitable path (thin stroke) there is only one bad decision, at Bar 1. Minor difference, stock 1 grew slightly worse than the stock 2. But, in the end, we got hurt with 0.30 units!

The gains and the losses are all compounding!

Case 2. There is a fee!

We go with a percent fee, because it is easier to compute. Also, we assume it is constant. Thinking about it, almost any kind of fee or spread can be mapped to this. This mechanism is simpler and can be easily generalized to more complex fee structures. The bigger issue for this scenario is when, and if, your future self will arrive.

Now, let’s run with intuition a bit!. Of course, the mantra is the same. Sell cheap, buy expensive! Oh wait, it’s the other way around! Only that now, each time we make a trade we lose some value. Small, but it adds up quickly. Does it make sense to buy a stock that will grow 0.5% if the fee to buy is 2%? Nope. Better stay on current stock (let’s say it grows only with 0.1%). Same for losses, sometimes it is better to “bite the bullet” and stay on a stock that will drop 1% on the next bar, than pay 2% to move on cash, overnight. More fun would be to “see” these drops in time, and to move to the winning stock, a few bars before the correction occurs.

Now you might think that 2% fee is a ripoff! And prices don’t just move 1-2% per day! However, if the fee is, let’s say 1/1000 and you have access to higher density data? Minutes? Seconds? Then, the price variation goes into the range of the fee!

Things are getting more complicated because maybe we are on stock X and we see in the future, a growth in stock Y. What is the best strategy to maximize profit? Sell high, ok, but also do it while doing as few as possible trades, to minimize the cost!

Let’s see a bit how this works out with some numbers:

We know what went up and down at each bar but moving from one bar to another is costly. How costly? Well, it depends on how much we move. It is not trivial to plot a strategy.

Already, for two bars, it is not clear HOW we are going to select the best strategy, How about 150 bars?

Looks ugly? Let’s move to a slightly better metaphor.

Tricking the fee troll

We enter a big hall divided in cells. When we step on a cell, a troll (we call it The Clearing House) will charge us a fee. This fee varies with each cell. And we know all the costs for all the cells! How can we get to the other side with a minimal cost? Also, we can’t move to any random cell, we must step only forward and only in adjacent cells.

The big hall where the troll operates. Each square has a fee. What is the cheapest way to cross the hall?

Above, is the hall we have to cross. We start from the left, we can enter the hall on any leftmost square. Once on a square we can move only forward or oblique, to the next column of cells.

Compare the blue route with the green one. The green takes a higher fee right in the beginning and then follows a valley up to the right edge. The blue one takes the lowest initial cell and in the end, pays a greater cost to reach the destination!

Ok, it is not trivial! But how do we solve it? Well, there is a thing called Dynamic Programming (DP) that can easily navigate this maze, providing you the absolute minimum total cost and the path that one should take. Let’s call our amazing trading algorithm, the Troll Navigator.

There is one major caveat though. The labyrinth must be known in advance. Glad that your future self dropped you those prices, right?

With some tweaking of the DP algorithm, one can compute a perfect transaction sheet for our stocks! Keeping track of Close-to-Open price variations and with any fee point.

Back to our stock sheet

Now, we have the perfect strategy, let’s apply it to see how rich we can get! What is the variable that we can tweak? The fee, of course! By selecting the cheapest exchange! Does it really matter the fee value? If it is 1/1000 or 1/100? In the end we have the best strategy!

Ok, no worries, play below with the fee point and see how fast your profit is going away!

Fee level: 0.0 %

Number of orders: 0

Playing around, you might note a few things: Maximum profit drops fast, even with a small increase in the fee level! Why? Because now the system avoids making changes. And as the fee rises, there are few and few opportunities to take the profit! Note how the number of transactions drops as the fee rises! At one point, it is more profitable NOT to play! Ring any bells? Who robbed Las Vegas in this way? Ping me with the answer on Twitter!

Ok, moving on! This is just a fantasy and as the good Doctor would say about time “it’s more like a big ball of wibbly-wobbly, timey-wimey stuff”. Doctor Who? you might ask. Nevermind that, the point is that our data is corrupt. Our troll navigator will make mistakes. So, besides fees, we will also lose money on not-so-good bets! Remember the compounding effect in the beginning of this rant?

Let’s check it out! Change the fee level and change the level of noise in the data!

Fee level: 0.0 %
Noise probability: 0.0 %

Number of orders: 0

Are you still winning? Now there is not even the notion of break-even! The troll navigator detects a path, takes it and all of the sudden you are broke! Because if the fee is so high, that there are no advantageous moves,  every wrong move equals a loss. How sudden is the loss? Watch the scale! Even with 10% bad decisions and 0.5% fee we go under! Increase the noise and bankruptcy is ensued!

Back to reality

Ok now, this is a bit scary, but there is no future self, providing us with this info! What’s the point of this rather theoretical exercise?

Well, glad you asked! What if you know the price only 3-4 seconds in advance? But you know it really well? Like close to 99%? What will be the profitability point? What fee do you have to negotiate to exploit this info?

But wait, is this even possible? Unfortunately, yes! High frequency traders will “sense” your order moving towards the market and they will act before it reaches the market. How will they exploit this info? Simple! Place a buy order? They will buy first and sell it back to you, at a slightly higher price! Go with a sell order? You got the picture! They will dump their stock and buy it back from you, at a lower price. These percentages are insignificant but let’s make the fees really really small, do many trades and compound!

Bitter lesson here, for day traders and people who think they can beat the market. The game is lost even before opening an account. Real money is made close to the clearing house, by big banks. They bid on cable length (!) to the exchange servers and have their algorithms printed in silicone. Costly? Yes! But they figured out that math checks out, a long long time ago.

Another bitter lesson is for those trying to beat the market using some smartass machine learning algorithm! Oh, the pains! Given any amount of fee, the performance of this algorithm must be close to perfection, to have a chance of not getting broke, fast.

Despite all the above, if you still believe that your strategy will beat the market, I will gladly implement your strategy! For the right fee, of course! Because in a gold rush, it is best to sell shovels! Or to own a stock exchange!