Skip to content

Commit

Permalink
add sample:
Browse files Browse the repository at this point in the history
DxFeedSample
  • Loading branch information
kosyloa committed Dec 5, 2023
1 parent 34dec3c commit 4864442
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 5 deletions.
2 changes: 2 additions & 0 deletions DXFeedFramework.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@
644BD7882A4475EF00A0BF99 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/MainAR.storyboard; sourceTree = "<group>"; };
644C528D2B1E1CB8002C034C /* ConvertTapeFile.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = ConvertTapeFile.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
644C528F2B1E1D73002C034C /* DxFeedFileParser.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = DxFeedFileParser.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
644C52902B1E345E002C034C /* DxFeedSample.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = DxFeedSample.playground; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
644FE5D02AC1F34000580E3A /* LatencyMetricsPrinter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LatencyMetricsPrinter.swift; sourceTree = "<group>"; };
645A34942A937C7200709F29 /* BinaryInteger+Ext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BinaryInteger+Ext.swift"; sourceTree = "<group>"; };
645BE8512AC31E7C0028243D /* PerfTestTool.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PerfTestTool.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1004,6 +1005,7 @@
children = (
644C528F2B1E1D73002C034C /* DxFeedFileParser.playground */,
644C528D2B1E1CB8002C034C /* ConvertTapeFile.playground */,
644C52902B1E345E002C034C /* DxFeedSample.playground */,
);
path = Playgrounds;
sourceTree = "<group>";
Expand Down
41 changes: 41 additions & 0 deletions DXFeedFrameworkTests/DXConnectionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,45 @@ final class DXConnectionTest: XCTestCase {
}
}

func testQuote() throws {
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)
}
}


let symbol = "AAPL"
let endpoint = try DXEndpoint.builder()
.withProperty("dxfeed.address", "demo.dxfeed.com:7300")
.build()
let subscription = try endpoint
.getFeed()?
.createSubscription(EventCode.trade)
let listener = Listener { listener in
listener.callback = { events in
print(events)
}
return listener
}

try subscription?.add(listener: listener)
try subscription?.addSymbols(symbol)
wait(seconds: 10)
}

}
15 changes: 10 additions & 5 deletions Samples/Playgrounds/ConvertTapeFile.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ class Listener: DXEventListener, Hashable {
}


let paths = Bundle.main.path(forResource: "ConvertTapeFile.in", ofType: nil)
let inputFilePath = Bundle.main.path(forResource: "ConvertTapeFile.in", ofType: nil)
let tempDirectory = NSTemporaryDirectory()
let fullURL = NSURL.fileURL(withPathComponents: [tempDirectory, "ConvertTapeFile.out"])?.path
let outputFilePath = NSURL.fileURL(withPathComponents: [tempDirectory, "ConvertTapeFile.out"])?.path

// Determine input and output tapes and specify appropriate configuration parameters.
let inputAddress = "file:\(paths ?? "")[readAs=stream_data,speed=max]"
let outputAddress = "tape:\(fullURL ?? "" )[saveAs=stream_data,format=text]"
let inputAddress = "file:\(inputFilePath ?? "")[readAs=stream_data,speed=max]"
let outputAddress = "tape:\(outputFilePath ?? "" )[saveAs=stream_data,format=text]"

// Create input endpoint configured for tape reading.
let inputEndpoint = try DXEndpoint.builder()
Expand Down Expand Up @@ -93,4 +93,9 @@ try inputEndpoint.closeAndAWaitTermination()
try outputEndpoint.awaitProcessed()
try outputEndpoint.closeAndAWaitTermination()

print("ConvertTapeFile: \(inputAddress) has been successfully tapped to \(outputAddress)")
print("""
ConvertTapeFile:
\(inputAddress)
has been successfully tapped to
\(outputAddress)
""")
68 changes: 68 additions & 0 deletions Samples/Playgrounds/DxFeedSample.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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
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 4864442

Please sign in to comment.