## Basic Portfolio Math¶

Some of the stuff in portfolio management is so basic that we often forget how really basic it is. The building block of a portfolio is the position taken in some shares of a listed company as an investment or as a short-term speculative move. In both cases, the objective is to make a profit. We buy an asset and hold on to it, or resell it later on for a profit. Trading is simply doing the latter often.

The problem in not in the understanding of the game, it comes from asking very basic questions, like: what, when, and how much. An even more basic question is: why initiate that trade, at that time, in the first place?

A trade origins from some stock price at some time $\,p_t\,$ taken from its price series $p(t)$ chosen from the available stock universe at time $t$ with the intention of profitably holding on to it over some time interval $(t + \tau).$ The quantity $\,q,\,$ bought at time $\,t,\,$ is held over the period $\,(t + \tau).$

A completed trade will generate a profit or a loss: $\,q \cdot (p_{t + \tau} - p_t ) = x.\,$ Looking backward in time, the same principle applies: $q \cdot (p_t - p_{t - \tau} ) = x.$ We can view the price difference as: $(p_t - p_{t - \tau} ) = \Delta p,\,$ and picture the outcome of a trade as: $\,q \cdot \Delta p= x.$ This last expression will hold for any trade. The value of a position at any one time will be: $\,q \cdot p_t$.

A trader, by definition, intends to do many trades. We can sum them all using the following expression: $\sum^n (q_i \cdot \Delta^i p),\,$ which adds the result of the sequence of trades as a vector of lenght $\,n.\,$ Here, $\,\Delta^i \,$ is understood to include the exit price associated with the initiating trade id $\,i.\,$ That there be hundreds, thousands or millions of trades, this sum of profits and losses (P$\&$L) will give the final outcome.

No matter how we slice and dice a stock price series, we can always know the end result, which is a simple addition: $\sum^n (q_i \cdot \Delta^i p).$

To resume all trades taken on that one stock, we can write the following equation: $F(t) = F_0 + \sum^n (q_i \cdot \Delta^i p)\,$ which states that the final outcome is composed of the original stake allocated to the task to which is added the result of all the taken trades. Again, very basic.

### A Portfolio¶

A portfolio is usually composed of many stocks. We can add an index to identify which stock we are dealing with. The trade outcome vector would be: $\sum^n (q_{i, \,j} \cdot \Delta^i p_{\,j}) = \sum^n (P\&L).$ Still, every profit and loss is accounted for. However, this makes it a sequence of trades hard to analyze since it is just an order sequence of trades without patterns to distinguish a particular stock $\,j\,$ even if it is easily identified in the portfolio.

It is why I prefer the matrix payoff representation of that same data: $\sum^n (\mathbf{H}_{d, \,j} \cdot \Delta^i \mathbf{P}_{d, \,j})$ which says the same thing but disposes everthing in neet rows and columns. And, where $\mathbf{H}$ is the ongoing inventory matrix, holding quantity of shares held ($\,h\,$) in each stock ($\,j\,$) at any one time ($\,d\,$).

The outcome of a trading strategy could be resumed to: $\sum^n (\mathbf{H} \cdot \Delta^i \mathbf{P})\,$ with the understanding that the structure has its implicit indexes. In Python, it could have for expression: $payoff\_matrix = (\textsf{H} \cdot d\textsf{P}).sum()$.

The inventory matrix $\mathbf{H}$ has a simple composition: $\mathbf{H} = \mathbf{B} - \mathbf{S}.\,$ It is the sum of the quantities $\mathbf{B}$ought minus the quantities $\mathbf{S}$old. These are usually sparse matrices, and can become even more sparse as the trading interval is increased.

$\textsf{The inventory matrix}$ $\,\textbf{is}\,$ $\textsf{the trading strategy.}$

It holds the historical record of all the trading decisions taken. It has for each element: $\;\mathbf{h}_{d, \,j} = \mathbf{h}_{d-1, \,j} + \mathbf{b}_{d, \,j} - \mathbf{s}_{d, \,j}.\,$ Which says: the current inventory in each stock is equal to the inventory values in the previous period to which is added the shares bought, and from which is subtracted the quantities sold in each of the stocks. What remains is the net running total of shares held as $\,d\,$ increases.

This also implies that if $\;\mathbf{h}_{d, \,j} = \mathbf{h}_{d-1, \,j}\,$ then, it can only mean that $\;\mathbf{b}_{d, \,j} = 0$, and $\;\mathbf{s}_{d, \,j }= 0$. Stating that the inventory did not change which is equivalent to holding all the current positions as they were.

I find it easier to handle the payoff matrix rather than the payoff vector even if both say the same thing. One can view the payoff matrix growing from period by period as it increases its number of rows ($\,d\,$) and/or columns ($\,j\,$). Also, we can easily distinguish the trade history of each stock $\,j$ since they are organized as columns in the matrix.

All the above is trivial. Has been known for generations.

Still, few look at the portfolio problem is such a manner. Usually what we see are trading strategies designed to go from period to period trying to forecast what is coming next. With little as generalized long-term views of where a strategy is going in time.

Whereas, the payoff matrix deals with the impact of a trading strategy from start to finish. Seeing the trading strategy as a whole with its strenghts and weaknesses as they are played out over the long term. It is concerned with period to period plays, but its real strenght is in giving the ability to plan for much further than just the next period.

$\textbf{A portfolio has for equation}$: $$F(t) = F_0 + \sum^n (\mathbf{H} \cdot \Delta^i \mathbf{P}) - \sum^n (\mathbf{E}xp)\,$$It reads: the portfolio value, or its net liquidation value, at time $\,t\,$ is the sum of the initial capital plus the sum of the result of all ($n$) trades taken over the time interval from $\,t_0\,$ to $\,t\,$ and from which is subtracted all trading expenses.

With that equation, every trade in every stock is accounted for over the entire life of a portfolio. This portfolio can be of any size, contain hundreds of stocks over thousands of days, and the equation will still hold.

The equation is not just for traders, it is the same for any kind of stock portfolio. That it be discretionary trading or investing, or the result of some software program.

What should stand out is the trading strategy $\,\mathbf{H}$. It acts as the engine for this equation, its profit or loss generator.

If somehow the outcome of the payoff matrix is zero: $\sum^n (\mathbf{H} \cdot \Delta^i \mathbf{P}) = 0,\,$ then no need to guess that much as to your terminal portfolio value.

Comparing strategies is simple. Provide them with an identifier, as in: $\sum^n (\mathbf{H}_a \cdot \Delta^i \mathbf{P}).\,$ Then, ask if strategy $\mathbf{a}$ is better than strategy $\mathbf{b}$: $\sum^n (\mathbf{H}_a \cdot \Delta^i \mathbf{P}) > \sum^n (\mathbf{H}_b \cdot \Delta^i \mathbf{P}) ?\,$ The output of a payoff matrix is just a number giving the total profit or loss. Therefore, one is actually comparing two numbers as to their respective size. How complicated can it be?

We are not just comparing from one period to the next, even if it is included. We are comparing strategies over their entire lifespan, and this using a single expression. In Python, one could write: $\, If \; (\textsf{H}_{a} \cdot d\textsf{P}).sum() > (\textsf{H}_{b} \cdot d\textsf{P}).sum(): \dots$

Evidently, if you can compare two trading strategies, you can compare any number of them. Sort them out according to performance or whatever other criteria. We can also include a benchmark in the lot. $$\sum^n (\mathbf{H}_a \cdot \Delta^i \mathbf{P}) > \sum^n (\mathbf{H}_b \cdot \Delta^i \mathbf{P}) >, \dots, > \sum^n (\mathbf{H}_{SPY} \cdot \Delta^i \mathbf{P}) > , \dots, > \sum^n (\mathbf{H}_z \cdot \Delta^i \mathbf{P})$$With this comparaison, a trader can locate the relative performance level of any trading strategy he/she might design or compare their respective benefits. One thing is sure, you can always order those numbers according to size.

### Trading More Than One Strategy¶

To trade a number of strategies simultenuously and still have the ability to summarize and compare them, add an index to the payoff matrix: $$\sum^n (\mathbf{H}_{k, \, d, \,j} \cdot \Delta^i \mathbf{P}_{k, \, d, \,j})$$ where $\,k = 1, \dots, k\,$ identifies the strategies. This $k$ by $d$ by $j$ payoff matrix is sufficient to evaluate all applied strategies over all their respective stocks, or any group of stocks in each portfolio for any duration.

This is the kind of problem Quantopian is trying to solve, which is to find, out of all the tested trading strategies on its site (over some 4,000,000 backtests to date) which are the best. Which are acting best have also a Python one-liner expression: $(\textsf{H} \cdot d\textsf{P}).sum(axis=0)$. The $\,k\,$ best strategies can be viewed as Quantopian's selected few it intends to execute live from all its backtested strategies including those selected from its contests.