匯率
獲取賬戶中所有貨幣對的當前外匯匯率。
SDK Links
Python |
Rust |
Go |
Node.js |
Java |
C++ |
Parameters
SDK 方法參數。
| Name | Type | Required | Description |
|---|---|---|---|
| base | string | 否 | 基礎貨幣,例如 USD,不傳則返回所有貨幣對 |
Request Example
from longbridge.openapi import PortfolioContext, Config, OAuthBuilder
oauth = OAuthBuilder("your-client-id").build(lambda url: print("Visit:", url))
config = Config.from_oauth(oauth)
ctx = PortfolioContext(config)
resp = ctx.exchange_rates()
print(resp)import asyncio
from longbridge.openapi import AsyncPortfolioContext, 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 = AsyncPortfolioContext.create(config)
resp = await ctx.exchange_rates()
print(resp)
if __name__ == "__main__":
asyncio.run(main())const { Config, PortfolioContext, 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 = PortfolioContext.new(config)
const resp = await ctx.exchange_rates()
console.log(resp)
}
main().catch(console.error)import com.longbridge.*;
import com.longbridge.portfolio.*;
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);
PortfolioContext ctx = PortfolioContext.create(config)) {
var resp = ctx.getExchangeRates().get();
System.out.println(resp);
}
}
}use std::sync::Arc;
use longbridge::{oauth::OAuthBuilder, portfolio::PortfolioContext, Config};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let oauth = OAuthBuilder::new("your-client-id").build(|url| println!("Open: {url}")).await?;
let config = Arc::new(Config::from_oauth(oauth));
let ctx = PortfolioContext::new(config);
let resp = ctx.exchange_rates().await?;
println!("{:?}", resp);
Ok(())
}#include <iostream>
#include <longbridge.hpp>
using namespace longbridge;
using namespace longbridge::portfolio;
int main() {
OAuthBuilder("your-client-id").build(
[](const std::string& url) { std::cout << "Open: " << url << std::endl; },
[](auto res) {
if (!res) return;
Config config = Config::from_oauth(*res);
PortfolioContext ctx = PortfolioContext::create(config);
ctx.exchange_rates([](auto resp) {
if (resp) std::cout << "OK" << std::endl;
});
});
std::cin.get();
}package main
import (
"context"
"fmt"
"log"
"github.com/longbridge/openapi-go/config"
"github.com/longbridge/openapi-go/oauth"
"github.com/longbridge/openapi-go/portfolio"
)
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 := portfolio.NewFromCfg(conf)
if err != nil {
log.Fatal(err)
}
defer c.Close()
resp, err := c.ExchangeRates(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Printf("%+v\n", resp)
}Response
Response Example
{
"code": 0,
"message": "success",
"data": {
"exchanges": [
{
"base_currency": "USD",
"other_currency": "HKD",
"bid_rate": 7.785,
"offer_rate": 7.795,
"average_rate": 7.79
}
]
}
}
Response Status
| Status | Description | Schema |
|---|---|---|
| 200 | 成功 | ExchangeRatesResponse |
| 400 | 請求錯誤 | None |
Schemas
ExchangeRatesResponse
| Name | Type | Required | Description |
|---|---|---|---|
| exchanges | object[] | true | 匯率列表, |
| ∟ base_currency | string | true | 基準貨幣 |
| ∟ other_currency | string | true | 報價貨幣 |
| ∟ bid_rate | number | false | 買入匯率 |
| ∟ offer_rate | number | false | 賣出匯率 |
| ∟ average_rate | number | false | 平均匯率 |