-
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.
DxFeedFileParser ConvertTapeFile
- Loading branch information
Showing
9 changed files
with
297 additions
and
4 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
96 changes: 96 additions & 0 deletions
96
Samples/Playgrounds/ConvertTapeFile.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,96 @@ | ||
import Foundation | ||
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) | ||
} | ||
} | ||
|
||
|
||
let paths = Bundle.main.path(forResource: "ConvertTapeFile.in", ofType: nil) | ||
let tempDirectory = NSTemporaryDirectory() | ||
let fullURL = 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]" | ||
|
||
// Create input endpoint configured for tape reading. | ||
let inputEndpoint = try DXEndpoint.builder() | ||
.withRole(.streamFeed) // Prevents event conflation and loss due to buffer overflow. | ||
.withProperty(DXEndpoint.Property.wildcardEnable.rawValue, "true") // Enables wildcard subscription. | ||
.withProperty(DXEndpoint.Property.eventTime.rawValue, "true") // Use provided event times. | ||
.build() | ||
|
||
// Create output endpoint configured for tape writing. | ||
var outputEndpoint = try DXEndpoint.builder() | ||
.withRole(.streamPublisher) // Prevents event conflation and loss due to buffer overflow. | ||
.withProperty(DXEndpoint.Property.wildcardEnable.rawValue, "true") // Enables wildcard subscription. | ||
.withProperty(DXEndpoint.Property.eventTime.rawValue, "true") // Use provided event times. | ||
.build() | ||
|
||
// Create and link event processor for all types of events. | ||
// Note: Set of processed event types could be limited if needed. | ||
let eventTypes: [EventCode] = EventCode.allCases.compactMap { eventCode in | ||
if EventCode.unsupported().contains(eventCode) { | ||
return nil | ||
} else { | ||
return eventCode | ||
} | ||
} | ||
|
||
let feed = inputEndpoint.getFeed() | ||
let subscription = try feed?.createSubscription(eventTypes) | ||
|
||
let listener = Listener { anonymCl in | ||
anonymCl.callback = { events in | ||
// Here event processing occurs. Events could be modified, removed, or new events added. | ||
// For example, the below code adds 1 hour to event times: | ||
// foreach (var e in events) | ||
// { | ||
// e.EventTime += 3600_000 | ||
// } | ||
|
||
// Publish processed events | ||
let publisher = outputEndpoint.getPublisher() | ||
try? publisher?.publish(events: events) | ||
} | ||
return anonymCl | ||
} | ||
|
||
try subscription?.add(listener: listener) | ||
|
||
// Subscribe to all symbols. | ||
// Note: Set of processed symbols could be limited if needed. | ||
try subscription?.addSymbols(WildcardSymbol.all) | ||
|
||
// Connect output endpoint and start output tape writing BEFORE starting input tape reading. | ||
try outputEndpoint.connect(outputAddress) | ||
// Connect input endpoint and start input tape reading AFTER starting output tape writing. | ||
try inputEndpoint.connect(inputAddress) | ||
|
||
// Wait until all data is read and processed, and then gracefully close input endpoint. | ||
try inputEndpoint.awaitNotConnected() | ||
try inputEndpoint.closeAndAWaitTermination() | ||
|
||
// Wait until all data is processed and written, and then gracefully close output endpoint. | ||
try outputEndpoint.awaitProcessed() | ||
try outputEndpoint.closeAndAWaitTermination() | ||
|
||
print("ConvertTapeFile: \(inputAddress) has been successfully tapped to \(outputAddress)") |
23 changes: 23 additions & 0 deletions
23
Samples/Playgrounds/ConvertTapeFile.playground/Resources/ConvertTapeFile.in
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,23 @@ | ||
==DXP3 type=tape version=QDS-3.315 time=field +STREAM_DATA | ||
==STREAM_DATA | ||
=Quote EventSymbol EventTime BidTime BidExchangeCode BidPrice BidSize AskTime AskExchangeCode AskPrice AskSize | ||
Quote AAPL 20230216-180402.785+0300 20230216-180402+0300 Q 153.94 2 20230216-180402+0300 Q 153.95 1 | ||
Quote AAPL 20230216-180402.805+0300 20230216-180402+0300 Q 153.94 2 20230216-180402+0300 Z 153.96 2 | ||
Quote AAPL 20230216-180402.871+0300 20230216-180402+0300 Q 153.94 2 20230216-180402+0300 Q 153.95 1 | ||
Quote AAPL 20230216-180402.878+0300 20230216-180402+0300 Q 153.94 2 20230216-180402+0300 Q 153.96 3 | ||
Quote AAPL 20230216-180402.922+0300 20230216-180402+0300 P 153.94 2 20230216-180402+0300 Q 153.96 4 | ||
Quote AAPL 20230216-180402.945+0300 20230216-180402+0300 P 153.94 2 20230216-180402+0300 Q 153.96 3 | ||
Quote AAPL 20230216-180402.954+0300 20230216-180402+0300 P 153.94 2 20230216-180402+0300 Q 153.96 2 | ||
Quote AAPL 20230216-180402.969+0300 20230216-180402+0300 P 153.94 2 20230216-180402+0300 Q 153.96 3 | ||
Quote AAPL 20230216-180402.989+0300 20230216-180402+0300 P 153.94 2 20230216-180402+0300 Q 153.95 1 | ||
Quote AAPL 20230216-180403.005+0300 20230216-180402+0300 P 153.94 2 20230216-180403+0300 Q 153.96 3 | ||
Quote AAPL 20230216-180403.010+0300 20230216-180402+0300 P 153.94 2 20230216-180403+0300 Q 153.96 4 | ||
Quote AAPL 20230216-180403.121+0300 20230216-180402+0300 P 153.94 2 20230216-180403+0300 Q 153.96 3 | ||
Quote AAPL 20230216-180403.149+0300 20230216-180402+0300 P 153.94 2 20230216-180403+0300 Q 153.95 1 | ||
Quote AAPL 20230216-180403.182+0300 20230216-180402+0300 P 153.94 2 20230216-180403+0300 Q 153.96 3 | ||
Quote AAPL 20230216-180403.245+0300 20230216-180402+0300 P 153.94 2 20230216-180403+0300 Q 153.96 4 | ||
Quote AAPL 20230216-180403.309+0300 20230216-180402+0300 P 153.94 2 20230216-180403+0300 Q 153.95 1 | ||
Quote AAPL 20230216-180403.353+0300 20230216-180402+0300 P 153.94 2 20230216-180403+0300 Z 153.96 3 | ||
Quote AAPL 20230216-180403.507+0300 20230216-180403+0300 Q 153.94 2 20230216-180403+0300 Q 153.96 3 | ||
Quote AAPL 20230216-180403.516+0300 20230216-180403+0300 Q 153.95 1 20230216-180403+0300 Z 153.96 1 | ||
Quote AAPL 20230216-180403.523+0300 20230216-180403+0300 Q 153.95 1 20230216-180403+0300 Q 153.96 1 |
4 changes: 4 additions & 0 deletions
4
Samples/Playgrounds/ConvertTapeFile.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> |
59 changes: 59 additions & 0 deletions
59
Samples/Playgrounds/DxFeedFileParser.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,59 @@ | ||
import Foundation | ||
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) | ||
} | ||
} | ||
|
||
|
||
var file: String = "" | ||
var types: [EventCode] = [.quote] | ||
var symbols = "AAPL" | ||
var eventCounter = 0 | ||
|
||
// Create endpoint specifically for file parsing. | ||
var endpoint = try DXEndpoint.create(.streamFeed) | ||
var feed = endpoint.getFeed() | ||
|
||
// Subscribe to a specified event and symbol. | ||
var sub = try feed?.createSubscription(types) | ||
let listener = Listener { anonymCl in | ||
anonymCl.callback = { events in | ||
events.forEach { event in | ||
eventCounter += 1 | ||
print("\(eventCounter): \(event)") | ||
} | ||
} | ||
return anonymCl | ||
} | ||
try sub?.add(listener: listener) | ||
|
||
// Add symbols. | ||
try sub?.addSymbols(symbols) | ||
|
||
// Connect endpoint to a file. | ||
try endpoint.connect("file:\(file)[speed=max]") | ||
|
||
// Wait until file is completely parsed. | ||
try endpoint.awaitNotConnected() | ||
|
||
// Close endpoint when we're done. | ||
// This method will gracefully close endpoint, waiting while data processing completes. | ||
try endpoint.closeAndAWaitTermination() |
4 changes: 4 additions & 0 deletions
4
Samples/Playgrounds/DxFeedFileParser.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> |
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