Trade context

Constructors

Methods

  • Set order changed callback, after receiving the order changed event, it will call back to this function.

    Parameters

    • callback: ((err, event) => void)

    Returns void

  • Subscribe

    Example

    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()));

    Parameters

    Returns Promise<void>

  • Unsubscribe

    Parameters

    Returns Promise<void>

  • Get history executions

    Example

    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());
    }
    });

    Parameters

    Returns Promise<Execution[]>

  • Get today executions

    Example

    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());
    }
    });

    Parameters

    Returns Promise<Execution[]>

  • Get history orders

    Example

    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());
    }
    });

    Parameters

    Returns Promise<Order[]>

  • Get today orders

    Example

    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());
    }
    });

    Parameters

    Returns Promise<Order[]>

  • Replace order

    Example

    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());
    }
    });

    Parameters

    Returns Promise<void>

  • Submit order

    Example

    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()));

    Parameters

    Returns Promise<SubmitOrderResponse>

  • Cancel order

    Example

    const { Config, TradeContext } = require("longport");

    let config = Config.fromEnv();
    TradeContext.new(config).then((ctx) => ctx.cancelOrder("709043056541253632"));

    Parameters

    • orderId: string

    Returns Promise<void>

  • Get account balance

    Example

    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());
    }
    });

    Parameters

    • Optional currency: string

    Returns Promise<AccountBalance[]>

  • Get cash flow

    Example

    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());
    }
    });

    Parameters

    Returns Promise<CashFlow[]>

  • Get fund positions

    Example

    const { Config, TradeContext } = require("longport")

    let config = Config.fromEnv()
    TradeContext.new(config)
    .then((ctx) => ctx.fundPositions())
    .then((resp) => console.log(resp))

    Parameters

    • Optional symbols: string[]

    Returns Promise<FundPositionsResponse>

  • Get stock positions

    Example

    const { Config, TradeContext } = require("longport")

    let config = Config.fromEnv()
    TradeContext.new(config)
    .then((ctx) => ctx.stockPositions())
    .then((resp) => console.log(resp))

    Parameters

    • Optional symbols: string[]

    Returns Promise<StockPositionsResponse>

  • Get margin ratio

    Example

    const { Config, TradeContext } = require("longport")

    let config = Config.fromEnv()
    TradeContext.new(config)
    .then((ctx) => ctx.marginRatio("700.HK"))
    .then((resp) => console.log(resp))

    Parameters

    • symbol: string

    Returns Promise<MarginRatio>

  • Get order detail

    Example

    const { Config, TradeContext } = require("longport")

    let config = Config.fromEnv()
    TradeContext.new(config)
    .then((ctx) => ctx.orderDetail("701276261045858304"))
    .then((resp) => console.log(resp))

    Parameters

    • orderId: string

    Returns Promise<OrderDetail>

  • Estimating the maximum purchase quantity for Hong Kong and US stocks, warrants, and options

    Example

    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))

    Returns Promise<EstimateMaxPurchaseQuantityResponse>