Skip to content

📦 SQA::Stock

Description

Represents a stock with price history, metadata, and technical analysis capabilities. This is the primary domain object for interacting with stock data.

Source Information

Defined in: lib/sqa/stock.rb:14

Inherits from: Object

🏭 Class Methods

.connection()

Returns the current Faraday connection for API requests. Allows injection of custom connections for testing or different configurations.

Returns

Type: Faraday::Connection

The current connection instance

Source Location

lib/sqa/stock.rb:30


.connection=(conn)

Sets a custom Faraday connection. Useful for testing with mocks/stubs or configuring different API endpoints.

Parameters

Name Type Description
conn Faraday::Connection Custom Faraday connection to use

Returns

Type: Faraday::Connection

The connection that was set

Source Location

lib/sqa/stock.rb:39


.default_connection()

Creates the default Faraday connection to Alpha Vantage.

Returns

Type: Faraday::Connection

A new connection to Alpha Vantage API

Source Location

lib/sqa/stock.rb:46


.reset_connection!()

Resets the connection to default. Useful for testing cleanup to ensure fresh state between tests.

Returns

Type: nil

Source Location

lib/sqa/stock.rb:54


.top()

Fetches top gainers, losers, and most actively traded stocks from Alpha Vantage. Results are cached after the first call.

Returns

Type: Hashie::Mash

Object with top_gainers, top_losers, and most_actively_traded arrays

Usage Examples

top = SQA::Stock.top
top.top_gainers.each { |stock| puts "#{stock.ticker}: +#{stock.change_percentage}%" }
top.top_losers.first.ticker  # => "XYZ"
Source Location

lib/sqa/stock.rb:341


.reset_top!()

Resets the cached top gainers/losers data. Useful for testing or forcing a refresh.

Returns

Type: nil

Source Location

lib/sqa/stock.rb:372


🔨 Instance Methods

#data()

Returns

Type: SQA::DataFrame::Data

Stock metadata (ticker, name, exchange, etc.)

Source Location

lib/sqa/stock.rb:69


#data=(value)

Returns

Type: SQA::DataFrame::Data

Stock metadata (ticker, name, exchange, etc.)

Source Location

lib/sqa/stock.rb:69


#df()

Returns

Type: SQA::DataFrame

Price and volume data as a DataFrame

Source Location

lib/sqa/stock.rb:69


#df=(value)

Returns

Type: SQA::DataFrame

Price and volume data as a DataFrame

Source Location

lib/sqa/stock.rb:69


#klass()

Returns

Type: Class

The data source class (e.g., SQA::DataFrame::AlphaVantage)

Source Location

lib/sqa/stock.rb:69


#klass=(value)

Returns

Type: Class

The data source class (e.g., SQA::DataFrame::AlphaVantage)

Source Location

lib/sqa/stock.rb:69


#transformers()

Returns

Type: Hash

Column transformers for data normalization

Source Location

lib/sqa/stock.rb:69


#transformers=(value)

Returns

Type: Hash

Column transformers for data normalization

Source Location

lib/sqa/stock.rb:69


#strategy()

Returns

Type: SQA::Strategy, nil

Optional trading strategy attached to this stock

Source Location

lib/sqa/stock.rb:69


#strategy=(value)

Sets the attribute strategy

Parameters

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

lib/sqa/stock.rb:69


#initialize(ticker:, source: = :alpha_vantage)

Creates a new Stock instance and loads or fetches its data.

Parameters

Name Type Description
ticker String The stock ticker symbol (e.g., 'AAPL', 'MSFT')
source Symbol The data source to use (:alpha_vantage or :yahoo_finance)

Returns

Type: Stock

a new instance of Stock

Usage Examples

stock = SQA::Stock.new(ticker: 'AAPL')
stock = SQA::Stock.new(ticker: 'GOOG', source: :yahoo_finance)
Source Location

lib/sqa/stock.rb:81


#load_or_create_data()

Loads existing data from cache or creates new data structure. If cached data exists, loads from JSON file. Otherwise creates minimal structure and attempts to fetch overview from API.

Returns

Type: void

Source Location

lib/sqa/stock.rb:107


#create_data()

Creates a new minimal data structure for the stock.

Returns

Type: SQA::DataFrame::Data

The newly created data object

Source Location

lib/sqa/stock.rb:126


#update()

Updates the stock's overview data from the API. Silently handles errors since overview data is optional.

Returns

Type: void

Usage Examples

stock = SQA::Stock.new(ticker: 'AAPL')
stock.update  # Fetches latest company overview data
stock.data.overview['market_capitalization']  # => 2500000000000
stock.data.overview['pe_ratio']  # => 28.5
stock.update  # No error raised if API is unavailable
# Warning logged but stock remains usable with cached data
Source Location

lib/sqa/stock.rb:134


#save_data()

Persists the stock's metadata to a JSON file.

Returns

Type: Integer

Number of bytes written

Source Location

lib/sqa/stock.rb:147


#ticker()

Returns

Type: String

The stock's ticker symbol

Source Location

lib/sqa/stock.rb:165


#name()

Returns

Type: String, nil

The company name

Source Location

lib/sqa/stock.rb:165


#exchange()

Returns

Type: String, nil

The exchange where the stock trades

Source Location

lib/sqa/stock.rb:165


#source()

Returns

Type: Symbol

The data source (:alpha_vantage or :yahoo_finance)

Source Location

lib/sqa/stock.rb:165


#indicators()

Returns

Type: Hash

Cached indicator values

Source Location

lib/sqa/stock.rb:165


#indicators=(value)

Parameters

Name Type Description
value Hash New indicator values
Source Location

lib/sqa/stock.rb:165


#overview()

Returns

Type: Hash, nil

Company overview data from API

Source Location

lib/sqa/stock.rb:165


#update_dataframe()

Updates the DataFrame with price data. Loads from cache if available, otherwise fetches from API. Applies migrations for old data formats and updates with recent data.

Returns

Type: void

Source Location

lib/sqa/stock.rb:173


#update_dataframe_with_recent_data()

Fetches recent data from API and appends to existing DataFrame. Only called if should_update? returns true.

Returns

Type: void

Source Location

lib/sqa/stock.rb:228


#update_the_dataframe()

Returns

Type: void

Source Location

lib/sqa/stock.rb:250


#should_update?()

Determines whether the DataFrame should be updated from the API. Returns false if lazy_update is enabled, API key is missing, or data is already current.

Returns

Type: Boolean

true if update should proceed, false otherwise

Source Location

lib/sqa/stock.rb:260


#to_s()

Returns a human-readable string representation of the stock.

Returns

Type: String

Summary including ticker, data points count, and date range

Usage Examples

stock.to_s  # => "aapl with 252 data points from 2023-01-03 to 2023-12-29"
Source Location

lib/sqa/stock.rb:294


#inspect()

Returns a human-readable string representation of the stock. Note: CSV data is stored in ascending chronological order (oldest to newest) This ensures compatibility with TA-Lib indicators which expect arrays in this order

Returns

Type: String

Summary including ticker, data points count, and date range

Usage Examples

stock.to_s  # => "aapl with 252 data points from 2023-01-03 to 2023-12-29"
Source Location

lib/sqa/stock.rb:299


#merge_overview()

Fetches and merges company overview data from Alpha Vantage API. Converts API response keys to snake_case and appropriate data types.

Returns

Type: Hash

The merged overview data

Source Location

lib/sqa/stock.rb:306


📝 Attributes

🔄 data read/write

🔄 df read/write

🔄 klass read/write

🔄 transformers read/write

🔄 strategy read/write