Class: Quant::Indicators::Indicator

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Mixins::Filters, Mixins::Functions, Mixins::MovingAverages
Defined in:
lib/quant/indicators/indicator.rb

Direct Known Subclasses

Ma, Ping

Constant Summary

Constants included from Mixins::UniversalFilters

Mixins::UniversalFilters::K

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixins::ExponentialMovingAverage

#exponential_moving_average

Methods included from Mixins::SimpleMovingAverage

#simple_moving_average

Methods included from Mixins::WeightedMovingAverage

#extended_weighted_moving_average, #weighted_moving_average

Methods included from Mixins::UniversalFilters

#universal_band_pass, #universal_ema, #universal_filter, #universal_one_pole_high_pass, #universal_one_pole_low_pass, #universal_two_pole_high_pass, #universal_two_pole_low_pass

Methods included from Mixins::ButterworthFilters

#three_pole_butterworth, #two_pole_butterworth

Methods included from Mixins::HighPassFilters

#high_pass_filter, #two_pole_high_pass_filter

Methods included from Mixins::Functions

#angle, #bars_to_alpha, #deg2rad, #period_to_alpha, #rad2deg

Constructor Details

#initialize(series:, source:) ⇒ Indicator

Returns a new instance of Indicator.



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

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.



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

def p0
  @p0
end

#p1Object (readonly)

Returns the value of attribute p1.



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

def p1
  @p1
end

#p2Object (readonly)

Returns the value of attribute p2.



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

def p2
  @p2
end

#p3Object (readonly)

Returns the value of attribute p3.



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

def p3
  @p3
end

#seriesObject (readonly)

include Mixins::HilbertTransform include Mixins::SuperSmoother include Mixins::Stochastic include Mixins::FisherTransform include Mixins::HighPassFilter include Mixins::Direction include Mixins::Filters



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

def series
  @series
end

#sourceObject (readonly)

include Mixins::HilbertTransform include Mixins::SuperSmoother include Mixins::Stochastic include Mixins::FisherTransform include Mixins::HighPassFilter include Mixins::Direction include Mixins::Filters



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

def source
  @source
end

#t0Object (readonly)

Returns the value of attribute t0.



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

def t0
  @t0
end

#t1Object (readonly)

Returns the value of attribute t1.



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

def t1
  @t1
end

#t2Object (readonly)

Returns the value of attribute t2.



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

def t2
  @t2
end

#t3Object (readonly)

Returns the value of attribute t3.



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

def t3
  @t3
end

Instance Method Details

#<<(tick) ⇒ Object



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

def <<(tick)
  @t0 = tick
  @p0 = points_class.new(tick:, 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



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

def [](index)
  values[index]
end

#computeObject

Raises:

  • (NotImplementedError)


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

def compute
  raise NotImplementedError
end

#each(&block) ⇒ Object



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

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

#indicator_nameObject



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

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)


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

def input
  t0.send(source)
end

#inspectObject



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

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

#p(offset) ⇒ Object

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

Raises:

  • (ArgumentError)


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

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

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

#points_classObject



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

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

#sizeObject



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

def size
  @points.size
end

#t(offset) ⇒ Object

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

Raises:

  • (ArgumentError)


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

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

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

#ticksObject



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

def ticks
  @points.keys
end

#valuesObject



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

def values
  @points.values
end