Skip to content

Commit

Permalink
add history candlesticks api for java
Browse files Browse the repository at this point in the history
  • Loading branch information
sunli829 committed Nov 21, 2023
1 parent 391aef1 commit 417710d
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 5 deletions.
54 changes: 54 additions & 0 deletions examples/java/history_candlesticks/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.github.longportapp</groupId>
<artifactId>history_candlesticks</artifactId>
<version>0.0.1</version>

<dependencies>
<dependency>
<groupId>io.github.longportapp</groupId>
<artifactId>openapi-sdk</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath />
<argument>Main</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<java.version>1.11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
</properties>
</project>
30 changes: 30 additions & 0 deletions examples/java/history_candlesticks/src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
30 changes: 30 additions & 0 deletions java/javasrc/src/main/java/com/longport/quote/QuoteContext.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.longport.quote;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.concurrent.CompletableFuture;

import com.longport.*;
Expand Down Expand Up @@ -738,6 +739,35 @@ public CompletableFuture<CapitalFlowLine[]> 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<Candlestick[]> 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<Candlestick[]> 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
*
Expand Down
25 changes: 20 additions & 5 deletions java/test/Main.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
}

0 comments on commit 417710d

Please sign in to comment.