Skip to content

Commit

Permalink
feat: OnDemandSample
Browse files Browse the repository at this point in the history
  • Loading branch information
kosyloa committed Apr 29, 2024
1 parent 844de99 commit 70f7f94
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
2 changes: 2 additions & 0 deletions DXFeedFramework.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,7 @@
64A42F4D2B0B9FA4001C3ACC /* DXTimeZone.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DXTimeZone.swift; sourceTree = "<group>"; };
64A42F4F2B0BA668001C3ACC /* DXTimeFormat.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DXTimeFormat.swift; sourceTree = "<group>"; };
64A42F512B0BBB6D001C3ACC /* DXTimePeriod.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DXTimePeriod.swift; sourceTree = "<group>"; };
64A631CF2BDFAA27002E1002 /* OnDemandSample.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = OnDemandSample.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
64AAF0522A8113E800E8942B /* String+Range.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "String+Range.swift"; sourceTree = "<group>"; };
64AAF0542A82499A00E8942B /* ConcurrentDict.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConcurrentDict.swift; sourceTree = "<group>"; };
64AAF0562A82A3FC00E8942B /* ICandleSymbolProperty.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ICandleSymbolProperty.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1112,6 +1113,7 @@
64F9C6C12B4BFD8F003ED014 /* DXFeedconnect.playground */,
644551C92B973A0D0069E3A2 /* FetchDailyCandles.playground */,
6433B1302BCE87D4004EFED7 /* RequestProfile.playground */,
64A631CF2BDFAA27002E1002 /* OnDemandSample.playground */,
643A329C2BD15F2900F6F790 /* LastEventsConsole.playground */,
);
path = Playgrounds;
Expand Down
4 changes: 2 additions & 2 deletions DXFeedFramework/Utils/Date+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

import Foundation

extension Date {
public extension Date {
func millisecondsSince1970() -> TimeInterval {
return timeIntervalSince1970 * 1000
}

func millisecondsSince1970() -> Int64 {
func millisecondsSince1970() -> Long {
return Int64(timeIntervalSince1970 * 1000)
}

Expand Down
75 changes: 75 additions & 0 deletions Samples/Playgrounds/OnDemandSample.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import Cocoa
import DXFeedFramework

// Empty Listener with handler
class Listener: DXEventListener, Hashable {

static func == (lhs: Listener, rhs: Listener) -> Bool {
lhs === rhs
}

func hash(into hasher: inout Hasher) {
hasher.combine("\(self):\(stringReference(self))")
}
var callback: ([MarketEvent]) -> Void = { _ in }

func receiveEvents(_ events: [MarketEvent]) {
self.callback(events)
}

init(overrides: (Listener) -> Listener) {
_ = overrides(self)
}
}

do {
// get on-demand-only data feed
let onDemand = try OnDemandService.getInstance()
let feed = onDemand.getEndpoint()?.getFeed()

// prepare time format
let timeZone = try DXTimeZone(timeZoneID: "America/New_York")
let defaultTimeFormat = try DXTimeFormat.init(timeZone: timeZone)

// subscribe to Accenture symbol ACN to print its quotes
let subscription = try feed?.createSubscription(Quote.self)

let listener = Listener { anonymCl in
anonymCl.callback = { events in
events.forEach { event in
if let quote = event as? Quote {
let time = (try? defaultTimeFormat.format(value: quote.eventTime)) ?? ""
print("\(time):bid \(quote.bidPrice) / ask \(quote.askPrice)")
}
}
}
return anonymCl
}

try subscription?.add(listener: listener)
try subscription?.addSymbols("ACN")

// Watch Accenture drop under $1 on May 6, 2010 "Flashcrash" from 14:47:48 to 14:48:02 EST
guard let fromDate: Date = try defaultTimeFormat.parse("2010-05-06 14:47:48.000 EST"),
let toDate: Date = try defaultTimeFormat.parse("2010-05-06 14:48:02.000 EST") else {
print("Couldn't parse date")
exit(-1)
}

// switch into historical on-demand data replay mode
try onDemand.replay(date: fromDate)

//// replaying events until end time reached
while (onDemand.getTime ?? Date.init(timeIntervalSince1970: 0)) < toDate {
if let time: Long = onDemand.getTime?.millisecondsSince1970() {
let timeStr = (try? defaultTimeFormat.format(value: time)) ?? "empty string"
let connectedState = (try? onDemand.getEndpoint()?.getState()) ?? .notConnected
print("Current state is \(connectedState), on-demand time is \(timeStr)")
}
Thread.sleep(forTimeInterval: 1000)
}
// close endpoint completely to release resources
try onDemand.getEndpoint()?.closeAndAwaitTermination()
} catch {
print("Exception during sample: \(error)")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='macos' buildActiveScheme='true' importAppTypes='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>

0 comments on commit 70f7f94

Please sign in to comment.