Files
mika@stocksharp.com 1cb1c5487c Let the engine own its portfolio book
IPortfolio and IPortfolioManager had one implementation each, and substitution
through them did not work: the adapter narrowed the manager back to the concrete
type before wiring it, so a foreign one got neither a margin controller nor a price
source and could compute neither margin nor unrealized result. An interface that
cannot be implemented by anything else is a contract with itself.

The adapter now holds an EmulatedPortfolioManager it creates and never replaces,
wired in the constructor, and the narrowing step is gone with the interfaces.
PositionInfo and TradeProcessingResult, which shared the interfaces' file, get files
of their own; the documentation the interfaces carried moves onto the members that
implement it.

TryGetPortfolio is added beside GetPortfolio, which creates on a miss - so asking
whether the book holds an account was writing to it. A caller that wants to read an
account it may not have now has a lookup that answers without opening one.

One test comes with them. ValidateFunds answers "no objection" for an account the
book has never heard of, which reads like a hole: with the money check on, an order
naming an unknown account would be paid for by nobody. It is not one - the register
path opens the account before the funds check runs, so the check sees it holding zero
and refuses the order - and now something says so.
2026-08-24 02:37:31 +03:00

55 lines
2.5 KiB
C#

namespace StockSharp.MatchingEngine;
/// <summary>
/// Interface for controlling margin trading logic.
/// Leverage is per-position (<see cref="PositionInfo.Leverage"/>).
/// Margin call/stop-out levels are per-portfolio (<see cref="EmulatedPortfolio.MarginCallLevel"/>, <see cref="EmulatedPortfolio.StopOutLevel"/>).
/// </summary>
public interface IMarginController
{
/// <summary>
/// Calculate required margin for an order, using position-level leverage.
/// </summary>
/// <param name="price">Order price.</param>
/// <param name="volume">Order volume.</param>
/// <param name="position">Position info (for leverage). Can be null (uses default leverage 1).</param>
/// <returns>Required margin amount.</returns>
decimal GetRequiredMargin(decimal price, decimal volume, PositionInfo position);
/// <summary>
/// Validate that portfolio has sufficient funds for order.
/// </summary>
/// <param name="portfolio">Portfolio to check.</param>
/// <param name="price">Order price.</param>
/// <param name="volume">Order volume.</param>
/// <param name="position">Position info (for leverage). Can be null.</param>
/// <returns>Error if insufficient funds, null otherwise.</returns>
InvalidOperationException ValidateOrder(EmulatedPortfolio portfolio, decimal price, decimal volume, PositionInfo position);
/// <summary>
/// Calculate current margin level for a portfolio.
/// </summary>
/// <param name="portfolio">Portfolio.</param>
/// <param name="unrealizedPnL">Current unrealized PnL.</param>
/// <returns>Margin level ratio (equity / blocked). Returns <see cref="decimal.MaxValue"/> if no blocked money.</returns>
decimal CheckMarginLevel(EmulatedPortfolio portfolio, decimal unrealizedPnL);
/// <summary>
/// Check if portfolio is in margin call state.
/// Uses <see cref="EmulatedPortfolio.MarginCallLevel"/>.
/// </summary>
/// <param name="portfolio">Portfolio.</param>
/// <param name="unrealizedPnL">Current unrealized PnL.</param>
/// <returns>True if margin level is at or below margin call threshold.</returns>
bool IsMarginCall(EmulatedPortfolio portfolio, decimal unrealizedPnL);
/// <summary>
/// Check if portfolio is in stop-out state.
/// Uses <see cref="EmulatedPortfolio.EnableStopOut"/> and <see cref="EmulatedPortfolio.StopOutLevel"/>.
/// </summary>
/// <param name="portfolio">Portfolio.</param>
/// <param name="unrealizedPnL">Current unrealized PnL.</param>
/// <returns>True if stop-out is enabled and margin level is at or below stop-out threshold.</returns>
bool IsStopOut(EmulatedPortfolio portfolio, decimal unrealizedPnL);
}