namespace StockSharp.Fix.Native; /// /// FIX/FAST extension methods. /// public static partial class Extensions { /// /// Default UTCDateOnly format. /// public const string DateFormat = "yyyyMMdd"; /// /// Default MonthYear format. /// public const string YearMonthFormat = "yyyyMM"; /// /// Default UTCTimeOnly format. /// public const string TimeFormat = "hh\\:mm\\:ss\\.fff"; /// /// Default UTCTimestamp format. /// public const string TimeStampFormat = "yyyyMMdd-HH:mm:ss.fff"; /// /// /// /// /// public static byte TryReplaceSoh(this byte value) { if (value == (int)AsciiSymbols.Soh) value = (byte)'|'; return value; } /// /// Copy content. /// /// Destination. /// Source. /// Cancellation token. public static async ValueTask WriteStreamAsync(this IFixWriter dest, IFixWriter source, CancellationToken cancellationToken) { var stream = (MemoryStream)source.Stream; await dest.WriteBytesAsync(new ReadOnlyMemory(stream.GetBuffer(), 0, (int)stream.Position), cancellationToken); source.ClearState(); stream.Position = 0; } /// /// Write FIX message. /// /// Whole message writer. /// Body only writer. /// Version. /// value. /// Sender ID. /// Target ID. /// Time parser. /// Sequence number. /// Handler. /// Cancellation token. public static async ValueTask WriteFixMessageAsync(this IFixWriter writer, IFixWriter bodyWriter, string version, string msgType, string senderCompId, string targetCompId, FastDateTimeParser sendingTimeParser, long seqNum, Action handler, CancellationToken cancellationToken) { if (handler == null) throw new ArgumentNullException(nameof(handler)); await writer.WriteAsync(FixTags.BeginString, cancellationToken); await writer.WriteAsync(version, cancellationToken); await bodyWriter.WriteAsync(FixTags.MsgType, cancellationToken); await bodyWriter.WriteAsync(msgType, cancellationToken); if (!senderCompId.IsEmpty()) { await bodyWriter.WriteAsync(FixTags.SenderCompID, cancellationToken); await bodyWriter.WriteAsync(senderCompId, cancellationToken); } if (!targetCompId.IsEmpty()) { await bodyWriter.WriteAsync(FixTags.TargetCompID, cancellationToken); await bodyWriter.WriteAsync(targetCompId, cancellationToken); } await bodyWriter.WriteAsync(FixTags.SendingTime, cancellationToken); await bodyWriter.WriteAsync(DateTime.UtcNow, sendingTimeParser, cancellationToken); await bodyWriter.WriteAsync(FixTags.MsgSeqNum, cancellationToken); await bodyWriter.WriteAsync(seqNum, cancellationToken); handler(bodyWriter); await writer.WriteAsync(FixTags.BodyLength, cancellationToken); await writer.WriteAsync(bodyWriter.Stream.Position, cancellationToken); await writer.WriteStreamAsync(bodyWriter, cancellationToken); var checkSum = writer.CalcCheckSum(); writer.CheckSum = 0; await writer.WriteAsync(FixTags.CheckSum, cancellationToken); await writer.WriteAsync($"{checkSum:000}", cancellationToken); } /// /// Write FIX message asynchronously with async handler. /// /// Whole message writer. /// Body only writer. /// Version. /// value. /// Sender ID. /// Target ID. /// Time parser. /// Sequence number. /// Async handler. /// Cancellation token. public static async ValueTask WriteFixMessageAsync(this IFixWriter writer, IFixWriter bodyWriter, string version, string msgType, string senderCompId, string targetCompId, FastDateTimeParser sendingTimeParser, long seqNum, Func handler, CancellationToken cancellationToken) { if (handler == null) throw new ArgumentNullException(nameof(handler)); try { await writer.WriteAsync(FixTags.BeginString, cancellationToken); await writer.WriteAsync(version, cancellationToken); await bodyWriter.WriteAsync(FixTags.MsgType, cancellationToken); await bodyWriter.WriteAsync(msgType, cancellationToken); if (!senderCompId.IsEmpty()) { await bodyWriter.WriteAsync(FixTags.SenderCompID, cancellationToken); await bodyWriter.WriteAsync(senderCompId, cancellationToken); } if (!targetCompId.IsEmpty()) { await bodyWriter.WriteAsync(FixTags.TargetCompID, cancellationToken); await bodyWriter.WriteAsync(targetCompId, cancellationToken); } await bodyWriter.WriteAsync(FixTags.SendingTime, cancellationToken); await bodyWriter.WriteAsync(DateTime.UtcNow, sendingTimeParser, cancellationToken); await bodyWriter.WriteAsync(FixTags.MsgSeqNum, cancellationToken); await bodyWriter.WriteAsync(seqNum, cancellationToken); await handler(bodyWriter, cancellationToken); await writer.WriteAsync(FixTags.BodyLength, cancellationToken); await writer.WriteAsync(bodyWriter.Stream.Position, cancellationToken); await writer.WriteStreamAsync(bodyWriter, cancellationToken); var checkSum = writer.CalcCheckSum(); writer.CheckSum = 0; await writer.WriteAsync(FixTags.CheckSum, cancellationToken); await writer.WriteAsync($"{checkSum:000}", cancellationToken); } catch { // A failure mid-write (most reachably the body handler throwing) must not leave partial // bytes in the REUSED body writer. WriteStreamAsync above is the only place that resets it // on success, so without this the next message is appended AFTER the aborted body and ships // a single BodyLength- and checksum-consistent frame that silently swallows a real message. bodyWriter.ClearState(); bodyWriter.Stream.SetLength(0); throw; } } /// /// To calculate the checksum. /// /// /// Checksum. public static int CalcCheckSum(this IFixBase fix) { if (fix is null) throw new ArgumentNullException(nameof(fix)); return (int)(fix.CheckSum % 256); } /// /// Empty tag. /// public const FixTags EmptyTag = (FixTags)(-1); /// /// Read message. /// /// Reader. /// Cancellation token. /// values. public static async IAsyncEnumerable ReadMessageAsync(this IFixReader reader, [EnumeratorCancellation]CancellationToken cancellationToken) { if (reader == null) throw new ArgumentNullException(nameof(reader)); while (true) { yield return await reader.ReadTagAsync(cancellationToken); } } /// /// Read message. /// /// Reader. /// Tag handler. /// Cancellation token. /// if the message was successfully read, othewise, returns . public static async ValueTask ReadMessageAsync(this IFixReader reader, Func> handler, CancellationToken cancellationToken) { await foreach (var tag in reader.ReadMessageAsync(cancellationToken).WithEnforcedCancellation(cancellationToken)) { if (tag == EmptyTag) return false; if (tag == FixTags.CheckSum) return true; if (!await handler(tag, cancellationToken)) await reader.SkipValueAsync(cancellationToken); } return true; } /// /// Read message. /// /// Reader. /// Async tag handler. /// Cancellation token. /// if the message was successfully read, othewise, returns . public static async ValueTask ReadMessageAsync(this IFixReader reader, Func> handler, CancellationToken cancellationToken) { if (reader == null) throw new ArgumentNullException(nameof(reader)); if (handler == null) throw new ArgumentNullException(nameof(handler)); while (true) { var tag = await reader.ReadTagAsync(cancellationToken); if (tag == EmptyTag) return false; if (tag == FixTags.CheckSum) return true; if (!await handler(tag)) await reader.SkipValueAsync(cancellationToken); } } private const int _checkSumOffset = (byte)'1' + (byte)'0' + (byte)AsciiSymbols.Eq; /// /// Read FIX header. /// /// Reader. /// Expected value. /// Cancellation token. /// value. public static async ValueTask ReadHeaderAsync(this IFixReader reader, string expectedVersion, CancellationToken cancellationToken) { reader.CheckSum = 0; reader.BytesCount = 2; if (!await reader.ReadTagAsync(FixTags.BeginString, cancellationToken)) return null; var version = await reader.ReadStringAsync(cancellationToken); if (!version.EqualsIgnoreCase(expectedVersion)) { // TODO } if (!await reader.ReadTagAsync(FixTags.BodyLength, cancellationToken)) return null; /*var length = */ await reader.ReadIntAsync(cancellationToken); if (!await reader.ReadTagAsync(FixTags.MsgType, cancellationToken)) return null; var msgType = await reader.ReadStringAsync(cancellationToken); reader.BytesCount = 0; return msgType; } /// /// Read FIX trailer. /// /// Reader. /// Cancellation token. /// if the message was successfully read, othewise, returns . public static async ValueTask ReadTrailerAsync(this IFixReader reader, CancellationToken cancellationToken) { var calcCheckSum = (reader.CheckSum - _checkSumOffset) % 256; var receivedCheckSum = (await reader.ReadStringAsync(cancellationToken)).To(); if (!reader.CheckSumDisabled && calcCheckSum != receivedCheckSum) throw new InvalidOperationException("Check sum: calculated '{0}', received '{1}'.".Put(calcCheckSum, receivedCheckSum)); return true; } /// /// Read tag and check if it matches expected tag. /// /// Reader. /// Expected tag. /// Cancellation token. /// if the tag matches, otherwise. public static async ValueTask ReadTagAsync(this IFixReader reader, FixTags expectedTag, CancellationToken cancellationToken) { var tag = await reader.ReadTagAsync(cancellationToken); return tag == expectedTag; } /// /// Skip reading message. /// /// Reader. /// Cancellation token. /// Possible error. public static async ValueTask SkipMessageAsync(this IFixReader reader, CancellationToken cancellationToken) { try { // skip broken message while (reader.LastTag != EmptyTag) { if (!reader.IsValueRead) await reader.SkipValueAsync(cancellationToken); if (reader.LastTag == FixTags.CheckSum) break; await reader.ReadTagAsync(cancellationToken); } return null; } catch (Exception ex1) { return ex1; } } /// /// Convert to value. /// /// value. /// value. /// value. public static char ToFixMDType(this DataType type, out string mdEntryArg) { if (type is null) throw new ArgumentNullException(nameof(type)); mdEntryArg = type.IsCandles && type.Arg != null ? type.DataTypeArgToString() : null; if (type == DataType.Level1) return Level1; else if (type == DataType.MarketDepth) return MDEntryType.Bid; else if (type == DataType.Ticks) return MDEntryType.Trade; else if (type == DataType.OrderLog) return OrderLog; else if (type == DataType.News) return News; else if (type == DataType.Transactions) return Transactions; else if (type.IsCandles) { if (_candleTypes.TryGetKey(type.MessageType, out var key)) return key; return _candleTypes[typeof(TimeFrameCandleMessage)]; } else throw new ArgumentOutOfRangeException(nameof(type), type, null); } /// /// Convert to value. /// /// value. /// value. /// value. public static DataType ToDataType(this char mdEntryType, string arg) { switch (mdEntryType) { case Level1: return DataType.Level1; case OrderLog: return DataType.OrderLog; case MDEntryType.Trade: return DataType.Ticks; case MDEntryType.Bid: case MDEntryType.Offer: return DataType.MarketDepth; case News: return DataType.News; case Transactions: return DataType.Transactions; default: { if (_candleTypes.TryGetValue(mdEntryType, out var candleType)) return DataType.Create(candleType, candleType.ToDataTypeArg(arg)); throw new InvalidOperationException(LocalizedStrings.UnknownType.Put(mdEntryType)); } } } /// /// Convert to value. /// /// value. /// value. public static TradSesStatus ToFix(this SessionStates state) { return state switch { SessionStates.Assigned => TradSesStatus.PreOpen, SessionStates.Active => TradSesStatus.Open, SessionStates.Paused => TradSesStatus.Halted, SessionStates.ForceStopped => TradSesStatus.Halted, SessionStates.Ended => TradSesStatus.Closed, _ => throw new ArgumentOutOfRangeException(nameof(state), state, null), }; } /// /// Convert to value. /// /// value. /// value. public static SessionStates FromFixStatus(this TradSesStatus status) { return status switch { TradSesStatus.Open or TradSesStatus.PreClose => SessionStates.Active, TradSesStatus.Closed => SessionStates.Ended, TradSesStatus.Halted => SessionStates.Paused, TradSesStatus.PreOpen => SessionStates.Assigned, _ => throw new ArgumentOutOfRangeException(nameof(status), status, LocalizedStrings.InvalidValue), }; } /// /// Convert to value. /// /// value. /// value. public static string ToFix(this CurrencyTypes type) { return type.To(); } /// /// Convert to value. /// /// value. /// value. public static int ToFix(this OptionTypes type) { return (int)(type == OptionTypes.Call ? PutOrCall.Call : PutOrCall.Put); } /// /// Convert to value. /// /// value. /// value. public static int ToFix(this OptionStyles style) => style switch { OptionStyles.European => 0, OptionStyles.American => 1, OptionStyles.Exotic => 2, _ => throw new ArgumentOutOfRangeException(nameof(style), style, LocalizedStrings.InvalidValue), }; /// /// Convert to value. /// /// value. /// value. public static OptionStyles FromFixOptionStyle(this int style) => style switch { 0 => OptionStyles.European, 1 => OptionStyles.American, 2 => OptionStyles.Exotic, _ => throw new ArgumentOutOfRangeException(nameof(style), style, LocalizedStrings.InvalidValue), }; /// /// Convert to value. /// /// value. /// value. public static int ToFix(this SettlementTypes type) => type switch { SettlementTypes.Delivery => 0, SettlementTypes.Cash => 1, _ => throw new ArgumentOutOfRangeException(nameof(type), type, LocalizedStrings.InvalidValue), }; /// /// Convert to value. /// /// value. /// value. public static SettlementTypes FromFixSettlType(this int style) => style switch { 0 => SettlementTypes.Delivery, 1 => SettlementTypes.Cash, _ => throw new ArgumentOutOfRangeException(nameof(style), style, LocalizedStrings.InvalidValue), }; /// /// Convert to value. /// /// value. /// value. public static OptionTypes FromFixOptionType(this PutOrCall type) { return type switch { PutOrCall.Call => OptionTypes.Call, PutOrCall.Put => OptionTypes.Put, _ => throw new ArgumentOutOfRangeException(), }; } /// /// Get value. /// /// A message containing info about the order. /// value. public static char GetFixType(this OrderMessage message) { if (message == null) throw new ArgumentNullException(nameof(message)); var condition = message.Condition as FixOrderCondition; switch (condition?.Type) { case null: { switch (message.OrderType) { case null: case OrderTypes.Limit: return OrdType.Limit; case OrderTypes.Market: return OrdType.Market; case OrderTypes.Conditional: { return message.Condition switch { IStopLossOrderCondition slcond => slcond.ActivationPrice == null ? OrdType.Stop : OrdType.StopLimit, ITakeProfitOrderCondition tpcond => tpcond.ActivationPrice == null ? OrdType.Stop : OrdType.StopLimit, _ => OrdType.Stop }; } default: throw new ArgumentOutOfRangeException(message.OrderType.ToString()); } } case FixStopOrderTypes.StopLoss: return condition.StopLoss is null ? OrdType.Stop : OrdType.StopLimit; case FixStopOrderTypes.StopLossTrailing: return StopTrailing; case FixStopOrderTypes.TakeProfit: return TakeProfit; case FixStopOrderTypes.TakeProfitTrailing: return TakeProfitTrailing; case FixStopOrderTypes.MarketOnClose: return OrdType.MarketOnClose; case FixStopOrderTypes.MarketIfTouched: return OrdType.MarketIfTouched; default: throw new NotSupportedException(LocalizedStrings.OrderUnsupportedType.Put(message.OrderType, message.TransactionId)); } } /// /// Set value. /// /// A message containing info about the order. /// value. public static void SetOrderType(this OrderMessage message, char ordType) { if (message is null) throw new ArgumentNullException(nameof(message)); message.OrderType = ordType switch { OrdType.Limit => (OrderTypes?)OrderTypes.Limit, OrdType.Market => (OrderTypes?)OrderTypes.Market, OrdType.Stop or OrdType.StopLimit or OrdType.MarketIfTouched or OrdType.MarketOnClose or TakeProfit or TakeProfitTrailing or StopTrailing => (OrderTypes?)OrderTypes.Conditional, _ => throw new ArgumentOutOfRangeException(nameof(ordType), ordType, LocalizedStrings.InvalidValue), }; } /// /// Convert to value. /// /// value. /// value. public static char ToTickDir(this bool dir) { return dir ? TickDirection.PlusTick : TickDirection.MinusTick; } /// /// Convert to value. /// /// value. /// value. public static bool FromTickDir(this char dir) { return dir switch { TickDirection.ZeroPlusTick or TickDirection.PlusTick => true, TickDirection.MinusTick or TickDirection.ZeroMinusTick => false, _ => throw new ArgumentOutOfRangeException(nameof(dir), dir, LocalizedStrings.InvalidValue), }; } /// /// Convert to value. /// /// value. /// value. public static char ToFix(this Sides side) { return side == Sides.Buy ? Side.Buy : Side.Sell; } /// /// Convert to value. /// /// value. /// is required. /// value. public static Sides FromFixSide(this char side, bool required = false) { if (side == Side.Undisclosed) { if (required) throw new ArgumentNullException(nameof(side)); return default; } return side.TryFromFixSide() ?? throw new ArgumentOutOfRangeException(nameof(side), side, LocalizedStrings.InvalidValue); } internal static Sides? TryFromFixSide(this char side) { switch (side) { case Side.Buy: case Side.BuyMinus: return Sides.Buy; case Side.Sell: case Side.SellShort: case Side.SellPlus: case Side.SellShortExempt: case Side.CrossShort: case Side.CrossShortExempt: return Sides.Sell; default: return null; } } /// /// Convert to value. /// /// value. /// value. public static char? ToFixOrdStatus(this ExecutionMessage message) { switch (message.OrderState) { case OrderStates.Active: return OrdStatus.New; case OrderStates.Done: return message.Balance == null || message.Balance == 0 ? OrdStatus.Filled : OrdStatus.Canceled; case OrderStates.Failed: return OrdStatus.Rejected; case OrderStates.Pending: return OrdStatus.PendingNew; } if (!message.IsOk()) return OrdStatus.Rejected; return null; } /// /// Convert to value. /// /// value. /// value or null. public static OrderStates? FromFixStatus2(this char status) { return status switch { OrdStatus.PendingNew or OrdStatus.PendingReplace => (OrderStates?)OrderStates.Pending, OrdStatus.New or OrdStatus.PartiallyFilled or OrdStatus.PendingCancel => (OrderStates?)OrderStates.Active, OrdStatus.Canceled or OrdStatus.Filled or OrdStatus.Expired or OrdStatus.Calculated => (OrderStates?)OrderStates.Done, OrdStatus.Replaced or OrdStatus.AcceptedForBidding => (OrderStates?)OrderStates.Active, OrdStatus.Rejected => (OrderStates?)OrderStates.Failed, _ => null, }; } /// /// Convert to value. /// /// value. /// value. public static OrderStates FromFixStatus(this char status) => status.FromFixStatus2() ?? throw new ArgumentOutOfRangeException(nameof(status), status, LocalizedStrings.InvalidValue); /// /// Encode the order's lifetime policy as a FIX value. /// /// /// Always answers, because an absent tag 59 reads as Day, so order entry always states it. /// No till-date gives GoodTillCancel; a till-date equal to the /// sentinel gives Day; any other date gives /// GoodTillDate, which is also what makes a writer emit the expiry tag alongside. /// /// The message containing the information for the order registration. /// value. public static char GetFixTimeInForce(this OrderRegisterMessage message) { if (message == null) throw new ArgumentNullException(nameof(message)); return message.TimeInForce.ToFix(message.TillDate); } /// /// Encode a lifetime policy and its expiry as a FIX value. /// /// /// The enum on its own cannot say how long an order lives: GoodTillCancel, Day and /// GoodTillDate are all , and the expiry is what /// tells them apart - no date gives GoodTillCancel, the /// sentinel gives Day, any other date gives /// GoodTillDate (which is also what makes a writer emit the expiry tag alongside). Encoding /// the enum alone collapses all three to GoodTillCancel. /// /// A policy is treated as PutInQueue: an absent tag 59 reads as Day, so /// a writer has to state something, and "unspecified" is not a thing the wire can carry. /// /// value. /// The order's expiry, when it has one. /// value. public static char ToFix(this Messages.TimeInForce? tif, DateTime? tillDate) { switch (tif) { case Messages.TimeInForce.PutInQueue: case null: { if (tillDate == null) return FixTimeInForce.GoodTillCancel; else if (tillDate.Value.IsToday()) return FixTimeInForce.Day; else return FixTimeInForce.GoodTillDate; } case Messages.TimeInForce.MatchOrCancel: return FixTimeInForce.FillOrKill; case Messages.TimeInForce.CancelBalance: return FixTimeInForce.ImmediateOrCancel; default: throw new ArgumentOutOfRangeException(nameof(tif), tif, LocalizedStrings.InvalidValue); } } /// /// Convert to value. /// /// value. /// value. public static char ToFix(this Messages.TimeInForce tif) { return tif switch { Messages.TimeInForce.PutInQueue => FixTimeInForce.GoodTillCancel, Messages.TimeInForce.MatchOrCancel => FixTimeInForce.FillOrKill, Messages.TimeInForce.CancelBalance => FixTimeInForce.ImmediateOrCancel, _ => throw new ArgumentOutOfRangeException(nameof(tif), tif, LocalizedStrings.InvalidValue), }; } private const string _secTypeCommodity = "COMMODITY"; private const string _secTypeSpread = "SPREAD"; private const string _secTypeStrategy = "STRATEGY"; private const string _secTypeVolatility = "VOLA"; private const string _cryptoCurrency = "CryptoCurrency"; private const string _secTypeRepo = "REPO"; /// /// Convert to value. /// /// value. /// value. public static string ToFix(this SecurityTypes type) { return type switch { SecurityTypes.Stock => SecurityType.CommonStock, SecurityTypes.Future => SecurityType.Future, SecurityTypes.Option => SecurityType.Option, SecurityTypes.Index => SecurityType.IndexLinked, SecurityTypes.Currency => SecurityType.FXSpot, SecurityTypes.CryptoCurrency => _cryptoCurrency, SecurityTypes.Bond => SecurityType.BradyBond, SecurityTypes.Warrant => SecurityType.Warrant, SecurityTypes.Forward => SecurityType.Forward, SecurityTypes.Swap => SecurityType.CreditDefaultSwap, SecurityTypes.Commodity => _secTypeCommodity, SecurityTypes.Cfd => SecurityType.FXForward, SecurityTypes.News => SecurityType.Overnight, SecurityTypes.Weather => SecurityType.LetterOfCredit, SecurityTypes.Fund => SecurityType.MutualFund, SecurityTypes.Adr => SecurityType.YankeeCertificateOfDeposit, SecurityTypes.Etf => SecurityType.ForeignExchangeContract, SecurityTypes.MultiLeg => SecurityType.MultilegInstrument, SecurityTypes.Loan => SecurityType.SecuritiesLoan, SecurityTypes.Spread => _secTypeSpread, SecurityTypes.Gdr => SecurityType.EuroCommercialPaper, SecurityTypes.Receipt => SecurityType.DepositNotes, SecurityTypes.Indicator => SecurityType.ExtendedCommNote, SecurityTypes.Strategy => _secTypeStrategy, SecurityTypes.Volatility => _secTypeVolatility, SecurityTypes.Repo => _secTypeRepo, _ => throw new ArgumentOutOfRangeException(nameof(type), type, LocalizedStrings.InvalidValue), }; } /// /// Convert to value. /// /// value. /// value. public static SecurityTypes? FromFixType(this string type) { if (type == null) throw new ArgumentNullException(nameof(type)); return type switch { SecurityType.CommonStock or "Equity" => (SecurityTypes?)SecurityTypes.Stock, SecurityType.Future => (SecurityTypes?)SecurityTypes.Future, SecurityType.Option => (SecurityTypes?)SecurityTypes.Option, SecurityType.IndexLinked => (SecurityTypes?)SecurityTypes.Index, SecurityType.FXSpot => (SecurityTypes?)SecurityTypes.Currency, SecurityType.FXForward => (SecurityTypes?)SecurityTypes.Cfd, _cryptoCurrency => (SecurityTypes?)SecurityTypes.CryptoCurrency, SecurityType.BradyBond => (SecurityTypes?)SecurityTypes.Bond, SecurityType.Warrant => (SecurityTypes?)SecurityTypes.Warrant, SecurityType.Forward => (SecurityTypes?)SecurityTypes.Forward, SecurityType.CreditDefaultSwap => (SecurityTypes?)SecurityTypes.Swap, _secTypeCommodity => (SecurityTypes?)SecurityTypes.Commodity, SecurityType.YankeeCertificateOfDeposit => (SecurityTypes?)SecurityTypes.Adr, SecurityType.Overnight => (SecurityTypes?)SecurityTypes.News, SecurityType.LetterOfCredit => (SecurityTypes?)SecurityTypes.Weather, SecurityType.ForeignExchangeContract => (SecurityTypes?)SecurityTypes.Etf, SecurityType.MultilegInstrument => (SecurityTypes?)SecurityTypes.MultiLeg, SecurityType.SecuritiesLoan => (SecurityTypes?)SecurityTypes.Loan, _secTypeSpread => (SecurityTypes?)SecurityTypes.Spread, SecurityType.EuroCommercialPaper => (SecurityTypes?)SecurityTypes.Gdr, SecurityType.DepositNotes => (SecurityTypes?)SecurityTypes.Receipt, SecurityType.ExtendedCommNote => (SecurityTypes?)SecurityTypes.Indicator, _secTypeStrategy => (SecurityTypes?)SecurityTypes.Strategy, _secTypeVolatility => (SecurityTypes?)SecurityTypes.Volatility, _secTypeRepo => (SecurityTypes?)SecurityTypes.Repo, _ => type.Iso10962ToSecurityType(), }; } /// /// /// /// A message containing info about the order. /// value. public static string ToSecurityType(this OrderMessage message) { return message.SecurityType?.ToFix(); } /// /// Convert to . /// /// Change. /// Time. /// Time parser. /// value. public static MDEntry ToFix(this KeyValuePair change, DateTime time, FastDateTimeParser dateTimeParser) { if (change.Value == null) throw new ArgumentNullException(nameof(change)); var fixType = change.Key.ToFix(); var isSize = false; switch (change.Key) { case Level1Fields.OpenInterest: case Level1Fields.BidsVolume: case Level1Fields.BidsCount: case Level1Fields.AsksVolume: case Level1Fields.AsksCount: case Level1Fields.VolumeStep: case Level1Fields.Multiplier: case Level1Fields.LastTradeVolume: case Level1Fields.BestBidVolume: case Level1Fields.BestAskVolume: case Level1Fields.Volume: case Level1Fields.TradesCount: case Level1Fields.IssueSize: case Level1Fields.MinVolume: case Level1Fields.MaxVolume: case Level1Fields.UnderlyingMinVolume: isSize = true; break; case Level1Fields.LastTradeId: case Level1Fields.LastTradeOrigin: case Level1Fields.LastTradeUpDown: case Level1Fields.IsSystem: return new MDEntry { Type = fixType, OtherValue = change.Value.To(), Time = time, }; case Level1Fields.LastTradeTime: case Level1Fields.BuyBackDate: case Level1Fields.CouponDate: case Level1Fields.BestBidTime: case Level1Fields.BestAskTime: return new MDEntry { Type = fixType, OtherValue = dateTimeParser.ToString((DateTime)change.Value), Time = time, }; case Level1Fields.LastTradeStringId: return new MDEntry { Type = fixType, OtherValue = (string)change.Value, Time = time, }; } var entry = new MDEntry { Type = fixType, Time = time, }; // some fields has integer values var value = change.Value.To(); if (isSize) entry.Size = value; else entry.Price = value; return entry; } /// /// . /// public const char Level1 = '*'; /// /// . /// public const char News = 'n'; /// /// . /// public const char CandleTimeFrame = 'W'; /// /// . /// public const char CandleVolume = 'V'; /// /// . /// public const char CandleTick = 'i'; /// /// . /// public const char CandleRange = 'z'; /// /// . /// public const char CandleRenko = 'R'; /// /// . /// public const char CandlePnF = 'x'; /// /// . /// public const char CandleHeikin = '%'; /// /// . /// public const char OrderLog = 'I'; /// /// . /// public const char Transactions = 'Q'; /// /// . /// public const char TakeProfit = 'T'; /// /// . /// public const char TakeProfitTrailing = 'W'; /// /// . /// public const char StopTrailing = 'Z'; private static readonly PairSet _candleTypes = new() { { CandleTimeFrame, typeof(TimeFrameCandleMessage) }, { CandleVolume, typeof(VolumeCandleMessage) }, { CandleTick, typeof(TickCandleMessage) }, { CandleRange, typeof(RangeCandleMessage) }, { CandleRenko, typeof(RenkoCandleMessage) }, { CandlePnF, typeof(PnFCandleMessage) }, { CandleHeikin, typeof(HeikinAshiCandleMessage) }, }; /// /// Register new candle type. /// /// value. /// The type of candle message. public static void RegisterCandleType(char code, Type messageType) => _candleTypes.Add(code, messageType ?? throw new ArgumentNullException(nameof(messageType))); /// /// Check the specified type is candle. /// /// value. /// Check result. public static bool IsCandleEntry(this char mdEntryType) => _candleTypes.ContainsKey(mdEntryType); /// /// Convert to value. /// /// value. /// value. public static CandleMessage ToCandleMessage(this char entryType) { if (_candleTypes.TryGetValue(entryType, out var candleType)) return candleType.CreateInstance(); throw new ArgumentOutOfRangeException(nameof(entryType), entryType, LocalizedStrings.InvalidValue); } private const char _mmOrderCapacity = 'P'; private const string _mmOrderRestrictions = "5"; /// /// Write and . /// /// Writer. /// Cancellation token. public static async ValueTask WriteMarketMakerAsync(this IFixWriter writer, CancellationToken cancellationToken) { await writer.WriteAsync(FixTags.OrderCapacity, cancellationToken); await writer.WriteAsync(_mmOrderCapacity, cancellationToken); await writer.WriteAsync(FixTags.OrderRestrictions, cancellationToken); await writer.WriteAsync(_mmOrderRestrictions, cancellationToken); } /// /// Is the order of market-maker. /// /// value. /// value. /// Check result. public static bool IsMarketMaker(char? orderCapacity, string orderRestrictions) { return orderCapacity == _mmOrderCapacity && orderRestrictions == _mmOrderRestrictions; } /// /// Convert to value. /// /// value. /// value. public static FixUserRequestTypes ToUserRequestType(this UserRequestType type) { return type switch { //case UserRequestType.LogOnUser: //case UserRequestType.RequestIndividualUserStatus: // return FixUserRequestTypes.Lookup; UserRequestType.LogOffUser => FixUserRequestTypes.LogoffForce, UserRequestType.ChangePasswordForUser => FixUserRequestTypes.ChangePassword, _ => throw new ArgumentOutOfRangeException(nameof(type), type, LocalizedStrings.InvalidValue), }; } /// /// Convert to value. /// /// value. /// /// /// /// /// value. public static UserStatus ToUserStatus(this FixUserResponseTypes type, Exception error, string userName, UserRequestType requestType, out string text) { text = error?.Message; switch (type) { case FixUserResponseTypes.PasswordChanged: if (text.IsEmpty()) text = LocalizedStrings.PasswordChangedOk; return UserStatus.PasswordChanged; case FixUserResponseTypes.UserLoggedOff: if (text.IsEmpty()) text = LocalizedStrings.UserLoggedOut; return UserStatus.ForcedUserLogoutByExchange; case FixUserResponseTypes.UserFound: text = string.Empty; return UserStatus.LoggedIn; case FixUserResponseTypes.UserNotFound: if (text.IsEmpty()) text = LocalizedStrings.ElementNotFoundParams.Put(userName); return UserStatus.UserNotRecognised; case FixUserResponseTypes.UserBlocked: if (text.IsEmpty()) text = LocalizedStrings.UserBlocked; return UserStatus.Other; case FixUserResponseTypes.OldPasswordIncorrect: if (text.IsEmpty()) text = LocalizedStrings.PasswordWasNotChangedCauseError; return UserStatus.PasswordIncorrect; case FixUserResponseTypes.NewPasswordIncorrect: if (text.IsEmpty()) text = LocalizedStrings.PasswordWasNotChangedCauseError; return UserStatus.PasswordIncorrect; case FixUserResponseTypes.NotSupported: if (text.IsEmpty()) text = LocalizedStrings.UnsupportedType.Put(requestType); return UserStatus.Other; case FixUserResponseTypes.UnknownError: if (text.IsEmpty()) text = LocalizedStrings.UnknownServerError; return UserStatus.Other; default: throw new ArgumentOutOfRangeException(nameof(type), type, LocalizedStrings.InvalidValue); } } /// /// Convert to value. /// /// value. /// value. public static SecurityStates? FromQuoteType(this int? type) { return type switch { null => null, (int)QuoteType.Tradeable or (int)QuoteType.Indicative or (int)QuoteType.Counter => (SecurityStates?)SecurityStates.Trading, (int)QuoteType.RestrictedTradeable => (SecurityStates?)SecurityStates.Stoped, _ => throw new ArgumentOutOfRangeException(nameof(type), type.Value, LocalizedStrings.InvalidValue), }; } /// /// Convert to value. /// /// value. /// value. public static NewsPriorities? ToNewsPriority(this char? urgency) { return urgency switch { null => null, Urgency.Background => (NewsPriorities?)NewsPriorities.Low, Urgency.Normal => (NewsPriorities?)NewsPriorities.Regular, Urgency.Flash => (NewsPriorities?)NewsPriorities.High, _ => null, }; } /// /// Write . /// /// Writer. /// Message. /// Default value. /// Cancellation token. public static async ValueTask WriteHandlInstAsync(this IFixWriter writer, OrderRegisterMessage message, char defaultValue, CancellationToken cancellationToken) { if (writer == null) throw new ArgumentNullException(nameof(writer)); if (message == null) throw new ArgumentNullException(nameof(message)); await writer.WriteAsync(FixTags.HandlInst, cancellationToken); await writer.WriteAsync(message.IsManual == true ? HandlInst.ManualOrder : defaultValue, cancellationToken); } /// /// Write . /// /// Writer. /// Side. /// Cancellation token. public static async ValueTask WriteSideAsync(this IFixWriter writer, Sides side, CancellationToken cancellationToken) { if (writer is null) throw new ArgumentNullException(nameof(writer)); await writer.WriteAsync(FixTags.Side, cancellationToken); await writer.WriteAsync(side.ToFix(), cancellationToken); } /// /// . /// public const string Commission = "COMM"; /// /// . /// public const string RealizedPnL = "RPNL"; /// /// . /// public const string UnrealizedPnL = "UPNL"; /// /// . /// public const string Leverage = "LVRG"; /// /// . /// public const string LiquidationPrice = "LP"; /// /// Convert to value. /// /// value. /// value. public static bool IsSubscribe(this char? type) { //if (type == null) // throw new InvalidOperationException("Request type is missing."); return type == null || type.Value.IsSubscribe(); } /// /// Convert to value. /// /// value. /// value. public static bool IsSubscribe(this char type) { return type == SubscriptionRequestType.Snapshot || type == SubscriptionRequestType.SnapshotPlusUpdates; } /// /// Get request id. /// /// Subscription. /// public static long GetRequestId(this ISubscriptionMessage msg) { if (msg == null) throw new ArgumentNullException(nameof(msg)); return msg.IsSubscribe ? msg.TransactionId : msg.OriginalTransactionId; } /// /// /// /// Subscription. /// value. public static char GetSubscriptionType(this ISubscriptionMessage msg) { if (msg == null) throw new ArgumentNullException(nameof(msg)); return msg.IsSubscribe ? (msg.To == null ? SubscriptionRequestType.SnapshotPlusUpdates : SubscriptionRequestType.Snapshot) : SubscriptionRequestType.DisablePrevious; } /// /// Read . /// /// Reader. /// Tag. /// Handler. /// Cancellation token. /// if the tag was handled, if the tag is unknown. public static async ValueTask ReadOrderConditionAsync(this IFixReader reader, FixTags tag, Func getCondition, CancellationToken cancellationToken) { switch (tag) { case FixTags.StopPx: ((FixOrderCondition)getCondition()).StopLoss = await reader.ReadDecimalAsync(cancellationToken); return true; case FixTags.PegOffsetValue: ((FixOrderCondition)getCondition()).Offset = await reader.ReadDecimalAsync(cancellationToken); return true; case FixTags.TakeProfit: ((FixOrderCondition)getCondition()).TakeProfit = await reader.ReadDecimalAsync(cancellationToken); return true; default: return false; } } /// /// Convert to value. /// /// value. /// value. public static QuoteConditions ToQuoteCondition(this string condition) { if (condition.IsEmpty()) return default; // TODO return condition[0] switch { QuoteCondition.Active => QuoteConditions.Active, QuoteCondition.Indicative => QuoteConditions.Indicative, _ => throw new ArgumentOutOfRangeException(nameof(condition), condition, LocalizedStrings.InvalidValue), }; } /// /// Convert to value. /// /// value. /// /// value. public static char? ToNative(this QuoteConditions condition, bool force = false) { return condition switch { QuoteConditions.Active => force ? QuoteCondition.Active : null, QuoteConditions.Indicative => QuoteCondition.Indicative, _ => throw new ArgumentOutOfRangeException(nameof(condition), condition, LocalizedStrings.InvalidValue), }; } /// /// Convert to value. /// /// value. /// value. public static char ToNative(this QuoteChangeActions action) { return action switch { QuoteChangeActions.New => MDUpdateAction.New, QuoteChangeActions.Update => MDUpdateAction.Change, QuoteChangeActions.Delete => MDUpdateAction.Delete, _ => throw new ArgumentOutOfRangeException(nameof(action), action, LocalizedStrings.InvalidValue), }; } /// /// Convert to value. /// /// value. /// value. public static QuoteChangeActions ToQuoteAction(this char action) { return action switch { MDUpdateAction.New => QuoteChangeActions.New, MDUpdateAction.Change => QuoteChangeActions.Update, MDUpdateAction.Delete or MDUpdateAction.DeleteFrom or MDUpdateAction.DeleteThru or MDUpdateAction.Overlay => QuoteChangeActions.Delete, _ => throw new ArgumentOutOfRangeException(nameof(action), action, LocalizedStrings.InvalidValue), }; } /// /// Read time. /// /// Reader. /// Time parser. /// Cancellation token. /// Time. public static async ValueTask ReadUtcAsync(this IFixReader reader, FastDateTimeParser parser, CancellationToken cancellationToken) => (await reader.ReadDateTimeAsync(parser, cancellationToken)).UtcKind(); /// /// Write time. /// /// Writer. /// Time. /// Time parser. /// Cancellation token. public static ValueTask WriteUtcAsync(this IFixWriter writer, DateTime dto, FastDateTimeParser parser, CancellationToken cancellationToken) => writer.WriteAsync(dto, parser, cancellationToken); /// /// Write . /// /// Writer. /// Time parser. /// Cancellation token. public static async ValueTask WriteTransactTimeAsync(this IFixWriter writer, FastDateTimeParser parser, CancellationToken cancellationToken) { await writer.WriteAsync(FixTags.TransactTime, cancellationToken); await writer.WriteAsync(DateTime.UtcNow, parser, cancellationToken); } /// /// Write . /// /// Writer. /// The message containing the information for the order registration. /// Time parser. /// Time zone. /// Cancellation token. public static async ValueTask WriteExpiryDateAsync(this IFixWriter writer, OrderRegisterMessage regMsg, FastDateTimeParser parser, TimeZoneInfo timeZone, CancellationToken cancellationToken) { var dto = regMsg.TillDate; if (dto == null) return; await writer.WriteAsync(FixTags.ExpireDate, cancellationToken); await writer.WriteAsync(dto.Value.To(destination: timeZone), parser, cancellationToken); } /// /// Convert to value. /// /// value. /// value. public static OrderPositionEffects? ToPositionEffect(this char? effect) { return effect switch { null => null, PositionEffect.Default => (OrderPositionEffects?)OrderPositionEffects.Default, PositionEffect.Close or PositionEffect.CloseNotify => (OrderPositionEffects?)OrderPositionEffects.CloseOnly, PositionEffect.Open or PositionEffect.Rolled or PositionEffect.FIFO => (OrderPositionEffects?)OrderPositionEffects.OpenOnly, _ => throw new ArgumentOutOfRangeException(nameof(effect), effect, LocalizedStrings.InvalidValue), }; } /// /// Write . /// /// Writer. /// value. /// Cancellation token. public static async ValueTask WritePositionEffectAsync(this IFixWriter writer, OrderPositionEffects? effect, CancellationToken cancellationToken) { char fix; switch (effect) { case null: return; case OrderPositionEffects.Default: fix = PositionEffect.Default; break; case OrderPositionEffects.CloseOnly: fix = PositionEffect.Close; break; case OrderPositionEffects.OpenOnly: fix = PositionEffect.Open; break; default: throw new ArgumentOutOfRangeException(nameof(effect), effect, LocalizedStrings.InvalidValue); } await writer.WriteAsync(FixTags.PositionEffect, cancellationToken); await writer.WriteAsync(fix, cancellationToken); } /// /// Convert string to dialect type. /// /// String value. /// Logs. /// Dialect type. public static Type ToDialect(this string dialect, ILogReceiver logs) { if (logs is null) throw new ArgumentNullException(nameof(logs)); try { return dialect.To(); } catch (Exception ex) { logs.AddErrorLog(ex); return null; } } /// /// Convert dialect type to string. /// /// Dialect type. /// String value. public static string FromDialect(this Type type) { if (type == null) throw new ArgumentNullException(nameof(type)); return type.To(); } /// /// /// /// /// /// /// /// public static void InitSecId(this SecurityMessage message, string symbol, string board, string idSource, string idValue) { var secId = new SecurityId { SecurityCode = symbol, BoardCode = board, }; switch (idSource) { case SecurityIDSource.Cusip: secId.Cusip = idValue; break; case SecurityIDSource.Sedol: secId.Sedol = idValue; break; case SecurityIDSource.IsinNumber: secId.Isin = idValue; break; case SecurityIDSource.RicCode: secId.Ric = idValue; break; default: secId.Native = idValue; break; } message.SecurityId = secId; } /// /// /// /// /// /// /// /// /// public static void FillLevel1(this Level1ChangeMessage message, char entryType, decimal? price, decimal? size, string otherValue, FastDateTimeParser dateTimeParser) { var field = entryType.ToLevel1(); object value; switch (field) { case Level1Fields.OpenInterest: case Level1Fields.VolumeStep: case Level1Fields.Multiplier: case Level1Fields.LastTradeVolume: case Level1Fields.BestBidVolume: case Level1Fields.BestAskVolume: case Level1Fields.BidsVolume: case Level1Fields.AsksVolume: case Level1Fields.Volume: case Level1Fields.IssueSize: case Level1Fields.MinVolume: case Level1Fields.MaxVolume: case Level1Fields.UnderlyingMinVolume: value = size.Value; break; case Level1Fields.BidsCount: case Level1Fields.AsksCount: case Level1Fields.TradesCount: value = (int)size.Value; break; case Level1Fields.State: value = (SecurityStates)price.Value; break; case Level1Fields.LastTradeId: value = otherValue.To(); break; case Level1Fields.LastTradeOrigin: value = otherValue.To(); break; case Level1Fields.BestBidTime: case Level1Fields.BestAskTime: case Level1Fields.LastTradeTime: case Level1Fields.BuyBackDate: case Level1Fields.CouponDate: value = dateTimeParser.Parse(otherValue).UtcKind(); break; case Level1Fields.LastTradeUpDown: case Level1Fields.IsSystem: // TODO 2025-09-03 remove price usage 1 year later value = otherValue.To() ?? price.To(); break; case Level1Fields.LastTradeStringId: value = otherValue; break; default: { value = price.Value; break; } } message.Add(field, value); } /// /// /// /// /// public static bool IsDump(this ILogSource source) { var level = source.GetLogLevel(); if (level == LogLevels.Inherit) { var logManager = LogManager.Instance; if (logManager != null) level = logManager.Application.LogLevel; } return level == LogLevels.Verbose; } /// /// /// /// /// public static MarginModes ToMarginMode(this char cashMargin) => cashMargin == CashMargin.MarginOpen ? MarginModes.Cross : MarginModes.Isolated; }