Class: SQA::Strategy

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeStrategy

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

#strategiesArray<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.

Examples:

Adding a class-based strategy

strategy.add(SQA::Strategy::RSI)

Adding a method directly

strategy.add(MyModule.method(:custom_trade))

Raises:



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.

Examples:

Load all except random

strategy.auto_load(except: [:common, :random])

Load only specific strategies

strategy.auto_load(only: [:rsi, :macd])


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

#availableArray<Class>

Returns all available strategy classes in the SQA::Strategy namespace.

Examples:

SQA::Strategy.new.available
# => [SQA::Strategy::RSI, SQA::Strategy::MACD, ...]


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.

Examples:

vector = OpenStruct.new(rsi: 25, prices: prices_array)
signals = strategy.execute(vector)  # => [:buy, :hold, :sell]


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