longport
v3.0.22
Published
A Node.js library for LongPort Open API
Readme
LongPort OpenAPI SDK for Node.js
longport provides an easy-to-use interface for invoking LongPort OpenAPI.
Documentation
- SDK docs: https://longportapp.github.io/openapi/nodejs/index.html
- LongPort OpenAPI: https://open.longportapp.com/en/
Examples
Runnable examples live in examples/nodejs/:
examples/nodejs/account_asset.jsexamples/nodejs/http_client.jsexamples/nodejs/subscribe_candlesticks.jsexamples/nodejs/subscribe_quote.jsexamples/nodejs/submit_order.jsexamples/nodejs/today_orders.js
Quickstart
Install LongPort OpenAPI SDK
npm install longportSetting 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"Quote API (Get basic information of securities)
const { Config, QuoteContext } = require("longport");
let config = Config.fromEnv();
QuoteContext.new(config)
.then((ctx) => ctx.quote(["700.HK", "AAPL.US", "TSLA.US", "NFLX.US"]))
.then((resp) => {
for (let obj of resp) {
console.log(obj.toString())
}
});Quote API (Subscribe quotes)
const { Config, QuoteContext, SubType } = require("longport");
let config = Config.fromEnv();
QuoteContext.new(config).then((ctx) => {
ctx.setOnQuote((_, event) => console.log(event.toString()));
ctx.subscribe(
["700.HK", "AAPL.US", "TSLA.US", "NFLX.US"],
[SubType.Quote],
true
);
});Trade API (Submit order)
const {
Config,
TradeContext,
Decimal,
OrderSide,
TimeInForceType,
OrderType,
} = require("longport");
let config = Config.fromEnv();
TradeContext.new(config)
.then((ctx) =>
ctx.submitOrder({
symbol: "700.HK",
orderType: OrderType.LO,
side: OrderSide.Buy,
timeInForce: TimeInForceType.Day,
submittedPrice: new Decimal("50"),
submittedQuantity: 200,
})
)
.then((resp) => console.log(resp.toString()));Troubleshooting
- Windows
setxrequires a new terminal; usesetfor the currentcmd.exesession. - If the script exits, you won't receive push events; keep Node running.
- For debugging, set
LONGPORT_LOG_PATHto enable SDK logs.
License
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.
