From 941888e70642748e07d4f6f98e30817fef9c9408 Mon Sep 17 00:00:00 2001 From: AKosylo Date: Wed, 13 Dec 2023 10:30:42 +0100 Subject: [PATCH] add hash code --- .../xcshareddata/xcschemes/Tools.xcscheme | 6 +- .../Events/Market/AnalyticOrder.swift | 9 +-- .../Events/Market/Candles/Candle.swift | 17 ++--- .../Market/Candles/CandleAlignment.swift | 2 + .../Market/Candles/CandleExchange.swift | 1 + .../Events/Market/Candles/CandlePeriod.swift | 1 + .../Events/Market/Candles/CandlePrice.swift | 1 + .../Events/Market/Candles/CandleSession.swift | 1 + .../Events/Market/Candles/CandleType.swift | 1 + .../Events/Market/Extra/IEventType.swift | 3 + .../Events/Market/Extra/MarketEvent.swift | 21 +++++- .../Events/Market/Extra/OrderBase.swift | 16 ++--- .../Events/Market/Extra/TradeBase.swift | 12 +--- DXFeedFramework/Events/Market/Greeks.swift | 43 ++++++------ .../Events/Market/OptionSale.swift | 65 +++++++++---------- DXFeedFramework/Events/Market/Order.swift | 12 +++- DXFeedFramework/Events/Market/Profile.swift | 16 ++--- DXFeedFramework/Events/Market/Quote.swift | 16 ++--- DXFeedFramework/Events/Market/Series.swift | 48 +++++++------- .../Events/Market/SpreadOrder.swift | 8 +-- DXFeedFramework/Events/Market/Summary.swift | 46 +++++++------ DXFeedFramework/Events/Market/TheoPrice.swift | 41 ++++++------ .../Events/Market/TimeAndSale.swift | 58 ++++++++--------- DXFeedFramework/Events/Market/Trade.swift | 5 +- DXFeedFramework/Events/Market/TradeETH.swift | 5 +- .../Events/Market/Underlying.swift | 43 ++++++------ .../Native/Events/Markets/Candle+Ext.swift | 2 +- DXFeedFrameworkTests/EventsTest.swift | 7 ++ .../Performance/PerfTestEventListener.swift | 6 ++ 29 files changed, 259 insertions(+), 253 deletions(-) diff --git a/DXFeedFramework.xcodeproj/xcshareddata/xcschemes/Tools.xcscheme b/DXFeedFramework.xcodeproj/xcshareddata/xcschemes/Tools.xcscheme index bdc3247e7..3740874f5 100644 --- a/DXFeedFramework.xcodeproj/xcshareddata/xcschemes/Tools.xcscheme +++ b/DXFeedFramework.xcodeproj/xcshareddata/xcschemes/Tools.xcscheme @@ -52,12 +52,12 @@ + argument = "PerfTest localhost:7777 TimeAndSale YQKNT" + isEnabled = "YES"> + isEnabled = "NO"> class. - public override init(_ eventSymbol: String) { - super.init(eventSymbol) + /// Initializes a new instance of the ``AnalyticOrder`` class. + public init(_ eventSymbol: String) { + super.init(eventSymbol: eventSymbol, type: .analyticOrder) } /// Returns string representation of this candle event. diff --git a/DXFeedFramework/Events/Market/Candles/Candle.swift b/DXFeedFramework/Events/Market/Candles/Candle.swift index a43b1d42c..1f4aab54c 100644 --- a/DXFeedFramework/Events/Market/Candles/Candle.swift +++ b/DXFeedFramework/Events/Market/Candles/Candle.swift @@ -11,10 +11,8 @@ import Foundation /// with a data taken from the specified ``CandleExchange`` from the specified ``CandleSession`` /// with further details of aggregation provided by ``CandleAlignment``. public class Candle: MarketEvent, ITimeSeriesEvent, ILastingEvent, CustomStringConvertible { - public let type: EventCode = .candle - /// Gets or sets candle symbol object. - public var eventSymbol: String { + public override var eventSymbol: String { get { candleSymbol?.toString() ?? "" } @@ -22,7 +20,6 @@ public class Candle: MarketEvent, ITimeSeriesEvent, ILastingEvent, CustomStringC candleSymbol = try? CandleSymbol.valueOf(newValue) } } - public var eventTime: Int64 = 0 /* * EventFlags property has several significant bits that are packed into an integer in the following way: @@ -65,7 +62,7 @@ public class Candle: MarketEvent, ITimeSeriesEvent, ILastingEvent, CustomStringC /// Initializes a new instance of the ``Candle`` class with the specified event symbol. public convenience init(_ symbol: CandleSymbol) { - self.init() + self.init(type: .candle) self.candleSymbol = symbol } @@ -91,6 +88,11 @@ impVolatility: \(impVolatility), \ openInterest: \(openInterest) """ } + + /// Returns string representation of this candle event. + public override func toString() -> String { + return "Candle{\(baseFieldsToString())}" + } } extension Candle { @@ -126,11 +128,6 @@ extension Candle { } } - /// Returns string representation of this candle event. - public func toString() -> String { - return "Candle{\(baseFieldsToString())}" - } - /// Returns string representation of this candle fields. func baseFieldsToString() -> String { return """ diff --git a/DXFeedFramework/Events/Market/Candles/CandleAlignment.swift b/DXFeedFramework/Events/Market/Candles/CandleAlignment.swift index 7157b45d3..0b0a5d60c 100644 --- a/DXFeedFramework/Events/Market/Candles/CandleAlignment.swift +++ b/DXFeedFramework/Events/Market/Candles/CandleAlignment.swift @@ -117,12 +117,14 @@ public enum CandleAlignment: DXCandleAlignment, CaseIterable { } return try parse(attribute) } + /// Returns string representation of this candle alignment. /// The string representation of candle alignment "m" for ``midnight`` /// and "s" for ``session``. public func toString() -> String { return self.rawValue.string } + /// Returns full string representation of this candle alignment. /// /// It is contains attribute key and its value. diff --git a/DXFeedFramework/Events/Market/Candles/CandleExchange.swift b/DXFeedFramework/Events/Market/Candles/CandleExchange.swift index a3d2f6148..7e83d53c2 100644 --- a/DXFeedFramework/Events/Market/Candles/CandleExchange.swift +++ b/DXFeedFramework/Events/Market/Candles/CandleExchange.swift @@ -40,6 +40,7 @@ public class CandleExchange { public static func getAttribute(_ symbol: String?) -> CandleExchange { return valueOf(MarketEventSymbols.getExchangeCode(symbol)) } + /// Returns string representation of this exchange. /// It is the string "COMPOSITE" for ``Composite`` exchange or /// exchange character otherwise. diff --git a/DXFeedFramework/Events/Market/Candles/CandlePeriod.swift b/DXFeedFramework/Events/Market/Candles/CandlePeriod.swift index e5e75c7bd..510fcf372 100644 --- a/DXFeedFramework/Events/Market/Candles/CandlePeriod.swift +++ b/DXFeedFramework/Events/Market/Candles/CandlePeriod.swift @@ -127,6 +127,7 @@ public class CandlePeriod { self.type = type self.periodIntervalMillis = type.rawValue.periodIntervalMillis * Long(value) } + /// Returns string representation of this candle period. /// /// - Returns: The string representation. diff --git a/DXFeedFramework/Events/Market/Candles/CandlePrice.swift b/DXFeedFramework/Events/Market/Candles/CandlePrice.swift index 582cb5c26..a256af9dd 100644 --- a/DXFeedFramework/Events/Market/Candles/CandlePrice.swift +++ b/DXFeedFramework/Events/Market/Candles/CandlePrice.swift @@ -140,6 +140,7 @@ public enum CandlePrice: DXCandlePrice, CaseIterable { } return sValue } + /// Returns string representation of this candle price. /// /// - Returns: The string representation. diff --git a/DXFeedFramework/Events/Market/Candles/CandleSession.swift b/DXFeedFramework/Events/Market/Candles/CandleSession.swift index 4ebe501dd..ab9c8ddf9 100644 --- a/DXFeedFramework/Events/Market/Candles/CandleSession.swift +++ b/DXFeedFramework/Events/Market/Candles/CandleSession.swift @@ -115,6 +115,7 @@ public enum CandleSession: DXCandleSession, CaseIterable { } return sValue } + /// Returns string representation of this candle session. /// /// - Returns: The string representation. diff --git a/DXFeedFramework/Events/Market/Candles/CandleType.swift b/DXFeedFramework/Events/Market/Candles/CandleType.swift index 902dda313..a47648ff3 100644 --- a/DXFeedFramework/Events/Market/Candles/CandleType.swift +++ b/DXFeedFramework/Events/Market/Candles/CandleType.swift @@ -167,6 +167,7 @@ public enum CandleType: DXCandleType, CaseIterable { } return sValue } + /// Returns string representation of this candle type. /// /// The string representation of candle type is the shortest unique prefix of the diff --git a/DXFeedFramework/Events/Market/Extra/IEventType.swift b/DXFeedFramework/Events/Market/Extra/IEventType.swift index 30c6bce34..a3fdd0fb3 100644 --- a/DXFeedFramework/Events/Market/Extra/IEventType.swift +++ b/DXFeedFramework/Events/Market/Extra/IEventType.swift @@ -25,4 +25,7 @@ public protocol IEventType { /// /// [For more details see](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/event/EventType.html#getEventTime--) var eventTime: Int64 { get set } + + /// Hashcode fot perftest and other purposes. + var hashCode: Int { get } } diff --git a/DXFeedFramework/Events/Market/Extra/MarketEvent.swift b/DXFeedFramework/Events/Market/Extra/MarketEvent.swift index eeba48a18..1c86fc916 100644 --- a/DXFeedFramework/Events/Market/Extra/MarketEvent.swift +++ b/DXFeedFramework/Events/Market/Extra/MarketEvent.swift @@ -10,9 +10,24 @@ import Foundation /// extend this class. Market event classes are simple beans with setter and getter methods for their /// properties and minimal business logic. All market events have ``type`` /// property that is defined by this class. -public protocol MarketEvent: IEventType { - var type: EventCode { get } - func toString() -> String +public class MarketEvent: IEventType { + public var eventSymbol: String = "" + + public var eventTime: Int64 = 0 + + public lazy var hashCode: Int = { + return Int(bitPattern: Unmanaged.passUnretained(self).toOpaque()) + }() + + public var type: EventCode + + public func toString() -> String { + return "Override toString() method" + } + + internal init(type: EventCode) { + self.type = type + } } public struct MarketEventConst { diff --git a/DXFeedFramework/Events/Market/Extra/OrderBase.swift b/DXFeedFramework/Events/Market/Extra/OrderBase.swift index 184290cef..3760cbae4 100644 --- a/DXFeedFramework/Events/Market/Extra/OrderBase.swift +++ b/DXFeedFramework/Events/Market/Extra/OrderBase.swift @@ -7,9 +7,6 @@ import Foundation public class OrderBase: MarketEvent, IIndexedEvent, CustomStringConvertible { - - public private(set) var type: EventCode = .orderBase - public var eventSource: IndexedEventSource { get { var sourceId = index >> 48 @@ -32,9 +29,6 @@ public class OrderBase: MarketEvent, IIndexedEvent, CustomStringConvertible { public private(set) var index: Long = 0 - public var eventSymbol: String - - public var eventTime: Int64 = 0 /* * Flags property has several significant bits that are packed into an integer in the following way: * 31..15 14..11 10..4 3 2 1 0 @@ -123,7 +117,8 @@ public class OrderBase: MarketEvent, IIndexedEvent, CustomStringConvertible { /// Gets or sets trade size for events containing trade-related action. public var tradeSize: Double = .nan - init(_ eventSymbol: String) { + init(eventSymbol: String, type: EventCode) { + super.init(type: type) self.eventSymbol = eventSymbol } @@ -148,9 +143,10 @@ tradeId: \(tradeId), \ tradePrice: \(tradePrice), \ tradeSize: \(tradeSize) """ - } + } + /// Returns string representation of this candle event. - public func toString() -> String { + public override func toString() -> String { return "OrderBase{\(baseFieldsToString())}" } } @@ -164,7 +160,7 @@ extension OrderBase { } self.index = value } - + /// Gets a value indicating whether this order has some size public func hsaSize() -> Bool { return size != 0 && !size.isNaN diff --git a/DXFeedFramework/Events/Market/Extra/TradeBase.swift b/DXFeedFramework/Events/Market/Extra/TradeBase.swift index c98de3db8..c7afb1a08 100644 --- a/DXFeedFramework/Events/Market/Extra/TradeBase.swift +++ b/DXFeedFramework/Events/Market/Extra/TradeBase.swift @@ -12,13 +12,6 @@ import Foundation /// /// [For more details see] (https://docs.dxfeed.com/dxfeed/api/com/dxfeed/event/market/TradeBase.html) public class TradeBase: MarketEvent, ILastingEvent { - public var type: EventCode { - return .trade - } - - public var eventSymbol: String - public var eventTime: Int64 = 0 - /* * Flags property has several significant bits that are packed into an integer in the following way: * 31..4 3 2 1 0 @@ -55,7 +48,8 @@ public class TradeBase: MarketEvent, ILastingEvent { /// Do not use this method directly. var flags: Int32 = 0 - public init(_ symbol: String) { + public init(symbol: String, type: EventCode) { + super.init(type: type) self.eventSymbol = symbol } @@ -78,7 +72,7 @@ flags: \(flags) } /// Returns string representation of this base trade event's. - public func toString() -> String { + public override func toString() -> String { return "\(typeName){\(baseFieldsToString())}" } diff --git a/DXFeedFramework/Events/Market/Greeks.swift b/DXFeedFramework/Events/Market/Greeks.swift index 2656815b3..3782826fb 100644 --- a/DXFeedFramework/Events/Market/Greeks.swift +++ b/DXFeedFramework/Events/Market/Greeks.swift @@ -23,12 +23,6 @@ import Foundation /// /// [For more details see](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/event/option/Greeks.html) public class Greeks: MarketEvent, ITimeSeriesEvent, ILastingEvent, CustomStringConvertible { - public var type: EventCode = .greeks - - public var eventSymbol: String - - public var eventTime: Int64 = 0 - public var eventSource: IndexedEventSource = .defaultSource public var eventFlags: Int32 = 0 @@ -62,6 +56,7 @@ public class Greeks: MarketEvent, ITimeSeriesEvent, ILastingEvent, CustomStringC public var vega: Double = .nan public init(_ eventSymbol: String) { + super.init(type: .greeks) self.eventSymbol = eventSymbol } @@ -79,6 +74,24 @@ gamma: \(gamma), \ theta: \(theta), \ rho: \(rho), \ vega: \(vega) +""" + } + + /// Returns string representation of this greeks fields. + public override func toString() -> String { + return """ +Greeks{\(eventSymbol), \ +eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \ +eventFlags=\(eventFlags.toHexString()), \ +time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \ +sequence=\(self.getSequence()), \ +price=\(price), \ +volatility=\(volatility), \ +delta=\(delta), \ +gamma=\(gamma), \ +theta=\(theta), \ +rho=\(rho), \ +vega=\(vega)} """ } } @@ -114,21 +127,5 @@ extension Greeks { } index = Long(index & ~Long(MarketEventConst.maxSequence)) | Long(sequence) } - /// Returns string representation of this greeks fields. - public func toString() -> String { - return """ -Greeks{\(eventSymbol), \ -eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \ -eventFlags=\(eventFlags.toHexString()), \ -time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \ -sequence=\(self.getSequence()), \ -price=\(price), \ -volatility=\(volatility), \ -delta=\(delta), \ -gamma=\(gamma), \ -theta=\(theta), \ -rho=\(rho), \ -vega=\(vega)} -""" - } + } diff --git a/DXFeedFramework/Events/Market/OptionSale.swift b/DXFeedFramework/Events/Market/OptionSale.swift index c4a47fe79..96101a0b9 100644 --- a/DXFeedFramework/Events/Market/OptionSale.swift +++ b/DXFeedFramework/Events/Market/OptionSale.swift @@ -17,7 +17,6 @@ import Foundation /// /// [For more details see](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/event/market/OptionSale.html) public class OptionSale: MarketEvent, IIndexedEvent { - public var type: EventCode = .optionSale public var eventSource: IndexedEventSource = .defaultSource @@ -25,10 +24,6 @@ public class OptionSale: MarketEvent, IIndexedEvent { public var index: Long = 0 - public var eventSymbol: String - - public var eventTime: Int64 = 0 - /* * EventFlags property has several significant bits that are packed into an integer in the following way: * 31..7 6 5 4 3 2 1 0 @@ -75,8 +70,39 @@ public class OptionSale: MarketEvent, IIndexedEvent { public var optionSymbol: String? public init(_ eventSymbol: String) { + super.init(type: .optionSale) self.eventSymbol = eventSymbol } + + /// Returns string representation of this time and sale event. + public override func toString() -> String { + return """ +OptionSale{\(eventSymbol), \ +eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \ +eventFlags=\(eventFlags.toHexString()), \ +index=\(index.toHexString()), \ +time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \ +timeNanoPart=\(timeNanoPart), \ +sequence=\(getSequence()), \ +exchange=\(StringUtil.encodeChar(char: exchangeCode)), \ +price=\(price), \ +size=\(size), \ +bid=\(bidPrice), \ +ask=\(askPrice), \ +ESC='\(exchangeSaleConditions ?? "null")', \ +TTE=\(StringUtil.encodeChar(char: Int16(getTradeThroughExempt().unicodeScalars.first?.value ?? 0))), \ +side=\(aggressorSide), \ +spread=\(isSpreadLeg), \ +ETH=\(isExtendedTradingHours), \ +validTick=\(isValidTick), \ +type=\(optionSaleType), \ +underlyingPrice=\(underlyingPrice), \ +volatility=\(volatility), \ +delta=\(delta), \ +optionSymbol='\(optionSymbol ?? "null")'\ +} +""" + } } extension OptionSale { @@ -233,33 +259,4 @@ extension OptionSale { optionSaleType == .cancel } - /// Returns string representation of this time and sale event. - public func toString() -> String { - return """ -OptionSale{\(eventSymbol), \ -eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \ -eventFlags=\(eventFlags.toHexString()), \ -index=\(index.toHexString()), \ -time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \ -timeNanoPart=\(timeNanoPart), \ -sequence=\(getSequence()), \ -exchange=\(StringUtil.encodeChar(char: exchangeCode)), \ -price=\(price), \ -size=\(size), \ -bid=\(bidPrice), \ -ask=\(askPrice), \ -ESC='\(exchangeSaleConditions ?? "null")', \ -TTE=\(StringUtil.encodeChar(char: Int16(getTradeThroughExempt().unicodeScalars.first?.value ?? 0))), \ -side=\(aggressorSide), \ -spread=\(isSpreadLeg), \ -ETH=\(isExtendedTradingHours), \ -validTick=\(isValidTick), \ -type=\(optionSaleType), \ -underlyingPrice=\(underlyingPrice), \ -volatility=\(volatility), \ -delta=\(delta), \ -optionSymbol='\(optionSymbol ?? "null")'\ -} -""" - } } diff --git a/DXFeedFramework/Events/Market/Order.swift b/DXFeedFramework/Events/Market/Order.swift index 2f3a3fdb3..1850d83f1 100644 --- a/DXFeedFramework/Events/Market/Order.swift +++ b/DXFeedFramework/Events/Market/Order.swift @@ -14,13 +14,19 @@ import Foundation /// /// [For more details see](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/event/market/Order.html) public class Order: OrderBase { - public override var type: EventCode { - return .order - } /// Gets or sets market maker or other aggregate identifier of this order. /// This value is defined for ``Scope/aggregate`` and ``Scope/order`` orders. public var marketMaker: String? + public convenience init(_ eventSymbol: String) { + self.init(eventSymbol: eventSymbol, type: .order) + } + + /// Initializes a new instance of the ``Order`` class. + override init(eventSymbol: String, type: EventCode) { + super.init(eventSymbol: eventSymbol, type: type) + } + /// Returns string representation of this candle event. public override func toString() -> String { return diff --git a/DXFeedFramework/Events/Market/Profile.swift b/DXFeedFramework/Events/Market/Profile.swift index af30e01dd..ccfa747aa 100644 --- a/DXFeedFramework/Events/Market/Profile.swift +++ b/DXFeedFramework/Events/Market/Profile.swift @@ -12,10 +12,6 @@ import Foundation /// /// [For more details see](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/event/market/Profile.html) public class Profile: MarketEvent, ILastingEvent, CustomStringConvertible { - public let type: EventCode = .profile - public var eventSymbol: String - public var eventTime: Int64 = 0 - /* * Flags property has several significant bits that are packed into an integer in the following way: * 31..4 3 2 1 0 @@ -71,6 +67,7 @@ public class Profile: MarketEvent, ILastingEvent, CustomStringConvertible { /// Initializes a new instance of the ``Profile`` class. public init(_ symbol: String) { + super.init(type: .profile) self.eventSymbol = symbol } @@ -97,6 +94,12 @@ freeFloat: \(freeFloat), \ flags: \(flags) """ } + + /// Returns string representation of this profile event. + public override func toString() -> String { + return "Profile{\(baseFieldsToString())}" + } + } extension Profile { @@ -133,11 +136,6 @@ extension Profile { return tradingStatus == .halted } - /// Returns string representation of this profile event. - public func toString() -> String { - return "Profile{\(baseFieldsToString())}" - } - /// Returns string representation of this order fields. func baseFieldsToString() -> String { return """ diff --git a/DXFeedFramework/Events/Market/Quote.swift b/DXFeedFramework/Events/Market/Quote.swift index 42dc6f89f..f03257308 100644 --- a/DXFeedFramework/Events/Market/Quote.swift +++ b/DXFeedFramework/Events/Market/Quote.swift @@ -13,9 +13,6 @@ import Foundation /// /// [For more details see](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/event/market/Quote.html) public class Quote: MarketEvent, ILastingEvent, CustomStringConvertible { - public let type: EventCode = .quote - public var eventSymbol: String - public var eventTime: Int64 = 0 /// Gets or sets time millis sequence. /// Do not sets this value directly. /// Change ``setSequence(_:)`` and/or ``time``. @@ -62,7 +59,8 @@ public class Quote: MarketEvent, ILastingEvent, CustomStringConvertible { public var askSize: Double = .nan /// Initializes a new instance of the ``Quote`` class. - public init(_ symbol: String) { + public required init(_ symbol: String) { + super.init(type: .quote) self.eventSymbol = symbol } public var description: String { @@ -82,6 +80,12 @@ askPrice: \(askPrice), \ askSize: \(askSize) """ } + + /// Returns string representation of this quote event. + public override func toString() -> String { + return "Quote{\(baseFieldsToString())}" + } + } extension Quote { @@ -120,10 +124,6 @@ extension Quote { public var timeNanos: Int64 { return TimeNanosUtil.getNanosFromMillisAndNanoPart(time, timeNanoPart) } - /// Returns string representation of this quote event. - public func toString() -> String { - return "Quote{\(baseFieldsToString())}" - } func baseFieldsToString() -> String { return """ diff --git a/DXFeedFramework/Events/Market/Series.swift b/DXFeedFramework/Events/Market/Series.swift index 086c437b9..66ec73aad 100644 --- a/DXFeedFramework/Events/Market/Series.swift +++ b/DXFeedFramework/Events/Market/Series.swift @@ -14,18 +14,12 @@ import Foundation /// /// (For more details see)[https://docs.dxfeed.com/dxfeed/api/com/dxfeed/event/option/Series.html] public class Series: MarketEvent, IIndexedEvent { - public var type: EventCode = .series - public var eventSource: IndexedEventSource = .defaultSource public var eventFlags: Int32 = 0 public var index: Long = 0 - public var eventSymbol: String - - public var eventTime: Int64 = 0 - /* * EventFlags property has several significant bits that are packed into an integer in the following way: * 31..7 6 5 4 3 2 1 0 @@ -59,8 +53,30 @@ public class Series: MarketEvent, IIndexedEvent { public var interest: Double = .nan public init(_ eventSymbol: String) { + super.init(type: .series) self.eventSymbol = eventSymbol } + + /// Returns string representation of this candle event. + public override func toString() -> String { + return +""" +Series{\(eventSymbol), \ +eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \ +eventFlags=\(eventFlags.toHexString()), \ +index=\(index.toHexString()), \ +time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \ +sequence=\(self.getSequence()), \ +expiration=\(DayUtil.getYearMonthDayByDayId(Int(expiration))), \ +volatility=\(volatility), \ +callVolume=\(callVolume), \ +putVolume=\(putVolume), \ +putCallRatio=\(putCallRatio), \ +forwardPrice=\(forwardPrice), \ +dividend=\(dividend), \ +interest=\(interest) +""" + } } extension Series { @@ -103,24 +119,4 @@ extension Series { return callVolume.isNaN ? putVolume : putVolume + putVolume } - /// Returns string representation of this candle event. - public func toString() -> String { - return -""" -Series{\(eventSymbol), \ -eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \ -eventFlags=\(eventFlags.toHexString()), \ -index=\(index.toHexString()), \ -time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \ -sequence=\(self.getSequence()), \ -expiration=\(DayUtil.getYearMonthDayByDayId(Int(expiration))), \ -volatility=\(volatility), \ -callVolume=\(callVolume), \ -putVolume=\(putVolume), \ -putCallRatio=\(putCallRatio), \ -forwardPrice=\(forwardPrice), \ -dividend=\(dividend), \ -interest=\(interest) -""" - } } diff --git a/DXFeedFramework/Events/Market/SpreadOrder.swift b/DXFeedFramework/Events/Market/SpreadOrder.swift index 863a66b21..1a7762617 100644 --- a/DXFeedFramework/Events/Market/SpreadOrder.swift +++ b/DXFeedFramework/Events/Market/SpreadOrder.swift @@ -21,14 +21,12 @@ import Foundation /// /// [For more details see](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/event/market/SpreadOrder.html) public class SpreadOrder: OrderBase { - public override var type: EventCode { - return .spreadOrder - } /// Gets or sets spread symbol of this event. public var spreadSymbol: String? - public override init(_ eventSymbol: String) { - super.init(eventSymbol) + /// Initializes a new instance of the ``SpreadOrder`` class. + public init(_ eventSymbol: String) { + super.init(eventSymbol: eventSymbol, type: .spreadOrder) } /// Returns string representation of this candle event. diff --git a/DXFeedFramework/Events/Market/Summary.swift b/DXFeedFramework/Events/Market/Summary.swift index 3e43e45be..6212d5ad6 100644 --- a/DXFeedFramework/Events/Market/Summary.swift +++ b/DXFeedFramework/Events/Market/Summary.swift @@ -16,10 +16,6 @@ import Foundation /// /// [For more details see](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/event/market/Summary.html) public class Summary: MarketEvent, ILastingEvent, CustomStringConvertible { - public let type: EventCode = .summary - public var eventSymbol: String - public var eventTime: Int64 = 0 - /* * Flags property has several significant bits that are packed into an integer in the following way: * 31..4 3 2 1 0 @@ -61,6 +57,7 @@ public class Summary: MarketEvent, ILastingEvent, CustomStringConvertible { /// Initializes a new instance of the ``Summary`` class. public init(_ eventSymbol: String) { + super.init(type: .summary) self.eventSymbol = eventSymbol } @@ -79,6 +76,27 @@ prevDayClosePrice: \(prevDayClosePrice), \ prevDayVolume: \(prevDayVolume), \ openInterest: \(openInterest), \ flags: \(flags) +""" + } + + /// Returns string representation of this summary fields. + public override func toString() -> String { + return +""" +Summary{\(eventSymbol), \ +eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \ +day=\(DayUtil.getYearMonthDayByDayId(dayId)), \ +dayOpen=\(dayOpenPrice), \ +dayHigh=\(dayHighPrice), \ +dayLow=\(dayLowPrice), \ +dayClose=\(dayClosePrice), \ +dayCloseType=\(dayClosePriceType), \ +prevDay=\(DayUtil.getYearMonthDayByDayId(prevDayId)), \ +prevDayClose=\(prevDayClosePrice), \ +prevDayCloseType=\(prevDayClosePriceType), \ +prevDayVolume=\(prevDayVolume), \ +openInterest=\(openInterest), \ +} """ } } @@ -113,24 +131,4 @@ extension Summary { } } - /// Returns string representation of this summary fields. - public func toString() -> String { - return -""" -Summary{\(eventSymbol), \ -eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \ -day=\(DayUtil.getYearMonthDayByDayId(dayId)), \ -dayOpen=\(dayOpenPrice), \ -dayHigh=\(dayHighPrice), \ -dayLow=\(dayLowPrice), \ -dayClose=\(dayClosePrice), \ -dayCloseType=\(dayClosePriceType), \ -prevDay=\(DayUtil.getYearMonthDayByDayId(prevDayId)), \ -prevDayClose=\(prevDayClosePrice), \ -prevDayCloseType=\(prevDayClosePriceType), \ -prevDayVolume=\(prevDayVolume), \ -openInterest=\(openInterest), \ -} -""" - } } diff --git a/DXFeedFramework/Events/Market/TheoPrice.swift b/DXFeedFramework/Events/Market/TheoPrice.swift index c9c02b5bb..4f2eeb451 100644 --- a/DXFeedFramework/Events/Market/TheoPrice.swift +++ b/DXFeedFramework/Events/Market/TheoPrice.swift @@ -18,11 +18,6 @@ import Foundation /// /// [For more details see](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/event/option/TheoPrice.html) public class TheoPrice: MarketEvent, ITimeSeriesEvent, ILastingEvent, CustomStringConvertible { - public var type: EventCode = .theoPrice - - public var eventSymbol: String - - public var eventTime: Int64 = 0 public var eventSource: IndexedEventSource = .defaultSource @@ -54,6 +49,7 @@ public class TheoPrice: MarketEvent, ITimeSeriesEvent, ILastingEvent, CustomStri public var interest: Double = .nan public init(_ eventSymbol: String) { + super.init(type: .theoPrice) self.eventSymbol = eventSymbol } @@ -72,6 +68,24 @@ dividend: \(dividend), \ interest: \(interest) """ } + + /// Returns string representation of this order fields. + public override func toString() -> String { + return """ +TheoPrice{\(eventSymbol) \ +eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \ +eventFlags=\(eventFlags.toHexString()), \ +time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \ +sequence=\(self.getSequence()), \ +price=\(price) \ +underlyingPrice=\(underlyingPrice), \ +delta=\(delta), \ +gamma=\(gamma), \ +dividend=\(dividend), \ +interest=\(interest), \ +} +""" + } } extension TheoPrice { @@ -105,21 +119,4 @@ extension TheoPrice { } index = Long(index & ~Long(MarketEventConst.maxSequence)) | Long(sequence) } - /// Returns string representation of this order fields. - public func toString() -> String { - return """ -TheoPrice{\(eventSymbol) \ -eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \ -eventFlags=\(eventFlags.toHexString()), \ -time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \ -sequence=\(self.getSequence()), \ -price=\(price) \ -underlyingPrice=\(underlyingPrice), \ -delta=\(delta), \ -gamma=\(gamma), \ -dividend=\(dividend), \ -interest=\(interest), \ -} -""" - } } diff --git a/DXFeedFramework/Events/Market/TimeAndSale.swift b/DXFeedFramework/Events/Market/TimeAndSale.swift index de9d3bea6..3a0b3e7d0 100644 --- a/DXFeedFramework/Events/Market/TimeAndSale.swift +++ b/DXFeedFramework/Events/Market/TimeAndSale.swift @@ -14,10 +14,6 @@ import Foundation /// /// [For more details see](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/event/market/TimeAndSale.html) public class TimeAndSale: MarketEvent, ITimeSeriesEvent, CustomStringConvertible { - public let type: EventCode = .timeAndSale - public var eventSymbol: String - public var eventTime: Int64 = 0 - /* * Flags property has several significant bits that are packed into an integer in the following way: * 31..16 15...8 7 6 5 4 3 2 1 0 @@ -73,6 +69,7 @@ public class TimeAndSale: MarketEvent, ITimeSeriesEvent, CustomStringConvertible public var index: Long = 0 init(_ symbol: String) { + super.init(type: .timeAndSale) self.eventSymbol = symbol } @@ -93,6 +90,33 @@ exchangeSaleConditions: \(exchangeSaleConditions ?? "null"), \ flags: \(flags), \ buyer: \(buyer ?? "null"), \ seller: \(seller ?? "null") +""" + } + + /// Returns string representation of this time and sale event. + public override func toString() -> String { + return """ +TimeAndSale{\(eventSymbol), \ +eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \ +eventFlags=\(eventFlags.toHexString()), \ +time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \ +timeNanoPart=\(timeNanoPart), \ +sequence=\(getSequence()), \ +exchange=\(StringUtil.encodeChar(char: exchangeCode)), \ +price=\(price), \ +size=\(size), \ +bid=\(bidPrice), \ +ask=\(askPrice), \ +ESC='\(exchangeSaleConditions ?? "null")', \ +TTE=\(StringUtil.encodeChar(char: Int16(getTradeThroughExempt().unicodeScalars.first?.value ?? 0))), \ +side=\(aggressorSide), \ +spread=\(isSpreadLeg), \ +ETH=\(isExtendedTradingHours), \ +validTick=\(isValidTick), \ +type=\(timeAndSaleType)\ +\(buyer == nil ? "" : ", buyer='\(buyer ?? "null")'")\ +\(seller == nil ? "" : ", seller='\(seller ?? "null")'")\ +} """ } } @@ -250,30 +274,4 @@ extension TimeAndSale { timeAndSaleType == .cancel } - /// Returns string representation of this time and sale event. - public func toString() -> String { - return """ -TimeAndSale{\(eventSymbol), \ -eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \ -eventFlags=\(eventFlags.toHexString()), \ -time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \ -timeNanoPart=\(timeNanoPart), \ -sequence=\(getSequence()), \ -exchange=\(StringUtil.encodeChar(char: exchangeCode)), \ -price=\(price), \ -size=\(size), \ -bid=\(bidPrice), \ -ask=\(askPrice), \ -ESC='\(exchangeSaleConditions ?? "null")', \ -TTE=\(StringUtil.encodeChar(char: Int16(getTradeThroughExempt().unicodeScalars.first?.value ?? 0))), \ -side=\(aggressorSide), \ -spread=\(isSpreadLeg), \ -ETH=\(isExtendedTradingHours), \ -validTick=\(isValidTick), \ -type=\(timeAndSaleType)\ -\(buyer == nil ? "" : ", buyer='\(buyer ?? "null")'")\ -\(seller == nil ? "" : ", seller='\(seller ?? "null")'")\ -} -""" - } } diff --git a/DXFeedFramework/Events/Market/Trade.swift b/DXFeedFramework/Events/Market/Trade.swift index 148883fe1..4d5d5de11 100644 --- a/DXFeedFramework/Events/Market/Trade.swift +++ b/DXFeedFramework/Events/Market/Trade.swift @@ -14,9 +14,10 @@ import Foundation /// /// [For more details see](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/event/market/Trade.html) public class Trade: TradeBase { - public override var type: EventCode { - return .trade + public init(_ symbol: String) { + super.init(symbol: symbol, type: .trade) } + /// Returns string representation of this trade event. public override func toString() -> String { return "Trade{\(baseFieldsToString())}" diff --git a/DXFeedFramework/Events/Market/TradeETH.swift b/DXFeedFramework/Events/Market/TradeETH.swift index adecf4a0f..46e021ab3 100644 --- a/DXFeedFramework/Events/Market/TradeETH.swift +++ b/DXFeedFramework/Events/Market/TradeETH.swift @@ -16,8 +16,9 @@ import Foundation /// /// [For more details see](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/event/market/TradeETH.html) public class TradeETH: TradeBase { - public override var type: EventCode { - return .tradeETH + + public init(_ symbol: String) { + super.init(symbol: symbol, type: .tradeETH) } /// Returns string representation of this trade event. diff --git a/DXFeedFramework/Events/Market/Underlying.swift b/DXFeedFramework/Events/Market/Underlying.swift index c578d2b19..064f2bd34 100644 --- a/DXFeedFramework/Events/Market/Underlying.swift +++ b/DXFeedFramework/Events/Market/Underlying.swift @@ -14,12 +14,6 @@ import Foundation /// /// [For more details see](https://docs.dxfeed.com/dxfeed/api/com/dxfeed/event/option/Underlying.html) public class Underlying: MarketEvent, ITimeSeriesEvent, ILastingEvent, CustomStringConvertible { - public var type: EventCode = .underlying - - public var eventSymbol: String - - public var eventTime: Int64 = 0 - public var eventSource: IndexedEventSource = .defaultSource public var eventFlags: Int32 = 0 @@ -48,6 +42,7 @@ public class Underlying: MarketEvent, ITimeSeriesEvent, ILastingEvent, CustomStr public var putCallRatio: Double = .nan public init(_ eventSymbol: String) { + super.init(type: .underlying) self.eventSymbol = eventSymbol } @@ -65,7 +60,24 @@ callVolume: \(callVolume), \ putVolume: \(putVolume), \ putCallRatio: \(putCallRatio) """ - } + } + + /// Returns string representation of this underlying fields. + public override func toString() -> String { + return """ +Underlying{"\(eventSymbol) \ +eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \ +eventFlags=\(eventFlags.toHexString()), \ +time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \ +sequence=\(self.getSequence()), \ +volatility=\(volatility), \ +frontVolatility=\(frontVolatility), \ +backVolatility=\(backVolatility), \ +callVolume=\(callVolume), \ +putVolume=\(putVolume), \ +putCallRatio=\(putCallRatio)} +""" + } } extension Underlying { @@ -106,20 +118,5 @@ extension Underlying { } index = Long(index & ~Long(MarketEventConst.maxSequence)) | Long(sequence) } - /// Returns string representation of this underlying fields. - public func toString() -> String { - return """ -Underlying{"\(eventSymbol) \ -eventTime=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: eventTime)) ?? ""), \ -eventFlags=\(eventFlags.toHexString()), \ -time=\((try? DXTimeFormat.defaultTimeFormat?.withMillis?.format(value: time)) ?? ""), \ -sequence=\(self.getSequence()), \ -volatility=\(volatility), \ -frontVolatility=\(frontVolatility), \ -backVolatility=\(backVolatility), \ -callVolume=\(callVolume), \ -putVolume=\(putVolume), \ -putCallRatio=\(putCallRatio)} -""" - } + } diff --git a/DXFeedFramework/Native/Events/Markets/Candle+Ext.swift b/DXFeedFramework/Native/Events/Markets/Candle+Ext.swift index 5868db630..a3fd1d4b0 100644 --- a/DXFeedFramework/Native/Events/Markets/Candle+Ext.swift +++ b/DXFeedFramework/Native/Events/Markets/Candle+Ext.swift @@ -9,7 +9,7 @@ import Foundation extension Candle { convenience init(native: dxfg_candle_t) { - self.init() + self.init(type: .candle) self.eventSymbol = String(pointee: native.event_symbol) self.eventTime = native.event_time self.eventFlags = native.event_flags diff --git a/DXFeedFrameworkTests/EventsTest.swift b/DXFeedFrameworkTests/EventsTest.swift index c50d7c8cd..6180e6d5d 100644 --- a/DXFeedFrameworkTests/EventsTest.swift +++ b/DXFeedFrameworkTests/EventsTest.swift @@ -8,6 +8,13 @@ import XCTest @testable import DXFeedFramework final class EventsTest: XCTestCase { + func testEventEquality() throws { + let quote = Quote("aa") + let quote1 = Quote("abc") + XCTAssert(quote.hashCode != quote1.hashCode) + XCTAssert(quote.hashCode == quote.hashCode) + } + func testConversion() throws { let convertedSet = Set(EventCode.allCases.map { $0.nativeCode() }.compactMap { EventCode.convert($0) }) let difValues = Array(Set(EventCode.allCases).symmetricDifference(convertedSet)) diff --git a/Samples/Utils/Performance/PerfTestEventListener.swift b/Samples/Utils/Performance/PerfTestEventListener.swift index ae064923b..41090c75e 100644 --- a/Samples/Utils/Performance/PerfTestEventListener.swift +++ b/Samples/Utils/Performance/PerfTestEventListener.swift @@ -9,7 +9,13 @@ import DXFeedFramework class PerfTestEventListener: AbstractEventListener { private let diagnostic = PerfDiagnostic() + private var blackHoleHashCode: Int = 0 + override func handleEvents(_ events: [MarketEvent]) { + events.forEach { + //use logical OR to avoid overflow + blackHoleHashCode = blackHoleHashCode | $0.hashCode + } let count = events.count diagnostic.updateCounters(Int64(count)) }