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

Class Method Summary collapse

Class Attribute Details

.configSQA::Config

Returns the current configuration.

Returns:



111
# File 'lib/sqa/init.rb', line 111

def config()            = @config

Class Method Details

.avSQA

Legacy accessor for backward compatibility with SQA.av.key usage.

Returns:

  • (SQA)

    Self, to allow SQA.av.key calls



77
78
79
# File 'lib/sqa/init.rb', line 77

def av
	self
end

.av_api_keyString

Returns the Alpha Vantage API key. Reads from AV_API_KEY or ALPHAVANTAGE_API_KEY environment variables.

Returns:

Raises:



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.

Parameters:

  • key (String)

    The API key to set

Returns:

  • (String)

    The key that was set



70
71
72
# File 'lib/sqa/init.rb', line 70

def av_api_key=(key)
	@av_api_key = key
end

.data_dirPathname

Returns the data directory as a Pathname.

Returns:

  • (Pathname)

    Data directory path



106
# File 'lib/sqa/init.rb', line 106

def data_dir() 					= Pathname.new(config.data_dir)

.debug?Boolean

Returns whether debug mode is enabled.

Returns:

  • (Boolean)

    true if debug mode is on



91
# File 'lib/sqa/init.rb', line 91

def debug?() 						= @config&.debug?

.homify(filepath) ⇒ String

Expands ~ to user’s home directory in filepath.

Parameters:

  • filepath (String)

    Path potentially containing ~

Returns:

  • (String)

    Path with ~ expanded



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.

Examples:

SQA.init
SQA.init("--debug --verbose")

Parameters:

  • argv (Array<String>, String) (defaults to: ARGV)

    Command line arguments (default: ARGV)

Returns:



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

.keyString

Returns the API key for compatibility with old SQA.av.key usage.

Returns:

Raises:



85
86
87
# File 'lib/sqa/init.rb', line 85

def key
	av_api_key
end

.verbose?Boolean

Returns whether verbose mode is enabled.

Returns:

  • (Boolean)

    true if verbose mode is on



95
# File 'lib/sqa/init.rb', line 95

def verbose?() 					= @config&.verbose?