Skip to content

Commit

Permalink
add OtcMarkets Mapper
Browse files Browse the repository at this point in the history
Waiting graal struct and event type
  • Loading branch information
kosyloa committed Mar 13, 2024
1 parent 084813f commit d86c6a6
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 50 deletions.
9 changes: 7 additions & 2 deletions DXFeedFramework/Api/DXFeed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,18 @@ public extension DXFeed {
})
}

func getIndexedEventsPromise(type: IEventType.Type, symbol: Symbol, source: IndexedEventSource) throws -> Promise? {
func getIndexedEventsPromise(type: IEventType.Type,
symbol: Symbol,
source: IndexedEventSource) throws -> Promise? {
let nativePromise = try native.getIndexedEventsPromise(type: type, symbol: symbol, source: source)
return Promise(native: nativePromise)
}

@available(macOS 10.15, *)
func getTimeSeries(type: IEventType.Type, symbol: Symbol, fromTime: Long, toTime: Long) -> Task<[MarketEvent]?, Error> {
func getTimeSeries(type: IEventType.Type,
symbol: Symbol,
fromTime: Long,
toTime: Long) -> Task<[MarketEvent]?, Error> {
let task = Task {
let defaultValue: [MarketEvent]? = nil
if Task.isCancelled {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ extension MarketEvent {
public var optionSale: OptionSale {
return (self as? OptionSale)!
}
/// Use only for event.type is ``EventCode/otcMarketsOrder``
public var otcMarketsOrder: OtcMarketsOrder {
return (self as? OtcMarketsOrder)!
}

/// Use only for event.type which supported ``ILastingEvent``
public var lastingEvent: ILastingEvent? {
switch self.type {
Expand Down
2 changes: 1 addition & 1 deletion DXFeedFramework/Events/Market/Order.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Order: OrderBase {
public convenience init(_ eventSymbol: String) {
self.init(eventSymbol: eventSymbol)
}

override func baseFieldsToString() -> String {
super.baseFieldsToString() + ", marketMaker='\(marketMaker ?? "null")'"
}
Expand Down
12 changes: 8 additions & 4 deletions DXFeedFramework/Events/Market/OtcMarketsOrder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class OtcMarketsOrder: Order {
private var quoteAccessPayment: Long = 0
var otcMarketsFlags: Long = 0

///Creates new OTC Markets order event with default values.
/// Creates new OTC Markets order event with default values.
public convenience init(_ eventSymbol: String) {
self.init(eventSymbol: eventSymbol)
}
Expand Down Expand Up @@ -82,7 +82,10 @@ public class OtcMarketsOrder: Order {
///
/// - Returns: OTC Markets price type of this OTC Markets order events.
public func getOtcMarketsPriceType() -> OtcMarketsPriceType {
return OtcMarketsPriceType.valueOf(Int(BitUtil.getBits(flags: otcMarketsFlags, mask: OtcMarketsOrder.OTC_PRICE_TYPE_MASK, shift: OtcMarketsOrder.OTC_PRICE_TYPE_SHIFT)))
return OtcMarketsPriceType.valueOf(
Int(BitUtil.getBits(flags: otcMarketsFlags,
mask: OtcMarketsOrder.OTC_PRICE_TYPE_MASK,
shift: OtcMarketsOrder.OTC_PRICE_TYPE_SHIFT)))
}

/// Changes OTC Markets price type of this OTC Markets order events.
Expand Down Expand Up @@ -111,7 +114,6 @@ public class OtcMarketsOrder: Order {
otcMarketsFlags & ~OtcMarketsOrder.SATURATED
}


/// Returns whether this event is in 'AutoEx' mode.
/// If this event is in 'AutoEx' mode then a response to an OTC Link trade message will be immediate.
///
Expand Down Expand Up @@ -143,7 +145,9 @@ public class OtcMarketsOrder: Order {
/// - Parameters:
/// - nmsConditional: true if this event represents a NMS conditional.
public func setNmsConditional(_ nmsConditional: Bool) {
otcMarketsFlags = nmsConditional ? otcMarketsFlags | OtcMarketsOrder.NMS_CONDITIONAL : otcMarketsFlags & ~OtcMarketsOrder.NMS_CONDITIONAL
otcMarketsFlags = nmsConditional ?
otcMarketsFlags | OtcMarketsOrder.NMS_CONDITIONAL :
otcMarketsFlags & ~OtcMarketsOrder.NMS_CONDITIONAL
}

override func baseFieldsToString() -> String {
Expand Down
2 changes: 1 addition & 1 deletion DXFeedFramework/Native/Feed/EventCode+Native.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ extension EventCode {
case .optionSale:
return DXFG_EVENT_OPTION_SALE
case .otcMarketsOrder:
return DXFG_EVENT_ORDER;
return DXFG_EVENT_ORDER
}
}
}
66 changes: 34 additions & 32 deletions DXFeedFramework/Native/Subscription/NativeSubscription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,38 +205,6 @@ class NativeSubscription {
listPointer))
}

func setSymbols(_ symbols: [Symbol]) throws {
let nativeSymbols = symbols.compactMap { SymbolMapper.newNative($0) }
let elements = ListNative(pointers: nativeSymbols)
let listPointer = elements.newList()
defer {
listPointer.deinitialize(count: 1)
listPointer.deallocate()
nativeSymbols.forEach { SymbolMapper.clearNative(symbol: $0) }
}

let thread = currentThread()
_ = try ErrorCheck.nativeCall(thread,
dxfg_DXFeedSubscription_setSymbols(thread,
self.subscription,
listPointer))
}

func getSymbols() throws -> [Symbol] {
let thread = currentThread()
let nativeResult = try ErrorCheck.nativeCall(thread,
dxfg_DXFeedSubscription_getSymbols(thread,
self.subscription))
defer {
_ = try? ErrorCheck.nativeCall(thread, dxfg_CList_symbol_release(thread, nativeResult))
}

var result: [Symbol] = SymbolMapper.newSymbols(symbols: nativeResult).compactMap({ obj in
obj as? Symbol
})
return result
}

func isClosed() -> Bool {
let thread = currentThread()
let success = try? ErrorCheck.nativeCall(thread, dxfg_DXFeedSubscription_isClosed(thread, self.subscription))
Expand Down Expand Up @@ -293,3 +261,37 @@ class NativeSubscription {
}
}
}

extension NativeSubscription {
func setSymbols(_ symbols: [Symbol]) throws {
let nativeSymbols = symbols.compactMap { SymbolMapper.newNative($0) }
let elements = ListNative(pointers: nativeSymbols)
let listPointer = elements.newList()
defer {
listPointer.deinitialize(count: 1)
listPointer.deallocate()
nativeSymbols.forEach { SymbolMapper.clearNative(symbol: $0) }
}

let thread = currentThread()
_ = try ErrorCheck.nativeCall(thread,
dxfg_DXFeedSubscription_setSymbols(thread,
self.subscription,
listPointer))
}

func getSymbols() throws -> [Symbol] {
let thread = currentThread()
let nativeResult = try ErrorCheck.nativeCall(thread,
dxfg_DXFeedSubscription_getSymbols(thread,
self.subscription))
defer {
_ = try? ErrorCheck.nativeCall(thread, dxfg_CList_symbol_release(thread, nativeResult))
}

var result: [Symbol] = SymbolMapper.newSymbols(symbols: nativeResult).compactMap({ obj in
obj as? Symbol
})
return result
}
}
2 changes: 2 additions & 0 deletions DXFeedFrameworkTests/FeedTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ final class FeedTest: XCTestCase {
return event is Series
case .optionSale:
return event is OptionSale
case .otcMarketsOrder:
return event is OtcMarketsOrder
}
return false
}
Expand Down
14 changes: 9 additions & 5 deletions Samples/Playgrounds/FetchDailyCandles.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import Cocoa
import DXFeedFramework

/// Fetches last N days of candles for a specified symbol, prints them, and exits.

// Fetches last N days of candles for a specified symbol, prints them, and exits.
// await couldn't use directly in Playground(this is accompanied by errors like: execution stopped with unexpected state)
Task {
let baseSymbol = CandleSymbol.valueOf("AAPL", [CandlePeriod.day])
var to: Long = Long(Date.now.timeIntervalSince1970 * 1000)
let from: Long = Long(Calendar.current.date(byAdding: .day, value: -20, to: Date())!.timeIntervalSince1970 * 1000)
var toTime: Long = Long(Date.now.timeIntervalSince1970 * 1000)
let fromFime: Long = Long(Calendar.current.date(byAdding: .day,
value: -20,
to: Date())!.timeIntervalSince1970 * 1000)
let endpoint = try DXEndpoint.getInstance().connect("demo.dxfeed.com:7300")
let feed = endpoint.getFeed()
guard let task = feed?.getTimeSeries(type: Candle.self, symbol: baseSymbol, fromTime: from, toTime: to) else {
guard let task = feed?.getTimeSeries(type: Candle.self,
symbol: baseSymbol,
fromTime: fromFime,
toTime: toTime) else {
print("Async task is nil")
exit(0)
}
Expand Down
13 changes: 8 additions & 5 deletions Samples/Tools/DumpTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ class DumpTool: ToolsCommand {

private var listeners = [EventListener]()

fileprivate func close(_ inputEndpoint: DXEndpoint, _ outputEndpoint: DXEndpoint?) throws {
try inputEndpoint.awaitNotConnected()
try inputEndpoint.closeAndAwaitTermination()
try outputEndpoint?.awaitProcessed()
try outputEndpoint?.closeAndAwaitTermination()
}

func execute() {
let address = arguments[1]
let symbols = arguments.parseSymbols()
Expand Down Expand Up @@ -106,11 +113,7 @@ class DumpTool: ToolsCommand {

try inputEndpoint.connect(address)

try inputEndpoint.awaitNotConnected()
try inputEndpoint.closeAndAwaitTermination()

try outputEndpoint?.awaitProcessed()
try outputEndpoint?.closeAndAwaitTermination()
try close(inputEndpoint, outputEndpoint)
} catch {
print("Dump tool error: \(error)")
}
Expand Down

0 comments on commit d86c6a6

Please sign in to comment.