Skip to content

Commit

Permalink
add sample:
Browse files Browse the repository at this point in the history
PrintQuoteEvents

fix test error(reproduced only on github builder)
  • Loading branch information
kosyloa committed Dec 5, 2023
1 parent 7f77c5f commit 0c47776
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 18 deletions.
2 changes: 2 additions & 0 deletions DXFeedFramework.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@
642DC9332AAA21C300974F5C /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
642DC9352AAA21C300974F5C /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
642DC9402AAA290300974F5C /* IpfCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IpfCell.swift; sourceTree = "<group>"; };
6435EE3C2B1F1E9200E8496C /* PrintQuoteEvents.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = PrintQuoteEvents.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
64437A8E2A9DEE6F005929B2 /* InstrumentProfile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstrumentProfile.swift; sourceTree = "<group>"; };
64437A912A9DF1DE005929B2 /* NativeInstrumentProfileReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativeInstrumentProfileReader.swift; sourceTree = "<group>"; };
6447A5DA2A8E559000739CCF /* ILastingEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ILastingEvent.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1006,6 +1007,7 @@
644C528F2B1E1D73002C034C /* DxFeedFileParser.playground */,
644C528D2B1E1CB8002C034C /* ConvertTapeFile.playground */,
644C52902B1E345E002C034C /* DxFeedSample.playground */,
6435EE3C2B1F1E9200E8496C /* PrintQuoteEvents.playground */,
);
path = Playgrounds;
sourceTree = "<group>";
Expand Down
10 changes: 5 additions & 5 deletions DXFeedFrameworkTests/DXConnectionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import XCTest
@testable import DXFeedFramework

private class Listener: DXEventListener {
private class DXConnectionListener: DXEventListener {
let expectation: XCTestExpectation

init(expectation: XCTestExpectation) {
Expand All @@ -24,8 +24,8 @@ private class Listener: DXEventListener {
}
}

extension Listener: Hashable {
static func == (lhs: Listener, rhs: Listener) -> Bool {
extension DXConnectionListener: Hashable {
static func == (lhs: DXConnectionListener, rhs: DXConnectionListener) -> Bool {
return lhs === rhs
}

Expand All @@ -51,7 +51,7 @@ final class DXConnectionTest: XCTestCase {
.build()
let subscription = try endpoint.getFeed()?.createSubscription(EventCode.quote)
let receivedEventsExpectation = expectation(description: "Received events")
let eventListener = Listener(expectation: receivedEventsExpectation)
let eventListener = DXConnectionListener(expectation: receivedEventsExpectation)
try subscription?.add(listener: eventListener)
try subscription?.addSymbols("AAPL")
wait(for: [receivedEventsExpectation], timeout: 2)
Expand All @@ -65,7 +65,7 @@ final class DXConnectionTest: XCTestCase {
.build()
let subscription = try endpoint.getFeed()?.createSubscription(EventCode.quote)
let receivedEventsExpectation = expectation(description: "Received events")
let eventListener = Listener(expectation: receivedEventsExpectation)
let eventListener = DXConnectionListener(expectation: receivedEventsExpectation)
try subscription?.add(listener: eventListener)
try subscription?.addSymbols("AAPL")
wait(for: [receivedEventsExpectation], timeout: 2)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ sudo /usr/bin/xattr -r -d com.apple.quarantine <directory_with_tools>

- [x] ConvertTapeFile demonstrates how to convert one tape file to another tape file with optional intermediate processing or filtering
- [x] DxFeedFileParser is a simple demonstration of how events are read form a tape file
- [ ] DxFeedSample is a simple demonstration of how to create multiple event listeners and subscribe to `Quote` and `Trade` events
- [ ] PrintQuoteEvents is a simple demonstration of how to subscribe to the `Quote` event, using a `DxFeed` instance singleton and `dxfeed.properties` file
- [x] DxFeedSample is a simple demonstration of how to create multiple event listeners and subscribe to `Quote` and `Trade` events
- [x] PrintQuoteEvents is a simple demonstration of how to subscribe to the `Quote` event, using a `DxFeed` instance singleton and `dxfeed.properties` file
- [ ] WriteTapeFile is a simple demonstration of how to write events to a tape file
- [x] [DxFeedIpfConnect](https://github.com/dxFeed/dxfeed-graal-swift-api/blob/swift/Samples/Tools/IpfConnect.swift) is a simple demonstration of how to get Instrument Profiles
- [x] [DXFeedLiveIpfSample](https://github.com/dxFeed/dxfeed-graal-swift-api/blob/swift/Samples/Tools/LiveIpfSample.swift)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Foundation
import DXFeedFramework

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

static func == (lhs: Listener, rhs: Listener) -> Bool {
Expand Down
20 changes: 10 additions & 10 deletions Samples/Playgrounds/DxFeedSample.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import DXFeedFramework
import Foundation
import Cocoa
import PlaygroundSupport
import DXFeedFramework

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

static func == (lhs: Listener, rhs: Listener) -> Bool {
Expand All @@ -24,18 +23,18 @@ class Listener: DXEventListener, Hashable {
}
}

/// 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.
// 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 subscription = try DXEndpoint.getInstance()
let subscriptionQuote = try DXEndpoint.getInstance()
.getFeed()?
.createSubscription(EventCode.quote)
// Listener must be attached before symbols are added.
let listener = Listener { listener in
listener.callback = { events in
events.forEach { event in
Expand All @@ -44,8 +43,8 @@ let listener = Listener { listener in
}
return listener
}
try subscription?.add(listener: listener)
try subscription?.addSymbols(symbol)
try subscriptionQuote?.add(listener: listener)
try subscriptionQuote?.addSymbols(symbol)

let subscriptionTrade = try DXEndpoint.getInstance()
.getFeed()?
Expand All @@ -58,6 +57,7 @@ let listenerTrade = Listener { listener in
}
return listener
}
// Listener must be attached before symbols are added.
try subscriptionTrade?.add(listener: listenerTrade)
try subscriptionTrade?.addSymbols(symbol)

Expand Down
60 changes: 60 additions & 0 deletions Samples/Playgrounds/PrintQuoteEvents.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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)
}
}

// A simple sample that shows how to subscribe to quotes for one instruments,
// and print all received quotes to the console.
// 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.

// Specified instrument name, for example AAPL, IBM, MSFT, etc.
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 ?? "")

// Creates a subscription attached to a default DXFeed with a Quote event type.
// The endpoint address to use is stored in the "dxfeed.properties" file.
let subscription = try DXEndpoint.getInstance()
.getFeed()?
.createSubscription(EventCode.quote)
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 subscription?.add(listener: listenerTrade)
try subscription?.addSymbols(symbol)


// infinity execution
PlaygroundPage.current.needsIndefiniteExecution = true

// to finish execution run this line
PlaygroundPage.current.finishExecution()
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
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 0c47776

Please sign in to comment.