Table of Contents

Class TradeStats

Namespace
Balsam
Assembly
Balsam.Backtester.dll

Encapsulates the results of a strategy simulation.

[ProtoContract]
[ProtoInclude(95, typeof(EquityCurveStats))]
public class TradeStats
Inheritance
TradeStats
Derived
Inherited Members
Extension Methods

Fields

excelMoney

A custom format string for currency values displayed in Excel.

protected const string excelMoney = "_($* #,##0.00_);_($* (#,##0.00);_($* \"-\"??_);_(@_)"

Field Value

string

Properties

AuditTrailId

Gets a globally unique identifier for this object.

public string AuditTrailId { get; set; }

Property Value

string

AvgBarsInLosers

Gets the average number of bars in losing trades.

public double AvgBarsInLosers { get; }

Property Value

double

AvgBarsInUnchanged

Gets the average number of bars in unchanged trades.

public double AvgBarsInUnchanged { get; }

Property Value

double

AvgBarsInWinners

Gets the average number of bars in winning trades.

public double AvgBarsInWinners { get; }

Property Value

double

AvgBarsPerTrade

Gets the average number of bars in all trades.

public double AvgBarsPerTrade { get; }

Property Value

double

AvgLosingTrade

Gets the average losing trade in dollars.

public double AvgLosingTrade { get; }

Property Value

double

AvgLosingTradePercent

Gets the average P&L of losing trades.

public double AvgLosingTradePercent { get; }

Property Value

double

AvgTrade

Gets the average P&L of all trades.

public double AvgTrade { get; }

Property Value

double

AvgTradePerContract

Gets the average P&L of all trades on a single contract basis.

public double AvgTradePerContract { get; }

Property Value

double

AvgTradePercent

Gets the average percent gain or loss for all trades.

public double AvgTradePercent { get; }

Property Value

double

AvgWinToAvgLoss

Gets the ratio of average winning trades to average losing trades.

public double AvgWinToAvgLoss { get; }

Property Value

double

AvgWinToAvgLossPercent

Gets the ratio of average winning trades percent gain to average losing trades percent loss.

public double AvgWinToAvgLossPercent { get; }

Property Value

double

AvgWinningTrade

Gets the average P&L of winning trades.

public double AvgWinningTrade { get; }

Property Value

double

AvgWinningTradePercent

Gets the average percent gain for winning trades.

public double AvgWinningTradePercent { get; }

Property Value

double

ClosedPnL

Gets the total P&L of all closed trades.

public double ClosedPnL { get; }

Property Value

double

ClosedTrades

Gets the number of closed trades.

public int ClosedTrades { get; }

Property Value

int

Commissions

Gets total commissions paid.

public double Commissions { get; }

Property Value

double

ElapsedTime

Gets the time it took to run the simulation (including money management if any).

public TimeSpan ElapsedTime { get; }

Property Value

TimeSpan

Expectation

Gets the expectation.

public double Expectation { get; }

Property Value

double

ExpectationPercent

Gets the expectation calculated using AvgWinToAvgLossPercent

public double ExpectationPercent { get; }

Property Value

double

GeometricAvgTrade

Gets the geometric average trade.

public double GeometricAvgTrade { get; }

Property Value

double

GrossPnL

Gets the Open + Closed P&L gross of commissions and slippage.

public double GrossPnL { get; }

Property Value

double

IgnoreSpreads

Used internally in money manager reporting routines.

public bool IgnoreSpreads { get; set; }

Property Value

bool

Kelly

Gets the Kelly criterion.

public double Kelly { get; }

Property Value

double

LargestLosingTrade

Gets the P&L of the largest losing trade.

public double LargestLosingTrade { get; }

Property Value

double

LargestLosingTradePercent

Gets the largest losing trade's percent loss.

public double LargestLosingTradePercent { get; }

Property Value

double

LargestWinningTrade

Gets the P&L of the largest winning trade.

public double LargestWinningTrade { get; }

Property Value

double

LargestWinningTradePercent

Gets the largest winning trade's percent gain.

public double LargestWinningTradePercent { get; }

Property Value

double

LongLosses

Gets the number of long trades that were losers.

public int LongLosses { get; }

Property Value

int

LongLossesPnL

Gets the total P&L of losing long trades.

public double LongLossesPnL { get; }

Property Value

double

LongPercentProfitable

Gets the percentage of profitable long trades.

public double LongPercentProfitable { get; }

Property Value

double

LongPnL

Gets the total P&L of all long trades.

public double LongPnL { get; }

Property Value

double

LongTrades

Gets the number of long trades.

public int LongTrades { get; }

Property Value

int

LongWins

Gets the number of winning long trades.

public int LongWins { get; }

Property Value

int

LongWinsPnL

Gets the total P&L of all winning long trades.

public double LongWinsPnL { get; }

Property Value

double

LosingPnL

Gets the total P&L all all losing trades.

public double LosingPnL { get; }

Property Value

double

LosingTrades

Gets the total number of losing trades.

public int LosingTrades { get; }

Property Value

int

MaxConsecutiveLosses

Gets the number of maximum consecutive losses.

public int MaxConsecutiveLosses { get; }

Property Value

int

MaxConsecutiveLossesPnL

Gets the P&L of the maximum consecutive losing streak.

public double MaxConsecutiveLossesPnL { get; }

Property Value

double

MaxConsecutiveWins

Gets the number of maximum consecutive wins.

public int MaxConsecutiveWins { get; }

Property Value

int

MaxConsecutiveWinsPnL

Gets the P&L of the maximum consecutive winning streak.

public double MaxConsecutiveWinsPnL { get; }

Property Value

double

MedianTradePercent

Gets the median percent gain or loss of all trades.

public double MedianTradePercent { get; }

Property Value

double

NetPnL

Gets the Open + Closed P&L.

public virtual double NetPnL { get; }

Property Value

double

OpenPnL

Gets the total P&L of all currently open trades.

public double OpenPnL { get; }

Property Value

double

OpenTrades

Gets the number of open trades.

public int OpenTrades { get; }

Property Value

int

Optimalf

Gets the optimal f value.

public double Optimalf { get; }

Property Value

double

OptimizationProperties

Gets optimization parameters in the form ParameterName1=value|ParameterName2=value|etc. or null if results were not from an optimization run.

public VariableDictionary OptimizationProperties { get; set; }

Property Value

VariableDictionary

PercentProfitable

Gets the percentage of trades that were profitable.

public double PercentProfitable { get; }

Property Value

double

ProfitFactor

Gets the profit factor, or absolute value of winning P&L to losing P&L.

public double ProfitFactor { get; }

Property Value

double

RollCommissions

Gets total commissions paid for rolls.

public double RollCommissions { get; }

Property Value

double

RollContractsTraded

Gets the total number of contracts rolled.

public double RollContractsTraded { get; }

Property Value

double

RollSlippage

Gets the total amount of slippage incurred on rolls.

public double RollSlippage { get; }

Property Value

double

Rolls

Gets the total number of rolls.

public int Rolls { get; }

Property Value

int

Runtime

Gets the timestamp when the results were run.

public DateTime Runtime { get; }

Property Value

DateTime

ShortLosses

Gets the number of short trades that were losers.

public int ShortLosses { get; }

Property Value

int

ShortLossesPnL

Gets the total P&L of all losing short trades.

public double ShortLossesPnL { get; }

Property Value

double

ShortPercentProfitable

Gets the percentage of profitable short trades.

public double ShortPercentProfitable { get; }

Property Value

double

ShortPnL

Gets the total P&L of all short trades.

public double ShortPnL { get; }

Property Value

double

ShortTrades

Gets the number of short trades.

public int ShortTrades { get; }

Property Value

int

ShortWins

Gets the number of winning short trades.

public int ShortWins { get; }

Property Value

int

ShortWinsPnL

Gets the total P&L of all winning short trades.

public double ShortWinsPnL { get; }

Property Value

double

Slippage

Gets the total amount of slippage incurred.

public double Slippage { get; }

Property Value

double

StdDevTradePercent

Gets the standard deviation of all trades' percent returns.

public double StdDevTradePercent { get; }

Property Value

double

StopsHit

Gets or sets the number of closed positions for which the last exit was a stop order type. Can be useful in optimizations to see how many times a stop-loss is responsible for closing out a position. This can also be set by the user if more nuanced calculations are required.

public int StopsHit { get; set; }

Property Value

int

StopsHitPct

Gets the number of stop exits as a percent of total trades.

public double StopsHitPct { get; }

Property Value

double

StrategyNames

Gets/sets the name(s) of the strategy.

public string StrategyNames { get; set; }

Property Value

string

Symbols

Gets the symbol(s) on which the simulation was run.

public string Symbols { get; }

Property Value

string

TStat

Gets the t-statistic using percent return of the trades.

public double TStat { get; }

Property Value

double

TWR

Gets Terminal Wealth Relative, or the total gain from compounding all trades.

public double TWR { get; }

Property Value

double

Tag

Gets/sets a convenience field for the user to store any arbitrary object. Note this property is not serialized.

[ProtoIgnore]
public object Tag { get; set; }

Property Value

object

TargetsHit

Gets/sets the number of closed positions for which the last exit was a limit order type. Can be useful in optimizations to see how many trades were exited on a target. This can also be set by the user if more nuanced calculations are required.

public int TargetsHit { get; }

Property Value

int

TargetsHitPct

Gets the number of limit exits as a percent of total trades.

public double TargetsHitPct { get; }

Property Value

double

TotalContractsTraded

Gets the total number of contracts/shares in round turns. (i.e. entry side only).

public double TotalContractsTraded { get; }

Property Value

double

Trades

Gets the total number of trades.

public int Trades { get; }

Property Value

int

Turnover

Gets turnover.

public double Turnover { get; }

Property Value

double

UnchangedTrades

Gets the total number of unchanged trades.

public int UnchangedTrades { get; }

Property Value

int

WinningPnL

Gets the total P&L of all winning trades.

public double WinningPnL { get; }

Property Value

double

WinningTrades

Gets the number of winning trades.

public int WinningTrades { get; }

Property Value

int

Methods

Calculate(IEnumerable<Position>)

Calculates trade stats based on the specified list of positions.

public void Calculate(IEnumerable<Position> positions)

Parameters

positions IEnumerable<Position>

Equals(TradeStats, TradeStats, double, bool, bool)

Returns true if all deterministic properties are equal.

public static bool Equals(TradeStats left, TradeStats right, double tolerance = 1E-10, bool strict = true, bool reportDifferences = false)

Parameters

left TradeStats
right TradeStats
tolerance double
strict bool

When true, types must be identical. When false, the union of properties will be compared. This allows for different or derived TradeStat types to be compared.

reportDifferences bool

Output differences to console.

Returns

bool

GetSummary()

Returns a summary presentation of the trade stats suitable for printing to the console.

public string GetSummary()

Returns

string

OnCalculate(IEnumerable<Position>)

Trade stats calculations take place here.

protected virtual void OnCalculate(IEnumerable<Position> trades)

Parameters

trades IEnumerable<Position>

ToString()

Returns a string representation of the trade stats.

public override string ToString()

Returns

string