Static
newSet order changed callback, after receiving the order changed event, it will call back to this function.
Subscribe
const {
Config,
TradeContext,
Decimal,
OrderSide,
TimeInForceType,
OrderType,
TopicType,
} = require("longport");
let config = Config.fromEnv();
TradeContext.new(config)
.then((ctx) => {`
ctx.setOnQuote((_, event) => console.log(event.toString()));
ctx.subscribe([TopicType.Private]);
return 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()));
Unsubscribe
Get history executions
const { Config, TradeContext } = require("longport");
let config = Config.fromEnv();
TradeContext.new(config)
.then((ctx) =>
ctx.historyExecutions({
symbol: "700.HK",
startAt: new Date(2022, 5, 9),
endAt: new Date(2022, 5, 12),
})
)
.then((resp) => {
for (let obj of resp) {
console.log(obj.toString());
}
});
Optional
opts: GetHistoryExecutionsOptionsGet today executions
const { Config, TradeContext } = require("longport");
let config = Config.fromEnv();
TradeContext.new(config)
.then((ctx) => ctx.todayExecutions({ symbol: "700.HK" }))
.then((resp) => {
for (let obj of resp) {
console.log(obj.toString());
}
});
Optional
opts: GetTodayExecutionsOptionsGet history orders
const {
Config,
TradeContext,
OrderStatus,
OrderSide,
Market,
} = require("longport");
let config = Config.fromEnv();
TradeContext.new(config)
.then((ctx) =>
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),
})
)
.then((resp) => {
for (let obj of resp) {
console.log(obj.toString());
}
});
Optional
opts: GetHistoryOrdersOptionsGet today orders
const {
Config,
TradeContext,
OrderStatus,
OrderSide,
Market,
} = require("longport");
let config = Config.fromEnv();
TradeContext.new(config)
.then((ctx) =>
ctx.todayOrders({
symbol: "700.HK",
status: [OrderStatus.Filled, OrderStatus.New],
side: OrderSide.Buy,
market: Market.HK,
})
)
.then((resp) => {
for (let obj of resp) {
console.log(obj.toString());
}
});
Optional
opts: GetTodayOrdersOptionsReplace order
const { Config, TradeContext, Decimal } = require("longport");
let config = Config.fromEnv();
TradeContext.new(config)
.then((ctx) =>
ctx.replaceOrder({
orderId: "709043056541253632",
quantity: 100,
price: new Decimal("300"),
})
)
.then((resp) => {
for (let obj of resp) {
console.log(obj.toString());
}
});
Submit order
const {
Config,
TradeContext,
OrderType,
OrderSide,
Decimal,
TimeInForceType,
} = 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,
submittedQuantity: 200,
submittedPrice: new Decimal("300"),
})
)
.then((resp) => console.log(resp.toString()));
Get account balance
const { Config, TradeContext } = require("longport");
let config = Config.fromEnv();
TradeContext.new(config)
.then((ctx) => ctx.accountBalance())
.then((resp) => {
for (let obj of resp) {
console.log(obj.toString());
}
});
Optional
currency: stringGet cash flow
const { Config, TradeContext, GetCashFlowOptions } = require("longport");
let config = Config.fromEnv();
TradeContext.new(config)
.then((ctx) =>
ctx.cashFlow({
startAt: new Date(2022, 5, 9),
endAt: new Date(2022, 5, 12),
})
)
.then((resp) => {
for (let obj of resp) {
console.log(obj.toString());
}
});
Get fund positions
const { Config, TradeContext } = require("longport")
let config = Config.fromEnv()
TradeContext.new(config)
.then((ctx) => ctx.fundPositions())
.then((resp) => console.log(resp))
Optional
symbols: string[]Get stock positions
const { Config, TradeContext } = require("longport")
let config = Config.fromEnv()
TradeContext.new(config)
.then((ctx) => ctx.stockPositions())
.then((resp) => console.log(resp))
Optional
symbols: string[]Get margin ratio
const { Config, TradeContext } = require("longport")
let config = Config.fromEnv()
TradeContext.new(config)
.then((ctx) => ctx.marginRatio("700.HK"))
.then((resp) => console.log(resp))
Get order detail
const { Config, TradeContext } = require("longport")
let config = Config.fromEnv()
TradeContext.new(config)
.then((ctx) => ctx.orderDetail("701276261045858304"))
.then((resp) => console.log(resp))
Estimating the maximum purchase quantity for Hong Kong and US stocks, warrants, and options
const { Config, TradeContext, OrderType, OrderSide } = require("longport")
let config = Config.fromEnv()
TradeContext.new(config)
.then((ctx) => ctx.estimateMaxPurchaseQuantity({
symbol: "700.HK",
orderType: OrderType.LO,
side: OrderSide.Buy,
}))
.then((resp) => console.log(resp))
Trade context