Place Order

place_order Place Order

value TradeClient::place_order(Order &order)

Description

Trading order placement interface.

After a successful place_order call, the order object's id field is populated and can be used for subsequent queries or cancellations. This only indicates that the order was submitted successfully, not that it has been executed. Order execution is asynchronous. After placing an order, use get_order or get_orders methods to query the order status.

Note

  1. Market orders (MKT) and stop orders (STP) do not support pre/post market trading
  2. For shortable symbols, position locking is not currently supported. You cannot hold both long and short positions for the same symbol simultaneously
  3. Directly opening a reverse position is prohibited

Parameters

Order object, built using the OrderUtil utility class

Return

web::json::value JSON object, returns order information on success


Building Contract Object Examples

#include "tigerapi/contract_util.h"

using namespace TIGER_API;

// US stock
Contract contract = ContractUtil::stock_contract(U("TIGR"), U("USD"));

// HK stock
Contract contract = ContractUtil::stock_contract(U("00700"), U("HKD"));

// Option
Contract contract = ContractUtil::option_contract(U("AAPL  240621C00190000"));
// or
Contract contract = ContractUtil::option_contract(U("AAPL"), U("20240621"), U("190"), U("CALL"));

// Futures
Contract contract = ContractUtil::future_contract(U("CL2312"), U("USD"));

Limit Order (LMT)

#include "tigerapi/trade_client.h"
#include "tigerapi/client_config.h"
#include "tigerapi/contract_util.h"
#include "tigerapi/order_util.h"

using namespace TIGER_API;

ClientConfig config(false, U("/path/to/your/properties/"));
TradeClient trade_client(config);

// Build stock contract
Contract contract = ContractUtil::stock_contract(U("AAPL"), U("USD"));

// Build limit order
Order order = OrderUtil::limit_order(
    config.account,  // Trading account
    contract,        // Contract object
    U("BUY"),        // Buy direction
    100,             // Quantity
    150.0            // Limit price
);

// Place order
value result = trade_client.place_order(order);
ucout << result.serialize() << std::endl;

// Get order ID
std::cout << "Order ID: " << order.id << std::endl;

Market Order (MKT)

#include "tigerapi/trade_client.h"
#include "tigerapi/client_config.h"
#include "tigerapi/contract_util.h"
#include "tigerapi/order_util.h"

using namespace TIGER_API;

ClientConfig config(false, U("/path/to/your/properties/"));
TradeClient trade_client(config);

// Build stock contract
Contract contract = ContractUtil::stock_contract(U("AAPL"), U("USD"));

// Build market order
Order order = OrderUtil::market_order(
    config.account,  // Trading account
    contract,        // Contract object
    U("BUY"),        // Buy direction
    100              // Quantity
);

// Place order
value result = trade_client.place_order(order);

Stop Order (STP)

#include "tigerapi/trade_client.h"
#include "tigerapi/client_config.h"
#include "tigerapi/contract_util.h"
#include "tigerapi/order_util.h"

using namespace TIGER_API;

ClientConfig config(false, U("/path/to/your/properties/"));
TradeClient trade_client(config);

Contract contract = ContractUtil::stock_contract(U("AAPL"), U("USD"));

// Build stop order
Order order = OrderUtil::stop_order(
    config.account,  // Trading account
    contract,        // Contract object
    U("SELL"),       // Sell direction
    100,             // Quantity
    140.0            // Stop trigger price
);

value result = trade_client.place_order(order);

Stop Limit Order (STP_LMT)

#include "tigerapi/trade_client.h"
#include "tigerapi/client_config.h"
#include "tigerapi/contract_util.h"
#include "tigerapi/order_util.h"

using namespace TIGER_API;

ClientConfig config(false, U("/path/to/your/properties/"));
TradeClient trade_client(config);

Contract contract = ContractUtil::stock_contract(U("AAPL"), U("USD"));

// Build stop limit order
Order order = OrderUtil::stop_limit_order(
    config.account,  // Trading account
    contract,        // Contract object
    U("SELL"),       // Sell direction
    100,             // Quantity
    139.0,           // Limit price
    140.0            // Stop trigger price
);

value result = trade_client.place_order(order);

Trailing Stop Order (TRAIL)

#include "tigerapi/trade_client.h"
#include "tigerapi/client_config.h"
#include "tigerapi/contract_util.h"
#include "tigerapi/order_util.h"

using namespace TIGER_API;

ClientConfig config(false, U("/path/to/your/properties/"));
TradeClient trade_client(config);

Contract contract = ContractUtil::stock_contract(U("AAPL"), U("USD"));

// Trailing stop order - by trailing amount
Order order = OrderUtil::trail_order(
    config.account,  // Trading account
    contract,        // Contract object
    U("SELL"),       // Sell direction
    100,             // Quantity
    5.0,             // Trailing amount (aux_price)
    0                // Trailing percent (0 means not used)
);

// Trailing stop order - by percentage
Order order2 = OrderUtil::trail_order(
    config.account,
    contract,
    U("SELL"),
    100,
    0,               // aux_price, 0 means not used
    8.0              // trailing_percent 8%
);

value result = trade_client.place_order(order);

Place HK Stock Order

For HK stock trading, the order quantity must be a multiple of the stock's "lot size".

ClientConfig config(false, U("/path/to/your/properties/"));
TradeClient trade_client(config);

Contract contract = ContractUtil::stock_contract(U("00700"), U("HKD"));

Order order = OrderUtil::limit_order(
    config.account,
    contract,
    U("BUY"),
    100,           // Tencent has 100 shares per lot
    400.0
);

value result = trade_client.place_order(order);

Place Futures Order

ClientConfig config(false, U("/path/to/your/properties/"));
TradeClient trade_client(config);

Contract contract = ContractUtil::future_contract(U("CL2312"), U("USD"));

Order order = OrderUtil::limit_order(
    config.account,
    contract,
    U("BUY"),
    1,
    70.0
);

value result = trade_client.place_order(order);

Place Option Order

ClientConfig config(false, U("/path/to/your/properties/"));
TradeClient trade_client(config);

// Build contract using option identifier
Contract contract = ContractUtil::option_contract(U("AAPL  240621C00190000"));

Order order = OrderUtil::limit_order(
    config.account,
    contract,
    U("BUY"),
    1,
    2.5
);

value result = trade_client.place_order(order);

Price Correction for Orders

Use PriceUtil to correct order prices. When a contract price is at different price tiers, there are different precision requirements. Submitting an order with an inappropriate precision will return an error.

#include "tigerapi/trade_client.h"
#include "tigerapi/client_config.h"
#include "tigerapi/contract_util.h"
#include "tigerapi/order_util.h"
#include "tigerapi/price_util.h"

using namespace TIGER_API;

ClientConfig config(false, U("/path/to/your/properties/"));
TradeClient trade_client(config);

// Query contract to get tickSizes information
value contract_info = trade_client.get_contract(U("AAPL"), U("STK"));
value tick_sizes = contract_info[U("tickSizes")];

double price = 150.173;

// Check if price matches tick size specification
bool is_ok = PriceUtil::match_tick_size(price, tick_sizes);

// Fix price (default rounds down)
double fixed_price = PriceUtil::fix_price_by_tick_size(price, tick_sizes);
// fixed_price = 150.17

// Fix price (rounds up)
double fixed_price_up = PriceUtil::fix_price_by_tick_size(price, tick_sizes, true);
// fixed_price_up = 150.18

// Place order with corrected price
Contract contract = ContractUtil::stock_contract(U("AAPL"), U("USD"));
Order order = OrderUtil::limit_order(config.account, contract, U("BUY"), 1, fixed_price);
trade_client.place_order(order);

Order Object Properties

PropertyTypeDescription
idunsigned long longOrder ID
order_idlongExternal order ID
accountutility::string_tFund account
contractContractContract object (contains symbol, sec_type, market, currency)
actionutility::string_tTrade direction BUY/SELL
order_typeutility::string_tOrder type MKT/LMT/STP/STP_LMT/TRAIL
total_quantitylong longTotal order quantity
total_quantity_scalelongQuantity precision factor
limit_pricedoubleLimit price
aux_pricedoubleStop trigger price
trail_stop_pricedoubleTrailing stop price
trailing_percentdoubleTrailing stop percentage
percent_offsetdoublePercent offset
time_in_forceutility::string_tOrder validity period DAY/GTC/GTD
outside_rthboolWhether to allow pre/post market trading
adjust_limitdoublePrice adjustment range
user_markutility::string_tUser remarks
expire_timetime_tExpiry time
statusutility::string_tOrder status
filled_quantitylong longFilled quantity
avg_fill_pricedoubleAverage fill price
realized_pnldoubleRealized P&L
commissiondoubleCommission
open_timetime_tOrder placement time
latest_timetime_tLatest fill time
update_timetime_tOrder update time
reasonutility::string_tOrder failure reason