Trade context

Constructors

Methods

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

    Parameters

    Returns void

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

    Parameters

    Returns Promise<void>

  • Unsubscribe

    Parameters

    Returns Promise<void>

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

    Parameters

    Returns Promise<Execution[]>

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

    Parameters

    Returns Promise<Execution[]>

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

    Parameters

    Returns Promise<Order[]>

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

    Parameters

    Returns Promise<Order[]>

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

    Parameters

    Returns Promise<void>

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

    Parameters

    Returns Promise<SubmitOrderResponse>

  • Cancel order

    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

    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

    • Optionalcurrency: string

    Returns Promise<AccountBalance[]>

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

    Parameters

    Returns Promise<CashFlow[]>

  • Get fund positions

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

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

    Parameters

    • Optionalsymbols: string[]

    Returns Promise<FundPositionsResponse>

  • Get stock positions

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

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

    Parameters

    • Optionalsymbols: string[]

    Returns Promise<StockPositionsResponse>

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

    Parameters

    • symbol: string

    Returns Promise<MarginRatio>

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

    Parameters

    • orderId: string

    Returns Promise<OrderDetail>

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

    Returns Promise<EstimateMaxPurchaseQuantityResponse>