Class: Quant::Indicators::Indicator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/quant/indicators/indicator.rb

Direct Known Subclasses

Ma, Ping

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(series:, source:) ⇒ Indicator

Returns a new instance of Indicator.



21
22
23
24
25
26
# File 'lib/quant/indicators/indicator.rb', line 21

def initialize(series:, source:)
  @series = series
  @source = source
  @points = {}
  series.each { |tick| self << tick }
end

Instance Attribute Details

#p0Object (readonly)

Returns the value of attribute p0.



44
45
46
# File 'lib/quant/indicators/indicator.rb', line 44

def p0
  @p0
end

#p1Object (readonly)

Returns the value of attribute p1.



44
45
46
# File 'lib/quant/indicators/indicator.rb', line 44

def p1
  @p1
end

#p2Object (readonly)

Returns the value of attribute p2.



44
45
46
# File 'lib/quant/indicators/indicator.rb', line 44

def p2
  @p2
end

#p3Object (readonly)

Returns the value of attribute p3.



44
45
46
# File 'lib/quant/indicators/indicator.rb', line 44

def p3
  @p3
end

#seriesObject (readonly)

# include Mixins::TrendMethods include Mixins::Trig include Mixins::WeightedAverage include Mixins::HilbertTransform include Mixins::SuperSmoother include Mixins::Stochastic include Mixins::FisherTransform include Mixins::HighPassFilter include Mixins::Direction include Mixins::Filters



19
20
21
# File 'lib/quant/indicators/indicator.rb', line 19

def series
  @series
end

#sourceObject (readonly)

# include Mixins::TrendMethods include Mixins::Trig include Mixins::WeightedAverage include Mixins::HilbertTransform include Mixins::SuperSmoother include Mixins::Stochastic include Mixins::FisherTransform include Mixins::HighPassFilter include Mixins::Direction include Mixins::Filters



19
20
21
# File 'lib/quant/indicators/indicator.rb', line 19

def source
  @source
end

#t0Object (readonly)

Returns the value of attribute t0.



45
46
47
# File 'lib/quant/indicators/indicator.rb', line 45

def t0
  @t0
end

#t1Object (readonly)

Returns the value of attribute t1.



45
46
47
# File 'lib/quant/indicators/indicator.rb', line 45

def t1
  @t1
end

#t2Object (readonly)

Returns the value of attribute t2.



45
46
47
# File 'lib/quant/indicators/indicator.rb', line 45

def t2
  @t2
end

#t3Object (readonly)

Returns the value of attribute t3.



45
46
47
# File 'lib/quant/indicators/indicator.rb', line 45

def t3
  @t3
end

Instance Method Details

#<<(tick) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/quant/indicators/indicator.rb', line 47

def <<(tick)
  @t0 = tick
  @p0 = points_class.new(tick: tick, source: source)
  @points[tick] = @p0

  @p1 = values[-2] || @p0
  @p2 = values[-3] || @p1
  @p3 = values[-4] || @p2

  @t1 = ticks[-2] || @t0
  @t2 = ticks[-3] || @t1
  @t3 = ticks[-4] || @t2

  compute
end

#[](index) ⇒ Object



32
33
34
# File 'lib/quant/indicators/indicator.rb', line 32

def [](index)
  values[index]
end

#computeObject

Raises:

  • (NotImplementedError)


71
72
73
# File 'lib/quant/indicators/indicator.rb', line 71

def compute
  raise NotImplementedError
end

#each(&block) ⇒ Object



63
64
65
# File 'lib/quant/indicators/indicator.rb', line 63

def each(&block)
  @points.each_value(&block)
end

#indicator_nameObject



75
76
77
# File 'lib/quant/indicators/indicator.rb', line 75

def indicator_name
  self.class.name.split("::").last
end

#inputNumeric

The input is the value derived from the source for the indicator for the current tick. For example, if the source is :oc2, then the input is the value of the current tick’s (open + close) / 2

Returns:

  • (Numeric)


110
111
112
# File 'lib/quant/indicators/indicator.rb', line 110

def input
  t0.send(source)
end

#inspectObject



67
68
69
# File 'lib/quant/indicators/indicator.rb', line 67

def inspect
  "#<#{self.class.name} symbol=#{series.symbol} source=#{source} #{ticks.size} ticks>"
end

#p(offset) ⇒ Object

p(0) => values p(1) => values p(2) => values p(3) => values

Raises:

  • (ArgumentError)


87
88
89
90
91
92
# File 'lib/quant/indicators/indicator.rb', line 87

def p(offset)
  raise ArgumentError, "offset must be a positive value" if offset < 0

  index = offset + 1
  values[[-index, -size].max]
end

#points_classObject



79
80
81
# File 'lib/quant/indicators/indicator.rb', line 79

def points_class
  Object.const_get "Quant::Indicators::#{indicator_name}Point"
end

#sizeObject



40
41
42
# File 'lib/quant/indicators/indicator.rb', line 40

def size
  @points.size
end

#t(offset) ⇒ Object

t(0) => ticks t(1) => ticks t(2) => ticks t(3) => ticks

Raises:

  • (ArgumentError)


98
99
100
101
102
103
# File 'lib/quant/indicators/indicator.rb', line 98

def t(offset)
  raise ArgumentError, "offset must be a positive value" if offset < 0

  index = offset + 1
  ticks[[-index, -size].max]
end

#ticksObject



28
29
30
# File 'lib/quant/indicators/indicator.rb', line 28

def ticks
  @points.keys
end

#valuesObject



36
37
38
# File 'lib/quant/indicators/indicator.rb', line 36

def values
  @points.values
end