Documentation
¶
Overview ¶
Package portfolio provides a client for the Longbridge Portfolio Analytics API. It covers exchange rates and profit/loss analysis.
Index ¶
- type AssetType
- type ExchangeRate
- type ExchangeRates
- type FlowDirection
- type FlowItem
- type PortfolioContext
- func (c *PortfolioContext) ExchangeRate(ctx context.Context) (*ExchangeRates, error)
- func (c *PortfolioContext) ProfitAnalysis(ctx context.Context, opts *ProfitAnalysisOptions) (*ProfitAnalysis, error)
- func (c *PortfolioContext) ProfitAnalysisByMarket(ctx context.Context, opts *ProfitAnalysisByMarketOptions) (*ProfitAnalysisByMarket, error)
- func (c *PortfolioContext) ProfitAnalysisDetail(ctx context.Context, opts *ProfitAnalysisDetailOptions) (*ProfitAnalysisDetail, error)
- func (c *PortfolioContext) ProfitAnalysisFlows(ctx context.Context, opts *ProfitAnalysisFlowsOptions) (*ProfitAnalysisFlows, error)
- type ProfitAnalysis
- type ProfitAnalysisByMarket
- type ProfitAnalysisByMarketItem
- type ProfitAnalysisByMarketOptions
- type ProfitAnalysisDetail
- type ProfitAnalysisDetailOptions
- type ProfitAnalysisFlows
- type ProfitAnalysisFlowsOptions
- type ProfitAnalysisItem
- type ProfitAnalysisOptions
- type ProfitAnalysisSublist
- type ProfitAnalysisSummary
- type ProfitDetailEntry
- type ProfitDetails
- type ProfitSummaryBreakdown
- type ProfitSummaryInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ExchangeRate ¶
type ExchangeRate struct {
// Average rate (base_currency / other_currency).
AverageRate float64
// Base currency, e.g. "USD".
BaseCurrency string
// Bid rate.
BidRate float64
// Offer rate.
OfferRate float64
// Other currency, e.g. "HKD".
OtherCurrency string
}
ExchangeRate is one currency exchange rate.
type ExchangeRates ¶
type ExchangeRates struct {
// List of exchange rates.
Exchanges []ExchangeRate
}
ExchangeRates is the response for ExchangeRate.
type FlowDirection ¶
type FlowDirection int
FlowDirection represents the direction of a portfolio flow.
const ( // FlowDirectionUnknown is an unknown flow direction. FlowDirectionUnknown FlowDirection = iota // FlowDirectionBuy is a buy flow. FlowDirectionBuy // FlowDirectionSell is a sell flow. FlowDirectionSell )
type FlowItem ¶
type FlowItem struct {
// Execution date string, e.g. "2024-01-15".
ExecutedDate string
// Execution timestamp string.
ExecutedTimestamp string
// Security code / ticker.
Code string
// Direction of the flow.
Direction FlowDirection
// Executed quantity.
ExecutedQuantity *decimal.Decimal
// Executed price.
ExecutedPrice *decimal.Decimal
// Executed cost.
ExecutedCost *decimal.Decimal
// Human-readable description.
Describe string
}
FlowItem is one profit-analysis flow record.
type PortfolioContext ¶
type PortfolioContext struct {
// contains filtered or unexported fields
}
PortfolioContext is a client for the Longbridge Portfolio Analytics API.
Example:
conf, err := config.NewFromEnv() pctx, err := portfolio.NewFromCfg(conf) rates, err := pctx.ExchangeRate(context.Background())
func NewFromCfg ¶
func NewFromCfg(cfg *config.Config) (*PortfolioContext, error)
NewFromCfg creates a PortfolioContext from a *config.Config.
func NewFromEnv ¶
func NewFromEnv() (*PortfolioContext, error)
NewFromEnv returns a PortfolioContext configured from environment variables.
func (*PortfolioContext) ExchangeRate ¶
func (c *PortfolioContext) ExchangeRate(ctx context.Context) (*ExchangeRates, error)
ExchangeRate returns the current exchange rates for supported currencies.
Path: GET /v1/asset/exchange_rates
func (*PortfolioContext) ProfitAnalysis ¶
func (c *PortfolioContext) ProfitAnalysis(ctx context.Context, opts *ProfitAnalysisOptions) (*ProfitAnalysis, error)
ProfitAnalysis returns the portfolio P&L analysis (summary + per-security breakdown). It concurrently calls two endpoints and merges the results.
Paths:
- GET /v1/portfolio/profit-analysis-summary
- GET /v1/portfolio/profit-analysis-sublist
func (*PortfolioContext) ProfitAnalysisByMarket ¶
func (c *PortfolioContext) ProfitAnalysisByMarket(ctx context.Context, opts *ProfitAnalysisByMarketOptions) (*ProfitAnalysisByMarket, error)
ProfitAnalysisByMarket returns paginated P&L analysis filtered by market.
Path: GET /v1/portfolio/profit-analysis/by-market
func (*PortfolioContext) ProfitAnalysisDetail ¶
func (c *PortfolioContext) ProfitAnalysisDetail(ctx context.Context, opts *ProfitAnalysisDetailOptions) (*ProfitAnalysisDetail, error)
ProfitAnalysisDetail returns P&L detail for a specific security.
Path: GET /v1/portfolio/profit-analysis/detail
func (*PortfolioContext) ProfitAnalysisFlows ¶
func (c *PortfolioContext) ProfitAnalysisFlows(ctx context.Context, opts *ProfitAnalysisFlowsOptions) (*ProfitAnalysisFlows, error)
ProfitAnalysisFlows returns paginated P&L flow records for a security.
Path: GET /v1/portfolio/profit-analysis/flows
type ProfitAnalysis ¶
type ProfitAnalysis struct {
// Summary overview.
Summary ProfitAnalysisSummary
// Per-security breakdown.
Sublist ProfitAnalysisSublist
}
ProfitAnalysis is the combined response for ProfitAnalysis. It merges the summary and per-security sublist.
type ProfitAnalysisByMarket ¶
type ProfitAnalysisByMarket struct {
// Total P&L across all returned items.
Profit *decimal.Decimal
// Whether more pages are available.
HasMore bool
// Per-security P&L items for the requested market/page.
StockItems []ProfitAnalysisByMarketItem
}
ProfitAnalysisByMarket is the response for ProfitAnalysisByMarket.
type ProfitAnalysisByMarketItem ¶
type ProfitAnalysisByMarketItem struct {
// Security symbol (ticker code).
Code string
// Security name.
Name string
// Market, e.g. "HK", "US".
Market string
// Profit/loss amount.
Profit *decimal.Decimal
}
ProfitAnalysisByMarketItem is one security entry in a by-market P&L response.
type ProfitAnalysisByMarketOptions ¶
type ProfitAnalysisByMarketOptions struct {
// Market filter, e.g. "HK", "US" (optional).
Market string
// Start date in YYYY-MM-DD format (optional).
Start string
// End date in YYYY-MM-DD format (optional).
End string
// Currency filter (optional).
Currency string
// Page number (1-based).
Page uint32
// Page size.
Size uint32
}
ProfitAnalysisByMarketOptions are the parameters for ProfitAnalysisByMarket.
type ProfitAnalysisDetail ¶
type ProfitAnalysisDetail struct {
// Total profit/loss.
Profit *decimal.Decimal
// Underlying stock P&L details.
UnderlyingDetails ProfitDetails
// Derivative P&L details.
DerivativePnlDetails ProfitDetails
// Security name.
Name string
// Last updated time (unix timestamp string).
UpdatedAt string
// Last updated date string.
UpdatedDate string
// Currency.
Currency string
// Default detail tab: 0 = underlying, 1 = derivative.
DefaultTag int32
// Query start time (unix timestamp string).
Start string
// Query end time (unix timestamp string).
End string
// Query start date string.
StartDate string
// Query end date string.
EndDate string
}
ProfitAnalysisDetail is the response for ProfitAnalysisDetail.
type ProfitAnalysisDetailOptions ¶
type ProfitAnalysisDetailOptions struct {
// Symbol, e.g. "TSLA.US".
Symbol string
// Start date in YYYY-MM-DD format (optional).
Start string
// End date in YYYY-MM-DD format (optional).
End string
}
ProfitAnalysisDetailOptions are the parameters for ProfitAnalysisDetail.
type ProfitAnalysisFlows ¶
type ProfitAnalysisFlows struct {
// Paginated list of flow items.
FlowsList []FlowItem
// Whether there are more pages.
HasMore bool
}
ProfitAnalysisFlows is the response for ProfitAnalysisFlows.
type ProfitAnalysisFlowsOptions ¶
type ProfitAnalysisFlowsOptions struct {
// Symbol, e.g. "TSLA.US".
Symbol string
// Page number (1-based).
Page uint32
// Page size.
Size uint32
// Whether to include derivative flows.
Derivative bool
// Start date in YYYY-MM-DD format (optional).
Start string
// End date in YYYY-MM-DD format (optional).
End string
}
ProfitAnalysisFlowsOptions are the parameters for ProfitAnalysisFlows.
type ProfitAnalysisItem ¶
type ProfitAnalysisItem struct {
// Security name.
Name string
// Market.
Market string
// Whether still holding.
IsHolding bool
// Profit/loss amount.
Profit *decimal.Decimal
// Profit/loss rate.
ProfitRate *decimal.Decimal
// Number of completed trades.
ClearanceTimes int64
// Asset type.
ItemType AssetType
// Currency.
Currency string
// Security symbol (converted from counter_id).
Symbol string
// Holding period display string.
HoldingPeriod string
// Ticker code.
SecurityCode string
// ISIN (for funds).
Isin string
// Underlying stock P&L.
UnderlyingProfit *decimal.Decimal
// Derivatives P&L.
DerivativesProfit *decimal.Decimal
// P&L in order currency.
OrderProfit *decimal.Decimal
}
ProfitAnalysisItem is the P&L for one security.
type ProfitAnalysisOptions ¶
type ProfitAnalysisOptions struct {
// Start date in YYYY-MM-DD format (optional).
Start string
// End date in YYYY-MM-DD format (optional).
End string
}
ProfitAnalysisOptions are the optional parameters for ProfitAnalysis.
type ProfitAnalysisSublist ¶
type ProfitAnalysisSublist struct {
// Start time (unix timestamp string).
Start string
// End time (unix timestamp string).
End string
// Start date string.
StartDate string
// End date string.
EndDate string
// Last updated time (unix timestamp string).
UpdatedAt string
// Last updated date string.
UpdatedDate string
// Per-security items.
Items []ProfitAnalysisItem
}
ProfitAnalysisSublist is the per-security P&L breakdown.
type ProfitAnalysisSummary ¶
type ProfitAnalysisSummary struct {
// Account currency.
Currency string
// Current total asset value.
CurrentTotalAsset *decimal.Decimal
// Query start date string.
StartDate string
// Query end date string.
EndDate string
// Start time (unix timestamp string).
StartTime string
// End time (unix timestamp string).
EndTime string
// Ending asset value.
EndingAssetValue *decimal.Decimal
// Initial asset value.
InitialAssetValue *decimal.Decimal
// Total invested amount.
InvestAmount *decimal.Decimal
// Whether any trades occurred.
IsTraded bool
// Total profit/loss.
SumProfit *decimal.Decimal
// Total profit/loss rate.
SumProfitRate *decimal.Decimal
// Per-asset-type breakdown.
Profits ProfitSummaryBreakdown
}
ProfitAnalysisSummary is the account-level P&L summary.
type ProfitDetailEntry ¶
type ProfitDetailEntry struct {
// Description.
Describe string
// Amount.
Amount *decimal.Decimal
}
ProfitDetailEntry is one P&L detail line item (credit, debit, or fee).
type ProfitDetails ¶
type ProfitDetails struct {
// Current holding market value.
HoldingValue *decimal.Decimal
// Total profit/loss.
Profit *decimal.Decimal
// Cumulative credited amount.
CumulativeCreditedAmount *decimal.Decimal
// Credit detail entries.
CreditedDetails []ProfitDetailEntry
// Cumulative debited amount.
CumulativeDebitedAmount *decimal.Decimal
// Debit detail entries.
DebitedDetails []ProfitDetailEntry
// Cumulative fee amount.
CumulativeFeeAmount *decimal.Decimal
// Fee detail entries.
FeeDetails []ProfitDetailEntry
// Short position holding value.
ShortHoldingValue *decimal.Decimal
// Long position holding value.
LongHoldingValue *decimal.Decimal
// Opening position market value at period start.
HoldingValueAtBeginning *decimal.Decimal
// Closing position market value at period end.
HoldingValueAtEnding *decimal.Decimal
}
ProfitDetails is the detailed P&L breakdown for one asset class.
type ProfitSummaryBreakdown ¶
type ProfitSummaryBreakdown struct {
// Stock P&L.
Stock *decimal.Decimal
// Fund P&L.
Fund *decimal.Decimal
// Crypto P&L.
Crypto *decimal.Decimal
// Money market fund P&L.
Mmf *decimal.Decimal
// Other P&L.
Other *decimal.Decimal
// Cumulative transaction amount.
CumulativeTransactionAmount *decimal.Decimal
// Total number of orders.
TradeOrderNum string
// Total number of traded securities.
TradeStockNum string
// IPO P&L.
Ipo *decimal.Decimal
// IPO hits.
IpoHit int32
// IPO subscriptions.
IpoSubscription int32
// Per-category summary info.
SummaryInfo []ProfitSummaryInfo
}
ProfitSummaryBreakdown is the P&L breakdown by asset type.
type ProfitSummaryInfo ¶
type ProfitSummaryInfo struct {
// Asset type.
AssetType AssetType
// Security with the maximum profit.
ProfitMax string
// Name of the max-profit security.
ProfitMaxName string
// Security with the maximum loss.
LossMax string
// Name of the max-loss security.
LossMaxName string
}
ProfitSummaryInfo holds P&L info for one asset category.