From 417710dc4f266ab1c5a155331d67b1db084cad51 Mon Sep 17 00:00:00 2001 From: Sunli Date: Tue, 21 Nov 2023 14:02:48 +0800 Subject: [PATCH] add history candlesticks api for java --- examples/java/history_candlesticks/pom.xml | 54 +++++++++++++++++++ .../src/main/java/Main.java | 30 +++++++++++ .../java/com/longport/quote/QuoteContext.java | 30 +++++++++++ java/test/Main.java | 25 +++++++-- 4 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 examples/java/history_candlesticks/pom.xml create mode 100644 examples/java/history_candlesticks/src/main/java/Main.java diff --git a/examples/java/history_candlesticks/pom.xml b/examples/java/history_candlesticks/pom.xml new file mode 100644 index 000000000..a8558f212 --- /dev/null +++ b/examples/java/history_candlesticks/pom.xml @@ -0,0 +1,54 @@ + + + 4.0.0 + + io.github.longportapp + history_candlesticks + 0.0.1 + + + + io.github.longportapp + openapi-sdk + LATEST + + + + + + + maven-compiler-plugin + 3.10.1 + + + org.codehaus.mojo + exec-maven-plugin + 3.0.0 + + + + exec + + + + + java + + -classpath + + Main + + + + + + + + 1.11 + 11 + 11 + utf-8 + + \ No newline at end of file diff --git a/examples/java/history_candlesticks/src/main/java/Main.java b/examples/java/history_candlesticks/src/main/java/Main.java new file mode 100644 index 000000000..e67445bfc --- /dev/null +++ b/examples/java/history_candlesticks/src/main/java/Main.java @@ -0,0 +1,30 @@ +import java.time.LocalDateTime; +import java.time.LocalDate; +import java.util.Arrays; + +import com.longport.*; +import com.longport.quote.*; + +class Main { + public static void main(String[] args) throws Exception { + try (Config config = Config.fromEnv(); QuoteContext ctx = QuoteContext.create(config).get()) { + System.out.println("get candlesticks by offset"); + System.out.println("===================="); + + Candlestick[] candlesticks = ctx + .getHistoryCandlesticksByOffset("700.HK", Period.Day, AdjustType.NoAdjust, false, + LocalDateTime.of(2023, 8, 18, 0, 0, 0, 0), 10) + .get(); + System.out.println(Arrays.toString(candlesticks)); + + System.out.println("get candlesticks by date"); + System.out.println("===================="); + + Candlestick[] candlesticks2 = ctx + .getHistoryCandlesticksByDate("700.HK", Period.Day, AdjustType.NoAdjust, + LocalDate.of(2022, 5, 5), LocalDate.of(2022, 6, 23)) + .get(); + System.out.println(Arrays.toString(candlesticks2)); + } + } +} diff --git a/java/javasrc/src/main/java/com/longport/quote/QuoteContext.java b/java/javasrc/src/main/java/com/longport/quote/QuoteContext.java index c2a88d2ca..d190de5f4 100644 --- a/java/javasrc/src/main/java/com/longport/quote/QuoteContext.java +++ b/java/javasrc/src/main/java/com/longport/quote/QuoteContext.java @@ -1,6 +1,7 @@ package com.longport.quote; import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.concurrent.CompletableFuture; import com.longport.*; @@ -738,6 +739,35 @@ public CompletableFuture getCapitalFlow(String symbol) throws }); } + /** + * Get history candlesticks by offset + * + * @return A Future representing the result of the operation + * @throws OpenApiException If an error occurs + */ + public CompletableFuture getHistoryCandlesticksByOffset(String symbol, Period period, + AdjustType adjustType, boolean forward, LocalDateTime datetime, int count) + throws OpenApiException { + return AsyncCallback.executeTask((callback) -> { + SdkNative.quoteContextHistoryCandlesticksByOffset(this.raw, symbol, period, adjustType, forward, datetime, + count, callback); + }); + } + + /** + * Get history candlesticks by date + * + * @return A Future representing the result of the operation + * @throws OpenApiException If an error occurs + */ + public CompletableFuture getHistoryCandlesticksByDate(String symbol, Period period, + AdjustType adjustType, LocalDate start, LocalDate end) + throws OpenApiException { + return AsyncCallback.executeTask((callback) -> { + SdkNative.quoteContextHistoryCandlesticksByDate(this.raw, symbol, period, adjustType, start, end, callback); + }); + } + /** * Get security calc indexes * diff --git a/java/test/Main.java b/java/test/Main.java index 84c359271..e67445bfc 100644 --- a/java/test/Main.java +++ b/java/test/Main.java @@ -1,15 +1,30 @@ +import java.time.LocalDateTime; +import java.time.LocalDate; +import java.util.Arrays; + import com.longport.*; -import com.longport.trade.*; import com.longport.quote.*; -import java.math.BigDecimal; -import java.util.Arrays; class Main { public static void main(String[] args) throws Exception { try (Config config = Config.fromEnv(); QuoteContext ctx = QuoteContext.create(config).get()) { - SecurityStaticInfo[] resp = ctx.getStaticInfo(new String[] { "700.HK", "AAPL.US", "TSLA.US", "NFLX.US" }) + System.out.println("get candlesticks by offset"); + System.out.println("===================="); + + Candlestick[] candlesticks = ctx + .getHistoryCandlesticksByOffset("700.HK", Period.Day, AdjustType.NoAdjust, false, + LocalDateTime.of(2023, 8, 18, 0, 0, 0, 0), 10) + .get(); + System.out.println(Arrays.toString(candlesticks)); + + System.out.println("get candlesticks by date"); + System.out.println("===================="); + + Candlestick[] candlesticks2 = ctx + .getHistoryCandlesticksByDate("700.HK", Period.Day, AdjustType.NoAdjust, + LocalDate.of(2022, 5, 5), LocalDate.of(2022, 6, 23)) .get(); - System.out.println(Arrays.toString(resp)); + System.out.println(Arrays.toString(candlesticks2)); } } }