longport
TypeScript icon, indicating that this package has built-in type declarations

2.1.5 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i longport

Weekly Downloads

173

Version

2.1.5

License

MIT OR Apache-2.0

Unpacked Size

96.7 kB

Total Files

6

Last publish

Collaborators

  • sunli829