Module: SQA
- Defined in:
- lib/sqa/gp.rb,
lib/sqa/fpop.rb,
lib/sqa/init.rb,
lib/sqa/config.rb,
lib/sqa/errors.rb,
lib/sqa/stream.rb,
lib/sqa/version.rb,
lib/sqa/ensemble.rb,
lib/sqa/risk_manager.rb,
lib/sqa/market_regime.rb,
lib/sqa/plugin_manager.rb,
lib/sqa/multi_timeframe.rb,
lib/sqa/pattern_matcher.rb,
lib/sqa/sector_analyzer.rb,
lib/sqa/seasonal_analyzer.rb,
lib/sqa/strategy_generator.rb,
lib/sqa/portfolio_optimizer.rb,
lib/sqa/strategy/kbs_strategy.rb
Overview
Knowledge-Based Strategy using RETE Forward Chaining
This strategy uses a rule-based system with the RETE algorithm for forward-chaining inference. It allows defining complex trading rules that react to market conditions.
The strategy asserts facts about market conditions (RSI, trends, volume, etc.) and fires rules when patterns are matched.
DSL Keywords:
- on : Assert a condition (fact must exist)
- without : Negated condition (fact must NOT exist)
- perform : Define action to execute when rule fires
- execute : Alias for perform
- action : Alias for perform
Example:
strategy = SQA::Strategy::KBS.new
# Capture kb for use in perform blocks
kb = strategy.kb
# Define custom rules using the DSL
strategy.add_rule :buy_oversold_uptrend do
on :rsi, { level: :oversold }
on :trend, { direction: :up }
without :position
perform do
kb.assert(:signal, { action: :buy, confidence: :high })
end
end
# Execute strategy
signal = strategy.trade(vector)
Note: Use ‘kb.assert’ (not just ‘assert’) in perform blocks to access the knowledge base.
Defined Under Namespace
Modules: FPOP, MarketRegime, SeasonalAnalyzer Classes: Backtest, BadParameterError, Config, ConfigurationError, DataFetchError, DataFrame, Ensemble, GeneticProgram, MultiTimeframe, PatternMatcher, PluginManager, Portfolio, PortfolioOptimizer, RiskManager, SectorAnalyzer, Stock, Strategy, StrategyGenerator, Stream, Ticker
Constant Summary collapse
- VERSION =
'0.0.38'
Class Attribute Summary collapse
-
.config ⇒ SQA::Config
Returns the current configuration.
Class Method Summary collapse
-
.av ⇒ SQA
Legacy accessor for backward compatibility with SQA.av.key usage.
-
.av_api_key ⇒ String
Returns the Alpha Vantage API key.
-
.av_api_key=(key) ⇒ String
Sets the Alpha Vantage API key.
-
.data_dir ⇒ Pathname
Returns the data directory as a Pathname.
-
.debug? ⇒ Boolean
Returns whether debug mode is enabled.
-
.homify(filepath) ⇒ String
Expands ~ to user’s home directory in filepath.
-
.init(argv = ARGV) ⇒ SQA::Config
Initializes the SQA library.
-
.key ⇒ String
Returns the API key for compatibility with old SQA.av.key usage.
-
.verbose? ⇒ Boolean
Returns whether verbose mode is enabled.
Class Attribute Details
.config ⇒ SQA::Config
Returns the current configuration.
111 |
# File 'lib/sqa/init.rb', line 111 def config() = @config |
Class Method Details
.av ⇒ SQA
Legacy accessor for backward compatibility with SQA.av.key usage.
77 78 79 |
# File 'lib/sqa/init.rb', line 77 def av self end |
.av_api_key ⇒ String
Returns the Alpha Vantage API key. Reads from AV_API_KEY or ALPHAVANTAGE_API_KEY environment variables.
61 62 63 64 |
# File 'lib/sqa/init.rb', line 61 def av_api_key @av_api_key ||= ENV['AV_API_KEY'] || ENV['ALPHAVANTAGE_API_KEY'] @av_api_key || raise(SQA::ConfigurationError, 'Alpha Vantage API key not set. Set AV_API_KEY or ALPHAVANTAGE_API_KEY environment variable.') end |
.av_api_key=(key) ⇒ String
Sets the Alpha Vantage API key.
70 71 72 |
# File 'lib/sqa/init.rb', line 70 def av_api_key=(key) @av_api_key = key end |
.data_dir ⇒ Pathname
Returns the data directory as a Pathname.
106 |
# File 'lib/sqa/init.rb', line 106 def data_dir() = Pathname.new(config.data_dir) |
.debug? ⇒ Boolean
Returns whether debug mode is enabled.
91 |
# File 'lib/sqa/init.rb', line 91 def debug?() = @config&.debug? |
.homify(filepath) ⇒ String
Expands ~ to user’s home directory in filepath.
101 |
# File 'lib/sqa/init.rb', line 101 def homify(filepath) = filepath.gsub(/^~/, Nenv.home) |
.init(argv = ARGV) ⇒ SQA::Config
Initializes the SQA library. Should be called once at application startup.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/sqa/init.rb', line 34 def init(argv=ARGV) if argv.is_a? String argv = argv.split() end # Ran at SQA::Config elaboration time # @config = Config.new if defined? CLI CLI.run! # CLI handles its own argument parsing else # There are no real command line parameters # because the sqa gem is being required within # the context of a larger program. end config.data_dir = homify(config.data_dir) config end |
.key ⇒ String
Returns the API key for compatibility with old SQA.av.key usage.
85 86 87 |
# File 'lib/sqa/init.rb', line 85 def key av_api_key end |
.verbose? ⇒ Boolean
Returns whether verbose mode is enabled.
95 |
# File 'lib/sqa/init.rb', line 95 def verbose?() = @config&.verbose? |