资产与持仓
get_accounts 获取账户列表
value TradeClient::get_accounts()
说明
获取当前开发者名下的所有账户列表
返回
web::json::value JSON 数组,包含账户信息
示例
#include "tigerapi/trade_client.h"
#include "tigerapi/client_config.h"
using namespace TIGER_API;
ClientConfig config(false, U("/path/to/your/properties/"));
TradeClient trade_client(config);
value accounts = trade_client.get_accounts();
ucout << accounts.serialize() << std::endl;get_asset 获取账户资产
value TradeClient::get_asset(utility::string_t account, const value &sub_accounts, bool segment, bool market_value)
说明
获取账户的资产信息(适用于综合账户/环球账户/模拟账户)
参数
| 参数名 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
| account | utility::string_t | No | 资金账号,不传使用默认账号 |
| sub_accounts | value | No | 子账号列表(机构用户),默认 value::array() |
| segment | bool | No | 是否按分段返回资产,默认 false |
| market_value | bool | No | 是否包含持仓市值信息,默认 false |
返回
web::json::value JSON 对象
返回属性
| 属性名 | 类型 | 描述 |
|---|---|---|
| account | string | 资金账号 |
| segments | array | 账户分段信息列表 |
| ├─ category | string | 分段类别 S(证券)/C(期货) |
| ├─ currency | string | 币种 |
| ├─ netLiquidation | float | 净清算值 |
| ├─ equityWithLoan | float | 含借贷资产 |
| ├─ initMargin | float | 初始保证金 |
| ├─ maintainMargin | float | 维持保证金 |
| ├─ buyingPower | float | 购买力 |
| ├─ cashBalance | float | 现金 |
| ├─ grossPositionValue | float | 持仓总市值 |
| ├─ unrealizedPL | float | 未实现盈亏 |
| └─ realizedPL | float | 已实现盈亏 |
示例
#include "tigerapi/trade_client.h"
#include "tigerapi/client_config.h"
using namespace TIGER_API;
ClientConfig config(false, U("/path/to/your/properties/"));
TradeClient trade_client(config);
// 获取全部资产
value assets = trade_client.get_asset();
ucout << assets.serialize() << std::endl;
// 获取带分段信息的资产
value assets_seg = trade_client.get_asset(config.account, value::array(), true);
ucout << assets_seg.serialize() << std::endl;get_prime_asset 获取 Prime 账户资产
value TradeClient::get_prime_asset(const utility::string_t &account, const utility::string_t &base_currency)
说明
获取环球账户(Prime)的资产信息,返回 JSON 格式
参数
| 参数名 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
| account | utility::string_t | No | 资金账号,默认使用配置账号 |
| base_currency | utility::string_t 或 Currency | No | 基础币种,默认 U("USD") / Currency::USD |
返回
web::json::value JSON 对象
示例
ClientConfig config(false, U("/path/to/your/properties/"));
TradeClient trade_client(config);
value prime_asset = trade_client.get_prime_asset();
ucout << prime_asset.serialize() << std::endl;get_prime_portfolio 获取 Prime 账户组合
PortfolioAccount TradeClient::get_prime_portfolio(const utility::string_t &account, const utility::string_t &base_currency)
说明
获取环球账户的组合资产信息,返回 PortfolioAccount 结构体(包含 Segment 和 CurrencyAsset 详细数据)
参数
| 参数名 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
| account | utility::string_t | No | 资金账号 |
| base_currency | utility::string_t | No | 基础币种,默认 U("USD") |
返回
PortfolioAccount 对象
PortfolioAccount 属性
| 属性名 | 类型 | 描述 |
|---|---|---|
| account | utility::string_t | 资金账号 |
| update_timestamp | long | 更新时间戳 |
| segments | vector<Segment> | 分段列表 |
Segment 属性
| 属性名 | 类型 | 描述 |
|---|---|---|
| category | utility::string_t | 分段类别 |
| capability | utility::string_t | 账户能力 |
| currency | utility::string_t | 币种 |
| buying_power | double | 购买力 |
| cash_available_for_trade | double | 可交易现金 |
| cash_available_for_withdrawal | double | 可提现金额 |
| cash_balance | double | 现金余额 |
| equity_with_loan | double | 含借贷资产 |
| excess_liquidation | double | 剩余流动性 |
| gross_position_value | double | 持仓总市值 |
| init_margin | double | 初始保证金 |
| maintain_margin | double | 维持保证金 |
| net_liquidation | double | 净清算值 |
| realized_pl | double | 已实现盈亏 |
| unrealized_pl | double | 未实现盈亏 |
| currency_assets | vector<CurrencyAsset> | 分币种资产列表 |
CurrencyAsset 属性
| 属性名 | 类型 | 描述 |
|---|---|---|
| currency | utility::string_t | 币种 |
| cash_balance | double | 现金余额 |
| cash_available_for_trade | double | 可交易现金 |
| gross_position_value | double | 持仓市值 |
| stock_market_value | double | 股票市值 |
| option_market_value | double | 期权市值 |
| realized_pl | double | 已实现盈亏 |
| unrealized_pl | double | 未实现盈亏 |
示例
ClientConfig config(false, U("/path/to/your/properties/"));
TradeClient trade_client(config);
PortfolioAccount portfolio = trade_client.get_prime_portfolio();
std::cout << "Account: " << portfolio.account << std::endl;
for (auto& seg : portfolio.segments) {
ucout << U("Segment: ") << seg.category
<< U(" Net Liquidation: ") << seg.net_liquidation
<< std::endl;
for (auto& asset : seg.currency_assets) {
ucout << U(" Currency: ") << asset.currency
<< U(" Cash: ") << asset.cash_balance
<< std::endl;
}
}get_positions 获取持仓
value TradeClient::get_positions(utility::string_t account, SecType sec_type, Currency currency, Market market, utility::string_t symbol, const value &sub_accounts, time_t expiry, utility::string_t strike, Right right)
说明
获取账户的持仓信息。此方法提供多个重载版本,参数支持枚举类型和字符串类型。
参数(枚举版本)
| 参数名 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
| account | utility::string_t | No | 资金账号 |
| sec_type | SecType | No | 合约类型,默认 SecType::ALL |
| currency | Currency | No | 币种,默认 Currency::ALL |
| market | Market | No | 市场,默认 Market::ALL |
| symbol | utility::string_t | No | 标的代码,默认空 |
| sub_accounts | value | No | 子账号列表 |
| expiry | time_t | No | 期权到期日时间戳,默认 -1 |
| strike | utility::string_t | No | 期权行权价 |
| right | Right | No | 期权方向,默认 Right::ALL |
参数(字符串版本)
| 参数名 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
| account | utility::string_t | No | 资金账号 |
| sec_type | utility::string_t | No | 合约类型,如 U("STK") |
| currency | utility::string_t | No | 币种,默认 U("ALL") |
| market | utility::string_t | No | 市场,默认 U("ALL") |
| symbol | utility::string_t | No | 标的代码 |
| sub_accounts | value | No | 子账号列表 |
| expiry | time_t | No | 期权到期日时间戳 |
| strike | utility::string_t | No | 期权行权价 |
| right | utility::string_t | No | 期权方向,如 U("PUT")/U("CALL") |
返回
web::json::value JSON 数组,或 vector<Position> 持仓对象列表(使用 get_position_list 方法)
Position 对象属性
| 属性名 | 类型 | 描述 |
|---|---|---|
| account | utility::string_t | 资金账号 |
| contract | Contract | 合约对象 |
| position | long long | 持仓数量 |
| average_cost | double | 持仓成本 |
| latest_price | double | 最新价格 |
| market_value | double | 持仓市值 |
| unrealized_pnl | double | 未实现盈亏 |
| realized_pnl | double | 已实现盈亏 |
示例
ClientConfig config(false, U("/path/to/your/properties/"));
TradeClient trade_client(config);
// 获取全部持仓(JSON 格式)
value positions = trade_client.get_positions();
ucout << positions.serialize() << std::endl;
// 获取指定标的持仓(枚举版本)
value pos_aapl = trade_client.get_positions(
config.account,
SecType::STK,
Currency::ALL,
Market::ALL,
U("AAPL")
);
ucout << pos_aapl.serialize() << std::endl;
// 获取持仓对象列表
vector<Position> pos_list = trade_client.get_position_list();
for (auto& pos : pos_list) {
ucout << pos.contract.symbol << U(" position: ") << pos.position
<< U(" avg_cost: ") << pos.average_cost << std::endl;
}get_analytics_asset 获取资产分析
value TradeClient::get_analytics_asset(utility::string_t account, utility::string_t start_date, utility::string_t end_date, utility::string_t seg_type, utility::string_t currency, utility::string_t sub_account)
说明
获取账户的资产分析数据,包含历史资产变动
参数
| 参数名 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
| account | utility::string_t | Yes | 资金账号 |
| start_date | utility::string_t | Yes | 起始日期,格式 "yyyy-MM-dd" |
| end_date | utility::string_t | Yes | 结束日期,格式 "yyyy-MM-dd" |
| seg_type | utility::string_t | No | 账户分段,U("SEC")/U("FUT"),默认 U("SEC") |
| currency | utility::string_t | No | 币种,默认 U("USD") |
| sub_account | utility::string_t | No | 子账号,默认空 |
返回
web::json::value JSON 对象
示例
ClientConfig config(false, U("/path/to/your/properties/"));
TradeClient trade_client(config);
value analytics = trade_client.get_analytics_asset(
config.account,
U("2024-01-01"),
U("2024-06-30")
);
ucout << analytics.serialize() << std::endl;get_estimate_tradable_quantity 获取预估可交易数量
value TradeClient::get_estimate_tradable_quantity(Order &order, utility::string_t seg_type)
说明
预估当前账户对指定标的可交易的最大数量。需要先构建一个 Order 对象传入。
参数
| 参数名 | 类型 | 是否必填 | 描述 |
|---|---|---|---|
| order | Order& | Yes | 订单对象(包含合约、方向、价格等信息) |
| seg_type | utility::string_t | No | 账户分段,默认 U("SEC") |
返回
web::json::value JSON 对象
示例
#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"));
Order order = OrderUtil::limit_order(config.account, contract, U("BUY"), 100, 150.0);
value result = trade_client.get_estimate_tradable_quantity(order);
ucout << result.serialize() << std::endl;Updated 11 days ago
