-
Notifications
You must be signed in to change notification settings - Fork 1
/
Contents.swift
69 lines (57 loc) · 2.14 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import Cocoa
import PlaygroundSupport
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)
}
}
// An sample that demonstrates a subscription using InstrumentProfile.
// Use default DXFeed instance for that data feed address is defined by "dxfeed.properties" file.
// The properties file is copied to the build output directory from the project directory.
var type = TimeAndSale.self
// You can use local ipf file
var ipfFile = "https://demo:[email protected]/ipf"
// Use path to dxfeed.properties with feed address
let propertiesFilePath = Bundle.main.path(forResource: "dxfeed.properties", ofType: nil)
try SystemProperty.setProperty(DXEndpoint.Property.properties.rawValue, propertiesFilePath ?? "")
print("Reading instruments from \(ipfFile)")
guard let profiles = try DXInstrumentProfileReader().readFromFile(address: ipfFile) else {
fatalError("No profiles in \(ipfFile)")
}
// This is just a sample, any arbitrary filtering may go here.
let symbols = profiles.filter({ ipf in
ipf.getIpfType() == .stock
}).map({ profile in
profile.symbol
})
print("Selected symbols are: \(symbols)")
let subscription = try DXEndpoint.getInstance().getFeed()?.createSubscription(type)
let listener = Listener { listener in
listener.callback = { events in
// Prints all received events.
events.forEach { event in
print(event.toString())
}
}
return listener
}
// Listener must be attached before symbols are added.
try subscription?.add(listener: listener)
// Adds specified symbol.
try subscription?.addSymbols(symbols)
// infinity execution
PlaygroundPage.current.needsIndefiniteExecution = true
// to finish execution run this line
PlaygroundPage.current.finishExecution()