StrategyPositionManager

StockSharp.Algo.Strategies

Manages strategy positions (per Security + Portfolio) and calculates position quantity, average price, realized PnL and commission incrementally from order executions. Additionally maintains cached per-position aggregates (blocked volume and active buy/sell orders count) via incremental updates (O(1) per order update, no rescans).

Constructors

StrategyPositionManager
public StrategyPositionManager(Func<string> strategyIdGetter)
strategyPositionManager = StrategyPositionManager(strategyIdGetter)

Manages strategy positions (per Security + Portfolio) and calculates position quantity, average price, realized PnL and commission incrementally from order executions. Additionally maintains cached per-position aggregates (blocked volume and active buy/sell orders count) via incremental updates (O(1) per order update, no rescans).

strategyIdGetter
Delegate returning strategy identifier to stamp into newly created StrategyId.

Properties

Positions
public Position[] Positions { get; }
value = strategyPositionManager.Positions

Snapshot array of managed positions (thread-safe copy).

StrategyIdGetter
public Func<string> StrategyIdGetter { get; }
value = strategyPositionManager.StrategyIdGetter

Delegate returning strategy id to assign into new positions.

TrackedAggsCount
public int TrackedAggsCount { get; }
value = strategyPositionManager.TrackedAggsCount
TrackedFinishedOrdersCount
public int TrackedFinishedOrdersCount { get; }
value = strategyPositionManager.TrackedFinishedOrdersCount

Number of terminal order states retained for replay detection.

TrackedOrderExecInfosCount
public int TrackedOrderExecInfosCount { get; }
value = strategyPositionManager.TrackedOrderExecInfosCount
TrackedOrderTracksCount
public int TrackedOrderTracksCount { get; }
value = strategyPositionManager.TrackedOrderTracksCount

Methods

IsProblem
public static bool IsProblem(OrderResults result)
result = StrategyPositionManager.IsProblem(result)

Whether the result reports a problem rather than ordinary order flow. A snapshot of an order that is not active yet, one for an order with no position, and a repeat of a final snapshot are all expected traffic; a fill going backwards or being dropped is not.

result
Processing result.

Returns: Check result.

ProcessOrder
public OrderResults ProcessOrder(Order order)
result = strategyPositionManager.ProcessOrder(order)

Process order state change (registration, balance change, partial/full execution, cancellation, done).

order
Order to process.

Returns: OrderResults

RemoveFinishedBefore
public void RemoveFinishedBefore(DateTime time)
strategyPositionManager.RemoveFinishedBefore(time)

Remove terminal order states older than .

time
Minimum order time to keep.
Reset
public void Reset()
strategyPositionManager.Reset()

Reset all internal caches (positions, execution info, aggregates, order tracks).

SetPosition
public void SetPosition(Security security, Portfolio portfolio, decimal value, DateTime time)
strategyPositionManager.SetPosition(security, portfolio, value, time)

Set current position value explicitly (utility for manual restoration / overrides).

security
Security.
portfolio
Portfolio.
value
New signed quantity.
time
Timestamp to assign into LocalTime and ServerTime if position is created anew.
TryGetPosition
public Position TryGetPosition(Security security, Portfolio portfolio)
result = strategyPositionManager.TryGetPosition(security, portfolio)

Try get existing position instance for and .

security
Security.
portfolio
Portfolio.

Returns: Existing Position or .

UpdateAggregates
private void UpdateAggregates(Order order, Position position)
strategyPositionManager.UpdateAggregates(order, position)

Incrementally update aggregates for the order (blocked volume and counts) and push them into the .

UpdateCurrentPrice
public void UpdateCurrentPrice(SecurityId secId, decimal price, DateTime serverTime, DateTime localTime)
strategyPositionManager.UpdateCurrentPrice(secId, price, serverTime, localTime)

Update current (market) price for all positions of the specified .

secId
Security identifier whose positions need price update.
price
New market price.
serverTime
Server time of the price snapshot.
localTime
Local time when the price was processed.

Events

PositionProcessed
public event Action<Position, bool> PositionProcessed
strategyPositionManager.PositionProcessed += handler

Occurs after position was processed (created or updated by an order execution or order state change affecting aggregates).