Comment
 Kamil’s notes
Home
By subject
By category
By author
💹 Op­ti­mal in­vesting
Technology📐Mathematics
TheoryKamil SzczerbaKamil Szczerba

Would it not be won­der­ful if, by look­ing at the mar­ket, we could in­fer the prob­a­bil­ity of the next or­der ? That is the goal of this ar­ti­cle. This is our roadmap:

  1. The Kelly cri­te­rion
    What ex­actly we must max­imise
  2. Com­put­ing op­ti­mal bets
    How to com­pute the op­ti­mal bet, know­ing the win­ning prob­a­bil­ity
  3. Re­verse-com­put­ing prob­a­bil­i­ties
    How to re­verse that com­pu­ta­tion
  4. The or­der book
    What pub­lic data to re­verse
The Kelly cri­te­rion

The first step is to find what cri­te­rion to op­ti­mise for. We can start with a sim­ple ex­am­ple. You have $1. You can bet any frac­tion ff of your money for a lim­ited num­ber of rounds.

  • 60% (pp) of time, your bet will dou­ble.
  • 40% (1p1 - p) of the time, you will lose your bet.

What is the op­ti­mal bet? Let us for­malise the prob­lem:

wealthn=(1+f)winsn×(1f)lossesnwealth_{n}=\left(1+f\right)^{wins_{n}}\times\left(1-f\right)^{losses_{n}}

Where nn is the step, and winsnwins_{n} and lossesnlosses_{n} are the num­ber of wins and losses at that step. In­deed, every win mul­ti­plies your wealth by 1+f1+f, and every loss, by 1f1-f. Per the law of large num­bers, winsnnpwins_{n}\approx np and lossesnn(1p)losses_{n}\approx n\left(1 - p\right). So:

wealthn(1+f)np×(1f)n(1p)wealth_{n}\approx\left(1+f\right)^{np}\times\left(1-f\right)^{n\left(1 - p\right)}

Here is what wealthnwealth_{n} looks like de­pend­ing on the frac­tion ff:

We see that the op­ti­mal bet does not de­pend on the num­ber of rounds nn. Since ln\ln is mo­not­o­n­i­cally in­creas­ing, find­ing the max­i­mum of wealthwealth is equiv­a­lent to find­ing the max­i­mum of ln(wealth)\ln\left(wealth\right): =p×ln(1+f)+(1p)ln(1f)=p\times\ln\left(1+f\right)+\left(1−p\right)\ln\left(1−f\right).

That is ex­actly the ex­pec­ta­tion of the log­a­rithm of our wealth! That is the Kelly cri­te­rion: we should max­imise the ex­pected log­a­rithm of our wealth.

Oth­er­wise, if we max­imised the ex­pected wealth, we would bet every­thing, and with every round, the prob­a­bil­ity of bank­ruptcy would tend to 1.

Com­put­ing op­ti­mal bets

Now that we have a value to max­imise, imag­ine we had the prob­a­bil­ity of wins and losses for any in­vest­ment in the mar­ket (would it not be won­der­ful?). Could we find the op­ti­mal bets? For ex­am­ple:

  • You have one dol­lar. You can bet on two coins:
  • You can bet any frac­tion f0f_{0} on a coin with prob­a­bil­ity p0=0.6p_{0} = 0.6.
  • You can bet any frac­tion f1f_{1} on a coin with prob­a­bil­ity p1=0.5p_{1} = 0.5.

Let us com­pute the Kelly frac­tions:

Run­ning this code will down­load ~30MB of com­pressed de­pen­den­cies.

Per this code, we should bet 20% on the first coin and 0% on the sec­ond (the right value for the 60% coin, if you look at the graph above). You can fid­dle with the val­ues of pip_{i} to see what hap­pens.

Re­verse-com­put­ing prob­a­bil­i­ties

Even bet­ter now: can we find the prob­a­bil­i­ties, know­ing the op­ti­mal frac­tions? Let us re­mem­ber that fif_{i} should max­imise the ex­pec­ta­tion, so its de­riv­a­tive should be zero:

It works! In other words, when we know the frac­tions of the op­ti­mal bets, we can in­fer the ini­tial prob­a­bil­i­ties of win­ning.

The or­der book

Once a price in­te­grates an in­for­ma­tion, it is too late to gain profit from it. In other words, the mar­ket re­flects past in­for­ma­tion. But how can we pre­dict the fu­ture?

Let us ex­am­ine how a mar­ket de­ter­mines a price. Imag­ine that cur­rently, 1 BTC = 1 USD, and you be­lieve that the BTC will fall, then rise. You can set an or­der to buy BTC. It re­quires a unit price (in USD) and a quan­tity (how much BTC). The goal is to pre­dict the prob­a­bil­ity of the next spot or­der.

  • If you ask for 1 BTC for 0.9 USD, but some­one is ready to sell for 0.8 USD, you have wasted money.
  • How­ever, if you ask for 0.7 USD, and no­body is ready to sell for that price, then you have locked your money for noth­ing.

So, you want to pre­dict that is not too high, nor too low. And since you may con­sider many pos­si­ble prices, you will spread your money across them. That is what traders do: pre­dict the prob­a­bil­ity for all pos­si­ble prices, then set or­ders. The record of such or­ders is called the or­der book, and it looks like this:

This is the cur­rent real-time or­der book for BTC/​USD, with data kindly pro­vided by Bi­nance.

That is ex­actly what we are look­ing for: each one of these bars is a bet about fu­ture or­ders, and by ap­ply­ing our strat­egy from above, we may find the prob­a­bil­ity for any price to be the next or­der.

The Coin­bot

The last step, de­vel­op­ing the bot who will au­to­mat­i­cally in­vest op­ti­mally, is left to you! How­ever, keep in mind that this ar­ti­cle is merely a math­e­mat­i­cal mus­ing, and not fi­nan­cial ad­vice.