Search Framework:
PositionList
Namespace: WealthLab.Backtest
Parent: List<Position>

PositionList is a List<Position> with additional utility properties for summarizing position performance and a method for generating an equity curve from the Positions it contains.

Members
AvgBarsHeld
public double AvgBarsHeld

Returns the average number of bars held by the Positions in the List. Returns Double.NaN if the PositionList is empty. The value is calculated from each Position's BarsHeld property.


AvgBarsHeldAsPctOfHistory
public double AvgBarsHeldAsPctOfHistory

Returns the average number of bars held as a percentage of each Position's BarHistory length. For each Position, the calculation is: BarsHeld � 100 / Bars.Count The results are then averaged across all Positions. Returns Double.NaN if the PositionList is empty.


AvgProfit
public double AvgProfit

Returns the average Profit of the Positions in the List. Returns Double.NaN if the PositionList is empty.


AvgProfitPct
public double AvgProfitPct

Returns the average ProfitPercent of the Positions in the List. Returns Double.NaN if the PositionList is empty.


GenerateEquityCurve
public TimeSeries GenerateEquityCurve(double startCap = 0.0)

Generates and returns a TimeSeries representing the combined equity curve of the Positions in the List. The optional startCap parameter specifies the starting capital. The default value is zero. The method excludes Positions whose NSF property is true, sorts the remaining Positions by EntryDate, and synchronizes their BarHistory instances so that equity can be calculated chronologically across multiple symbols. When a Position enters, its CostBasis is removed from available equity. While the Position remains open, its current value is included using Position.ValueAsOf. When the Position exits, its closed value is returned to equity. Returns an empty TimeSeries if the PositionList contains no Positions.

Example Code
using WealthLab.Backtest;
using WealthLab.Core;
using WealthLab.Indicators;
namespace WealthScript
{
    public class PositionListEquityCurveExample : UserStrategyBase
    {
        private SMA _sma;
        public override void Initialize(BarHistory bars)
        {
            StartIndex = 20;
            _sma = SMA.Series(bars.Close, 20);
            PlotIndicator(_sma);
        }
        public override void Execute(BarHistory bars, int idx)
        {
            if (!HasOpenPosition(bars, PositionType.Long))
            {
                if (bars.Close.CrossesOver(_sma, idx))
                    PlaceTrade(
                        bars,
                        TransactionType.Buy,
                        OrderType.Market);
            }
            else
            {
                if (bars.Close.CrossesUnder(_sma, idx))
                    ClosePosition(
                        LastPosition,
                        OrderType.Market);
            }
        }
        public override void BacktestComplete()
        {
            PositionList positions = new PositionList();
            foreach (Position position in GetPositionsAllSymbols())
                positions.Add(position);
            TimeSeries equity =
                positions.GenerateEquityCurve(100000.0);
            WriteToDebugLog(
                "Generated equity curve points: " +
                equity.Count);
        }
    }
}

Profit
public double Profit

Returns the total Profit of all Positions in the List. Returns zero if the PositionList is empty.


ProfitPct
public double ProfitPct

Returns the total profit as a percentage of TotalSize. The calculation is: Profit � 100 / TotalSize Returns zero if TotalSize is zero.


TotalSize
public double TotalSize

Returns the combined current size of all Positions in the List. For each Position, the method adds: CostBasis + Profit The results are summed across all Positions.