Van Tharp's Max Expectancy
Van Tharp's Max Expectancy
Started: July 26th, 2009 (02:13 AM) by caprica Views / Replies: 14,762 /
Last Reply: January 22nd, 2015 (11:55 AM) Attachments:
Welcome to futures.io.
This forum was established to help traders (especially futures traders) by openly sharing indicators, strategies, methods, trading journals and discussing the psychology of trading.
We are fundamentally different than most other trading forums:
We work extremely hard to keep things positive on our forums.
We do not tolerate rude behavior, trolling, or vendor advertising in posts.
We firmly believe in openness and encourage sharing. The holy grail is within you, it is not something tangible you can download.
We expect our members to participate and become a part of the community. Help yourself by helping others.
You'll need to
in order to view the content of the threads and start contributing to our community.
It's free and simple, and we will never resell your private information.
-- Big Mike
Van Tharp's Max Expectancy
February 20th, 2014, 02:21 PM
31 ( )
Bellingham, WA USA
Futures Experience: Beginner
Broker/Data: Mirus (Broker), Continuum (Data), Dorman (Clearing)
Favorite Futures: Futures
Posts: 202 since Mar 2013
Thanks: 426 given,
I wrote two optimization types (that work in NT 7 quite nicely) based on the information from this thread and Van Tharp's book. Here are the links:
X opportunity), and
(which does not include opportunity).
I use the Expectancy optimization all the time and find it gives me great direction when I am optimizing my stategies.
@ -- gordo
Thanks for coding the Expectancy Score optimizer.
I'm currently exploring the use of a few optimizers, and I have taken a look at the rationale behind the Expectancy Score, which appears pretty decent. So thanks for coding it up.
I did notice that the Expectancy Score does not appear to take commissions or slippage into account when calculating winsAmount or losesAmount.
Was the exclusion of commission and slippage on purpose?
It seems to me that we should be optimizing to a net expectancy score as opposed to a gross expectancy score.
What are your thoughts?
Here is the code for those that have not downloaded the .cs file.
#region Using declarations
// Expectany score calculations based on Van K. Tharp's book 'Trade Your Way to Financial Freedom'.
// Portions of this code borrowed from fluxsmith at futures.io (formerly BMT). Thanks for the start!
// 4/28/2011. Gordon Brest.
public class ExpectancyScore : OptimizationType
private int normalizedNumberTradesPerYear;
private int numberScratchTrades;
private int numberOfTrades;
private double studyDays;
private double averageWinningTrade;
private double probabilityOfWinning;
private double averageLosingTrade;
private double probabilityOfLosing;
private double nonScratchTrades;
private double valueScratchTrades;
private double winsAmount;
private double winsCount;
private double losesAmount;
private double losesCount;
private double expectancy;
private double opportunity;
private double commission;
private bool init = false;
private double val;
public double expectancyScore;
private DateTime start = new DateTime();
private DateTime stop = new DateTime();
private TimeSpan span = new TimeSpan();
public override double GetPerformanceValue(SystemPerformance systemPerformance)
#region Logic Description
EXPECTANCY is how much you expect to earn from each trade for every dollar you risk. Opportunity is how often your strategy trades.
You want to maximize the product of both.
Expectancy = (AW × PW + AL × PL) ⁄ |AL|
(expected profit per dollar risked)
Expectancy score = Expectancy × Opportunity
AW = average winning trade (excluding maximum win)
AL = average losing trade (negative, excluding scratch losses)
|AL| = absolute value of AL
PW = probability of winning: PW = <wins> ⁄ NST (where <wins> is total wins excluding maximum win)
PL = probability of losing: PL = <non-scratch losses> ⁄ NST
Opportunity = NST × 365 ⁄ studydays (opportunities to trade in a year)
NST = <total trades> − <scratch trades> − 1
In other words, NST = non-scratch trades during the period under test (a scratch trade loses commission+slippage or less) minus 1 (to exclude the maximum win).
studydays = calendar days of history being tested
NOTE: The above verbage from the referenced website has been copied into the code below to explain the code's logic.
/// Number of trades.
numberOfTrades = systemPerformance.AllTrades.TradesPerformance.TradesCount;
normalizedNumberTradesPerYear = (int)(systemPerformance.AllTrades.TradesPerformance.TradesPerDay * 365.0);
commission = systemPerformance.AllTrades.TradesPerformance.Commission / numberOfTrades * -1;
// studydays = calendar days of history being tested
foreach (Trade allTrades in systemPerformance.AllTrades)
init = true;
start = allTrades.Entry.Time.Date;
stop = allTrades.Entry.Time.Date;
span = stop.Subtract(start);
studyDays = span.TotalDays + 1;
/// Calculate scratch trades.
numberScratchTrades = 0;
valueScratchTrades = 0;
// NST = <total trades> − <scratch trades> − 1
// In other words, NST = non-scratch trades during the period under test (a scratch trade loses commission+slippage or less) minus 1 (to exclude the maximum win). foreach (Trade myTrade in systemPerformance.AllTrades.LosingTrades)
foreach (Trade losingTrades in systemPerformance.AllTrades.LosingTrades)
if (losingTrades.ProfitCurrency >= 1.25 * commission)
valueScratchTrades += losingTrades.ProfitCurrency;
nonScratchTrades = numberOfTrades - numberScratchTrades - 1;
winsCount = systemPerformance.AllTrades.WinningTrades.Count;
losesCount = systemPerformance.AllTrades.LosingTrades.Count - numberScratchTrades;
/// Reject optimizations that do not make any trades.
if ( normalizedNumberTradesPerYear == 0 )
/// Reject optimizations that trade less than once a week.
if ( normalizedNumberTradesPerYear < 50 )
/// Reject optimizations that do not have any losing trades as unrealistic.
if ( systemPerformance.AllTrades.LosingTrades.Count == 0 )
/// Calculate averages.
// AW = average winning trade (excluding maximum win)
winsAmount = systemPerformance.AllTrades.TradesPerformance.GrossProfit - systemPerformance.AllTrades.TradesPerformance.Currency.LargestWinner;
averageWinningTrade = winsAmount / (systemPerformance.AllTrades.WinningTrades.Count - 1);
// AL = average losing trade (negative, excluding scratch losses)
losesAmount = systemPerformance.AllTrades.TradesPerformance.GrossLoss - valueScratchTrades;
averageLosingTrade = losesAmount / (systemPerformance.AllTrades.LosingTrades.Count - numberScratchTrades);
/// Calculate probabilities.
// PW = probability of winning: PW = <wins> ⁄ NST (where <wins> is total wins excluding maximum win)
probabilityOfWinning = winsCount / nonScratchTrades;
// PL = probability of losing: PL = <non-scratch losses> ⁄ NST
probabilityOfLosing = losesCount / nonScratchTrades;
/// Final calculations.
// Expectancy = (AW × PW + AL × PL) ⁄ |AL| where |AL| = absolute value of AL.
expectancy = (averageWinningTrade * probabilityOfWinning + averageLosingTrade * probabilityOfLosing) / Math.Abs(averageLosingTrade);
//val = expectancy;
// Opportunity = NST × 365 ⁄ studydays (opportunities to trade in a year)
opportunity = nonScratchTrades * 365 / studyDays;
// Expectancy score = Expectancy × Opportunity
expectancyScore = expectancy * opportunity;
/// Return the final value.
Last edited by aventeren; February 20th, 2014 at
Reason: Added code...
The following user says Thank You to aventeren for this post:
January 22nd, 2015, 11:55 AM
32 ( )
Toronto, Ontario, Canada
Futures Experience: Advanced
Platform: Ninja, MultiCharts
Favorite Futures: ES
Posts: 13 since Feb 2014
Thanks: 25 given,
- Replying to your post about dividing by AvgLoss vs. dividing by rickt Std(TradeResults), I think that the latter gives a sense as to the likelihood that the TotalProfit occurred by chance or due to a true edge. This van Tharp approach strikes me as just a variation of the Sharpe Ratio (for good or ill).
In contrast the division by AvgLoss gives you what your results would look like over an extended period in terms of how much better your gains did over your losses, provided your system makes it over that extended period.
I like to analogize this to betting on the race car driver that wins the most races in a season as opposed to the one that is the wildest, fastest driver in the season's first three events but then crashes out in flames.
Van Tharp's Max Expectancy
All times are GMT -4. The time now is