longport
v2.0.5
Published
A Node.js library for LongPort Open API
Downloads
511
Readme
LongPort OpenAPI SDK for Node.js
longport
provides an easy-to-use interface for invokes LongPort OpenAPI
.
Quickstart
Install LongPort OpenAPI SDK
npm install longport
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"
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()));
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.