Package com.longport.trade
Class TradeContext
- java.lang.Object
-
- com.longport.trade.TradeContext
-
- All Implemented Interfaces:
AutoCloseable
public class TradeContext extends Object implements AutoCloseable
Trade context
-
-
Constructor Summary
Constructors Constructor Description TradeContext()
-
Method Summary
-
-
-
Method Detail
-
create
public static CompletableFuture<TradeContext> create(Config config) throws OpenApiException
Create a TradeContext object- Parameters:
config
- Config object- Returns:
- A Future representing the result of the operation
- Throws:
OpenApiException
- If an error occurs
-
close
public void close() throws Exception
- Specified by:
close
in interfaceAutoCloseable
- Throws:
Exception
-
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
Subscribeimport 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
-
unsubscribe
public CompletableFuture<Void> unsubscribe(TopicType[] topics) throws OpenApiException
Unsubscribe- 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 executionsimport 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 executionsimport 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 ordersimport 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 ordersimport 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 orderimport 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 orderimport 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 orderimport 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 currencyimport 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); } } } }
- Parameters:
currency
- Currency- Returns:
- A Future representing the result of the operation
- Throws:
OpenApiException
- If an error occurs
-
getAccountBalance
public CompletableFuture<AccountBalance[]> getAccountBalance() throws OpenApiException
Get account balanceimport 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 flowimport 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 positionsimport 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 positionsimport 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 ratioimport 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 detailimport 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
-
getEstimateMaxPurchaseQuantity
public CompletableFuture<EstimateMaxPurchaseQuantityResponse> getEstimateMaxPurchaseQuantity(EstimateMaxPurchaseQuantityOptions opts) throws OpenApiException
Estimating the maximum purchase quantity for Hong Kong and US stocks, warrants, and options- Parameters:
opts
- Options for this request- Returns:
- A Future representing the result of the operation
- Throws:
OpenApiException
- If an error occurs
-
-