Tracking Cash, Positions, Wealth, and PnL in a Backtest
Summary
The document gives a basic accounting framework for backtesting a strategy that trades one instrument. It tracks cash separately from the number of units held, allowing the position to be long, flat, or short subject to chosen constraints. A purchase reduces cash and increases holdings; a sale raises cash and reduces holdings.
At each valuation point, wealth is marked as cash plus the market value of the position. Period PnL is the change in wealth, and the period return is that change divided by prior wealth. This framework handles an initial sale by representing it as a short position when permitted, rather than requiring an asset already held. The answer does not discuss execution prices, transaction costs, financing, position sizing, or Sharpe-ratio calculation, so those need separate modeling choices in a complete backtest.
Key ideas
- Track cash and asset units as separate state variables.
- A short position can be represented by negative holdings if the strategy permits shorting.
- Mark portfolio wealth as cash plus the current market value of holdings.
- Compute period PnL from the change in wealth and period return relative to prior wealth.
- Set cash and position limits explicitly to reflect the intended trading rules.
Tags
Full text
# How to compute a portfolio PnL and Sharpe?
# How to compute a portfolio PnL and Sharpe?
I understand this is quite the common question but I haven't been able to understand this concept through the previous posts.
My situation is that each day, I'm interested in buying/selling one financial instrument (always the same). Every day, I have a signal that tells me if I should buy or sell or do nothing. In order to test this signal, I'm trying to backtest the strategy on a 5 years period and I have two questions:
- How do I compute the PnL? I believe I should specify an initial fortune $V_0$ that I'm willing to invest on day 0. Then, if say my first signal tells me to buy, should I substract the today's price of the instrument $S_0$ to $V_0$ and my PnL today would be $V_0 - S_0$? What about the other days and the total PnL?
What if my first signal tells me to sell? How can I do that when I don't have any positions?
- My signals are the expected value $\mu_t$ and the risk $\sigma_t$ of the instrument's return on each day $t$. I can from then compute a daily Sharpe ratio $\text{Sharpe}_t = \mu_t/\sigma_t$ . If I want to have a Sharpe ratio of at least $1$ at the end of the 5 years, then I believe I should buy/sell on day $t$ if $|\text{Sharpe}_t| > 1/\sqrt{5\times250}$, is that right?
Thanks in advance.
## Answer by nbbo2 (score 1)
https://quant.stackexchange.com/a/63283
On any day you should keep track of two things: The cash on hand $V_t$ and the amount of the asset you own $A_t$ ($A$ can be negative if you allow short positions. Or if you want you can restrict $A$ to a range such as $0\le A_t \le U$ for example, or $-1 \le A_t \le 1$ for a simple startegy that is either neutral or long/short 1 unit). You don't allow the cash $V_t$ to go negative (print an error message if this happens).
When you buy the asset, $V$ decreases by the cost of the asset and $A$ increases by 1. When you sell, the opposite: the cash $V$ increases by the price of the asset and $A$ decreases by 1. It does not matter which event come first.
On any day your wealth is $W_t=V_t+P_t*A_t$, where $P$ is the price of the asset. The P&L is $\pi_t=W_t-W_{t-1}$ and the return is $r_t=\frac{W_t-W_{t-1}}{W_{t-1}} $Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.