longport
provides an easy-to-use interface for invokes LongPort OpenAPI
.
Install LongPort OpenAPI SDK
Add io.github.longportapp:openapi-sdk
to pom.xml
<dependencies>
<dependency>
<groupId>io.github.longportapp</groupId>
<artifactId>openapi-sdk</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
Setting environment variables(MacOS/Linux)
export LONGPORT_APP_KEY="App Key get from user center"
export LONGPORT_APP_SECRET="App Secret get from user center"
export LONGPORT_ACCESS_TOKEN="Access Token get from user center"
Setting environment variables(Windows)
setx LONGPORT_APP_KEY "App Key get from user center"
setx LONGPORT_APP_SECRET "App Secret get from user center"
setx LONGPORT_ACCESS_TOKEN "Access Token get from user center"
import com.longport.*;
import com.longport.quote.*;
import java.math.BigDecimal;
class Main {
public static void main(String[] args) throws Exception {
try (Config config = Config.fromEnv(); QuoteContext ctx = QuoteContext.create(config).get()) {
SecurityQuote[] resp = ctx.getQuote(new String[] { "700.HK", "AAPL.US", "TSLA.US", "NFLX.US" }).get();
for (SecurityQuote obj : resp) {
System.out.println(obj);
}
}
}
}
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()) {
ctx.setOnQuote((symbol, quote) -> {
System.out.printf("%s\t%s\n", symbol, quote);
});
ctx.subscribe(new String[] { "700.HK", "AAPL.US", "TSLA.US", "NFLX.US" }, SubFlags.Quote, true).get();
Thread.sleep(30000);
}
}
}
import com.longport.*;
import com.longport.trade.*;
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) throws Exception {
try (Config config = Config.fromEnv(); TradeContext ctx = TradeContext.create(config).get()) {
SubmitOrderOptions opts = new SubmitOrderOptions("700.HK",
OrderType.LO,
OrderSide.Buy,
200,
TimeInForceType.Day).setSubmittedPrice(new BigDecimal(50));
SubmitOrderResponse resp = ctx.submitOrder(opts).get();
System.out.println(resp);
}
}
}
Licensed under either of
- Apache License, Version 2.0,(LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) at your option.