📦 SQA::Portfolio¶
🏭 Class Methods¶
.load_from_csv(filename)¶
Load portfolio from CSV file
Parameters
| Name | Type | Description |
|---|---|---|
filename |
String |
Path to CSV file |
Source Location
🔨 Instance Methods¶
#positions()¶
Returns the value of attribute positions.
Source Location
#positions=(value)¶
Sets the attribute positions
Parameters
| Name | Type | Description |
|---|---|---|
value |
Any |
the value to set the attribute positions to. |
Source Location
#trades()¶
Returns the value of attribute trades.
Source Location
#trades=(value)¶
Sets the attribute trades
Parameters
| Name | Type | Description |
|---|---|---|
value |
Any |
the value to set the attribute trades to. |
Source Location
#cash()¶
Returns the value of attribute cash.
Source Location
#cash=(value)¶
Sets the attribute cash
Parameters
| Name | Type | Description |
|---|---|---|
value |
Any |
the value to set the attribute cash to. |
Source Location
#initial_cash()¶
Returns the value of attribute initial_cash.
Source Location
#initial_cash=(value)¶
Sets the attribute initial_cash
Parameters
| Name | Type | Description |
|---|---|---|
value |
Any |
the value to set the attribute initial_cash to. |
Source Location
#commission()¶
Returns the value of attribute commission.
Source Location
#commission=(value)¶
Sets the attribute commission
Parameters
| Name | Type | Description |
|---|---|---|
value |
Any |
the value to set the attribute commission to. |
Source Location
#initialize(initial_cash: = 10_000.0, commission: = 0.0)¶
Returns
Type: Portfolio
a new instance of Portfolio
Source Location
#buy(ticker, shares:, price:, date: = Date.today)¶
Buy shares of a stock
Parameters
| Name | Type | Description |
|---|---|---|
ticker |
String |
Stock ticker symbol |
shares |
Integer |
Number of shares to buy |
price |
Float |
Price per share |
date |
Date |
Date of trade |
Returns
Type: Trade
The executed trade
Usage Examples
Source Location
#sell(ticker, shares:, price:, date: = Date.today)¶
Sell shares of a stock
Parameters
| Name | Type | Description |
|---|---|---|
ticker |
String |
Stock ticker symbol |
shares |
Integer |
Number of shares to sell |
price |
Float |
Price per share |
date |
Date |
Date of trade |
Returns
Type: Trade
The executed trade
Usage Examples
Source Location
#position(ticker)¶
Get current position for a ticker
Parameters
| Name | Type | Description |
|---|---|---|
ticker |
String |
Stock ticker symbol |
Returns
Type: Position, nil
The position or nil if not found
Source Location
#all_positions()¶
Get all current positions
Returns
Type: Hash
Hash of ticker => Position
Source Location
#value(current_prices = {})¶
Calculate total portfolio value
Parameters
| Name | Type | Description |
|---|---|---|
current_prices |
Hash |
Hash of ticker => current_price |
Returns
Type: Float
Total portfolio value (cash + positions)
Usage Examples
Source Location
#profit_loss(current_prices = {})¶
Calculate total profit/loss across all positions
Parameters
| Name | Type | Description |
|---|---|---|
current_prices |
Hash |
Hash of ticker => current_price |
Returns
Type: Float
Total P&L
Source Location
#profit_loss_percent(current_prices = {})¶
Calculate profit/loss percentage
Parameters
| Name | Type | Description |
|---|---|---|
current_prices |
Hash |
Hash of ticker => current_price |
Returns
Type: Float
P&L percentage
Source Location
#total_return(current_prices = {})¶
Calculate total return (including dividends if tracked)
Parameters
| Name | Type | Description |
|---|---|---|
current_prices |
Hash |
Hash of ticker => current_price |
Returns
Type: Float
Total return as decimal (e.g., 0.15 for 15%)
Source Location
#trade_history()¶
Get trade history
Returns
Type: Array<Trade>
Array of all trades
Source Location
#summary(current_prices = {})¶
Get summary statistics
Parameters
| Name | Type | Description |
|---|---|---|
current_prices |
Hash |
Hash of ticker => current_price |
Returns
Type: Hash
Summary statistics
Usage Examples
portfolio = SQA::Portfolio.new(initial_cash: 10_000, commission: 1.0)
portfolio.buy('AAPL', shares: 10, price: 150.0)
portfolio.sell('AAPL', shares: 5, price: 160.0)
summary = portfolio.summary({ 'AAPL' => 165.0 })
summary[:initial_cash] # => 10_000.0
summary[:current_cash] # => 8798.0
summary[:positions_count] # => 1
summary[:total_value] # => 9623.0
summary[:profit_loss_percent] # => -3.77%
summary[:total_trades] # => 2
summary[:buy_trades] # => 1
summary[:sell_trades] # => 1
Source Location
#save_to_csv(filename)¶
Save portfolio to CSV file
Parameters
| Name | Type | Description |
|---|---|---|
filename |
String |
Path to CSV file |
Source Location
#save_trades_to_csv(filename)¶
Save trade history to CSV file
Parameters
| Name | Type | Description |
|---|---|---|
filename |
String |
Path to CSV file |
Source Location
📝 Attributes¶
🔄 positions read/write¶
Returns the value of attribute positions.
🔄 trades read/write¶
Returns the value of attribute trades.
🔄 cash read/write¶
Returns the value of attribute cash.
🔄 initial_cash read/write¶
Returns the value of attribute initial_cash.
🔄 commission read/write¶
Returns the value of attribute commission.