Backtesting Trading Signals: Hit Rate, Costs, and Return Calculation
Summary
The document considers how to evaluate a simple signal that uses past data to predict a subsequent price move. It proposes comparing the signal with a label based on the next period, but cautions that a single accuracy percentage is an incomplete measure. The response recommends treating the proportion of winning trades as a hit ratio and examining it alongside the average winning trade relative to the average losing trade, since a high hit rate can still accompany negative returns.
It also raises two practical checks: whether the target label should account for more price history when the strategy is path dependent, and whether transaction costs are included in the win threshold. For a position sized as a fraction of initial capital, the response expresses profit as the signal multiplied by the percentage price change and capital. These points are useful starting checks, but the exchange does not establish a full backtest design or address portfolio effects, execution assumptions, or statistical validation.
Key ideas
- A signal based only on historical data can be evaluated against a subsequent price outcome, provided the timing is handled carefully.
- A hit ratio alone does not show whether a strategy is profitable.
- Average winning and losing trade sizes help put the win rate in context.
- Path-dependent strategies may require labels that account for more than the next incremental price change.
- Profit based on a capital allocation uses the signal times the relative price move times invested capital.
Tags
Full text
# Computing the profits for a simple trading strategy (Backtest)
# Computing the profits for a simple trading strategy (Backtest)
I have developed a trading algorithm, surprisingly simple in nature (I did start off with grand plans of applying Machine Learning to this problem as I am a data scientist by trade).
I would place the code here, but I would like to do some appropriate backtesting before throwing it open for peer review. In any case, I am seeing accuracies of `predicted_trading_signal` compared with `perfect_insight` of $+80\%$ across many of the russell 2000 group, which as a data scientist, and a rational person seems too good to be true. To this end, I would like to know if this is an appropriate test
Here is some pseudocode:
```
# generate trading signal up onto data[i]
predicted_trading_signal = some_algo(data[:i])
# generate perfect insight signal
perfect_insight = another_algo(data[i], data[i+1])
# where data[i+1] is say the next day
if some_statistical_property_of_data > lambda:
if predicted_trading_signal == perfect_insight:
correct+=1
else:
incorrect+=1
# after running over all windows of interest for a particular stock
accuracy = 100.0* correct /float(correct + incorrect)
```
I wish I could be more concrete with the code!
- Is this a valid approach to computing accuracy
- is conditioning on information up until todays date and using the following days price typical in testing strategies without data leakage (a subtle problem to a naive person such as myself in trading)
- Is there a better approach?
My final question (hopefully this is considered part of the bigger question - I hate multiple questions in one post on mathstackexchange)
Can I determine the profit-loss calculation as $$ P_i = \text{trading_signal}_{i-1}\left(S_{i} - S_{i-1}\right)\cdot C_{i-i} $$
Here
- $C_{i-1}$ is the initial capital invested.
- $S_i$ stock price
- $\text{trading_signal}_{i-1}$ is initial trading signal
## Answer by Rehan (score 2, accepted)
https://quant.stackexchange.com/a/31969
To begin with, I'm assuming that your trading strategy gives output something of the kind - Buy, Sell, Neutral
While on a cursory glance, your calculation seems fine, here are a few things that you can look at
- Why does the perfect insight only look at the incremental change? Many trading ideas are path dependent - so re-check if your best suited action would have changed if you added history.
- The accuracy parameter, as I understand is the percentage of wins in the strategy trades. The more commonly used term for this is "hit ratio". However, the hit ratio is a misleading number. The hit ratio should always be look at, in conjunction with the "Average Winning Trade / Average Losing Trade" number. Only then can you arrive at the expected values. What can happen is that you have a strategy which has an 80% hit ratio, but your average win/average loss is 0.1, which would mean that overall, the strategy is generating negative returns.
- I see that you are using a statistical condition on the data - I hope that this accounts for the transaction costs - as in, the trade is called a win only if it is sufficiently higher than the entry price.
As for your query on Profit calculation, you have missed out on the dividing factor.
$P_i=trading\_signal_{i−1}.\frac{(S_i−S_{i−1})}{S_{i−1}}⋅C_{i−i}$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.