StaticnewSet order changed callback, after receiving the 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
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 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());
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");
Get account balance
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
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
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
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
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 order detail
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);
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