Class: SQA::Strategy

Inherits:
Object
  • Object
show all
Defined in:
lib/sqa/strategy.rb,
lib/sqa/strategy/common.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: Consensus, EMA, MP, MR, RSI, Random, SMA

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStrategy

Returns a new instance of Strategy.



6
7
8
# File 'lib/sqa/strategy.rb', line 6

def initialize
  @strategies = []
end

Instance Attribute Details

#strategiesObject

Returns the value of attribute strategies.



4
5
6
# File 'lib/sqa/strategy.rb', line 4

def strategies
  @strategies
end

Instance Method Details

#add(a_strategy) ⇒ Object

Raises:



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sqa/strategy.rb', line 10

def add(a_strategy)
  raise BadParameterError unless [Class, Method].include? a_strategy.class

  a_proc  = if Class == a_strategy.class
              a_strategy.method(:trade)
            else
              a_strategy
            end

  @strategies << a_proc
end

#auto_load(except: [:common], only: []) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sqa/strategy.rb', line 29

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

#availableObject



50
51
52
53
54
# File 'lib/sqa/strategy.rb', line 50

def available
  ObjectSpace.each_object(Class).select { |klass|
    klass.to_s.start_with?("SQA::Strategy::")
  }
end

#execute(v) ⇒ Object



22
23
24
25
26
27
# File 'lib/sqa/strategy.rb', line 22

def execute(v)
  result = []
  # TODO: Can do this in parallel ...
  @strategies.each { |signal| result << signal.call(v) }
  result
end