How to Calculate Strategy Expectancy in Python

Strategy expectancy tells you the average profit or loss you can expect from each trade over the long run. To calculate it in Python, you need your win rate, loss rate, average win amount, and average loss amount.

TrustyBull Editorial 5 min read

The Big Misconception About Winning Trades

Many traders believe a high win rate is the secret to success. They search for a strategy that wins 80% or 90% of the time. But what if I told you a strategy that wins 8 out of 10 trades could still lose you money? This is a common problem for those starting to explore what is sebi-regulations">algorithmic trading in India. The issue isn't how often you win; it's how much you win when you're right versus how much you lose when you're wrong.

The solution is a simple but powerful metric called strategy expectancy. It cuts through the noise of win rates and tells you the truth about your strategy's mcx-and-commodity-trading/trading-mcx-base-metals-limited-capital-risk-tips">margin-negative">profitability over the long run. It answers one question: On average, how much money can you expect to make or lose per trade? Knowing this number is fundamental before you let any algorithm trade with your real money.

Understanding Strategy Expectancy

Strategy expectancy is the average amount you can expect to earn (or lose) for every single trade you make, assuming you follow your strategy over a large number of trades. A positive expectancy means your strategy is profitable over time. A negative expectancy means it's a losing strategy, even if it wins frequently.

The formula looks like this:

Expectancy = (Win Rate × Average Win Size) – (Loss Rate × Average Loss Size)

Let's break down the parts:

  • Win Rate: The percentage of your trades that are profitable. If you make 100 trades and 60 are winners, your win rate is 60% or 0.60.
  • Average Win Size: The average profit from your winning trades.
  • Loss Rate: The percentage of your trades that lose money. This is simply 100% minus your win rate. In our example, it's 40% or 0.40.
  • Average Loss Size: The average loss from your losing trades. This should always be a positive number in the formula, as the minus sign is already there.

Imagine a strategy with a 70% win rate. Sounds great! But the average win is 100 rupees, and the average loss is 300 rupees. Let's calculate:

Expectancy = (0.70 × 100) – (0.30 × 300) = 70 – 90 = -20 rupees.

This strategy, despite winning most of the time, is expected to lose 20 rupees on average with every trade. This is why expectancy is the true measure of a strategy's health.

How to Calculate Algorithmic Trading Expectancy in Python: A Step-by-Step Guide

Now, let's get practical. You can easily calculate this using Python, which is a popular language for algorithmic trading. You don't need complex tools, just a list of your past trade results.

Step 1: Gather Your Trade Data

First, you need a record of your profits and losses from each trade. For this example, we'll use a simple Python list. Positive numbers are wins, and negative numbers are losses.

trades = [150, -50, 200, -80, 120, -60, 300, -100, 50, -40]

In a real-world scenario, you would likely import this data from a CSV file using a library like Pandas, especially if you get your data from sources like the National Stock Exchange (NSE).

Step 2: Separate Winning and Losing Trades

Next, we need to split this list into two: one for all the profitable trades and one for all the losing trades. We can do this easily with a list comprehension.

wins = [trade for trade in trades if trade > 0]
losses = [trade for trade in trades if trade < 0]

After running this, `wins` will be `[150, 200, 120, 300, 50]` and `losses` will be `[-50, -80, -60, -100, -40]`.

Step 3: Calculate the Key Metrics

Now we calculate the four components of our expectancy formula. It is crucial to handle the case where there might be no wins or no losses to avoid a division by zero error.

total_trades = len(trades)

# Win and Loss Rates
win_rate = len(wins) / total_trades if total_trades > 0 else 0
loss_rate = len(losses) / total_trades if total_trades > 0 else 0

# Average Win and Loss
avg_win = sum(wins) / len(wins) if len(wins) > 0 else 0
avg_loss = abs(sum(losses) / len(losses)) if len(losses) > 0 else 0

Notice we use `abs()` for the average loss to make the number positive, as required by the formula.

Step 4: Compute the Final Strategy Expectancy

Finally, plug these metrics into the formula to get your answer.

expectancy = (win_rate * avg_win) - (loss_rate * avg_loss)

print(f"The strategy expectancy is: {expectancy:.2f} per trade.")

For our example data, the win rate is 50%, the average win is 164, the loss rate is 50%, and the average loss is 66. The expectancy is (0.5 * 164) - (0.5 * 66) = 82 - 33 = 49. This means the strategy is expected to make 49 rupees per trade on average.

Common Mistakes to Avoid

Calculating expectancy is simple, but a few mistakes can give you a dangerously misleading result.

  • Ignoring Costs: Your calculation must include all trading costs. Brokerage, equity-trading">intraday-trading-income">Securities Transaction Tax (STT), exchange fees, and slippage can turn a profitable strategy into a losing one. Subtract these costs from each trade's result before you start calculating.
  • Using a Small Sample Size: An expectancy calculated from 20 trades is statistically meaningless. You need a large sample of data, ideally hundreds or thousands of trades, to have confidence in the result.
  • Curve-Fitting: This happens when you over-optimize a strategy on historical data. It looks perfect on your backtest but fails in live markets. Always test your strategy on data it has never seen before (overfitting-difference">out-of-sample data) to get a more realistic expectancy.

How to Improve Your Strategy's Expectancy

If your expectancy is low or negative, you can work on improving it. You can adjust one of the four levers in the formula.

Lever to Pull How to Do It Example
Increase Average Win Let your winning trades run longer. Use a ma-buy-or-wait">stop-loss-checklist-algo-bots">trailing stop-loss instead of a fixed target. Aiming for a 3:1 risk-reward ratio.
Decrease Average Loss Cut your losses quickly. Use a strict, non-negotiable stop-loss on every trade. Never risk more than 1% of your capital on a single trade.
Increase Win Rate Refine your entry signals. Add more confirming factors before entering a trade. Adding a backtesting">moving average filter to your RSI signals.

Often, focusing on increasing your average win while strictly controlling your average loss has the biggest impact. Many professional traders have win rates below 50% but are highly profitable because their winners are much larger than their losers.

Strategy expectancy is a foundational concept. By calculating it in Python, you move from guessing to making data-driven decisions. It is a critical step for anyone serious about building robust systems for algorithmic trading in India or anywhere else in the world.

Frequently Asked Questions

What is a good expectancy for a trading strategy?
A good expectancy is any positive number. The higher the better, but it must be positive after all costs like brokerage and taxes are considered. A positive value indicates the strategy is profitable over the long term.
Can a trading strategy with a low win rate be profitable?
Absolutely. Many successful trend-following strategies have win rates below 50%. They are profitable because their average win size is significantly larger than their average loss size, resulting in a positive expectancy.
Why is a large sample size important for calculating expectancy?
A small number of trades can be influenced by luck or unusual market conditions. A large sample size, with hundreds or thousands of trades, gives you a more statistically reliable and accurate measure of the strategy's true performance.
Does a positive expectancy guarantee future profits?
No, it does not. Expectancy is a statistical measure based on past performance. While it's the best predictor we have, market conditions can change, and a strategy's performance can degrade. It's a measure of probability, not a guarantee.