Longbridge Developers
立即開始

美股已實現盈虧

Longbridge US 賬戶

此方法僅適用於美國數據中心賬戶。

獲取美股賬戶已實現盈虧,按資產類別(股票/期權/加密貨幣)分組。

>_ CLI
# 美股已實現盈虧
longbridge profit-analysis realized
# 按股票類別篩選
longbridge profit-analysis realized --category stock

Parameters

SDK 方法參數。

名稱類型必填描述
currencystring是結算貨幣,例如 USD
categorystring否資產類別:ALL | STOCK | OPTION | CRYPTO(默認:ALL)

Request Example

from longbridge.openapi import TradeContext, Config, OAuthBuilder

oauth = OAuthBuilder("your-client-id").build(lambda url: print("請訪問:", url))
config = Config.from_oauth(oauth)
ctx = TradeContext(config)
resp = ctx.us_realized_pl("USD", category="STOCK")
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("請訪問:", url))
    config = Config.from_oauth(oauth)
    ctx = AsyncTradeContext.create(config)
    resp = await ctx.us_realized_pl("USD", category="STOCK")
    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('請訪問此 URL 授權:' + url)
  })
  const config = Config.fromOAuth(oauth)
  const ctx = TradeContext.new(config)
  const resp = await ctx.usRealizedPl("USD", "STOCK")
  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)) {
            var resp = ctx.getUsRealizedPl("USD", "STOCK").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!("請訪問:{url}")).await?;
    let config = Arc::new(Config::from_oauth(oauth));
    let ctx = TradeContext::new(config);
    let opts = longbridge::trade::GetUSRealizedPLOptions {
        currency: "USD".to_string(),
        category: "STOCK".to_string(),
    };
    let resp = ctx.us_realized_pl(opts).await?;
    println!("{:?}", resp);
    Ok(())
}
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)
	}
	c, err := trade.NewFromCfg(conf)
	if err != nil {
		log.Fatal(err)
	}
	defer c.Close()
	cat := "STOCK"
	resp, err := c.USRealizedPL(context.Background(), &trade.GetUSRealizedPL{Currency: "USD", Category: &cat})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v\n", resp)
}

Response

Response Example

{
  "realized_pl_list": [
    {
      "category": 1,
      "currency": "USD",
      "metrics": [
        {"amount": "1250.50", "period": 1, "rate": "0.0312"}
      ]
    },
    {
      "category": 3,
      "currency": "USD",
      "metrics": [
        {"amount": "-85.20", "period": 1, "rate": "-0.0215"}
      ]
    }
  ]
}

Response Status

狀態碼描述結構
200成功USRealizedPL
400請求錯誤None

Schemas

USRealizedPL

名稱類型必填描述
realized_pl_listUSRealizedPLEntry[]是按資產類別分列的盈虧明細

USRealizedPLEntry

名稱類型必填描述
categoryint是資產類別:1=股票,2=期權,3=加密貨幣
currencystring是貨幣代碼,如 USD
metricsUSRealizedPLMetric[]是按時期分列的盈虧指標

USRealizedPLMetric

名稱類型必填描述
amountstring是已實現盈虧金額
periodint是時間週期
ratestring是收益率(%)