Skip to content

📦 SQA::Ensemble

Description

Ensemble - Combine multiple trading strategies

Provides methods for: - Majority voting - Weighted voting based on past performance - Meta-learning (strategy selection) - Strategy rotation based on market conditions - Confidence-based aggregation

Source Information

Defined in: lib/sqa/ensemble.rb:22

Inherits from: Object

🏭 Class Methods

.trade(vector)

Make ensemble compatible with Backtest (acts like a strategy)

Parameters

Name Type Description
vector OpenStruct Market data

Returns

Type: Symbol

Trading signal

Source Location

lib/sqa/ensemble.rb:275


🔨 Instance Methods

#strategies()

Returns the value of attribute strategies.

Source Location

lib/sqa/ensemble.rb:23


#strategies=(value)

Sets the attribute strategies

Parameters

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

lib/sqa/ensemble.rb:23


#weights()

Returns the value of attribute weights.

Source Location

lib/sqa/ensemble.rb:23


#weights=(value)

Sets the attribute weights

Parameters

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

lib/sqa/ensemble.rb:23


#performance_history()

Returns the value of attribute performance_history.

Source Location

lib/sqa/ensemble.rb:23


#performance_history=(value)

Sets the attribute performance_history

Parameters

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

lib/sqa/ensemble.rb:23


#initialize(strategies:, voting_method: = :majority, weights: = nil)

Initialize ensemble

Parameters

Name Type Description
strategies Array<Class> Array of strategy classes
voting_method Symbol :majority, :weighted, :unanimous, :confidence
weights Array<Float> Optional weights for weighted voting

Returns

Type: Ensemble

a new instance of Ensemble

Source Location

lib/sqa/ensemble.rb:32


#signal(vector)

Generate ensemble signal

Parameters

Name Type Description
vector OpenStruct Market data vector

Returns

Type: Symbol

:buy, :sell, or :hold

Source Location

lib/sqa/ensemble.rb:46


#majority_vote(vector)

Majority voting

Parameters

Name Type Description
vector OpenStruct Market data

Returns

Type: Symbol

Signal with most votes

Source Location

lib/sqa/ensemble.rb:67


#weighted_vote(vector)

Weighted voting based on strategy performance

Parameters

Name Type Description
vector OpenStruct Market data

Returns

Type: Symbol

Weighted signal

Source Location

lib/sqa/ensemble.rb:84


#unanimous_vote(vector)

Unanimous voting (all strategies must agree)

Parameters

Name Type Description
vector OpenStruct Market data

Returns

Type: Symbol

:buy/:sell only if unanimous, otherwise :hold

Source Location

lib/sqa/ensemble.rb:104


#confidence_vote(vector)

Confidence-based voting

Weight votes by strategy confidence scores.

Parameters

Name Type Description
vector OpenStruct Market data

Returns

Type: Symbol

Signal weighted by confidence

Source Location

lib/sqa/ensemble.rb:125


#update_weight(strategy_index, performance)

Update strategy weights based on performance

Adjust weights to favor better-performing strategies.

Parameters

Name Type Description
strategy_index Integer Index of strategy
performance Float Performance metric (e.g., return)
Source Location

lib/sqa/ensemble.rb:147


#update_confidence(strategy_class, correct)

Update confidence score for strategy

Parameters

Name Type Description
strategy_class Class Strategy class
correct Boolean Was the prediction correct?
Source Location

lib/sqa/ensemble.rb:160


#select_strategy(market_regime:, volatility: = :medium)

Select best strategy for current market conditions

Meta-learning approach: choose the strategy most likely to succeed.

Parameters

Name Type Description
market_regime Symbol Current market regime (:bull, :bear, :sideways)
volatility Symbol Volatility regime (:low, :medium, :high)

Returns

Type: Class

Best strategy class for conditions

Source Location

lib/sqa/ensemble.rb:181


#rotate(stock)

Rotate strategies based on market conditions

Parameters

Name Type Description
stock SQA::Stock Stock object

Returns

Type: Class

Strategy to use

Source Location

lib/sqa/ensemble.rb:213


#statistics()

Get ensemble statistics

Returns

Type: Hash

Performance statistics

Source Location

lib/sqa/ensemble.rb:227


#backtest_comparison(stock, initial_capital: = 10_000)

Backtest ensemble vs individual strategies

Parameters

Name Type Description
stock SQA::Stock Stock to backtest
initial_capital Float Starting capital

Returns

Type: Hash

Comparison results

Source Location

lib/sqa/ensemble.rb:245


#trade(vector)

Instance method for compatibility

Source Location

lib/sqa/ensemble.rb:283


📝 Attributes

🔄 strategies read/write

Returns the value of attribute strategies.

🔄 weights read/write

Returns the value of attribute weights.

🔄 performance_history read/write

Returns the value of attribute performance_history.