Documentation
¶
Index ¶
- type BalanceChange
- type BalanceSnapshot
- type BoltDBStore
- type EventType
- type FeeSchedule
- type JSONFileStore
- type Option
- type PairParser
- type Portfolio
- func (p *Portfolio) AddTransaction(transaction Transaction) error
- func (p *Portfolio) BalancesSnapshot() []BalanceSnapshot
- func (p *Portfolio) ClosePosition(symbol string) error
- func (p *Portfolio) GetAllPositions() map[string]Position
- func (p *Portfolio) GetBalance(asset string) float64
- func (p *Portfolio) GetPosition(symbol string) (Position, bool)
- func (p *Portfolio) GetTotalValue(quoteAsset string, prices map[string]float64) float64
- func (p *Portfolio) GetTransactions() []Transaction
- func (p *Portfolio) LoadFromStore() error
- func (p *Portfolio) PositionsSnapshot() []PositionSnapshot
- func (p *Portfolio) Restore(snapshot PortfolioSnapshot)
- func (p *Portfolio) SetBalance(asset string, amount float64) error
- func (p *Portfolio) SetDefaultFee(fee float64)
- func (p *Portfolio) SetFeeForBase(base string, fee float64)
- func (p *Portfolio) SetFeeForQuote(quote string, fee float64)
- func (p *Portfolio) SetFeeForSymbol(symbol string, fee float64)
- func (p *Portfolio) SetFeeSchedule(schedule *FeeSchedule)
- func (p *Portfolio) SetRiskPolicy(policy *RiskPolicy)
- func (p *Portfolio) Snapshot() PortfolioSnapshot
- func (p *Portfolio) Subscribe(buffer int) (<-chan PortfolioEvent, func())
- func (p *Portfolio) UpdatePosition(position Position) error
- type PortfolioEvent
- type PortfolioSnapshot
- type Position
- type PositionChange
- type PositionSnapshot
- type RiskPolicy
- type SQLiteStore
- type SnapshotDiff
- type SnapshotStore
- type Transaction
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BalanceChange ¶
type BalanceChange struct {
Asset string `json:"asset"`
Amount float64 `json:"amount,omitempty"`
Deleted bool `json:"deleted,omitempty"`
}
BalanceChange flags an asset balance that was added/updated/removed.
type BalanceSnapshot ¶
BalanceSnapshot is a DTO representing a copy of an asset balance.
type BoltDBStore ¶
type BoltDBStore struct {
// contains filtered or unexported fields
}
BoltDBStore persists snapshots inside a BoltDB bucket.
func NewBoltDBStore ¶
func NewBoltDBStore(path string) (*BoltDBStore, error)
NewBoltDBStore opens a BoltDB database at the supplied path.
func (*BoltDBStore) Close ¶
func (s *BoltDBStore) Close() error
Close releases the underlying BoltDB handle.
func (*BoltDBStore) Load ¶
func (s *BoltDBStore) Load() (PortfolioSnapshot, error)
Load fetches the snapshot bytes from BoltDB.
func (*BoltDBStore) Save ¶
func (s *BoltDBStore) Save(snapshot PortfolioSnapshot) error
Save writes the snapshot bytes into BoltDB.
type FeeSchedule ¶
type FeeSchedule struct {
Symbols map[string]float64
Bases map[string]float64
Quotes map[string]float64
Default float64
HasDefault bool
}
FeeSchedule describes per-symbol/per-asset fee settings.
type JSONFileStore ¶
type JSONFileStore struct {
Path string
}
JSONFileStore persists snapshots to a local JSON file.
func NewJSONFileStore ¶
func NewJSONFileStore(path string) *JSONFileStore
NewJSONFileStore creates a JSON-backed snapshot store.
func (*JSONFileStore) Load ¶
func (s *JSONFileStore) Load() (PortfolioSnapshot, error)
Load reads the snapshot from disk. Missing files return the zero snapshot.
func (*JSONFileStore) Save ¶
func (s *JSONFileStore) Save(snapshot PortfolioSnapshot) error
Save writes the snapshot to disk using an atomic rename.
type Option ¶
type Option func(*Portfolio)
Option configures a Portfolio instance.
func WithFeeSchedule ¶
func WithFeeSchedule(schedule *FeeSchedule) Option
WithFeeSchedule configures the portfolio with a fee schedule.
func WithPairParser ¶
func WithPairParser(parser PairParser) Option
WithPairParser overrides the default BASE/QUOTE parser used for balance updates.
func WithRiskPolicy ¶
func WithRiskPolicy(policy *RiskPolicy) Option
WithRiskPolicy configures the portfolio with risk limits.
func WithSnapshotStore ¶
func WithSnapshotStore(store SnapshotStore) Option
WithSnapshotStore wires a persistence layer that receives snapshots after every mutation.
type PairParser ¶
PairParser resolves a trading symbol (e.g. "BTC/USDT") into its base and quote assets. Implementations should return ok=false if they cannot derive a valid pair so callers can surface an error.
type Portfolio ¶
type Portfolio struct {
// contains filtered or unexported fields
}
Portfolio manages the trading portfolio
func NewPortfolioWithOptions ¶
NewPortfolioWithOptions creates a portfolio configured by the supplied options.
func (*Portfolio) AddTransaction ¶
func (p *Portfolio) AddTransaction(transaction Transaction) error
AddTransaction adds a transaction to the portfolio and updates balances/positions.
func (*Portfolio) BalancesSnapshot ¶
func (p *Portfolio) BalancesSnapshot() []BalanceSnapshot
BalancesSnapshot returns a slice of balances safe for serialization.
func (*Portfolio) ClosePosition ¶
ClosePosition closes a position
func (*Portfolio) GetAllPositions ¶
GetAllPositions returns all positions
func (*Portfolio) GetBalance ¶
GetBalance returns the balance of an asset
func (*Portfolio) GetPosition ¶
GetPosition returns a position by symbol
func (*Portfolio) GetTotalValue ¶
GetTotalValue returns the total portfolio value in the specified quote asset
func (*Portfolio) GetTransactions ¶
func (p *Portfolio) GetTransactions() []Transaction
GetTransactions returns all transactions
func (*Portfolio) LoadFromStore ¶
LoadFromStore pulls the most recent snapshot from the configured store.
func (*Portfolio) PositionsSnapshot ¶
func (p *Portfolio) PositionsSnapshot() []PositionSnapshot
PositionsSnapshot returns a slice of Position DTOs safe for serialization.
func (*Portfolio) Restore ¶
func (p *Portfolio) Restore(snapshot PortfolioSnapshot)
Restore replaces the current in-memory state with the provided snapshot.
func (*Portfolio) SetBalance ¶
SetBalance sets the balance of an asset and persists/emits events if configured.
func (*Portfolio) SetDefaultFee ¶
SetDefaultFee updates the default fee applied when no symbol/base/quote match is found.
func (*Portfolio) SetFeeForBase ¶
SetFeeForBase wires a fee that is applied to any symbol with the provided base asset.
func (*Portfolio) SetFeeForQuote ¶
SetFeeForQuote wires a fee that is applied to any symbol with the provided quote asset.
func (*Portfolio) SetFeeForSymbol ¶
SetFeeForSymbol wires a fixed fee for a specific trading symbol.
func (*Portfolio) SetFeeSchedule ¶
func (p *Portfolio) SetFeeSchedule(schedule *FeeSchedule)
SetFeeSchedule replaces the existing fee schedule at runtime.
func (*Portfolio) SetRiskPolicy ¶
func (p *Portfolio) SetRiskPolicy(policy *RiskPolicy)
SetRiskPolicy updates the risk controls enforced for subsequent transactions.
func (*Portfolio) Snapshot ¶
func (p *Portfolio) Snapshot() PortfolioSnapshot
Snapshot returns a deep copy of the portfolio state.
func (*Portfolio) Subscribe ¶
func (p *Portfolio) Subscribe(buffer int) (<-chan PortfolioEvent, func())
Subscribe returns a channel for streaming portfolio events and an unsubscribe function.
func (*Portfolio) UpdatePosition ¶
UpdatePosition upserts a position and notifies subscribers.
type PortfolioEvent ¶
type PortfolioEvent struct {
Type EventType `json:"type"`
Balance *BalanceSnapshot `json:"balance,omitempty"`
Position *PositionSnapshot `json:"position,omitempty"`
Transaction *Transaction `json:"transaction,omitempty"`
Snapshot *PortfolioSnapshot `json:"snapshot,omitempty"`
Diff *SnapshotDiff `json:"diff,omitempty"`
Timestamp time.Time `json:"timestamp"`
Message string `json:"message,omitempty"`
}
PortfolioEvent is sent to subscribers whenever the portfolio mutates.
type PortfolioSnapshot ¶
type PortfolioSnapshot struct {
Balances []BalanceSnapshot `json:"balances"`
Positions []PositionSnapshot `json:"positions"`
Transactions []Transaction `json:"transactions"`
}
PortfolioSnapshot represents a serializable copy of the portfolio.
type Position ¶
type Position struct {
Symbol string
EntryPrice float64
CurrentPrice float64
Quantity float64
OpenTime time.Time
LastUpdated time.Time
RealizedPnL float64
UnrealizedPnL float64
}
Position represents a trading position
type PositionChange ¶
type PositionChange struct {
Symbol string `json:"symbol"`
Position *PositionSnapshot `json:"position,omitempty"`
Deleted bool `json:"deleted,omitempty"`
}
PositionChange reports a position mutation. The snapshot is nil when deleted.
type PositionSnapshot ¶
type PositionSnapshot struct {
Symbol string `json:"symbol"`
EntryPrice float64 `json:"entryPrice"`
CurrentPrice float64 `json:"currentPrice"`
Quantity float64 `json:"quantity"`
OpenTime time.Time `json:"openTime"`
LastUpdated time.Time `json:"lastUpdated"`
RealizedPnL float64 `json:"realizedPnl"`
UnrealizedPnL float64 `json:"unrealizedPnl"`
}
PositionSnapshot is a DTO mirroring Position but intended for serialization.
type RiskPolicy ¶
type RiskPolicy struct {
MaxExposure float64
MaxExposureBySymbol map[string]float64
DailyLossLimit float64
}
RiskPolicy defines the exposure and loss limits enforced before recording trades.
type SQLiteStore ¶
type SQLiteStore struct {
// contains filtered or unexported fields
}
SQLiteStore persists snapshots to an embedded SQLite database.
func NewSQLiteStore ¶
func NewSQLiteStore(path string) (*SQLiteStore, error)
NewSQLiteStore opens/creates an on-disk SQLite database at path.
func (*SQLiteStore) Close ¶
func (s *SQLiteStore) Close() error
Close releases the SQLite database handle.
func (*SQLiteStore) Load ¶
func (s *SQLiteStore) Load() (PortfolioSnapshot, error)
Load fetches the snapshot from SQLite.
func (*SQLiteStore) Save ¶
func (s *SQLiteStore) Save(snapshot PortfolioSnapshot) error
Save writes the snapshot into SQLite using an UPSERT.
type SnapshotDiff ¶
type SnapshotDiff struct {
Balances []BalanceChange `json:"balances,omitempty"`
Positions []PositionChange `json:"positions,omitempty"`
TransactionsAppended []Transaction `json:"transactionsAppended,omitempty"`
TransactionsReset bool `json:"transactionsReset,omitempty"`
}
SnapshotDiff captures the mutations between two snapshots so downstream consumers can update their mirrors without reprocessing the entire state.
func DiffSnapshots ¶
func DiffSnapshots(prev, next PortfolioSnapshot) SnapshotDiff
DiffSnapshots compares two snapshots and emits the minimal change-set needed to transform prev into next.
func (SnapshotDiff) IsEmpty ¶
func (d SnapshotDiff) IsEmpty() bool
IsEmpty reports whether the diff contains any useful information.
type SnapshotStore ¶
type SnapshotStore interface {
Save(snapshot PortfolioSnapshot) error
Load() (PortfolioSnapshot, error)
}
SnapshotStore persists and loads portfolio snapshots.