Framework Reference
WEALTHLAB FRAMEWORK / CLASS REFERENCE

PositionSize

API documentation, members, signatures, and C# examples.

Search the framework

Find classes, members, and documentation.

NamespaceWealthLab.Backtest
Parent
Object

Represents the position sizing settings established for a backtest. PositionSize defines the sizing method and amount, starting capital, margin factor, maximum open positions, maximum entry signals per bar, and optional advanced PositionSizer. It also contains the settings used when optimizing position sizing parameters.

Basis Price

1

UseNextBarOpenAsBasisPrice

public bool UseNextBarOpenAsBasisPrice

Determines how the basis price used for position sizing is established. When false, which is the default, the basis price is based on the closing price of the signal bar. If the market gaps on the following bar, the actual cost of the trade can exceed the amount available and potentially cause the simulated trade to receive NSF status. When true, the basis price is based on the open price of the following bar. This can avoid NSF results caused by opening gaps, but assumes that the execution environment can accommodate the resulting position size.

Constructors

1

PositionSize

public PositionSize()
public PositionSize(
PositionSizeType pst,
double amt)

The parameterless constructor creates a PositionSize configured to use 5% of equity, $100,000 starting capital, and a margin factor of 1.0. The second constructor creates a PositionSize using the specified pst sizing method and amt amount. StartingCapital defaults to $100,000 and MarginFactor to 1.0.

Object Members

5

Equals

public override bool Equals(object obj)

Returns true if obj is a PositionSize whose primary backtest sizing settings are equal to this instance. The comparison includes StartingCapital, PositionSizeType, Amount, MarginFactor, UseNextBarOpenAsBasisPrice, and MaxOpenPositions.

GetHashCode

public override int GetHashCode()

Returns a hash code for the PositionSize based on its string representation.

operator !=

public static bool operator !=(
PositionSize ps1,
PositionSize ps2)

Returns true if the two PositionSize instances do not have equal primary sizing settings.

operator ==

public static bool operator ==(
PositionSize ps1,
PositionSize ps2)

Returns true if the two PositionSize instances have equal primary sizing settings. The comparison includes StartingCapital, PositionSizeType, Amount, MarginFactor, UseNextBarOpenAsBasisPrice, and MaxOpenPositions. The operator also handles null PositionSize references.

ToString

public override string ToString()

Returns a concise description of the PositionSize suitable for display or debugging. The format depends on PositionSizeType. Examples include:

  • Value=5,000.00
  • Shares=100
  • % Equity=5.00%
  • Max Risk=2.00%
  • PosSizer=Position Sizer Name If MaxSignalsPerBar is greater than zero, the maximum entry count is appended to the string.

Optimization

16

AmountOpt

public bool AmountOpt

Gets or sets whether the position sizing Amount is enabled for optimization. The optimization range used depends on PositionSizeType.

ApplyValueFromParameter

public void ApplyValueFromParameter(
int tag,
Parameter p,
bool assignValue)

Applies the optimization settings represented by Parameter p to one of the built-in PositionSize parameters. The tag identifies the PositionSize parameter:

  • 0 - StartingCapital
  • 1 - Amount
  • 2 - MarginFactor
  • 3 - MaxOpenPositions When assignValue is true, the Parameter's current value is also assigned to the corresponding PositionSize property.

ApplyValuesFrom

public void ApplyValuesFrom(ParameterList pl)

Applies optimized values from the specified ParameterList to this PositionSize. Built-in PositionSize parameters are identified by their PositionSize optimization tags. When an advanced PositionSizer is being used, matching optimized PositionSizer Parameters are also updated.

FixedAmountOptValues

public Steppable FixedAmountOptValues

Gets or sets the optimization range used for Amount when PositionSizeType is PositionSizeType.Dollar. The default range is 1,000 through 10,000 in steps of 1,000.

FixedQtyOptValues

public Steppable FixedQtyOptValues

Gets or sets the optimization range used for Amount when PositionSizeType is PositionSizeType.Quantity. The default range is 100 through 1,000 in steps of 100.

GetOptimizableParameter

public Parameter GetOptimizableParameter(int tag)

Returns a Parameter representing one of the built-in optimizable PositionSize settings. The tag values are:

  • 0 - StartingCapital
  • 1 - Amount
  • 2 - MarginFactor
  • 3 - MaxOpenPositions The returned Parameter contains the appropriate current value, optimization range, and optimization state.

GetOptimizableParameters

public ParameterList GetOptimizableParameters()

Returns a ParameterList containing the PositionSize parameters currently enabled for optimization. If PositionSizeType is PositionSizeType.PosSizer, optimizable Parameters belonging to the advanced PositionSizer are also included.

IsOptimizing

public bool IsOptimizing

Returns true if any PositionSize parameter is enabled for optimization. This includes StartingCapOpt, AmountOpt, MarginOpt, MaxOpenPosOpt, or an optimizable Parameter belonging to an advanced PositionSizer.

MarginOpt

public bool MarginOpt

Gets or sets whether MarginFactor is enabled for optimization.

MarginOptValues

public Steppable MarginOptValues

Gets or sets the optimization range for MarginFactor. The default range is 1.0 through 2.0 in steps of 0.1.

MaxOpenPosOpt

public bool MaxOpenPosOpt

Gets or sets whether MaxOpenPositions is enabled for optimization.

MaxOpenPosOptValues

public Steppable MaxOpenPosOptValues

Gets or sets the optimization range for MaxOpenPositions. The default range is 1 through 1,000 in steps of 1.

MaxRiskOptValues

public Steppable MaxRiskOptValues

Gets or sets the optimization range used for Amount when PositionSizeType is PositionSizeType.MaxRiskPct. The default range is 5% through 50% in steps of 5%.

PctEquityOptValues

public Steppable PctEquityOptValues

Gets or sets the optimization range used for Amount when PositionSizeType is PositionSizeType.PctOfEquity. The default range is 1% through 20% in steps of 1%.

StartingCapOpt

public bool StartingCapOpt

Gets or sets whether StartingCapital is enabled for optimization.

StartingCapOptValues

public Steppable StartingCapOptValues

Gets or sets the optimization range for StartingCapital. The default range is 50,000 through 200,000 in steps of 10,000.

Persistence

3

Clone

public PositionSize Clone()

Returns a new PositionSize containing a copy of the current settings. When using an advanced PositionSizer, the PositionSizer and its Parameters are also cloned. Optimization settings and their Steppable ranges are copied to the new instance.

Parse

public static PositionSize Parse(string s)

Creates and returns a PositionSize by parsing the persisted representation specified in s. The method also supports legacy PositionSize persistence formats. If a persisted advanced PositionSizer cannot be created, PositionSizeType is changed to PositionSizeType.Dollar.

Persist

public string Persist()

Returns a string containing a serialized representation of the PositionSize settings. The persisted data includes the sizing type and amount, starting capital, margin factor, advanced PositionSizer information when applicable, maximum open Positions, basis-price setting, optimization settings, maximum Signals per bar, and optimization ranges.

Position Limits

3

HasMaxOpen

public bool HasMaxOpen

Returns true when MaxOpenPositions is greater than zero.

MaxOpenPositions

public int MaxOpenPositions

Gets or sets the maximum number of Positions that can be open simultaneously during the backtest. A value of zero indicates no limit.

MaxSignalsPerBar

public int MaxSignalsPerBar

Gets or sets the maximum number of entry Signals that can be generated on a single bar. A value of zero indicates no limit.

Position Sizing

7

Amount

public double Amount

Gets or sets the amount used by the position sizing method. The meaning of Amount depends on PositionSizeType:

  • Dollar - Fixed currency amount.
  • Quantity - Fixed number of shares or contracts.
  • PctOfEquity - Percentage of current simulated equity.
  • MaxRiskPct - Percentage of current simulated equity to risk based on the Strategy's risk stop level.
  • PosSizer - Position sizing is controlled by the PositionSizer instance.

MarginFactor

public double MarginFactor

Gets or sets the margin factor used by the backtest. The value must be at least 1.0. Attempts to assign a smaller value are ignored.

PositionSizer

public PositionSizerBase PositionSizer

Gets or sets the PositionSizerBase instance used when PositionSizeType is PositionSizeType.PosSizer.

PositionSizerName

public string PositionSizerName

Gets or sets the name of the advanced PositionSizer. This property is used when serializing PositionSize.

PositionSizerParameters

public string PositionSizerParameters

Gets or sets the serialized ParameterList for the advanced PositionSizer. This property is used when serializing PositionSize.

PositionSizeType

public PositionSizeType PositionSizeType

Gets or sets the position sizing method. Possible PositionSizeType values include:

  • Dollar - Uses a fixed currency amount.
  • Quantity - Uses a fixed number of shares or contracts.
  • PctOfEquity - Uses a percentage of current simulated equity.
  • MaxRiskPct - Risks a percentage of current simulated equity based on the Strategy's risk stop level.
  • PosSizer - Uses a PositionSizerBase instance for advanced position sizing.

StartingCapital

public double StartingCapital

Gets or sets the starting capital for the backtest.

Need help with this API?

Ask the community or explore custom development services.