# The Game Inside¶

## Résumé¶

We all have a vision of what our trading programs should do. Presented in this notebook, as in the prior one (Payoff Matrix) are building blocks for what I want to do with Quantopian. As if putting on paper, preparing an overall plan on how I want to use the facilities on Q. The process could help others.

The first step is to design and structure this plan, to be followed later, by learning what is needed to do the job. Already I know the tools are there. But, I do have to learn the programming language anew and adapt my preferred programs and trading methods.

## Choices¶

In the stock market, I see a game to be played, inside what the "game" may represent to each one of us. And that is our game, our method of play, our views on how to profit from designing trading strategies. I see it all as an ensemble, a block, for lack of a better word, a matter of choices, yours, as well as mine.

The what you want to trade and the how you want to trade it is not just any idea you throw in the arena. It has to be thought out, planned, and structured so that not only will your trading strategy survive in its trading environment, it will thrive and exceed market averages. Otherwise, you might have been better off just buying index funds and sitting it out. You want to trade, then your mission is already on the table: do better than average.

It should be after understanding the math of the game we want to play that we should program our trading strategies to comply with this understanding. And maybe not the other way around as specifying the trading methods and then finding a theory that will or might comply with the results.

Once you have the math, understand what you want to do, then what is left is finding ways to program the trading procedures to match. And these trading procedures will be supported by the mathematical research and models you provided. The task will be to build from these models so that they reflects reality as much as possible.

An advantage of this kind of thinking is that you will be able to spot in someone else's program pivotal decision points that can affect their trading strategy and easily make improvements to either your own trading strategies or theirs.

Understanding the math of what you intend to do and knowing what it does, or will do in the long run, will give you added abilities helping you focus on achieving more.

## From the Previous Notebook¶

In the Payoff Matrix notebook was presented the mathematical heart of the game. It had this singular look among others: $$A(t) = A_0\, + \displaystyle{\sum_{i=1, \, j=1}^{n, \,k} (H}\,.^*\Delta{P}) = A_0 + n \cdot \bar x$$ It sums up all the profits and losses from all the trades in all the stocks over the entire investment period. Be it one trade or a million. It remains true for any trading strategy whatsoever.

The notebook concluded with this simplified asset model: $A(t) = A_0\, + n \cdot \bar x \,$, where $n$ is the number of trades over the trading interval, and $\bar x$ is the average profit per trade. This payoff matrix with its thousands or millions of entries was reduced to the status of a simple bean counter. The market's complexity in one expression: $A(t) = A_0 + n \cdot\bar x$. $\,$ Evidently, if you do no trades, or have $\bar x = 0$, you remain in cash: $A(t) = A_0$.

To profit, one single requirement: $\bar x > 0$. Whatever trading method used, this is a sufficient condition to profit. That you used wrong assumptions or not, relied on technical indicators or not, used fundamental data or not, all your trading strategy has to show is: $\bar x > 0$. Even if it is not your fault that it does which should give you reasons to find out why.

That a strategy be over-fitted or not, over-optimized or not; for any trading strategy, if you could show $\bar x > 0$, you would have won. However, if you over-fitted or over-optimized, then, even with $\bar x > 0$, it might not amount to much since the future might not deliver on your misconceptualization of the trading problem.

This simplified asset model reduced a trading strategy, with its total profit or loss, to two variables: $n \cdot \bar x$, one of which $n$ is just a sequential counter giving how many trades were executed.

The Payoff Matrix notebook also ended by suggesting that a strategy redesign could give an added boost to propel one's strategies beyond just achieving average market returns:

$$A(t) = A_0\, + (\, n \cdot f(g)\,) \cdot (\,\bar x \cdot \,f(h)\,)$$

I will try to elaborate on this. But first, I would like to address frictional costs.

## Frictional Costs¶

Trading costs (commissions, slippage) should be accounted for. One could simply add them to the payoff matrix itself. It would look like this: $$\displaystyle{\sum_{i=1, \, j=1}^{n, \,k} (H}\,.^*(\Delta{P}-2 \cdot c -2 \cdot sl))$$ where $c$ stands for commissions, and $sl$ for slippage, each charge being per share for the entries and exits of all taken positions. $\Delta P$ is the price differential matrix of all the trades. Its running total would end with the sum of all price variations for each of the stocks in the historical trade list $P$ of tradable stocks considered: $\sum \Delta P$. It is the same as expressing a price series as: $p(t) = p_0 + \sum \Delta p$, an initial price plus the sum of all price variations.

Applying a \$0.01 per share commission, and a \$ 0.01$^+$ for slippage. The payoff matrix would now look like: $$\displaystyle{\sum_{i=1, \, j=1}^{n, \,k} (H}\,.^*(\Delta{P}-0.02 -0.02 ^+))$$ In all, not that much. The above should somewhat trivializes the impact of commissions and slippage.

Using $n \cdot \bar x$, commissions and slippage could appear as: $n \cdot (\bar x -2.0 -2.0^+)$ using as example a \$1.00 minimum commission on averaged 100-share trades. Why be concerned that much by what might be just a small part of the cost of doing business? Some argue that commissions and slippage are important and should be accounted for. Yes, for sure. In real trading, they will. But, they should not be a major concern. Unless,$\bar x$is averaging close to its frictional costs. If frictional costs were of major consideration in my trading strategies, I might lose interest in them as if being too fragile if left to the whims of the market, too close to a potential disaster. It would be like saying that profits, due to commissions have a tendency towards zero or less:$\bar x - \$\, 4.00 \rightarrow 0.00 ,$ or $\bar x \rightarrow \,< 0.00$. In which case, the trading strategy providing such numbers might not be performing that well. On the other hand, a very large $n$ even with a small net edge $\bar x$ can show one can succeed and also be interesting as in HFT scenarios.

Is implied that the average profit per trade has to exceed at least frictional costs: $\bar x \geq \$ \,4.00 ^+$. Any trading strategy having$\bar x \rightarrow \$\,4.00$ should be considered as being highly impacted by frictional costs. But, then again, one can trade any system they want, for whatever reason they want. After all, it is their money and they can do with it as they please.

Based on the example provided in the Payoff Matrix notebook. The mentioned strategy came in at: $\bar x = \$ \, 5.53$. And since Quantopian already charges two way commissions and slippage by default, I could assume that the strategy had in fact$ \bar x$valued at about$ \$\, 9.53$ without commissions and slippage. Therefore, one could estimate that 41% of all the profits went to pay for frictional costs. This might be viewed as a very high expense ratio to trade. Nonetheless, the strategy did end up profitable, even though it was less than its indexed surrogate: SPY.

## Then, What is Left?¶

If all that is left is: $\,\bf {n \cdot \bar x} \,$, or equivalently the payoff matrix, then one should consider that that is all there is, as profound as it may seem. And your trading strategy, whatever it is, can be explained by those two numbers.

You only have 2 variables to work with, one of which $n$ is just an ordered number sequence that offers no predictive powers whatsoever. In reality, there is only one variable to contend with: $\,\bf \bar x$. Nonetheless, we naturally want $n$ to be as large as it possibly can.

We need to find ways that will raise or expand $\bar x$, the average profit per trade. Let's put aside commissions and slippage just to simplify the equations. My intention is to design trading strategy that far exceed frictional costs: $\bar x \gg (2c + 2sl)$. So, I will not be losing much in generalities, only a small fraction of $\bar x$. And this, as shown, can be easily reapplied after test anyway, and give a reasonable estimate of those costs.

The profit, or loss, on any one trade is: $x_{i,j} = q_{i,j} \cdot \Delta_i p_{i,j}$. It is one data element in the payoff matrix which could number in the thousands, even millions of entries. All trades are summed up in the payoff matrix, then averaged.

$$\bar x = \displaystyle{\frac {\sum_{i=1,\,j=1}^{n,\,k}(H\,.^*\Delta{P})}{n}}$$

So, one can improve results based on this equation, by either increasing $n$ the number of trades, increasing $q$ the quantity traded, increasing $\Delta p$ the price spread, or any two, or all three at a time. This will have an impact on $n$, $\bar x$, or both.

However, one item I see rarely fully considered in the financial literature, and that is $H$ the inventory matrix. Trading by definition is in and out. So entries in $H$ often go to zero, being sold out. While my techniques favor keeping and accumulating long term inventories. This can change the landscape considerably and enable some of what follows. More on this in a future notebook. I consider $H$ as a pulsating huge matrix that spans stocks and time where every trade is recorded. And I look for trading procedures that will influence the whole inventory matrix $H$ as it evolves and grow with time.

## The Methods¶

The search for trading methods. The biggest constraint is probably $A(t)$ itself. You start too small with a small initial stake $A_0$ and you are almost just wasting your time and resources. Starting small means making small bets over a limited range of stocks. You can't seriously trade 100 stocks with a $\$\,10,000$account, commissions will simply deplete your portfolio. Some should realize that such a small account is less than 50 shares of SPY! You are playing a CAGR game. Meaning that all that really matters is$r$and$t$as in:$(1+r)^t$, since, however you want to look at it, at termination time$T$you will get:$A(T) = A_0 \cdot (1 + r)^t$. Your path to termination time will not be that smooth, but, the end result will be the same. It will be as if your portfolio had operated at that CAGR level for that period of time. It is another way of saying that time matters a lot in this game. So be prepared for a long game and plan to reach the finish line which with time, will be pushed further and further away. If$A_0 + n \cdot \bar x$is all there is, then$A_0 \cdot ( 1+r)^t$has to reach the same number. You are ready to ask questions. What is the CAGR needed depending on$A_0$,$n$, and$\bar x$? $$CAGR = r = \displaystyle {\sqrt[\frac {1}{t}]{1 + \frac {n \cdot \bar x}{A_0} }-1}$$ How many trades might be needed?$ \,\, n = \displaystyle \frac {A_0 \cdot (1+r)^t - A_0}{\bar x}$. And, what might be the average profit per trade?$ \quad \bar x = \displaystyle \frac {A(T) - A_0}{n}$. With these functions, and from your own trading experience, you could fill in expectation blanks based on your current trading strategies. But, still, the easiest extrapolation might be to start with a napkin calculation of what you want since:$A(T) - A_0 = n \cdot \bar x$. If you have$A_0 = \$\, 10,000$ and you want $\$ \, 20$Million in 20 years, then you would need an average CAGR of:$46.24 \%$. Good luck on that one. If you increase$A_0$, the task becomes easier.$A_0 = \$\, 100,000$ would need a CAGR of: $30.33 \%$. While starting with $A_0 = \$ \, 1,000,000$, the task requires:$16.16 \%$. This is more than within reach. Could I emphasize the importance of starting the game well funded in order to reach your goals. Look at it this way. You intend to grow your portfolio over the next 20+ years. You design a trading strategy that can provide a 20% CAGR over the period. Then put it out to work with a \$ 10,000 stake. The results are in: $\$\, 10,000 \cdot (1+0.20)^{20} = \$\, 383,376$. However, working harder on raising capital could have done: $\$\, 1,000,000 \cdot (1+0.20)^{20} = \$\, 38,337,600$. You add 2 zeros to the left side, you can add 2 zeros to the right side.

You worked hard to design a program to maintain a 20% CAGR over 20 years, then let it rot working with peanuts when you could have done so much more. If you could maintain the same CAGR for the next 10 years, you would get: $\$\, 1,000,000 \cdot (1+0.20)^{30} = \$\, 237,376,314$. The cost of money, the cost of missed opportunities. My suggestion is simple and does not need to be expressed.

It should be noted that as your strategy progresses in time, it will become harder and harder to flip your entire portfolio all the time. So, you have to remedy that too. You need to plan now for what is to come. Saying: let's go back in time and start all over will not be an option. Do it right the first time out, there is no replay.

## The Objectives¶

You are faced with a game which expressed in its simplest form is: $A(t) = A_0\, + n \cdot \bar x \,$. Since, you are the one to supply $A_0$, I can not do anything about that, nor can I fix your long term objectives $A(T)$. You are the one to supply those numbers.

If you want 1 trade to reach your goals, then I can tell you that: $A(T) - A_0 = \bar x$. Hope you bought Berkshire Hathaway 50 years ago. The point being made is that everything will depend on: $n \cdot \bar x$.

### Increasing $n$¶

Finding ways to increase the number of profitable trades is one way to increase profits: $n \cdot (1+z) \cdot \bar x$. Oftentimes, it might only require taking more trade opportunities of the same kind that produced $\bar x$ in the first place. It should be evident that if you double the number of trades, this will double the profits.

Recently I had a discussion with the author of a trading script on Quantopian who could not accept that his trading strategy could perform better under my hands than under his. And yet, all I did was increase the potential tradable candidates from 3,000 to 3,200 from which only the top 10 were trade candidates. The move had for result to present the program with more tradable opportunities over the lifespan of the strategy and more trades were taken resulting in an increased performance level. Not that difficult really, insignificant as a matter of fact. But, still productive, meaning increased profitability. I knew the move would increase $n$ even before doing the test.

That is the task at hand. Finding ways to increase long term performance levels. And knowing from the start that the trading procedures you intend to put in place will have to perform as programmed, behave as dictated and produce what they are supposed to. If you increase $n$, it will have an impact: $n \cdot (1+z) \cdot \bar x \geq n \cdot \bar x$. It has for requirement: $\,z > 0$, and the use of cash reserves when available.

### Increasing $\bar x$¶

Increasing the average profit per trade is a one, two or three prong problem depending on how one wants to look at it. A trade $x$ can be expressed anywhere in the payoff matrix as: $\displaystyle {x_{i,\,j} = q_{i,\,j} \cdot \Delta_i p_{i,\,j}}$. It can be a trade lasting the entire duration or holding period. A trade will have a fluctuating portfolio weight while in inventory, up to reaching its stopping time. An opened position can be as ephemeral as one wants. But, once closed, a trade's footprint (its profit or loss) stays permanently in the payoff matrix.

Evidently, raising $q$ will raise $|x|$. You might need added capital since you are affecting the bet size, or, you could use more of the available cash reserves, if there are any.

The other area where one could increase $|x|$ would be in increasing $\Delta_i p_{i,\,j}$. One could put in a restriction like: $\Delta_i p_{i,\,j} > 0$, for a long trade to generate a profit. This could be part of more elaborate trading procedures.

One could also opt to increase both. Then it becomes a problem of finding more and more profitable opportunities where you could increase $n$. If reserves are available, increase $\Delta p$ by grabbing more of the price differential or by wait for a futher price increase, giving more time for the stock price to appreciate. You can do both at the same time as was said. I usually go for both propositions.

### Increasing $\, n \cdot \bar x$¶

Increasing $n \cdot \bar x$ could result in the following: $n \cdot (1+z) \cdot \bar x \cdot (1+v) > n \cdot \bar x$. All that seems to be required is: $z > 0$, and $v > 0$.

But it is not the only way to increase performance. One could also use leverage depending on the condition that the trading strategy is designed to support it, like in a stock to bond rotation system. When the market is in a downfall, you are in bonds sparing you all the margin calls. And when the market is going up, you are taking full advantage of the general rise in price.

Adding leverage would result in: $(1+l) \cdot n \cdot (1+z) \cdot \bar x \cdot (1+v) > n \cdot \bar x$. Again, with $l > 0$ it would have for impact to boost performance higher with some added risk. However, being in the market only when prices are considered rising, is not that much added risk. On the contrary, if there is a time for being in the market, it is went prices are rising. And you have for protection that if general prices fall, you will move to cash equivalents anyway. So, even with leverage, no double whammy here, no fears of margin calls. Just a profit booster for when prices are going up.

When you add all these up, it will have an impact at the CAGR level. The asset model is now:

$$\displaystyle {A(t) = A_0\, + \lbrace (1+l) \cdot n \cdot (1+z) \cdot \bar x \cdot (1+v)\rbrace}$$

Each factor contributing to the mix. Therefore, total profits ($n \cdot \bar x$) will have increased by the product of these factors : $(1+l) \cdot (1+z) \cdot (1+v)$, provided that $l\geq 0$, $z\geq 0$ and $v\geq 0$. A combination of these can propel your long term portfolio to new hights. For instance, with $(1+l) \cdot (1+z) \cdot (1+v) \geq 2$, it will increase total profit by a factor of $\geq 2$. It will be your strategy design that will need to respond to these factors.

But, in order to do so, they need to be programmed in. Otherwise, how could they ever be there or even work to your advantage?

$$A(t) = A_0\, + \lbrack (1+0) \cdot n \cdot (1+0) \cdot \bar x \cdot (1+0)\rbrack \, = A_0\, + n \cdot \bar x$$

I have strategies where I have made these factors conditional to other factors, even one where outside controls enables to direct the future orientation of the strategy by controlling its positive feedback loop and trade agressiveness.

I prefer having control over these functions rather than having fix settings for the duration of a portfolio's lifespan. This way, I can turn these functions on and off at will giving me controls to influence the general behavior of where a trading strategy might go.

$$A(t) = A_0\, + \lbrack (1+f(l)) \cdot n \cdot (1+f(z)) \cdot \bar x \cdot (1+f(v))\rbrack \, = A_0\, + n \cdot \bar x$$