-
Notifications
You must be signed in to change notification settings - Fork 1
/
Contents.swift
68 lines (57 loc) · 2.04 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
import Foundation
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)
}
}
// Creates multiple event listener and subscribe to Quote and Trade events.
// Use default DXFeed instance for that data feed address is defined by "dxfeed.properties" file.
let symbol = "AAPL"
// 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 ?? "")
let subscriptionQuote = try DXEndpoint.getInstance()
.getFeed()?
.createSubscription(Quote.self)
// Listener must be attached before symbols are added.
let listener = Listener { listener in
listener.callback = { events in
events.forEach { event in
print("Mid = \((event.quote.bidPrice + event.quote.askPrice) / 2)")
}
}
return listener
}
try subscriptionQuote?.add(listener: listener)
try subscriptionQuote?.addSymbols(symbol)
let subscriptionTrade = try DXEndpoint.getInstance()
.getFeed()?
.createSubscription([Trade.self, Quote.self])
let listenerTrade = Listener { listener in
listener.callback = { events in
events.forEach { event in
print(event.toString())
}
}
return listener
}
// Listener must be attached before symbols are added.
try subscriptionTrade?.add(listener: listenerTrade)
try subscriptionTrade?.addSymbols(symbol)
// infinity execution
PlaygroundPage.current.needsIndefiniteExecution = true
// to finish execution run this line
PlaygroundPage.current.finishExecution()