Class TradeContext

    • Constructor Detail

      • TradeContext

        public TradeContext()
    • Method Detail

      • setOnOrderChange

        public void setOnOrderChange​(OrderChangedHandler handler)
        Set order changed event callback, after receiving the order changed event, it will call back to this handler.
        Parameters:
        handler - A order changed handler
      • subscribe

        public CompletableFuture<Void> subscribe​(TopicType[] topics)
                                          throws OpenApiException
        Subscribe
         
         import com.longport.*;
         import com.longport.trade.*;
         import java.math.BigDecimal;
         
         class Main {
             public static void main(String[] args) throws Exception {
                 try (Config config = Config.fromEnv(); TradeContext ctx = TradeContext.create(config).get()) {
                     ctx.setOnOrderChange((order_changed) -> {
                         System.out.println(order_changed);
                     });
                     ctx.subscribe(new TopicType[] { TopicType.Private }).get();
         
                     SubmitOrderOptions opts = new SubmitOrderOptions("700.HK",
                             OrderType.LO,
                             OrderSide.Buy,
                             200,
                             TimeInForceType.Day).setSubmittedPrice(new BigDecimal(50));
                     SubmitOrderResponse resp = ctx.submitOrder(opts).get();
                     System.out.println(resp);
                     Thread.sleep(3000);
                 }
             }
         }
         
         
        Parameters:
        topics - Topics
        Returns:
        A Future representing the result of the operation
        Throws:
        OpenApiException - If an error occurs
      • getHistoryExecutions

        public CompletableFuture<Execution[]> getHistoryExecutions​(GetHistoryExecutionsOptions opts)
                                                            throws OpenApiException
        Get history executions
         
         import com.longport.*;
         import com.longport.trade.*;
         import java.time.*;
         
         class Main {
             public static void main(String[] args) throws Exception {
                 try (Config config = Config.fromEnv(); TradeContext ctx = TradeContext.create(config).get()) {
                     GetHistoryExecutionsOptions opts = new GetHistoryExecutionsOptions().setSymbol("700.HK")
                             .setStartAt(OffsetDateTime.of(2022, 5, 9, 0, 0, 0, 0, ZoneOffset.UTC))
                             .setEndAt(OffsetDateTime.of(2022, 5, 12, 0, 0, 0, 0, ZoneOffset.UTC));
                     Execution[] resp = ctx.getHistoryExecutions(opts).get();
                     for (Execution obj : resp) {
                         System.out.println(obj);
                     }
                 }
             }
         }
         
         
        Parameters:
        opts - Options for this request
        Returns:
        A Future representing the result of the operation
        Throws:
        OpenApiException - If an error occurs
      • getTodayExecutions

        public CompletableFuture<Execution[]> getTodayExecutions​(GetTodayExecutionsOptions opts)
                                                          throws OpenApiException
        Get today executions
         
         import com.longport.*;
         import com.longport.trade.*;
         
         class Main {
             public static void main(String[] args) throws Exception {
                 try (Config config = Config.fromEnv(); TradeContext ctx = TradeContext.create(config).get()) {
                     GetTodayExecutionsOptions opts = new GetTodayExecutionsOptions().setSymbol("700.HK");
                     Execution[] resp = ctx.getTodayExecutions(opts).get();
                     for (Execution obj : resp) {
                         System.out.println(obj);
                     }
                 }
             }
         }
         
         
         
        Parameters:
        opts - Options for this request
        Returns:
        A Future representing the result of the operation
        Throws:
        OpenApiException - If an error occurs
      • getHistoryOrders

        public CompletableFuture<Order[]> getHistoryOrders​(GetHistoryOrdersOptions opts)
                                                    throws OpenApiException
        Get history orders
         
         import com.longport.*;
         import com.longport.trade.*;
         import java.time.*;
         
         class Main {
             public static void main(String[] args) throws Exception {
                 try (Config config = Config.fromEnv(); TradeContext ctx = TradeContext.create(config).get()) {
                     GetHistoryOrdersOptions opts = new GetHistoryOrdersOptions().setSymbol("700.HK")
                             .setStatus(new OrderStatus[] { OrderStatus.Filled, OrderStatus.New })
                             .setSide(OrderSide.Buy)
                             .setMarket(Market.HK)
                             .setStartAt(OffsetDateTime.of(2022, 5, 9, 0, 0, 0, 0, ZoneOffset.UTC))
                             .setStartAt(OffsetDateTime.of(2022, 5, 12, 0, 0, 0, 0, ZoneOffset.UTC));
                     Order[] resp = ctx.getHistoryOrders(opts).get();
                     for (Order obj : resp) {
                         System.out.println(obj);
                     }
                 }
             }
         }
         
         
        Parameters:
        opts - Options for this request
        Returns:
        A Future representing the result of the operation
        Throws:
        OpenApiException - If an error occurs
      • getTodayOrders

        public CompletableFuture<Order[]> getTodayOrders​(GetTodayOrdersOptions opts)
                                                  throws OpenApiException
        Get today orders
         
         import com.longport.*;
         import com.longport.trade.*;
         
         class Main {
             public static void main(String[] args) throws Exception {
                 try (Config config = Config.fromEnv(); TradeContext ctx = TradeContext.create(config).get()) {
                     GetTodayOrdersOptions opts = new GetTodayOrdersOptions().setSymbol("700.HK")
                             .setStatus(new OrderStatus[] { OrderStatus.Filled, OrderStatus.New })
                             .setSide(OrderSide.Buy)
                             .setMarket(Market.HK);
                     Order[] resp = ctx.getTodayOrders(opts).get();
                     for (Order obj : resp) {
                         System.out.println(obj);
                     }
                 }
             }
         }
         
         
        Parameters:
        opts - Options for this request
        Returns:
        A Future representing the result of the operation
        Throws:
        OpenApiException - If an error occurs
      • replaceOrder

        public CompletableFuture<Void> replaceOrder​(ReplaceOrderOptions opts)
                                             throws OpenApiException
        Replace order
         
         import com.longport.*;
         import com.longport.trade.*;
         import java.math.BigDecimal;
         
         class Main {
             public static void main(String[] args) throws Exception {
                 try (Config config = Config.fromEnv(); TradeContext ctx = TradeContext.create(config).get()) {
                     ReplaceOrderOptions opts = new ReplaceOrderOptions("709043056541253632", 100)
                             .setPrice(new BigDecimal(300));
                     ctx.replaceOrder(opts).get();
                 }
             }
         }
         
         
        Parameters:
        opts - Options for this request, not null
        Returns:
        A Future representing the result of the operation
        Throws:
        OpenApiException - If an error occurs
      • submitOrder

        public CompletableFuture<SubmitOrderResponse> submitOrder​(SubmitOrderOptions opts)
                                                           throws OpenApiException
        Submit order
         
         import com.longport.*;
         import com.longport.trade.*;
         import java.math.BigDecimal;
         
         class Main {
             public static void main(String[] args) throws Exception {
                 try (Config config = Config.fromEnv(); TradeContext ctx = TradeContext.create(config).get()) {
                     SubmitOrderOptions opts = new SubmitOrderOptions(
                             "700.HK",
                             OrderType.LO,
                             OrderSide.Buy,
                             200,
                             TimeInForceType.Day).setSubmittedPrice(new BigDecimal(50));
                     SubmitOrderResponse resp = ctx.submitOrder(opts).get();
                     System.out.println(resp);
                 }
             }
         }
         
         
        Parameters:
        opts - Options for this request, not null
        Returns:
        A Future representing the result of the operation
        Throws:
        OpenApiException - If an error occurs
      • cancelOrder

        public CompletableFuture<Void> cancelOrder​(String orderId)
                                            throws OpenApiException
        Cancel order
         
         import com.longport.*;
         import com.longport.trade.*;
         
         class Main {
             public static void main(String[] args) throws Exception {
                 try (Config config = Config.fromEnv(); TradeContext ctx = TradeContext.create(config).get()) {
                     ctx.cancelOrder("709043056541253632").get();
                 }
             }
         }
         
         
        Parameters:
        orderId - Order ID
        Returns:
        A Future representing the result of the operation
        Throws:
        OpenApiException - If an error occurs
      • getAccountBalance

        public CompletableFuture<AccountBalance[]> getAccountBalance​(String currency)
                                                              throws OpenApiException
        Get account balance with currency
         
         import com.longport.*;
         import com.longport.trade.*;
         
         class Main {
             public static void main(String[] args) throws Exception {
                 try (Config config = Config.fromEnv(); TradeContext ctx = TradeContext.create(config).get()) {
                     AccountBalance[] resp = ctx.getAccountBalance("HKD").get();
                     for (AccountBalance obj : resp) {
                         System.out.println(obj);
                     }
                 }
             }
         }
         
         
        Returns:
        A Future representing the result of the operation
        Throws:
        OpenApiException - If an error occurs
      • getAccountBalance

        public CompletableFuture<AccountBalance[]> getAccountBalance()
                                                              throws OpenApiException
        Get account balance
         
         import com.longport.*;
         import com.longport.trade.*;
         
         class Main {
             public static void main(String[] args) throws Exception {
                 try (Config config = Config.fromEnv(); TradeContext ctx = TradeContext.create(config).get()) {
                     AccountBalance[] resp = ctx.getAccountBalance().get();
                     for (AccountBalance obj : resp) {
                         System.out.println(obj);
                     }
                 }
             }
         }
         
         
        Returns:
        A Future representing the result of the operation
        Throws:
        OpenApiException - If an error occurs
      • getCashFlow

        public CompletableFuture<CashFlow[]> getCashFlow​(GetCashFlowOptions opts)
                                                  throws OpenApiException
        Get cash flow
         
         import com.longport.*;
         import com.longport.trade.*;
         import java.time.*;
         
         class Main {
             public static void main(String[] args) throws Exception {
                 try (Config config = Config.fromEnv(); TradeContext ctx = TradeContext.create(config).get()) {
                     GetCashFlowOptions opts = new GetCashFlowOptions(
                             OffsetDateTime.of(2022, 5, 9, 0, 0, 0, 0, ZoneOffset.UTC),
                             OffsetDateTime.of(2022, 5, 12, 0, 0, 0, 0, ZoneOffset.UTC));
                     CashFlow[] resp = ctx.getCashFlow(opts).get();
                     for (CashFlow obj : resp) {
                         System.out.println(obj);
                     }
                 }
             }
         }
         
         
        Parameters:
        opts - Options for this request, not null
        Returns:
        A Future representing the result of the operation
        Throws:
        OpenApiException - If an error occurs
      • getFundPositions

        public CompletableFuture<FundPositionsResponse> getFundPositions​(GetFundPositionsOptions opts)
                                                                  throws OpenApiException
        Get fund positions
         
         import com.longport.*;
         import com.longport.trade.*;
         
         class Main {
             public static void main(String[] args) throws Exception {
                 try (Config config = Config.fromEnv(); TradeContext ctx = TradeContext.create(config).get()) {
                     FundPositionsResponse resp = ctx.getFundPositions(null).get();
                     System.out.println(resp);
                 }
             }
         }
         
         
        Parameters:
        opts - Options for this request
        Returns:
        A Future representing the result of the operation
        Throws:
        OpenApiException - If an error occurs
      • getStockPositions

        public CompletableFuture<StockPositionsResponse> getStockPositions​(GetStockPositionsOptions opts)
                                                                    throws OpenApiException
        Get stock positions
         
         import com.longport.*;
         import com.longport.trade.*;
         
         class Main {
             public static void main(String[] args) throws Exception {
                 try (Config config = Config.fromEnv(); TradeContext ctx = TradeContext.create(config).get()) {
                     StockPositionsResponse resp = ctx.getStockPositions(null).get();
                     System.out.println(resp);
                 }
             }
         }
         
         
        Parameters:
        opts - Options for this request
        Returns:
        A Future representing the result of the operation
        Throws:
        OpenApiException - If an error occurs
      • getMarginRatio

        public CompletableFuture<MarginRatio> getMarginRatio​(String symbol)
                                                      throws OpenApiException
        Get margin ratio
         
         import com.longport.*;
         import com.longport.trade.*;
         
         class Main {
             public static void main(String[] args) throws Exception {
                 try (Config config = Config.fromEnv(); TradeContext ctx = TradeContext.create(config).get()) {
                     StockPositionsResponse resp = ctx.getMarginRatio("700.HK").get();
                     System.out.println(resp);
                 }
             }
         }
         
         
        Parameters:
        symbol - Security symbol
        Returns:
        A Future representing the result of the operation
        Throws:
        OpenApiException - If an error occurs
      • getOrderDetail

        public CompletableFuture<OrderDetail> getOrderDetail​(String orderId)
                                                      throws OpenApiException
        Get order detail
         
         import com.longport.*;
         import com.longport.trade.*;
         
         class Main {
             public static void main(String[] args) throws Exception {
                 try (Config config = Config.fromEnv(); TradeContext ctx = TradeContext.create(config).get()) {
                     OrderDetail detail = ctx.getOrderDetail("701276261045858304").get();
                     System.out.println(resp);
                 }
             }
         }
         
         
        Parameters:
        orderId - Order id
        Returns:
        A Future representing the result of the operation
        Throws:
        OpenApiException - If an error occurs