EmulatedPortfolio

StockSharp.MatchingEngine

Emulated portfolio implementation that tracks positions and money in-memory.

Constructors

EmulatedPortfolio
public EmulatedPortfolio(string name, IMarkPrices markPrices)
emulatedPortfolio = EmulatedPortfolio(name, markPrices)

Initializes a new instance.

name
Portfolio name.
markPrices
Where the prices to revalue open positions come from.

Properties

AvailableMoney
public decimal AvailableMoney { get; }
value = emulatedPortfolio.AvailableMoney

Available money (current - blocked).

BeginMoney
public decimal BeginMoney { get; }
value = emulatedPortfolio.BeginMoney

Initial money amount.

BlockedMoney
public decimal BlockedMoney { get; }
value = emulatedPortfolio.BlockedMoney

Blocked money for pending orders.

Commission
public decimal Commission { get; }
value = emulatedPortfolio.Commission

Total commission paid.

CurrentMoney
public decimal CurrentMoney { get; }
value = emulatedPortfolio.CurrentMoney

Current money (begin + PnL).

EnableStopOut
public bool EnableStopOut { get; set; }
value = emulatedPortfolio.EnableStopOut
emulatedPortfolio.EnableStopOut = value

Enable automatic position liquidation on stop-out.

MarginCallLevel
public decimal MarginCallLevel { get; set; }
value = emulatedPortfolio.MarginCallLevel
emulatedPortfolio.MarginCallLevel = value

Margin call level threshold. When margin level falls to this value, a warning is triggered.

Name
public string Name { get; }
value = emulatedPortfolio.Name

Portfolio name.

RealizedPnL
public decimal RealizedPnL { get; }
value = emulatedPortfolio.RealizedPnL

Total realized PnL.

StopOutLevel
public decimal StopOutLevel { get; set; }
value = emulatedPortfolio.StopOutLevel
emulatedPortfolio.StopOutLevel = value

Stop-out level threshold. When margin level falls to this value, positions are liquidated.

TotalPnL
public decimal TotalPnL { get; }
value = emulatedPortfolio.TotalPnL

Total PnL (realized - commission).

UnrealizedPnL
public decimal UnrealizedPnL { get; }
value = emulatedPortfolio.UnrealizedPnL

What the open positions have gained or lost since they were taken, at the prices they could be closed at now. A position the market has not priced counts for nothing.

Version
public long Version { get; private set; }
value = emulatedPortfolio.Version
emulatedPortfolio.Version = value

Changes whenever an order, a fill or the opening state changes the account, so a report on it can be told from a newer one.

Methods

CalculateUnrealizedPnL
public decimal CalculateUnrealizedPnL(Func<SecurityId, decimal?> getCurrentPrice)
result = emulatedPortfolio.CalculateUnrealizedPnL(getCurrentPrice)

Calculate unrealized PnL across all positions.

getCurrentPrice
Function to get current market price for a security. Returns null if price unavailable.

Returns: Total unrealized PnL.

Clear
internal void Clear()
emulatedPortfolio.Clear()

Clear all state (used by Reset).

GetAllPositions
public IEnumerable<PositionInfo> GetAllPositions()
result = emulatedPortfolio.GetAllPositions()

Get all position info objects.

GetPosition
public PositionInfo GetPosition(SecurityId securityId)
result = emulatedPortfolio.GetPosition(securityId)

Get position for security.

securityId
Security ID.

Returns: Position info or null.

GetPositions
public IEnumerable<ValueTuple<SecurityId, decimal, decimal>> GetPositions()
result = emulatedPortfolio.GetPositions()

Get all positions.

Returns: Enumeration of positions.

GetUnrealizedPnL
public decimal? GetUnrealizedPnL(PositionInfo position)
result = emulatedPortfolio.GetUnrealizedPnL(position)

What one position has gained or lost since it was taken, at the price it could be closed at now.

position
A position of this portfolio.

Returns: The floating profit, zero for a flat position, or when the market has not priced an open one.

GetUnrealizedPnL
public decimal? GetUnrealizedPnL(SecurityId securityId)
result = emulatedPortfolio.GetUnrealizedPnL(securityId)

What one position has gained or lost since it was taken, at the price it could be closed at now.

securityId
Security ID.

Returns: The floating profit, zero for a flat position, or when the market has not priced an open one.

ProcessOrderCancellation
public void ProcessOrderCancellation(SecurityId securityId, Sides side, decimal volume, decimal price)
emulatedPortfolio.ProcessOrderCancellation(securityId, side, volume, price)

Process order cancellation (unblock funds).

securityId
Security ID.
side
Order side.
volume
Cancelled volume.
price
Price used for margin calculation.
ProcessOrderRegistration
public void ProcessOrderRegistration(SecurityId securityId, Sides side, decimal volume, decimal price)
emulatedPortfolio.ProcessOrderRegistration(securityId, side, volume, price)

Process order registration (block funds).

securityId
Security ID.
side
Order side.
volume
Order volume.
price
Order price for margin calculation.
ProcessTrade
public TradeProcessingResult ProcessTrade(SecurityId securityId, Sides side, decimal price, decimal volume, decimal? commission, decimal? marginPrice)
result = emulatedPortfolio.ProcessTrade(securityId, side, price, volume, commission, marginPrice)

Process a trade execution.

securityId
Security ID.
side
Trade side.
price
Trade price.
volume
Trade volume.
commission
Commission amount.
marginPrice
Price at which the filled order reserved funds.

Returns: Trade processing result.

SetMoney
public void SetMoney(decimal money)
emulatedPortfolio.SetMoney(money)

Set initial money.

money
Money amount.
SetPosition
public void SetPosition(SecurityId securityId, decimal volume, decimal avgPrice)
emulatedPortfolio.SetPosition(securityId, volume, avgPrice)

Set initial position.

securityId
Security ID.
volume
Position volume.
avgPrice
Average entry price.