Diese Seite ist noch nicht in Ihrer Sprache verfügbar – es wird eine andere Version angezeigt.

Statistics Reference

The StatisticManager manages a collection of IStatisticParameter instances. Each parameter tracks a specific metric during strategy execution. All available parameters are created using the StatisticParameterRegistry.

For a general overview of working with strategy statistics, see the Statistics section.

Interfaces

The statistics system is built on a hierarchy of interfaces. Each interface defines the data source for calculating the parameter:

Interface Description
IStatisticParameter Base interface: properties Name, Type, Value, DisplayName, Description, Category, Order; method Reset()
IPnLStatisticParameter Parameters based on profit/loss: method Add(marketTime, pnl, commission)
ITradeStatisticParameter Parameters based on trades: method Add(PnLInfo)
IOrderStatisticParameter Parameters based on orders: methods New(order), Changed(order), RegisterFailed(fail), CancelFailed(fail)
IPositionStatisticParameter Parameters based on positions: method Add(marketTime, position)
IRiskFreeRateStatisticParameter Parameters with risk-free rate: property RiskFreeRate
IBeginValueStatisticParameter Parameters with initial value: property BeginValue

Profit and Loss (P&L) Parameters

All parameters in this group implement the IPnLStatisticParameter interface and inherit from BasePnLStatisticParameter. They receive data on each update of the strategy's P&L value.

Class Description Value Type
NetProfitParameter Net profit for the entire period. Set equal to the current P&L value decimal
NetProfitPercentParameter Net profit as a percentage. Requires setting BeginValue (initial capital). Formula: pnl * 100 / BeginValue decimal
MaxProfitParameter Maximum profit (highest P&L value over the entire period) decimal
MaxProfitPercentParameter Maximum profit as a percentage. Requires BeginValue. Formula: MaxProfit * 100 / BeginValue decimal
MaxProfitDateParameter Date when maximum profit was reached DateTime
MaxDrawdownParameter Maximum absolute drawdown. Difference between the peak and trough of the equity curve decimal
MaxDrawdownPercentParameter Maximum drawdown as a percentage. Formula: MaxDrawdown * 100 / MaxEquity decimal
MaxDrawdownDateParameter Date of maximum drawdown DateTime
MaxRelativeDrawdownParameter Maximum relative drawdown. Calculated as the ratio of drawdown to peak equity value decimal
ReturnParameter Relative return for the entire period. Maximum growth from trough to current value in relative terms decimal
CommissionParameter Total commission paid. Accumulates all commission values decimal
AverageDrawdownParameter Average drawdown. Arithmetic mean of all completed and current drawdowns decimal
RecoveryFactorParameter Recovery factor. Formula: NetProfit / MaxDrawdown decimal
SharpeRatioParameter Sharpe ratio. Formula: (annualized return - risk-free rate) / annualized standard deviation decimal
SortinoRatioParameter Sortino ratio. Similar to Sharpe, but only accounts for downside deviations decimal
CalmarRatioParameter Calmar ratio. Formula: NetProfit / MaxDrawdown decimal
SterlingRatioParameter Sterling ratio. Formula: NetProfit / AverageDrawdown decimal

Risk Coefficients

The SharpeRatioParameter and SortinoRatioParameter inherit from the base class RiskAdjustedRatioParameter and implement the IRiskFreeRateStatisticParameter interface.

They support the following settings:

  • RiskFreeRate -- annual risk-free rate (e.g., 0.03m = 3%)
  • Period -- return calculation period (default TimeSpan.FromDays(1))

The CalmarRatioParameter and SterlingRatioParameter depend on other parameters (NetProfitParameter, MaxDrawdownParameter, AverageDrawdownParameter) and are automatically linked when created via StatisticParameterRegistry.

Trade Parameters

All parameters in this group implement the ITradeStatisticParameter interface. They receive data through the PnLInfo object for each trade executed.

Class Description Value Type
TradeCountParameter Total number of trades (only trades with ClosedVolume > 0 are counted) int
WinningTradesParameter Number of profitable trades (ClosedVolume > 0 and PnL > 0) int
LossingTradesParameter Number of losing trades (ClosedVolume > 0 and PnL < 0) int
RoundtripCountParameter Number of completed round-trips (closing trades with ClosedVolume > 0) int
AverageTradeProfitParameter Average profit per trade. Formula: SumPnL / Count decimal
AverageWinTradeParameter Average profit of profitable trades. Only trades with PnL > 0 are considered decimal
AverageLossTradeParameter Average loss of losing trades. Only trades with PnL < 0 are considered decimal
ProfitFactorParameter Profit factor. Formula: GrossProfit / GrossLoss decimal
ExpectancyParameter Mathematical expectancy. Formula: P(win) * AvgWin + P(loss) * AvgLoss decimal
PerMonthTradeParameter Average number of trades per month decimal
PerDayTradeParameter Average number of trades per day decimal
GrossProfitParameter Gross profit. Sum of P&L of all profitable trades (PnL > 0) decimal
GrossLossParameter Gross loss. Sum of P&L of all losing trades (PnL < 0, value is negative) decimal

Position Parameters

Parameters in this group implement the IPositionStatisticParameter interface. They receive data on each position change.

Class Description Value Type
MaxLongPositionParameter Maximum long position. Highest positive position value decimal
MaxShortPositionParameter Maximum short position. Highest absolute negative position value decimal

Order Parameters

All parameters in this group implement the IOrderStatisticParameter interface and inherit from BaseOrderStatisticParameter. They receive data on order registration, changes, and errors.

Class Description Value Type
OrderCountParameter Total number of registered orders int
OrderRegisterErrorCountParameter Number of order registration errors int
OrderInsufficientFundErrorCountParameter Number of "insufficient funds" errors (type InsufficientFundException) int
OrderCancelErrorCountParameter Number of order cancellation errors int

Latency Parameters

Latency parameters also implement IOrderStatisticParameter, but track the time characteristics of order processing.

Class Description Value Type
MaxLatencyRegistrationParameter Maximum order registration latency (Order.LatencyRegistration property) TimeSpan
MinLatencyRegistrationParameter Minimum order registration latency TimeSpan
MaxLatencyCancellationParameter Maximum order cancellation latency (Order.LatencyCancellation property) TimeSpan
MinLatencyCancellationParameter Minimum order cancellation latency TimeSpan

Usage

Accessing Strategy Statistics

var strategy = new MyStrategy();

// Access statistics after execution
foreach (var param in strategy.StatisticManager.Parameters)
{
    Console.WriteLine($"{param.DisplayName}: {param.Value}");
}

Retrieving a Specific Parameter

// Get the net profit value
var netProfit = strategy.StatisticManager.Parameters
    .OfType<NetProfitParameter>()
    .First();

Console.WriteLine($"Net profit: {netProfit.Value}");

Configuring the Risk-Free Rate for Coefficients

The Sharpe and Sortino ratios require setting the risk-free rate for correct calculation:

// Set a 3% risk-free rate for all coefficients
foreach (var param in strategy.StatisticManager.Parameters
    .OfType<IRiskFreeRateStatisticParameter>())
{
    param.RiskFreeRate = 0.03m;
}

Configuring Initial Capital for Percentage Parameters

The NetProfitPercentParameter and MaxProfitPercentParameter parameters require setting the initial capital value:

// Set initial capital for percentage calculations
foreach (var param in strategy.StatisticManager.Parameters
    .OfType<IBeginValueStatisticParameter>())
{
    param.BeginValue = 1_000_000m; // 1,000,000
}

Resetting Statistics

// Reset all statistic parameters
strategy.StatisticManager.Reset();

Saving and Loading State

All parameters support serialization via the IPersistable interface:

// Saving
var storage = new SettingsStorage();
strategy.StatisticManager.Save(storage);

// Loading
strategy.StatisticManager.Load(storage);

See Also