API Reference¶
Complete API documentation for SQA classes and modules.
Auto-Generated API Documentation
For detailed API documentation with all methods, parameters, and return values, see the Complete API Reference - automatically generated from YARD comments with the same Material theme!
Quick Navigation¶
| Class | Description |
|---|---|
| SQA Module | Main module, initialization, configuration |
| SQA::Stock | Stock data management |
| SQA::DataFrame | High-performance data structures |
| SQA::Strategy | Trading strategy framework |
| SQA::Portfolio | Position and trade tracking |
| SQA::Backtest | Strategy simulation |
| SQA::Config | Configuration management |
| SQAI/SQA::TAI | Technical indicators |
SQA Module¶
Main module containing initialization and global accessors.
Module Methods¶
SQA.init(argv = ARGV)¶
Initializes the SQA library. Should be called once at application startup.
Returns: SQA::Config - The configuration instance
SQA.config¶
Returns the current configuration object.
Returns: SQA::Config
SQA.data_dir¶
Returns the data directory as a Pathname.
Returns: Pathname
SQA.av_api_key¶
Returns the Alpha Vantage API key from environment variables.
Returns: String
Raises: SQA::ConfigurationError if not set
SQA.debug? / SQA.verbose?¶
Check if debug or verbose mode is enabled.
Returns: Boolean
SQA::Stock¶
Represents a stock with historical data and metadata.
Constructor¶
SQA::Stock.new(ticker:, source: :alpha_vantage)¶
Creates a new Stock instance and loads/fetches its data.
stock = SQA::Stock.new(ticker: 'AAPL')
stock = SQA::Stock.new(ticker: 'MSFT', source: :yahoo_finance)
Parameters:
- ticker (String) - Stock ticker symbol
- source (Symbol) - Data source (:alpha_vantage or :yahoo_finance)
Raises: SQA::DataFetchError if data cannot be fetched
Instance Attributes¶
| Attribute | Type | Description |
|---|---|---|
ticker |
String | Stock symbol (lowercase) |
name |
String | Company name |
exchange |
String | Exchange (NASDAQ, NYSE, etc.) |
source |
Symbol | Data source used |
df |
SQA::DataFrame | Price/volume DataFrame |
data |
SQA::DataFrame::Data | Stock metadata |
indicators |
Hash | Cached indicator values |
overview |
Hash | Company overview from API |
Instance Methods¶
#to_s / #inspect¶
Returns human-readable string representation.
#update¶
Updates stock's overview data from API.
#save_data¶
Persists metadata to JSON file.
Class Methods¶
SQA::Stock.top¶
Fetches top gainers, losers, and most actively traded stocks.
top = SQA::Stock.top
top.top_gainers.each { |s| puts s.ticker }
top.top_losers.first
top.most_actively_traded
Returns: Hashie::Mash with three arrays
SQA::Stock.connection / SQA::Stock.connection=¶
Get or set the Faraday connection for API requests.
SQA::DataFrame¶
High-performance wrapper around Polars DataFrame.
Constructor¶
SQA::DataFrame.new(raw_data = nil, mapping: {}, transformers: {})¶
Creates a new DataFrame instance.
Instance Attributes¶
| Attribute | Type | Description |
|---|---|---|
data |
Polars::DataFrame | Underlying Polars DataFrame |
Instance Methods¶
#columns / #keys¶
Returns column names.
#[](column_name)¶
Access a column (delegates to Polars).
#size / #nrows / #length¶
Returns number of rows.
#ncols¶
Returns number of columns.
#to_csv(path)¶
Writes DataFrame to CSV file.
#to_h¶
Converts to Ruby Hash.
#append!(other_df) / #concat!(other_df)¶
Appends another DataFrame in place.
#concat_and_deduplicate!(other_df, sort_column: "timestamp")¶
Appends, removes duplicates, and sorts. Enforces ascending order for TA-Lib compatibility.
#rename_columns!(mapping)¶
Renames columns according to mapping.
#apply_transformers!(transformers)¶
Applies transformer functions to columns.
#fpl(column:, fpop:)¶
Calculates Future Period Loss/Profit.
#fpl_analysis(column:, fpop:)¶
FPL analysis with risk metrics.
Class Methods¶
SQA::DataFrame.load(source:, mapping: {}, transformers: {})¶
Loads DataFrame from CSV file.
SQA::DataFrame.from_aofh(aofh, mapping: {}, transformers: {})¶
Creates DataFrame from array of hashes.
SQA::DataFrame.from_csv_file(source, mapping: {}, transformers: {})¶
Creates DataFrame from CSV file.
SQA::DataFrame.from_json_file(source, mapping: {}, transformers: {})¶
Creates DataFrame from JSON file.
SQA::Strategy¶
Framework for managing trading strategies.
Constructor¶
SQA::Strategy.new¶
Creates a new Strategy manager with empty collection.
Instance Attributes¶
| Attribute | Type | Description |
|---|---|---|
strategies |
Array |
Collection of strategy methods |
Instance Methods¶
#add(strategy)¶
Adds a trading strategy.
Parameters:
- strategy (Class or Method) - Strategy to add
Raises: BadParameterError if not Class or Method
#execute(vector)¶
Executes all strategies with given data.
Parameters:
- vector (OpenStruct) - Data for strategy analysis
Returns: Array<Symbol> - Array of :buy, :sell, or :hold
#auto_load(except: [:common], only: [])¶
Auto-loads strategy files from strategy directory.
#available¶
Lists all available strategy classes.
Strategy Interface¶
All strategy classes must implement:
SQA::Portfolio¶
Track positions and calculate P&L.
Constructor¶
SQA::Portfolio.new(initial_cash:, commission: 0.0)¶
Instance Attributes¶
| Attribute | Type | Description |
|---|---|---|
cash |
Float | Available cash balance |
positions |
Hash | Ticker → share count |
trades |
Array | Trade history |
Instance Methods¶
#buy(ticker, shares:, price:)¶
Purchases shares.
#sell(ticker, shares:, price:)¶
Sells shares.
#value(current_prices)¶
Calculates total portfolio value.
Parameters:
- current_prices (Hash) - Ticker → current price
SQA::Backtest¶
Simulate strategies on historical data.
Constructor¶
SQA::Backtest.new(stock:, strategy:, initial_cash: 10_000, commission: 0.0)¶
backtest = SQA::Backtest.new(
stock: stock,
strategy: SQA::Strategy::RSI,
initial_cash: 10_000,
commission: 1.0
)
Instance Methods¶
#run¶
Executes the backtest simulation.
Returns: Results object with metrics:
| Metric | Description |
|---|---|
total_return |
Total percentage return |
sharpe_ratio |
Risk-adjusted return |
sortino_ratio |
Downside risk-adjusted return |
max_drawdown |
Largest peak-to-trough decline |
win_rate |
Percentage of profitable trades |
num_trades |
Total number of trades |
final_value |
Ending portfolio value |
SQA::Config¶
Configuration management with Hashie::Dash.
Properties¶
| Property | Type | Default | Description |
|---|---|---|---|
data_dir |
String | ~/sqa_data |
Data storage directory |
log_level |
Symbol | :info |
Log level |
debug |
Boolean | false |
Debug mode |
verbose |
Boolean | false |
Verbose output |
lazy_update |
Boolean | false |
Skip API updates |
plotting_library |
Symbol | :gruff |
Plotting library |
Instance Methods¶
#debug? / #verbose?¶
Check mode flags.
#from_file¶
Loads configuration from file (YAML, TOML, or JSON).
#dump_file¶
Saves configuration to file.
Class Methods¶
SQA::Config.reset¶
Resets configuration to defaults.
SQA::Config.initialized?¶
Check if config has been initialized.
SQAI Indicators¶
All indicators from TA-Lib via the sqa-tai gem.
Common Usage¶
prices = stock.df["adj_close_price"].to_a
# Moving Averages
sma = SQAI.sma(prices, period: 20)
ema = SQAI.ema(prices, period: 12)
# Momentum
rsi = SQAI.rsi(prices, period: 14)
macd, signal, hist = SQAI.macd(prices,
fast_period: 12,
slow_period: 26,
signal_period: 9
)
# Volatility
upper, middle, lower = SQAI.bbands(prices, period: 20)
atr = SQAI.atr(high, low, close, period: 14)
List All Indicators¶
See Technical Indicators for complete reference.
Error Classes¶
SQA::DataFetchError¶
Raised when unable to fetch data from API or file.
begin
stock = SQA::Stock.new(ticker: 'INVALID')
rescue SQA::DataFetchError => e
puts e.message
puts e.original_error # Wrapped exception
end
SQA::ConfigurationError¶
Raised for invalid or missing configuration.
SQA::BadParameterError¶
Raised for invalid method parameters.
ApiError¶
Raised when external API returns error response.
NotImplemented¶
Raised for unimplemented features.
Introspection¶
Use Ruby introspection to explore the API: