Skip to content

📦 SQA::Portfolio

Source Information

Defined in: lib/sqa/portfolio.rb:7

Inherits from: Object

🏭 Class Methods

.load_from_csv(filename)

Load portfolio from CSV file

Parameters

Name Type Description
filename String Path to CSV file
Source Location

lib/sqa/portfolio.rb:236


🔨 Instance Methods

#positions()

Returns the value of attribute positions.

Source Location

lib/sqa/portfolio.rb:8


#positions=(value)

Sets the attribute positions

Parameters

Name Type Description
value Any the value to set the attribute positions to.
Source Location

lib/sqa/portfolio.rb:8


#trades()

Returns the value of attribute trades.

Source Location

lib/sqa/portfolio.rb:8


#trades=(value)

Sets the attribute trades

Parameters

Name Type Description
value Any the value to set the attribute trades to.
Source Location

lib/sqa/portfolio.rb:8


#cash()

Returns the value of attribute cash.

Source Location

lib/sqa/portfolio.rb:8


#cash=(value)

Sets the attribute cash

Parameters

Name Type Description
value Any the value to set the attribute cash to.
Source Location

lib/sqa/portfolio.rb:8


#initial_cash()

Returns the value of attribute initial_cash.

Source Location

lib/sqa/portfolio.rb:8


#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

lib/sqa/portfolio.rb:8


#commission()

Returns the value of attribute commission.

Source Location

lib/sqa/portfolio.rb:8


#commission=(value)

Sets the attribute commission

Parameters

Name Type Description
value Any the value to set the attribute commission to.
Source Location

lib/sqa/portfolio.rb:8


#initialize(initial_cash: = 10_000.0, commission: = 0.0)

Returns

Type: Portfolio

a new instance of Portfolio

Source Location

lib/sqa/portfolio.rb:41


#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

portfolio = SQA::Portfolio.new(initial_cash: 10_000, commission: 1.0)
trade = portfolio.buy('AAPL', shares: 10, price: 150.0)
trade.action  # => :buy
trade.total   # => 1500.0
portfolio.cash  # => 8499.0 (10_000 - 1500 - 1.0 commission)
portfolio.buy('AAPL', shares: 10, price: 150.0)
portfolio.buy('MSFT', shares: 5, price: 300.0)
portfolio.positions.size  # => 2
Source Location

lib/sqa/portfolio.rb:55


#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

portfolio = SQA::Portfolio.new(initial_cash: 10_000, commission: 1.0)
portfolio.buy('AAPL', shares: 10, price: 150.0)
trade = portfolio.sell('AAPL', shares: 10, price: 160.0)
trade.total  # => 1600.0
portfolio.cash  # => 8498.0 + 1599.0 = 10097.0 (after commissions)
portfolio.buy('AAPL', shares: 100, price: 150.0)
portfolio.sell('AAPL', shares: 50, price: 160.0)  # Sell half
portfolio.position('AAPL').shares  # => 50
Source Location

lib/sqa/portfolio.rb:98


#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

lib/sqa/portfolio.rb:135


#all_positions()

Get all current positions

Returns

Type: Hash

Hash of ticker => Position

Source Location

lib/sqa/portfolio.rb:141


#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

portfolio = SQA::Portfolio.new(initial_cash: 10_000)
portfolio.buy('AAPL', shares: 10, price: 150.0)
portfolio.buy('MSFT', shares: 5, price: 300.0)

current_prices = { 'AAPL' => 160.0, 'MSFT' => 310.0 }
portfolio.value(current_prices)  # => 10_000 - 1500 - 1500 + 1600 + 1550 = 10_150
portfolio.value  # Uses purchase prices if no current prices provided
Source Location

lib/sqa/portfolio.rb:148


#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

lib/sqa/portfolio.rb:160


#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

lib/sqa/portfolio.rb:167


#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

lib/sqa/portfolio.rb:175


#trade_history()

Get trade history

Returns

Type: Array<Trade>

Array of all trades

Source Location

lib/sqa/portfolio.rb:182


#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

lib/sqa/portfolio.rb:189


#save_to_csv(filename)

Save portfolio to CSV file

Parameters

Name Type Description
filename String Path to CSV file
Source Location

lib/sqa/portfolio.rb:206


#save_trades_to_csv(filename)

Save trade history to CSV file

Parameters

Name Type Description
filename String Path to CSV file
Source Location

lib/sqa/portfolio.rb:217


📝 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.