mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-05 00:51:48 +00:00
The engine had a CheckTradingState setting and nothing behind it: the venue's trading state arrived on Level1 and was dropped, so nothing ever knew an instrument was halted and every order stood as usual. A stand that had switched the check on believed it was protected and was not. The state the venue last stated is now recorded on the security - whatever the setting says, since recording is not refusing - and an order for an instrument that is stopped is refused when the check is on, before anything is asked about money.
127 lines
3.3 KiB
C#
127 lines
3.3 KiB
C#
namespace StockSharp.MatchingEngine;
|
|
|
|
/// <summary>
|
|
/// Per-security state for matching engine.
|
|
/// Contains order book, order manager, and security definition.
|
|
/// </summary>
|
|
public class SecurityState(SecurityId securityId)
|
|
{
|
|
private SecurityMessage _securityDefinition;
|
|
|
|
private bool _priceStepUpdated;
|
|
private bool _volumeStepUpdated;
|
|
private long? _depthSubscription;
|
|
|
|
/// <summary>
|
|
/// Security identifier.
|
|
/// </summary>
|
|
public SecurityId SecurityId { get; } = securityId;
|
|
|
|
/// <summary>
|
|
/// Order book for this security.
|
|
/// </summary>
|
|
public OrderBook OrderBook { get; } = new(securityId);
|
|
|
|
/// <summary>
|
|
/// Active order manager.
|
|
/// </summary>
|
|
public OrderLifecycleManager OrderManager { get; } = new();
|
|
|
|
/// <summary>
|
|
/// Price step from security definition or auto-detected.
|
|
/// </summary>
|
|
public decimal PriceStep => _securityDefinition?.PriceStep ?? 0.01m;
|
|
|
|
/// <summary>
|
|
/// Volume step from security definition or auto-detected.
|
|
/// </summary>
|
|
public decimal VolumeStep => _securityDefinition?.VolumeStep ?? 1m;
|
|
|
|
/// <summary>
|
|
/// Whether depth subscription is active.
|
|
/// </summary>
|
|
public bool HasDepthSubscription => _depthSubscription.HasValue;
|
|
|
|
/// <summary>
|
|
/// Trading state as the venue last stated it, or <see langword="null"/> when it never has.
|
|
/// </summary>
|
|
public SecurityStates? TradingState { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Process security definition message.
|
|
/// </summary>
|
|
public void ProcessSecurity(SecurityMessage msg)
|
|
{
|
|
_securityDefinition = msg;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Record the trading state the venue reports for this security.
|
|
/// </summary>
|
|
/// <param name="state">Trading state.</param>
|
|
public void ProcessTradingState(SecurityStates state)
|
|
{
|
|
TradingState = state;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Process quote change (snapshot) — update order book.
|
|
/// </summary>
|
|
public void ProcessQuoteChange(QuoteChangeMessage msg, List<Message> results)
|
|
{
|
|
if (!_priceStepUpdated || !_volumeStepUpdated)
|
|
{
|
|
var quote = msg.GetBestBid() ?? msg.GetBestAsk();
|
|
if (quote != null)
|
|
UpdateSteps(quote.Value.Price, quote.Value.Volume);
|
|
}
|
|
|
|
if (msg.State is null)
|
|
{
|
|
OrderBook.SetSnapshot(msg.Bids, msg.Asks);
|
|
}
|
|
|
|
if (_depthSubscription.HasValue)
|
|
{
|
|
results.Add(OrderBook.ToMessage(msg.LocalTime, msg.ServerTime));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Process market data subscriptions (depth only).
|
|
/// </summary>
|
|
public void ProcessMarketData(MarketDataMessage msg)
|
|
{
|
|
if (msg.IsSubscribe)
|
|
{
|
|
if (msg.DataType2 == DataType.MarketDepth)
|
|
_depthSubscription = msg.TransactionId;
|
|
}
|
|
else
|
|
{
|
|
if (_depthSubscription == msg.OriginalTransactionId)
|
|
_depthSubscription = null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Auto-detect price/volume step from market data.
|
|
/// </summary>
|
|
public void UpdateSteps(decimal price, decimal? volume)
|
|
{
|
|
if (!_priceStepUpdated && price > 0)
|
|
{
|
|
_securityDefinition ??= new SecurityMessage { SecurityId = SecurityId };
|
|
_securityDefinition.PriceStep = price.GetDecimalInfo().EffectiveScale.GetPriceStep();
|
|
_priceStepUpdated = true;
|
|
}
|
|
|
|
if (!_volumeStepUpdated && volume > 0)
|
|
{
|
|
_securityDefinition ??= new SecurityMessage { SecurityId = SecurityId };
|
|
_securityDefinition.VolumeStep = volume.Value.GetDecimalInfo().EffectiveScale.GetPriceStep();
|
|
_volumeStepUpdated = true;
|
|
}
|
|
}
|
|
}
|