📦 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.
🏭 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
.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
.default_connection()¶
Creates the default Faraday connection to Alpha Vantage.
Returns
Type: Faraday::Connection
A new connection to Alpha Vantage API
Source Location
.reset_connection!()¶
Resets the connection to default. Useful for testing cleanup to ensure fresh state between tests.
Returns
Type: nil
Source Location
.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
Source Location
.reset_top!()¶
Resets the cached top gainers/losers data. Useful for testing or forcing a refresh.
Returns
Type: nil
Source Location
🔨 Instance Methods¶
#data()¶
Returns
Type: SQA::DataFrame::Data
Stock metadata (ticker, name, exchange, etc.)
Source Location
#data=(value)¶
Returns
Type: SQA::DataFrame::Data
Stock metadata (ticker, name, exchange, etc.)
Source Location
#df()¶
Returns
Type: SQA::DataFrame
Price and volume data as a DataFrame
Source Location
#df=(value)¶
Returns
Type: SQA::DataFrame
Price and volume data as a DataFrame
Source Location
#klass()¶
Returns
Type: Class
The data source class (e.g., SQA::DataFrame::AlphaVantage)
Source Location
#klass=(value)¶
Returns
Type: Class
The data source class (e.g., SQA::DataFrame::AlphaVantage)
Source Location
#transformers()¶
Returns
Type: Hash
Column transformers for data normalization
Source Location
#transformers=(value)¶
Returns
Type: Hash
Column transformers for data normalization
Source Location
#strategy()¶
Returns
Type: SQA::Strategy, nil
Optional trading strategy attached to this stock
Source Location
#strategy=(value)¶
Sets the attribute strategy
Parameters
| Name | Type | Description |
|---|---|---|
value |
Any |
the value to set the attribute strategy to. |
Source Location
#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
Source Location
#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
#create_data()¶
Creates a new minimal data structure for the stock.
Returns
Type: SQA::DataFrame::Data
The newly created data object
Source Location
#update()¶
Updates the stock's overview data from the API. Silently handles errors since overview data is optional.
Returns
Type: void
Usage Examples
Source Location
#save_data()¶
Persists the stock's metadata to a JSON file.
Returns
Type: Integer
Number of bytes written
Source Location
#ticker()¶
Returns
Type: String
The stock's ticker symbol
Source Location
#name()¶
Returns
Type: String, nil
The company name
Source Location
#exchange()¶
Returns
Type: String, nil
The exchange where the stock trades
Source Location
#source()¶
Returns
Type: Symbol
The data source (:alpha_vantage or :yahoo_finance)
Source Location
#indicators()¶
Returns
Type: Hash
Cached indicator values
Source Location
#indicators=(value)¶
Parameters
| Name | Type | Description |
|---|---|---|
value |
Hash |
New indicator values |
Source Location
#overview()¶
Returns
Type: Hash, nil
Company overview data from API
Source Location
#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
#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
#update_the_dataframe()¶
Returns
Type: void
Source Location
#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
#to_s()¶
Returns a human-readable string representation of the stock.
Returns
Type: String
Summary including ticker, data points count, and date range
Source Location
#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
Source Location
#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