StaticnewSet order changed callback, after receiving the order changed event, it will call back to this function.
Set grid order changed callback, after receiving the grid order changed event, it will call back to this function.
Subscribe
const {
OAuth, Config,
TradeContext,
Decimal,
OrderSide,
TimeInForceType,
OrderType,
TopicType,
} = require('longbridge');
const oauth = await OAuth.build('your-client-id', (_, url) => console.log('Visit:', url));
const ctx = TradeContext.new(Config.fromOAuth(oauth));
ctx.setOnOrderChanged((_, event) => console.log(event.toString()));
await ctx.subscribe([TopicType.Private]);
const resp = await ctx.submitOrder({
symbol: "700.HK",
orderType: OrderType.LO,
side: OrderSide.Buy,
timeInForce: TimeInForceType.Day,
submittedPrice: new Decimal("50"),
submittedQuantity: 200,
});
console.log(resp.toString());
Get history executions
side may be Unknown on Longbridge US accounts.
const { OAuth, Config, TradeContext } = require('longbridge');
const oauth = await OAuth.build('your-client-id', (_, url) => console.log('Visit:', url));
const ctx = TradeContext.new(Config.fromOAuth(oauth));
const resp = await ctx.historyExecutions({
symbol: "700.HK",
startAt: new Date(2022, 5, 9),
endAt: new Date(2022, 5, 12),
});
for (let obj of resp) {
console.log(obj.toString());
}
Optionalopts: GetHistoryExecutionsOptions | nullGet today executions
const { OAuth, Config, TradeContext } = require('longbridge');
const oauth = await OAuth.build('your-client-id', (_, url) => console.log('Visit:', url));
const ctx = TradeContext.new(Config.fromOAuth(oauth));
const resp = await ctx.todayExecutions({ symbol: "700.HK" });
for (let obj of resp) {
console.log(obj.toString());
}
Optionalopts: GetTodayExecutionsOptions | nullGet all executions
AP accounts only: US data-center accounts get a region error; use
historyExecutions instead.
Optionalopts: GetAllExecutionsOptions | nullGet history orders
const {
OAuth, Config,
TradeContext,
OrderStatus,
OrderSide,
Market,
} = require('longbridge');
const oauth = await OAuth.build('your-client-id', (_, url) => console.log('Visit:', url));
const ctx = TradeContext.new(Config.fromOAuth(oauth));
const resp = await ctx.historyOrders({
symbol: "700.HK",
status: [OrderStatus.Filled, OrderStatus.New],
side: OrderSide.Buy,
market: Market.HK,
startAt: new Date(2022, 5, 9),
endAt: new Date(2022, 5, 12),
});
for (let obj of resp) {
console.log(obj.toString());
}
Optionalopts: GetHistoryOrdersOptions | nullGet today orders
const {
OAuth, Config,
TradeContext,
OrderStatus,
OrderSide,
Market,
} = require('longbridge');
const oauth = await OAuth.build('your-client-id', (_, url) => console.log('Visit:', url));
const ctx = TradeContext.new(Config.fromOAuth(oauth));
const resp = await ctx.todayOrders({
symbol: "700.HK",
status: [OrderStatus.Filled, OrderStatus.New],
side: OrderSide.Buy,
market: Market.HK,
});
for (let obj of resp) {
console.log(obj.toString());
}
Optionalopts: GetTodayOrdersOptions | nullReplace order
const { OAuth, Config, TradeContext, Decimal } = require('longbridge');
const oauth = await OAuth.build('your-client-id', (_, url) => console.log('Visit:', url));
const ctx = TradeContext.new(Config.fromOAuth(oauth));
await ctx.replaceOrder({
orderId: "709043056541253632",
quantity: 100,
price: new Decimal("300"),
});
Submit order
const {
OAuth, Config,
TradeContext,
OrderType,
OrderSide,
Decimal,
TimeInForceType,
} = require('longbridge');
const oauth = await OAuth.build('your-client-id', (_, url) => console.log('Visit:', url));
const ctx = TradeContext.new(Config.fromOAuth(oauth));
const resp = await ctx.submitOrder({
symbol: "700.HK",
orderType: OrderType.LO,
side: OrderSide.Buy,
timeInForce: TimeInForceType.Day,
submittedQuantity: 200,
submittedPrice: new Decimal("300"),
});
console.log(resp.toString());
Submit a multi-leg option combination order (such as vertical spreads, straddles, strangles, collars, etc.). All legs are submitted together as a single strategy order.
const {
OAuth, Config,
TradeContext,
OrderType,
OrderSide,
Decimal,
MultiLegStrategy,
} = require('longbridge');
const oauth = await OAuth.build('your-client-id', (_, url) => console.log('Visit:', url));
const ctx = TradeContext.new(Config.fromOAuth(oauth));
const resp = await ctx.submitMultileg({
side: OrderSide.Buy,
orderType: OrderType.LO,
submittedQuantity: new Decimal("1"),
strategy: MultiLegStrategy.VerticalCallSpread,
submittedPrice: new Decimal("1.5"),
legs: [
{ symbol: "QQQ260731C764000.US", ratioQuantity: new Decimal("1") },
{ symbol: "QQQ260731C767000.US", ratioQuantity: new Decimal("1") },
],
});
console.log(resp.toString());
Cancel order
const { OAuth, Config, TradeContext } = require('longbridge');
const oauth = await OAuth.build('your-client-id', (_, url) => console.log('Visit:', url));
const ctx = TradeContext.new(Config.fromOAuth(oauth));
await ctx.cancelOrder("709043056541253632");
OptionalisAttached: boolean | nullGet account balance
AP accounts only: US data-center accounts get a region error; use
usCashAndBuyPower (or usAssetOverview) instead.
const { OAuth, Config, TradeContext } = require('longbridge');
const oauth = await OAuth.build('your-client-id', (_, url) => console.log('Visit:', url));
const ctx = TradeContext.new(Config.fromOAuth(oauth));
const resp = await ctx.accountBalance();
for (let obj of resp) {
console.log(obj.toString());
}
Optionalcurrency: string | nullGet cash flow
AP accounts only: US data-center accounts get a region error; use
usTransactionHistory instead.
const { OAuth, Config, TradeContext } = require('longbridge');
const oauth = await OAuth.build('your-client-id', (_, url) => console.log('Visit:', url));
const ctx = TradeContext.new(Config.fromOAuth(oauth));
const resp = await ctx.cashFlow({
startAt: new Date(2022, 5, 9),
endAt: new Date(2022, 5, 12),
});
for (let obj of resp) {
console.log(obj.toString());
}
Get fund positions
AP accounts only: US data-center accounts have no fund holdings and get a region error. No US equivalent.
const { OAuth, Config, TradeContext } = require('longbridge')
const oauth = await OAuth.build('your-client-id', (_, url) => console.log('Visit:', url));
const ctx = TradeContext.new(Config.fromOAuth(oauth));
const resp = await ctx.fundPositions();
console.log(resp);
Optionalsymbols: string[] | nullGet stock positions
AP accounts only: US data-center accounts get a region error; use
usAssetOverview (stockList) instead.
const { OAuth, Config, TradeContext } = require('longbridge')
const oauth = await OAuth.build('your-client-id', (_, url) => console.log('Visit:', url));
const ctx = TradeContext.new(Config.fromOAuth(oauth));
const resp = await ctx.stockPositions();
console.log(resp);
Optionalsymbols: string[] | nullGet margin ratio
AP accounts only: US data-center accounts get a region error. No US
equivalent; for US margin info see usCashAndBuyPower
(marginStatus).
const { OAuth, Config, TradeContext } = require('longbridge')
const oauth = await OAuth.build('your-client-id', (_, url) => console.log('Visit:', url));
const ctx = TradeContext.new(Config.fromOAuth(oauth));
const resp = await ctx.marginRatio("700.HK");
console.log(resp);
Get the risk assessment (suitability questionnaire) result of the current member
const { OAuth, Config, TradeContext } = require('longbridge')
const oauth = await OAuth.build('your-client-id', (_, url) => console.log('Visit:', url));
const ctx = TradeContext.new(Config.fromOAuth(oauth));
const resp = await ctx.riskAssessment();
console.log(resp);
Get order detail
AP accounts only: US data-center accounts get a region error; use
usOrderDetail instead.
const { OAuth, Config, TradeContext } = require('longbridge')
const oauth = await OAuth.build('your-client-id', (_, url) => console.log('Visit:', url));
const ctx = TradeContext.new(Config.fromOAuth(oauth));
const resp = await ctx.orderDetail("701276261045858304");
console.log(resp);
Query US order list. Returns JSON string. US token required. symbol: user-facing symbol e.g. "AAPL.US"; action: 0=all/1=buy/2=sell.
Get US order detail. US token required.
Get US account asset overview. US token required.
Get US realized P&L. US token required.
Optionalcategory: string | nullGet cash, buying power and margin status for the US account. US token required.
currency: optional, e.g. "USD".
Optionalcurrency: string | nullGet the quantity of a US holding that is available to trade. US token required.
equityType: "STOCK" (default), "OPTION" or "CRYPTO";
positionSide: "LONG" (default) or "SHORT".
OptionalequityType: string | nullOptionalpositionSide: string | nullGet the US account transaction history, one page at a time. US token required.
Get the exchange rates used by the US account. US token required.
Estimating the maximum purchase quantity for Hong Kong and US stocks, warrants, and options
const { OAuth, Config, TradeContext, OrderType, OrderSide } = require('longbridge')
const oauth = await OAuth.build('your-client-id', (_, url) => console.log('Visit:', url));
const ctx = TradeContext.new(Config.fromOAuth(oauth));
const resp = await ctx.estimateMaxPurchaseQuantity({
symbol: "700.HK",
orderType: OrderType.LO,
side: OrderSide.Buy,
});
console.log(resp);
Trade context