mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-04 08:32:23 +00:00
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.
55 lines
1.7 KiB
C#
55 lines
1.7 KiB
C#
namespace StockSharp.MatchingEngine;
|
|
|
|
/// <summary>
|
|
/// Default implementation of <see cref="IMarginController"/>.
|
|
/// Leverage is taken from <see cref="PositionInfo.Leverage"/> (default 1).
|
|
/// Margin call/stop-out levels are taken from <see cref="EmulatedPortfolio"/>.
|
|
/// </summary>
|
|
public class MarginController : IMarginController
|
|
{
|
|
/// <inheritdoc />
|
|
public decimal GetRequiredMargin(decimal price, decimal volume, PositionInfo position)
|
|
{
|
|
var leverage = position?.Leverage ?? 1m;
|
|
if (leverage < 1)
|
|
leverage = 1;
|
|
|
|
return price * volume / leverage;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public InvalidOperationException ValidateOrder(EmulatedPortfolio portfolio, decimal price, decimal volume, PositionInfo position)
|
|
{
|
|
if (portfolio is null)
|
|
throw new ArgumentNullException(nameof(portfolio));
|
|
|
|
var needMoney = GetRequiredMargin(price, volume, position);
|
|
|
|
if (portfolio.AvailableMoney < needMoney)
|
|
return new InvalidOperationException($"Insufficient funds: need {needMoney}, available {portfolio.AvailableMoney}");
|
|
|
|
return null;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public decimal CheckMarginLevel(EmulatedPortfolio portfolio, decimal unrealizedPnL)
|
|
{
|
|
if (portfolio is null)
|
|
throw new ArgumentNullException(nameof(portfolio));
|
|
|
|
if (portfolio.BlockedMoney <= 0)
|
|
return decimal.MaxValue;
|
|
|
|
var equity = portfolio.CurrentMoney + unrealizedPnL;
|
|
return equity / portfolio.BlockedMoney;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public bool IsMarginCall(EmulatedPortfolio portfolio, decimal unrealizedPnL)
|
|
=> CheckMarginLevel(portfolio, unrealizedPnL) <= portfolio.MarginCallLevel;
|
|
|
|
/// <inheritdoc />
|
|
public bool IsStopOut(EmulatedPortfolio portfolio, decimal unrealizedPnL)
|
|
=> portfolio.EnableStopOut && CheckMarginLevel(portfolio, unrealizedPnL) <= portfolio.StopOutLevel;
|
|
}
|