-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DxFeedSample
- Loading branch information
Showing
6 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
Samples/Playgrounds/DxFeedSample.playground/Contents.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import DXFeedFramework | ||
import Foundation | ||
import Cocoa | ||
import PlaygroundSupport | ||
|
||
//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" | ||
|
||
let propertiesFilePath = Bundle.main.path(forResource: "dxfeed.properties", ofType: nil) | ||
try SystemProperty.setProperty(DXEndpoint.Property.properties.rawValue, propertiesFilePath ?? "") | ||
|
||
|
||
|
||
let subscription = try DXEndpoint.getInstance() | ||
.getFeed()? | ||
.createSubscription(EventCode.quote) | ||
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 subscription?.add(listener: listener) | ||
try subscription?.addSymbols(symbol) | ||
|
||
let subscriptionTrade = try DXEndpoint.getInstance() | ||
.getFeed()? | ||
.createSubscription([EventCode.trade, EventCode.quote]) | ||
let listenerTrade = Listener { listener in | ||
listener.callback = { events in | ||
events.forEach { event in | ||
print(event.toString()) | ||
} | ||
} | ||
return listener | ||
} | ||
try subscriptionTrade?.add(listener: listenerTrade) | ||
try subscriptionTrade?.addSymbols(symbol) | ||
|
||
|
||
// infinity execution | ||
PlaygroundPage.current.needsIndefiniteExecution = true | ||
|
||
// to finish execution run this line | ||
PlaygroundPage.current.finishExecution() |
2 changes: 2 additions & 0 deletions
2
Samples/Playgrounds/DxFeedSample.playground/Resources/dxfeed.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Connection address for an endpoint with role Feed and OnDemandFeed. | ||
dxfeed.address=demo.dxfeed.com:7300 |
4 changes: 4 additions & 0 deletions
4
Samples/Playgrounds/DxFeedSample.playground/contents.xcplayground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |