下单交易

大约 14 分钟

创建订单

对应的请求类:TradeOrderRequest

说明

交易下单接口。关于如何选择标的、订单类型、方向数量等,请见下方说明。请在运行程序前结合本文档的概述部分及FAQ-交易-支持的订单列表部分,检查您的账户是否支持所请求的订单,并检查交易规则是否允许在程序运行时段对特定标的下单。若下单失败,可首先阅读文档FAQ-交易部分排查

CAUTION

1、市价单(MKT)和止损单(STP)不支持盘前盘后阶段交易,在调用下单接口时,需要把 outside_rth 设置为 false
2、对于可做空标的,暂不支持锁仓功能,因此无法同时持有相同标的多头和空头持仓
3、附加订单的主订单类型暂时仅支持限价单
4、限价价格不匹配tickSize,可以参考合约返回tickSizes字段,利用StockPriceUtils工具类判断是否匹配,修复价格符合tickSize要求
5、市价单(MKT)和模拟账号,对'time_in_force'参数都不支持GTC
6、模拟账号暂不支持窝轮和牛熊证的订单

订单状态说明

如何判断综合和模拟账号的部分成交状态?

当订单状态不是Initial和Filled(有可能是PendingSubmit,Cancelled,Invalid,Inactive其中一种)时,都有可能是部分成交的状态,可以通过订单成交数量是否大于0来判断

如何判断环球账号部分成交状态?

订单状态是 Filled,且订单成交数量大于0

订单状态变化流程:

订单状态流程图

其他说明

  • 指数期权除 IWM/SPY/QQQ 外, 不支持非周五期权的交易
  • 不可以直接开反向仓位,如持仓100股,直接卖出200股是不允许的,需先平仓

参数

参数类型描述市价单限价单止损单止损限价单跟踪止损单
accountstring用户授权账户:402901
order_idint订单编号,作用是防止重复下单。可以通过订单号接口获取。如果传0,则服务器端会自动生成订单编号,传0时无法防止重复下单,请谨慎选择选填选填选填选填选填
symbolstring股票代码 如:AAPL;(sec_typ为窝轮牛熊证时,在app窝轮/牛熊证列表中名称下面的5位数字)
sec_typestring合约类型 (STK 股票;OPT 美股期权; WAR 港股窝轮; IOPT 港股牛熊证; FUT 期货; FUND 基金)
actionstring交易方向 BUY/SELL
order_typestring订单类型. MKT(市价单), LMT(限价单), STP(止损单), STP_LMT(止损限价单), TRAIL(跟踪止损单)MKTLMTSTPSTP_LMTTRAIL
total_quantitylong下单数量(港股,沪港通,窝轮,牛熊证有最小数量限制)
total_quantity_scaleint下单数量的偏移量,默认为0。碎股单的total_quantity 和 total_quantity_scale 结合起来代表真实下单数量,如 total_quantity=111 total_quantity_scale=2,那么真实 quantity=111*10^(-2)=1.11选填选填选填选填选填
cash_amountDouble订单金额(基金等金额订单)选填
limit_pricedouble限价,当 order_type 为LMT,STP_LMT时该参数必需
aux_pricedouble股票订单止损触发价。含义为价差,与trailing_percent同时存在时被trailing_percent覆盖。当 order_type 为STP,STP_LMT时该参数必需,当 order_type 为 TRAIL时,为跟踪额选填
trailing_percentdouble跟踪止损单-止损百分比 。当 order_type 为 TRAIL时,aux_price和trailing_percent两者互斥,优先使用trailing_percent选填
outside_rthbooleantrue: 允许盘前盘后交易(美股专属), false: 不允许,默认为允许。(市价单、止损单、跟踪止损单只在盘中有效,将忽略outside_rth参数)选填选填选填
trading_session_typeTradeSessionOverNight: 美股夜盘订单。(仅限价单)选填选填
adjust_limitdouble价格微调幅度(默认为0表示不调整,正数为向上调整,负数向下调整),对传入价格自动调整到合法价位上。例如:0.001 代表向上调整且幅度不超过 0.1%;-0.001 代表向下调整且幅度不超过 0.1%。默认 0 表示不调整选填选填选填选填
marketstring市场 (美股 US 港股 HK 沪港通 CN)选填选填选填选填选填
currencystring货币(美股 USD 港股 HKD 沪港通 CNH)选填选填选填选填选填
time_in_forcestring订单有效期,只能是 DAY(当日有效)、GTC(取消前有效,最长有效时间180天)、GTD(在指定时间前有效),默认为DAY选填选填选填选填选填
expire_timelong订单有效的截止时间, 13位的时间戳,精确到秒(time_in_force为GTD时为必填,其他类型时无效)选填选填选填
exchangestring交易所 (美股 SMART 港股 SEHK 沪港通 SEHKNTL 深港通 SEHKSZSE)否选填选填选填选填选填
expirystring过期日(期权、窝轮、牛熊证专属)选填选填选填选填选填
strikestring底层价格(期权、窝轮、牛熊证专属)选填选填选填选填选填
rightstring期权方向 PUT/CALL(期权、窝轮、牛熊证专属)选填选填选填选填选填
multiplierfloat1手单位(期权、窝轮、牛熊证专属)选填选填选填选填选填
local_symbolstring窝轮牛熊证该字段必填,在app窝轮/牛熊证列表中名称下面的5位数字选填选填选填选填选填
secret_keystring机构用户专用,交易员密钥选填选填选填选填选填
user_markString下单备注信息,下单后不能修改,查询订单时返回userMark信息选填选填选填选填选填
  • 附加订单参数

附加订单(Attached Order )是指能通过附加的子订单对主订单起到止盈或止损效果的订单,可以附加的子订单类型有限价单(可用于止盈)、止损限价单/止损单(可用于止损)。通过增加以下参数可以实现附加订单

参数类型描述附加止损附加止盈附加跟踪止损附加括号
attach_typestring附加订单类型,下附加订单时必填。(order_type应为LMT): PROFIT-止盈单,LOSS-止损单,BRACKETS-括号订单(包含附加止盈单和附加止损单)
profit_taker_orderIdint止盈单编号,可以通过订单号接口获取。如果传0,则服务器端会自动生成止盈单编号选填选填选填
profit_taker_pricedouble止盈单价格,下止盈单时必填
profit_taker_tifstring同time_in_force字段,订单有效期,只能是 DAY(当日有效)和GTC(取消前有效),下止盈单时必填
profit_taker_rthboolean同outside_rth字段
stop_loss_orderIdint止损单编号,可以通过订单号接口获取。如果传0,则服务器端会自动生成止损单编号
stop_loss_pricedouble止损单价格(止损单的触发价),下止损单时必填
stop_loss_limit_pricedouble止损单的执行限价(暂只对综合账号有效)。止损单的限价没有填写时,为附加止损市价单选填选填
stop_loss_tifstring同time_in_force字段,订单有效期,只能是 DAY(当日有效)和GTC(取消前有效),下止损单时必填
stop_loss_trailing_percentdouble跟踪止损单-止损百分比,当下跟踪止损单时,止损百分比(stopLossTrailingPercent)和止损额(stopLossTrailingAmount)其中一项必填,如果都填时,会使用止损百分比作为参数。选填选填
stop_loss_trailing_amountdouble跟踪止损单-止损额,当下跟踪止损单时,止损百分比(stopLossTrailingPercent)和止损额(stopLossTrailingAmount)其中一项必填,如果都填时,会使用止损百分比作为参数。选填选填
  • TWAP/VWAP订单参数

TWAP/VWAP订单,只支持美股股票标的,只能在盘中下单,不支持预挂单

参数类型算法参数描述TWAPVWAP
order_typestring订单类型,TWAP/VWAP
accountstring资金账号
symbolstring股票代码 如:AAPL
sec_typestring只支持STK
total_quantityboolean订单数量
algo_paramsList<TagValue>算法参数选填选填
-longstart_time策略开始时间(时间戳)选填选填
-longend_time策略结束时间(时间戳)选填选填
-stringparticipation_rate最大参与率(成交量为日均成交量的最大比例,0.01-0.5)选填

返回

名称类型说明
idlong唯一单号ID,可用于查询订单/修改订单/取消订单
subIdsList<Long>附加单时,返回子订单号ID列表
ordersList<TradeOrder>返回订单详细信息

构建合约对象

// 美股股票合约
ContractItem contract = ContractItem.buildStockContract("SPY", "USD");

// 港股股票合约
ContractItem contract = ContractItem.buildStockContract("00700", "HKD");

// 港股窝轮合约(需要注意同一个symbol,环球账号和综合账号的expiry可能不同)
ContractItem contract = ContractItem.buildWarrantContract("13745", "20211217", 719.38D, Right.CALL.name());
// 港股牛熊证合约
ContractItem contract = ContractItem.buildCbbcContract("50296", "20220331", 457D, Right.CALL.name());

// 美股期权合约
ContractItem contract = ContractItem.buildOptionContract("AAPL  190118P00160000");
ContractItem contract = ContractItem.buildOptionContract("AAPL", "20211119", 150.0D, "CALL");

// 期货合约
// 环球账户
ContractItem contract = ContractItem.buildFutureContract("CL", "USD", "SGX", "20190328", 1.0D);
// 综合账户
ContractItem contract = ContractItem.buildFutureContract("CL2112", "USD");

市价单(MKT)

// get contract(use default account)
ContractRequest contractRequest = ContractRequest.newRequest(new ContractModel("AAPL"));
ContractResponse contractResponse = client.execute(contractRequest);
ContractItem contract = contractResponse.getItem();
// market order(use default account)
TradeOrderRequest request = TradeOrderRequest.buildMarketOrder(contract, ActionType.BUY, 10);
TradeOrderResponse response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

// get contract(use account parameter)
ContractRequest contractRequest = ContractRequest.newRequest(new ContractModel("AAPL"), "402901");
ContractResponse contractResponse = client.execute(contractRequest);
ContractItem contract = contractResponse.getItem();
// market order(use account parameter)
request = TradeOrderRequest.buildMarketOrder("402901", contract, ActionType.BUY, 10);
response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

限价单(LMT)

// use default account
TradeOrderRequest request = TradeOrderRequest.buildLimitOrder(contract, ActionType.BUY, 1, 100.0d);
TradeOrderResponse response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

// use account parameter
request = TradeOrderRequest.buildLimitOrder("402901", contract, ActionType.BUY, 1, 100.0d);
// set user_mark
request.setUserMark("test001");
// set GTD order's expire_time
request.setTimeInForce(TimeInForce.GTD);
request.setExpireTime(1669363583804L);
response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

// place overnight order in the US market
request = TradeOrderRequest.buildLimitOrder("402901", contract, ActionType.BUY, 1, 100.0d);
// set overnight order
request.setTradingSessionType(TradeSession.OverNight);
response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

竞价单(AM/AL)

// auction order in hk market
TradeOrderRequest request = TradeOrderRequest.buildLimitOrder("402901", contract, ActionType.BUY, 100, 100.0d);
// 盘前竞价: AM or AL + OPG, 如果未成交参与盘中交易; 盘后竞价: AM or AL + DAY
// participate in the pre-market auction, set auction limit order
request.setAuctionOrder(OrderType.AL, TimeInForce.OPG);
response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

request = TradeOrderRequest.buildMarketOrder("402901", contract, ActionType.BUY, 100);
// Participate in the after-hours auction, set auction market order
request.setAuctionOrder(OrderType.AM, TimeInForce.OPG);
response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

止损单(STP)

// use default account
TradeOrderRequest request = TradeOrderRequest.buildStopOrder(contract, ActionType.BUY, 1, 120.0d);
TradeOrderResponse response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

// use account parameter
request = TradeOrderRequest.buildStopOrder("402901", contract, ActionType.BUY, 1, 120.0d);
response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

止损限价单(STP_LMT)

// use default account
TradeOrderRequest request = TradeOrderRequest.buildStopLimitOrder(contract, ActionType.BUY, 1,150d,130.0d);
TradeOrderResponse response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

// use account parameter
request = TradeOrderRequest.buildStopLimitOrder("402901", contract, ActionType.BUY, 1,150d,130.0d);
response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

跟踪止损单(TRAIL)

// use default account
TradeOrderRequest request = TradeOrderRequest.buildTrailOrder(contract, ActionType.BUY, 1,10d,130.0d);
TradeOrderResponse response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

// use account parameter. standard account currently not supported
request = TradeOrderRequest.buildTrailOrder("402901", contract, ActionType.BUY, 1, 10d, 130.0d);
response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

主订单+附加止盈单

// use default account
TradeOrderRequest request = TradeOrderRequest.buildLimitOrder(contract, ActionType.BUY, 1, 199d);
TradeOrderRequest.addProfitTakerOrder(request, 250D, TimeInForce.DAY, Boolean.FALSE);
TradeOrderResponse response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

// use account parameter
request = TradeOrderRequest.buildLimitOrder("402901", contract, ActionType.BUY, 1, 199d);
TradeOrderRequest.addProfitTakerOrder(request, 250D, TimeInForce.DAY, Boolean.FALSE);
response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

主订单+附加止损单

// use default account
TradeOrderRequest request = TradeOrderRequest.buildLimitOrder(contract, ActionType.BUY, 1, 129d);
TradeOrderRequest.addStopLossOrder(request, 100D, TimeInForce.DAY);
TradeOrderResponse response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

// use account parameter
request = TradeOrderRequest.buildLimitOrder("402901", contract, ActionType.BUY, 1, 129d);
// 添加附加止损市价单,附加止损价格是触发价(不支持期权标的)
TradeOrderRequest.addStopLossOrder(request, 100D, TimeInForce.DAY);
response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

// 期权可以使用附加止损限价单
ContractItem optionContract = ContractItem.buildOptionContract("AAPL", "20211231", 175.0D, "CALL");
request = TradeOrderRequest.buildLimitOrder("402901", optionContract, ActionType.BUY, 1, 2.0d);
// 添加附加止损限价单,其中第一个价格1.7是触发价,第二个价格1.69是附加止损单的挂单限价(暂只支持综合账号)
TradeOrderRequest.addStopLossLimitOrder(request, 1.7D, 1.69D, TimeInForce.DAY);
response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

主订单+附加跟踪止损单

ContractItem contract = ContractItem.buildStockContract("AAPL", "USD");
// use default account
TradeOrderRequest request = TradeOrderRequest.buildLimitOrder(contract, ActionType.BUY, 1, 165D);
TradeOrderRequest.addStopLossTrailOrder(request, 10.0D, null, TimeInForce.DAY);
TradeOrderResponse response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

// use account parameter
request = TradeOrderRequest.buildLimitOrder("402901", contract, ActionType.BUY, 1, 165D);
TradeOrderRequest.addStopLossTrailOrder(request, 10.0D, null, TimeInForce.DAY);
response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

主订单+附加括号订单

// use default account
TradeOrderRequest request = TradeOrderRequest.buildLimitOrder(contract, ActionType.BUY, 1, 199d);
TradeOrderRequest.addBracketsOrder(request, 250D, TimeInForce.DAY, Boolean.FALSE, 180D, TimeInForce.GTC);
TradeOrderResponse response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

// use account parameter
request = TradeOrderRequest.buildLimitOrder("13810712", contract, ActionType.BUY, 1, 199d);
TradeOrderRequest.addBracketsOrder(request, 250D, TimeInForce.DAY, Boolean.FALSE, 180D, TimeInForce.GTC);
response = client.execute(request);
System.out.println(JSONObject.toJSONString(response));

换汇单

ForexTradeOrderRequest request = ForexTradeOrderRequest.buildRequest("402901", 
    SegmentType.SEC, Currency.HKD, 1000D, Currency.USD);

ForexTradeOrderResponse response = client.execute(request);
if (response.isSuccess()) {
  System.out.println(JSONObject.toJSONString(response));
} else {
  System.out.println(response.getMessage());
}

基金金额单

    ContractItem contract = ContractItem.buildFundContract("IE00B464Q616.USD", "USD");
    TradeOrderRequest request = TradeOrderRequest.buildAmountOrder(
        "13810712", contract, ActionType.BUY, 100.0D);
    request.setUserMark("test-amount-order");
    TradeOrderResponse response = client.execute(request);
    if (response.isSuccess()) {
      System.out.println(JSONObject.toJSONString(response));
    } else {
      System.out.println(response.getMessage());
    }

TWAP/VWAP订单

只支持美股股票,只支持盘中下单。不能改单,可以撤单

// TWAP order
TradeOrderRequest twapRequest = TradeOrderRequest.buildTWAPOrder(
    "572386", "DM", ActionType.BUY, 500,
    DateUtils.getTimestamp("2023-06-20 09:30:00", TimeZoneId.NewYork),
    DateUtils.getTimestamp("2023-06-20 11:00:00", TimeZoneId.NewYork),
     1.5D)
  .setUserMark("testTWAP001")
  .setLang(Language.en_US);

TradeOrderResponse twapResponse = client.execute(twapRequest);
if (twapResponse.isSuccess()) {
  System.out.println(JSONObject.toJSONString(twapResponse));
} else {
  System.out.println(twapResponse.getMessage());
}

// VWAP order
TradeOrderRequest vwapRequest = TradeOrderRequest.buildVWAPOrder(
    "572386", "DM", ActionType.BUY, 500,
    DateUtils.getTimestamp("2023-06-20 09:30:00", TimeZoneId.NewYork),
    DateUtils.getTimestamp("2023-06-20 11:00:00", TimeZoneId.NewYork),
    0.5D, 1.5D)
  .setUserMark("testVWAP001")
  .setLang(Language.en_US);

TradeOrderResponse vwapResponse = client.execute(vwapRequest);
if (vwapResponse.isSuccess()) {
  System.out.println(JSONObject.toJSONString(vwapResponse));
} else {
  System.out.println(vwapResponse.getMessage());
}

期权多腿订单

只支持盘中下单

List<ContractLeg> contractLegs = new ArrayList<>();
ContractLeg leg1 = new ContractLeg(SecType.OPT, "AAPL",
    "170.0", "20231013", Right.CALL,
    ActionType.BUY, 1);
contractLegs.add(leg1);
ContractLeg leg2 = new ContractLeg(SecType.OPT, "AAPL",
    "170.0", "20231013", Right.PUT,
    ActionType.BUY, 1);
contractLegs.add(leg2);

TradeOrderRequest request = TradeOrderRequest.buildMultiLegOrder(
    "572386", contractLegs, ComboType.CUSTOM,
        ActionType.BUY, 3,
        OrderType.LMT, 2.01d, null, null)
    .setLang(Language.en_US)
    .setUserMark("test_multi_leg");
TradeOrderResponse response = client.execute(request);
if (response.isSuccess()) {
  System.out.println(JSONObject.toJSONString(response));
} else {
  System.out.println(response.getMessage());
}

OCA括号单

OCA括号订单内的两个订单标的相同,一个止盈限价单,另一个为止损单或者止损限价单。其中一个成交时,自动取消另一个订单。下单后返回两个order对象,订单中的'ocaGroupId'相同的为一个组合。 不支持模拟盘

ContractItem contract = ContractItem.buildStockContract("BILI", "USD");
TradeOrderRequest request = TradeOrderRequest.buildOCABracketsOrder(
        "13810712", contract, ActionType.SELL, 1,
        17.0D, TimeInForce.DAY, Boolean.TRUE,
        12.0D, null, TimeInForce.DAY, Boolean.FALSE);
request.setLang(Language.en_US).setUserMark("test-oca");

TradeOrderResponse response = client.execute(request);
if (response.isSuccess()) {
  System.out.println(JSONObject.toJSONString(response));
  // get oca order info
  List<TradeOrder> ocaOrders = response.getItem().getOrders();
} else {
  System.out.println(response.getMessage());
}

返回示例 附加括号单

{
    "id":30325712346546176,
    "orderId":0,
    "subIds":[
        30325712346546177,
        30325712346677250
    ],
    "orders":[
        {
            "account":"13810712",
            "action":"BUY",
            "algoStrategy":"LMT",
            "attrDesc":"",
            "avgFillPrice":0,
            "canCancel":true,
            "canModify":true,
            "commission":0,
            "currency":"HKD",
            "discount":0,
            "filledQuantity":0,
            "id":30325712346546176,
            "identifier":"00700",
            "latestPrice":385.8,
            "latestTime":1680266023000,
            "limitPrice":295,
            "liquidation":false,
            "market":"HK",
            "name":"腾讯控股",
            "openTime":1680266023000,
            "orderId":91,
            "orderType":"LMT",
            "outsideRth":true,
            "realizedPnl":0,
            "remark":"",
            "secType":"STK",
            "source":"OpenApi",
            "status":"Initial",
            "symbol":"00700",
            "timeInForce":"DAY",
            "totalQuantity":100,
            "updateTime":1680266023000,
            "userMark":"test_bracket"
        },
        {
            "account":"13810712",
            "action":"SELL",
            "algoStrategy":"LMT",
            "attrDesc":"",
            "avgFillPrice":0,
            "canCancel":true,
            "canModify":true,
            "commission":0,
            "currency":"HKD",
            "discount":0,
            "filledQuantity":0,
            "id":30325712346546177,
            "identifier":"00700",
            "latestPrice":385.8,
            "latestTime":1680266023000,
            "limitPrice":320,
            "liquidation":false,
            "market":"HK",
            "name":"腾讯控股",
            "ocaGroupId":87055,
            "openTime":1680266023000,
            "orderId":92,
            "orderType":"LMT",
            "outsideRth":true,
            "parentId":30325712346546176,
            "realizedPnl":0,
            "remark":"",
            "secType":"STK",
            "source":"OpenApi",
            "status":"Initial",
            "symbol":"00700",
            "timeInForce":"DAY",
            "totalQuantity":100,
            "updateTime":1680266023000,
            "userMark":"test_bracket"
        },
        {
            "account":"13810712",
            "action":"SELL",
            "algoStrategy":"STP_LMT",
            "attrDesc":"",
            "auxPrice":280,
            "avgFillPrice":0,
            "canCancel":true,
            "canModify":true,
            "commission":0,
            "currency":"HKD",
            "discount":0,
            "filledQuantity":0,
            "id":30325712346677248,
            "identifier":"00700",
            "latestPrice":385.8,
            "latestTime":1680266023000,
            "limitPrice":278,
            "liquidation":false,
            "market":"HK",
            "name":"腾讯控股",
            "ocaGroupId":87055,
            "openTime":1680266023000,
            "orderId":93,
            "orderType":"STP_LMT",
            "outsideRth":true,
            "parentId":30325712346546176,
            "realizedPnl":0,
            "remark":"",
            "secType":"STK",
            "source":"OpenApi",
            "status":"Initial",
            "symbol":"00700",
            "timeInForce":"DAY",
            "totalQuantity":100,
            "updateTime":1680266023000,
            "userMark":"test_bracket"
        }
    ]
}
上次编辑于: