Class: SQA::Strategy
- Inherits:
-
Object
- Object
- SQA::Strategy
- Defined in:
- lib/sqa/strategy.rb,
lib/sqa/strategy/common.rb,
lib/sqa/strategy/kbs_strategy.rb
Overview
This module needs to be extend’ed within a strategy class so that these common class methods are available in every trading strategy.
Defined Under Namespace
Modules: Common Classes: BollingerBands, Consensus, EMA, KBS, MACD, MP, MR, RSI, Random, SMA, Stochastic, VolumeBreakout
Instance Attribute Summary collapse
-
#strategies ⇒ Array<Method>
Collection of strategy trade methods.
Instance Method Summary collapse
-
#add(a_strategy) ⇒ Array<Method>
Adds a trading strategy to the collection.
-
#auto_load(except: [:common], only: []) ⇒ nil
Auto-loads strategy files from the strategy directory.
-
#available ⇒ Array<Class>
Returns all available strategy classes in the SQA::Strategy namespace.
-
#execute(v) ⇒ Array<Symbol>
Executes all registered strategies with the given data vector.
-
#initialize ⇒ Strategy
constructor
Creates a new Strategy instance with an empty strategies collection.
Constructor Details
#initialize ⇒ Strategy
Creates a new Strategy instance with an empty strategies collection.
22 23 24 |
# File 'lib/sqa/strategy.rb', line 22 def initialize @strategies = [] end |
Instance Attribute Details
#strategies ⇒ Array<Method>
19 20 21 |
# File 'lib/sqa/strategy.rb', line 19 def strategies @strategies end |
Instance Method Details
#add(a_strategy) ⇒ Array<Method>
Adds a trading strategy to the collection. Strategies must be either a Class with a .trade method or a Method object.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/sqa/strategy.rb', line 39 def add(a_strategy) raise BadParameterError unless a_strategy.is_a?(Class) || a_strategy.is_a?(Method) a_proc = if a_strategy.is_a?(Class) a_strategy.method(:trade) else a_strategy end @strategies << a_proc end |
#auto_load(except: [:common], only: []) ⇒ nil
Auto-loads strategy files from the strategy directory.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/sqa/strategy.rb', line 79 def auto_load(except: [:common], only: []) dir_path = Pathname.new(__dir__) + "strategy" except = Array(except).map{|f| f.to_s.downcase} only = Array(only).map{|f| f.to_s.downcase} dir_path.children.each do |child| next unless ".rb" == child.extname.downcase basename = child.basename.to_s.split('.').first.downcase next if except.include? basename next if !only.empty? && !only.include?(basename) print "loading #{basename} ... " load child puts "done" end nil end |
#available ⇒ Array<Class>
Returns all available strategy classes in the SQA::Strategy namespace.
108 109 110 111 112 |
# File 'lib/sqa/strategy.rb', line 108 def available ObjectSpace.each_object(Class).select { |klass| klass.name&.start_with?("SQA::Strategy::") } end |
#execute(v) ⇒ Array<Symbol>
Executes all registered strategies with the given data vector.
60 61 62 63 64 65 |
# File 'lib/sqa/strategy.rb', line 60 def execute(v) result = [] # NOTE: Could be parallelized with Parallel gem for large strategy sets @strategies.each { |signal| result << signal.call(v) } result end |