股票持倉
該接口用於獲取包括賬戶、股票代碼、持倉股數、可用股數、持倉均價(按賬戶設置計算均價方式)、幣種在內的股票持倉信息。
SDK Links
Python |
Rust |
Go |
Node.js |
Java |
C++ |
Request
| HTTP Method | GET |
| HTTP URL | /v1/asset/stock |
Parameters
Content-Type: application/json; charset=utf-8
| Name | Type | Required | Description |
|---|---|---|---|
| symbol | string[] | NO | 股票代碼,使用 ticker.region 格式,例如:AAPL.US |
Request Example
from longbridge.openapi import TradeContext, Config, OAuthBuilder
oauth = OAuthBuilder("your-client-id").build(lambda url: print("Visit:", url))
config = Config.from_oauth(oauth)
ctx = TradeContext(config)
resp = ctx.stock_positions()
print(resp)import asyncio
from longbridge.openapi import AsyncTradeContext, Config, OAuthBuilder
async def main() -> None:
oauth = await OAuthBuilder("your-client-id").build_async(lambda url: print("Visit:", url))
config = Config.from_oauth(oauth)
ctx = AsyncTradeContext.create(config)
resp = await ctx.stock_positions()
print(resp)
if __name__ == "__main__":
asyncio.run(main())const { Config, TradeContext, OAuth } = require('longbridge')
async function main() {
const oauth = await OAuth.build("your-client-id", (_, url) => { console.log("Open this URL to authorize: " + url) })
const config = Config.fromOAuth(oauth)
const ctx = TradeContext.new(config)
const resp = await ctx.stockPositions()
console.log(resp)
}
main().catch(console.error)import com.longbridge.*;
import com.longbridge.trade.*;
class Main {
public static void main(String[] args) throws Exception {
try (OAuth oauth = new OAuthBuilder("your-client-id").build(url -> System.out.println("Open to authorize: " + url)).get();
Config config = Config.fromOAuth(oauth);
TradeContext ctx = TradeContext.create(config)) {
StockPositionsResponse resp = ctx.getStockPositions(null).get();
System.out.println(resp);
}
}
}use std::sync::Arc;
use longbridge::{oauth::OAuthBuilder, trade::TradeContext, Config};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let oauth = OAuthBuilder::new("your-client-id").build(|url| println!("Open this URL to authorize: {url}")).await?;
let config = Arc::new(Config::from_oauth(oauth));
let (ctx, _) = TradeContext::new(config);
let resp = ctx.stock_positions(None).await?;
println!("{:?}", resp);
Ok(())
}#include <iostream>
#include <longbridge.hpp>
#ifdef WIN32
#include <windows.h>
#endif
using namespace longbridge;
using namespace longbridge::trade;
static void
run(const OAuth& oauth)
{
Config config = Config::from_oauth(oauth);
TradeContext ctx = TradeContext::create(config);
ctx.stock_positions(std::nullopt, [](auto res) {
if (!res) { std::cout << "failed" << std::endl; return; }
std::cout << "positions" << std::endl;
});
}
int main(int argc, char const* argv[]) {
#ifdef WIN32
SetConsoleOutputCP(CP_UTF8);
#endif
const std::string client_id = "your-client-id";
OAuthBuilder(client_id).build(
[](const std::string& url) {
std::cout << "Open this URL to authorize: " << url << std::endl;
},
[](auto res) {
if (!res) {
std::cout << "authorization failed: " << *res.status().message() << std::endl;
return;
}
run(*res);
});
std::cin.get();
return 0;
}package main
import (
"context"
"fmt"
"log"
"github.com/longbridge/openapi-go/config"
"github.com/longbridge/openapi-go/oauth"
"github.com/longbridge/openapi-go/trade"
)
func main() {
o := oauth.New("your-client-id").
OnOpenURL(func(url string) { fmt.Println("Open this URL to authorize:", url) })
if err := o.Build(context.Background()); err != nil {
log.Fatal(err)
}
conf, err := config.New(config.WithOAuthClient(o))
if err != nil {
log.Fatal(err)
}
tctx, err := trade.NewFromCfg(conf)
if err != nil {
log.Fatal(err)
}
defer tctx.Close()
positions, err := tctx.StockPositions(context.Background(), []string{"AAPL.US", "700.HK"})
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", positions)
}Response
Response Headers
- Content-Type: application/json
Response Example
{
"code": 0,
"data": {
"list": [
{
"account_channel": "lb",
"stock_info": [
{
"symbol": "700.HK",
"symbol_name": "騰訊控股",
"currency": "HKD",
"quantity": "650",
"market": "HK",
"available_quantity": "-450",
"cost_price": "457.53",
"init_quantity": "214"
},
{
"symbol": "9991.HK",
"symbol_name": "寶尊電商-SW",
"currency": "HKD",
"market": "HK",
"quantity": "200",
"available_quantity": "0",
"cost_price": "32.25",
"init_quantity": "214"
},
{
"symbol": "TCEHY.US",
"symbol_name": "騰訊控股 (ADR)",
"currency": "USD",
"market": "US",
"quantity": "10",
"available_quantity": "10",
"init_quantity": "18"
},
{
"symbol": "2628.HK",
"symbol_name": "中國人壽",
"currency": "HKD",
"market": "HK",
"quantity": "9000",
"available_quantity": "0",
"init_quantity": "8000"
},
{
"symbol": "5.HK",
"symbol_name": "匯豐控股",
"currency": "HKD",
"market": "HK",
"quantity": "2400",
"available_quantity": "2000",
"init_quantity": "2000"
},
{
"symbol": "BABA.US",
"symbol_name": "阿里巴巴",
"currency": "USD",
"market": "US",
"quantity": "2000209",
"available_quantity": "2000209",
"init_quantity": "214"
},
{
"symbol": "2.HK",
"symbol_name": "中電控股",
"currency": "HKD",
"market": "HK",
"quantity": "2000",
"available_quantity": "2000",
"init_quantity": "2000"
},
{
"symbol": "NOK.US",
"symbol_name": "諾基亞",
"currency": "USD",
"market": "US",
"quantity": "1",
"available_quantity": "0",
"init_quantity": "1"
}
]
}
]
}
}
Response Status
| Status | Description | Schema |
|---|---|---|
| 200 | 返回成功 | stock_rsp |
| 400 | 內部錯誤 | None |
Schemas
stock_rsp
| Name | Type | Required | Description |
|---|---|---|---|
| list | object[] | false | 股票持倉信息 |
| ∟ account_channel | string | true | 賬戶類型 |
| ∟ stock_info | object[] | false | 股票列表 |
| ∟∟ symbol | string | true | 股票代碼 |
| ∟∟ symbol_name | string | true | 股票名稱 |
| ∟∟ quantity | string | true | 持倉股數 |
| ∟∟ available_quantity | string | false | 可用股數 |
| ∟∟ currency | string | true | 幣種 |
| ∟∟ market | string | true | 市場 |
| ∟∟ cost_price | string | true | 成本價格 (具體根據客戶端選擇平均買入還是攤薄成本) |
| ∟∟ init_quantity | string | false | 開盤前初始持倉 |