Place Order

Placing Order

value place_order(value &order); value place_order(Order &order);

Description

Interface for placing order. This method allows you to set order types, quantity, buy/sell, of you choice, please see below for more information.

Please read the Introduction section of this documentation, as well as FAQ-Trade-Supported Order Types section, to make sure that the order types involved in your program are actually allowed.

If this method returns an error, or that your order gets canceled by the system, it is recommended that you refer to FAQ-Trade section for a quick inspection yourself.

When a order is successfully placed, the id attribute in Order object will be filled (order.id). This id can be used to identify a unique order and thus can be used to modify or cancel an order.

  • Market Order(MKT)and Stop Order(STP)do not support pre-market and post-market transactions. When placing the order, you need to set 'outside_rth' to false
  • For symbols that can be shorted, the lock-up function is not currently supported, so it is impossible to hold long and short positions of the same symbol at the same time
  • For Market Orders (MKT) and the Paper Trade account, GTC is not supported for 'time_in_force' parameter

Order Status Explanation

  • How can I determine the partial fulfillment status of my Prime or Paper accounts?

When the order status is not FILLED (NEW, CANCELLED, EXPIRED, REJECTED), it may be a partially filled status, which can be judged by whether the number of orders filled is greater than 0.

Order Status Flow

Parameters

Order Object

You can construct an Order Object using the functions defined in src/order_util.cpp, such as limit_order() or market_order(). Please see Order Object - Construction in Appendix 1: Objects for more details.

Common Attributes:

AttributeTypeDescription
order_typeutility::string_tOrder Types, 'MKT'-Market Order / 'LMT'-Limit Order / 'STP'-Stop Order / 'STP_LMT'-Stop-Limit Order
accountutility::string_tAccount to which the order belongs
contractContractOrder contract
actionutility::string_tOrder direction, 'BUY' for buying, 'SELL' for selling orders
total_quantitylong longQuantity of the order, must be an integer greater than 0. Quantity must be a multiple of the lot size, You can use TradeClient.get_trade_metas to check the lot size

Response

if the order is successfully placed , will return success, otherwise it will throw an exception.

Example Call

#include "tigerapi/quote_client.h"
#include "tigerapi/trade_client.h"
#include "tigerapi/contract_util.h"
#include "tigerapi/order_util.h"
#include <iostream>
#include <memory>
using namespace std;
using namespace web;
using namespace web::json;
using namespace TIGER_API;

class TestTradeClient {
public:
    static void test_place_order(const std::shared_ptr<TradeClient>& trade_client) {
    Contract contract = TIGER_API::ContractUtil::stock_contract(U("SPCX"),U("USD"));
    Order order = TIGER_API::OrderUtil::limit_order(contract, U("BUY"), 1, 157.0);
    value res = trade_client->place_order(order);
    long id = res[U("id")].as_integer(); 
    cout << "order id: " << id << endl;
    ucout << "place order result: " << res << endl;
    }
      static void test_trade(const std::shared_ptr<TradeClient>& trade_client) {
      TestTradeClient::test_place_order(trade_client);
    }
};

int main(int argc, char* args[]) {

    ClientConfig config(true, U("tiger_openapi_config.properties"));

    std::shared_ptr<TradeClient> trade_client = std::make_shared<TradeClient>(config);
    TestTradeClient::test_trade(trade_client);
    
        return 0;
};

Market Order (MKT)

Order
OrderUtil::market_order(const utility::string_t account, Contract &contract, const utility::string_t action, long quantity) {
    // market order
    return Order(U("MKT"), account, contract, action, quantity);
    }

Example Call

#include "tigerapi/quote_client.h"
#include "tigerapi/trade_client.h"
#include "tigerapi/contract_util.h"
#include "tigerapi/order_util.h"
#include <iostream>
#include <memory>
using namespace std;
using namespace web;
using namespace web::json;
using namespace TIGER_API;

class TestTradeClient {
public:
    static void test_place_order(const std::shared_ptr<TradeClient>& trade_client) {
        Contract contract = TIGER_API::ContractUtil::stock_contract(U("SPCX"), U("USD"));
        Order order = TIGER_API::OrderUtil::market_order(trade_client->client_config.account, contract, U("BUY"), 1);
        value res = trade_client->place_order(order);
        long id = res[U("id")].as_integer();
        cout << "order id: " << id << endl;
        ucout << "place order result: " << res << endl;
    }
    static void test_trade(const std::shared_ptr<TradeClient>& trade_client) {
        TestTradeClient::test_place_order(trade_client);
    }
};

int main(int argc, char* args[]) {

    ClientConfig config(true, U("tiger_openapi_config.properties"));

    std::shared_ptr<TradeClient> trade_client = std::make_shared<TradeClient>(config);
    TestTradeClient::test_trade(trade_client);

    return 0;
}

Limit Order (LMT)

    Order
    OrderUtil::limit_order(const utility::string_t account, Contract &contract, const utility::string_t action, long quantity,
                double limit_price) {
        // limit order
        return Order(U("LMT"), account, contract, action, quantity, limit_price);
    }

    Order OrderUtil::limit_order(Contract &contract, const utility::string_t action, long quantity, double limit_price) {
        // limit order
        return Order(U("LMT"), U(""), contract, action, quantity, limit_price);
    }

    Order OrderUtil::limit_order(Contract& contract, const utility::string_t action, long quantity, utility::string_t limit_price) {
        // limit order, price is string type
        Order order = Order();
        order.order_type = U("LMT");
        order.contract = contract;
        order.action = action;
        order.total_quantity = quantity;
        order.s_limit_price = limit_price;
        return order;
    }

Example Call

#include "tigerapi/quote_client.h"
#include "tigerapi/trade_client.h"
#include "tigerapi/contract_util.h"
#include "tigerapi/order_util.h"
#include <iostream>
#include <memory>
using namespace std;
using namespace web;
using namespace web::json;
using namespace TIGER_API;

class TestTradeClient {
public:
    static void test_place_order(const std::shared_ptr<TradeClient>& trade_client) {
        Contract contract = TIGER_API::ContractUtil::stock_contract(U("SPCX"), U("USD"));
        Order order = TIGER_API::OrderUtil::limit_order(trade_client->client_config.account, contract, U("BUY"), 1, 160);
        value res = trade_client->place_order(order);
        long id = res[U("id")].as_integer();
        cout << "order id: " << id << endl;
        ucout << "place order result: " << res << endl;
    }
    static void test_trade(const std::shared_ptr<TradeClient>& trade_client) {
        TestTradeClient::test_place_order(trade_client);
    }
};

int main(int argc, char* args[]) {

    ClientConfig config(true, U("tiger_openapi_config.properties"));

    std::shared_ptr<TradeClient> trade_client = std::make_shared<TradeClient>(config);
    TestTradeClient::test_trade(trade_client);

    return 0;
}

Stop Order (STP)

Order OrderUtil::stop_order(const utility::string_t account, Contract &contract, const utility::string_t action, long quantity,
                     double aux_price) {
        // stop order
        return Order(U("STP"), account, contract, action, quantity, 0, aux_price);
    }

Example Call

#include "tigerapi/quote_client.h"
#include "tigerapi/trade_client.h"
#include "tigerapi/contract_util.h"
#include "tigerapi/order_util.h"
#include <iostream>
#include <memory>
using namespace std;
using namespace web;
using namespace web::json;
using namespace TIGER_API;

class TestTradeClient {
public:
    static void test_place_order(const std::shared_ptr<TradeClient>& trade_client) {
        Contract contract = TIGER_API::ContractUtil::stock_contract(U("SPCX"), U("USD"));
        Order order = TIGER_API::OrderUtil::stop_order(trade_client->client_config.account, contract, U("SELL"), 2, 157.50);
        value res = trade_client->place_order(order);
        long id = res[U("id")].as_integer();
        cout << "order id: " << id << endl;
        ucout << "place order result: " << res << endl;
    }
    static void test_trade(const std::shared_ptr<TradeClient>& trade_client) {
        TestTradeClient::test_place_order(trade_client);
    }
};

int main(int argc, char* args[]) {

    ClientConfig config(true, U("tiger_openapi_config.properties"));

    std::shared_ptr<TradeClient> trade_client = std::make_shared<TradeClient>(config);
    TestTradeClient::test_trade(trade_client);

    return 0;
}

Output

order id: 1883917312
place order result: {"id":43792012419943424,"order_id":7,"orders":[{"account":"138913941672019696","action":"SELL","algoStrategy":"STP","attrDesc":"","attrList":[],"auxPrice":157.5,"avgFillPrice":0,"canCancel":true,"canModify":true,"cancelStatus":"NONE","commission":0,"currency":"USD","discount":0,"externalId":"7","filledCashAmount":0,"filledQuantity":0,"filledQuantityScale":0,"gst":0,"id":43792012419943424,"identifier":"SPCX","isOpen":false,"latestPrice":157.97499999999999,"latestTime":1783005740000,"liquidation":false,"market":"US","name":"SpaceX","openTime":1783005740000,"orderDiscount":0,"orderId":7,"orderType":"STP","outsideRth":false,"realizedPnl":0,"remark":"","replaceStatus":"NONE","secType":"STK","source":"openapi","status":"Initial","symbol":"SPCX","timeInForce":"DAY","totalQuantity":2,"totalQuantityScale":0,"tradingSessionType":"RTH","updateTime":1783005740000,"userMark":""}],"subIds":[]}

Stop Limit Order (STP_LMT)

    Order
    OrderUtil::stop_limit_order(const utility::string_t account, Contract &contract, const utility::string_t action, long quantity,
                     double limit_price, double aux_price) {
        // stop limit order
        return Order(U("STP_LMT"), account, contract, action, quantity, limit_price, aux_price);
    }

Example Call

using namespace std;
using namespace web;
using namespace web::json;
using namespace TIGER_API;

class TestTradeClient {
public:
    static void test_place_order(const std::shared_ptr<TradeClient>& trade_client) {
        Contract contract = TIGER_API::ContractUtil::stock_contract(U("SPY"), U("USD"));
        Order order = TIGER_API::OrderUtil::stop_limit_order(trade_client->client_config.account, contract, U("SELL"), 2, 700.5, 700);

        value res = trade_client->place_order(order);
        long id = res[U("id")].as_integer();
        cout << "order id: " << id << endl;
    }
    static void test_trade(const std::shared_ptr<TradeClient>& trade_client) {
        TestTradeClient::test_place_order(trade_client);
    }
};

int main(int argc, char* args[]) {

    ClientConfig config(true, U("tiger_openapi_config.properties"));

    std::shared_ptr<TradeClient> trade_client = std::make_shared<TradeClient>(config);
    TestTradeClient::test_trade(trade_client);

    return 0;
}

Sample response

{
                                "account":"138913941672019696",
                                "action":"SELL",
                                "algoStrategy":"STP_LMT",
                                "attrDesc":"",
                                "attrList":[
                                        
                                ],
                                "auxPrice":700,
                                "avgFillPrice":0,
                                "canCancel":true,
                                "canModify":true,
                                "cancelStatus":"NONE",
                                "commission":0,
                                "currency":"USD",
                                "discount":0,
                                "externalId":"21",
                                "filledCashAmount":0,
                                "filledQuantity":0,
                                "filledQuantityScale":0,
                                "gst":0,
                                "id":43848474365804544,
                                "identifier":"SPY",
                                "isOpen":true,
                                "latestPrice":746.15999999999997,
                                "latestTime":1783436510000,
                                "limitPrice":700.5,
                                "liquidation":false,
                                "market":"US",
                                "name":"SPDR S&P 500 ETF Trust",
                                "openTime":1783436510000,
                                "orderDiscount":0,
                                "orderId":21,
                                "orderType":"STP_LMT",
                                "outsideRth":true,
                                "realizedPnl":0,
                                "remark":"",
                                "replaceStatus":"NONE",
                                "secType":"STK",
                                "source":"openapi",
                                "status":"Initial",
                                "symbol":"SPY",
                                "timeInForce":"DAY",
                                "totalQuantity":2,
                                "totalQuantityScale":0,
                                "tradingSessionType":"PRE_RTH_POST",
                                "updateTime":1783436510000,
                                "userMark":""
                        }

Did this page help you?