From 18120e3b04a519d784d821aad09f2f30a3a1d1d8 Mon Sep 17 00:00:00 2001 From: Aleksey Kosylo Date: Tue, 5 Mar 2024 12:56:00 +0100 Subject: [PATCH] add FetchDailyCandles sample --- DXFeedFramework.xcodeproj/project.pbxproj | 2 ++ README.md | 1 + .../Contents.swift | 26 +++++++++++++++++++ .../contents.xcplayground | 4 +++ 4 files changed, 33 insertions(+) create mode 100644 Samples/Playgrounds/FetchDailyCandles.playground/Contents.swift create mode 100644 Samples/Playgrounds/FetchDailyCandles.playground/contents.xcplayground diff --git a/DXFeedFramework.xcodeproj/project.pbxproj b/DXFeedFramework.xcodeproj/project.pbxproj index 1de354c22..0d6e6765b 100644 --- a/DXFeedFramework.xcodeproj/project.pbxproj +++ b/DXFeedFramework.xcodeproj/project.pbxproj @@ -647,6 +647,7 @@ 6435EE3C2B1F1E9200E8496C /* PrintQuoteEvents.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = PrintQuoteEvents.playground; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; 64437A8E2A9DEE6F005929B2 /* InstrumentProfile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InstrumentProfile.swift; sourceTree = ""; }; 64437A912A9DF1DE005929B2 /* NativeInstrumentProfileReader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NativeInstrumentProfileReader.swift; sourceTree = ""; }; + 644551C92B973A0D0069E3A2 /* FetchDailyCandles.playground */ = {isa = PBXFileReference; lastKnownFileType = file.playground; path = FetchDailyCandles.playground; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.swift; }; 6447A5DA2A8E559000739CCF /* ILastingEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ILastingEvent.swift; sourceTree = ""; }; 6447A5DC2A8E56CF00739CCF /* ITimeSeriesEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ITimeSeriesEvent.swift; sourceTree = ""; }; 6447A5DE2A8E56FC00739CCF /* IIndexedEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IIndexedEvent.swift; sourceTree = ""; }; @@ -1059,6 +1060,7 @@ 640885C62B1F657300E6CF88 /* ScheduleSample.playground */, 641C64AE2B344E770023CFAD /* PublishProfiles.playground */, 64F9C6C12B4BFD8F003ED014 /* DXFeedconnect.playground */, + 644551C92B973A0D0069E3A2 /* FetchDailyCandles.playground */, ); path = Playgrounds; sourceTree = ""; diff --git a/README.md b/README.md index 1c42799aa..95f65302a 100644 --- a/README.md +++ b/README.md @@ -248,6 +248,7 @@ is a simple demonstration of how to get live updates for Instrument Profiles - [ ] DxFeedPublishProfiles is a simple demonstration of how to publish market events - [x] [ScheduleSample](https://github.com/dxFeed/dxfeed-graal-swift-api/blob/swift/Samples/Playgrounds/ScheduleSample.playground/Contents.swift) is a simple demonstration of how to get various scheduling information for instruments +- [x] [FetchDailyCandles](https://github.com/dxFeed/dxfeed-graal-swift-api/blob/swift/Samples/Playgrounds/FetchDailyCandles.playground/Contents.swift) is a simple demonstration of how to fetch last N-days of candles for a specified symbol ## Current State diff --git a/Samples/Playgrounds/FetchDailyCandles.playground/Contents.swift b/Samples/Playgrounds/FetchDailyCandles.playground/Contents.swift new file mode 100644 index 000000000..7ab013805 --- /dev/null +++ b/Samples/Playgrounds/FetchDailyCandles.playground/Contents.swift @@ -0,0 +1,26 @@ +import Cocoa +import DXFeedFramework + +/// Fetches last N days of candles for a specified symbol, prints them, and exits. + +// await couldn't use directly in Playground(this is accompanied by errors like: execution stopped with unexpected state) +Task { + let baseSymbol = "AAPL{=d}" + var to: Long = Long(Date.now.timeIntervalSince1970 * 1000) + let from: Long = Long(Calendar.current.date(byAdding: .day, value: -20, to: Date())!.timeIntervalSince1970 * 1000) + let endpoint = try DXEndpoint.getInstance().connect("demo.dxfeed.com:7300") + let feed = endpoint.getFeed() + guard let task = feed?.getTimeSeries(type: Candle.self, symbol: baseSymbol, fromTime: from, toTime: to) else { + print("Async task is nil") + exit(0) + } + let result = await task.result + switch result { + case .success(let value): + value?.forEach({ event in + print(event) + }) + case .failure(let error): + print(error) + } +} diff --git a/Samples/Playgrounds/FetchDailyCandles.playground/contents.xcplayground b/Samples/Playgrounds/FetchDailyCandles.playground/contents.xcplayground new file mode 100644 index 000000000..1c968e7d1 --- /dev/null +++ b/Samples/Playgrounds/FetchDailyCandles.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file