texttextThis is a function proposed by Van K. Tharp in his book Trading Your Way to Financial Freedom (1999) as a way to measure strategy performance.

Although Expectancy is not one of the objectives that the user can select when choosing how Tradestation will optimize a strategy (under View Chart Analysis Preferences), calculating Expectancy will allow the user to output this information for each strategy run to an external file which may contain the values of the variables being optimized along with several user-defined objective functions for the strategy, such as Tharp’s ExpectancyTharp’s System Quality Number or Sharpe Ratio.

This external output file may then be imported into Excel, sorted in descending order by Expectancy, Sharpe Ratio, or any other user-defined objective for strategy performance, to determine which parameter values are likely to produce the greatest returns.

The Expectancy function is defined by Tharp as follows:

Expectancy

=  – (AW * PW + AL * PL) / AL

=  expected profit per dollar risked per trade

where

AW = average winning trade
PW = probably of winning (total wins / total trades )
AL = average losing trade
PL = probably of losing (total losses / total trades )

It is useful to annualize the result, making this objective more useful when comparing strategies that were run on different spans of time or on different trading vehicles that have been available for different periods of time:

Annualized Expectancy

=  Expectancy * Trades Per Year

=  Expectancy * Total Trades * 365 / Strategy Calendar Days

or

=  Expectancy * Total Trades * 365 / Strategy Trading Days

teNote that there are 365 calendar days in a year, but only an average of 252 trading days per year when weekends and holidays are accounted for.

It is somewhat easier to count the number of days the strategy has been trading (StrategyTradingDays) than to deal with the date functions that calculate the number of calendar days between a Strategy Start Date and Strategy End Date, so my personal preference is to use StrategyTradingDays. However, either formula is equally valid.

## Usage

This function is normally called from within the strategy when LastBarOntextChart is reached. The following code illustrates how the function may be called from within a strategy:

vars:
int TradingYears(0),
float oExpectPerTrade(0),
float oExpectancy(0);

Once begin
{ If StrategyStartdate or      StrategyStopDate not specified }
If StrategyStartDate = 0 then      StrategyStartDate = Date;
If StrategyStopDate = 0 then StrategyStopDate = CurrentDate;

TradingYears = (DateToJulian(StrategyStopDate) –
DateToJulian(StrategyStartDate))/365;
end;

TradingYears = (DateToJulian(StrategyStopDate) – DateToJulian(StrategyStartDate))/365;

if LastBarOnChart then begin
value1 = _Expectancy(TradingYears, oExpectPerTrade, oExpectancy );
end;

## Downloads

Initially posted version:  09/09/09

Latest Update:  09/17/09

*.ELD files are compiled for TS 8.6

All ELD and code text files packaged here:

Users of earlier versions of TradeStation may compile the code

from the text files included in the above *.zip file.

The code may be visualized here:

ExpectancyFunction

StrategyCodeCallingFunction