Class: Quant::IndicatorsSource

Inherits:
Object
  • Object
show all
Includes:
IndicatorsRegistry
Defined in:
lib/quant/indicators_source.rb

Overview

IndicatorSource holds a collection of Quant::Indicators::Indicator for a given input source. This class ensures dominant cycle computations come before other indicators that depend on them.

The IndicatorSource class is responsible for lazily loading indicators so that not all indicators are always engaged and computing their values. If the indicator is never accessed, it’s never computed, saving valuable processing CPU cycles.

Indicators are generally built around the concept of a source input value and that source is designated by the source parameter when instantiating the IndicatorSource class.

By design, the Quant::Indicators::Indicator class holds the Ticks::Tick instance alongside the indicator’s computed values for that tick.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from IndicatorsRegistry

#define_indicator_accessors

Constructor Details

#initialize(series:, source:) ⇒ IndicatorsSource

Returns a new instance of IndicatorsSource.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/quant/indicators_source.rb', line 23

def initialize(series:, source:)
  @series = series
  @source = source

  @indicators = {}
  @ordered_indicators = []
  define_indicator_accessors(indicator_source: self)

  @dominant_cycles = DominantCyclesSource.new(indicator_source: self)
  @pivots = PivotsSource.new(indicator_source: self)
end

Instance Attribute Details

#dominant_cyclesObject (readonly)

Returns the value of attribute dominant_cycles.



21
22
23
# File 'lib/quant/indicators_source.rb', line 21

def dominant_cycles
  @dominant_cycles
end

#pivotsObject (readonly)

Returns the value of attribute pivots.



21
22
23
# File 'lib/quant/indicators_source.rb', line 21

def pivots
  @pivots
end

#seriesObject (readonly)

Returns the value of attribute series.



21
22
23
# File 'lib/quant/indicators_source.rb', line 21

def series
  @series
end

#sourceObject (readonly)

Returns the value of attribute source.



21
22
23
# File 'lib/quant/indicators_source.rb', line 21

def source
  @source
end

Instance Method Details

#<<(tick) ⇒ Object



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

def <<(tick)
  @ordered_indicators.each { |indicator| indicator << tick }
end

#[](indicator_class) ⇒ Object



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

def [](indicator_class)
  indicator(indicator_class)
end

#attach(name:, indicator_class:) ⇒ Object

Attaches a given Indicator class and defines the method for accessing it using the given name. Indicators take care of computing their values when first attached to a populated series.

NOTE: You can also use the ‘register` method on the indicator class to accomplish the same thing. `attach` lets you inject a custom indicator at run-time when you have an instance of Quant::IndicatorsSource while the `register` method is used to define the indicator at load-time.

NOTE Calling ‘attach` also registers the indicator with the framework, so you only have to `attach` once.

The indicators shipped with the library are all wired into the framework, thus this method should be used for custom indicators not shipped with the library.

Examples:

series.indicators.oc2.attach(name: :foo, indicator_class: Indicators::Foo)

Parameters:

  • name (Symbol)

    The name of the method to define for accessing the indicator.

  • indicator_class (Class)

    The class of the indicator to attach.



63
64
65
66
# File 'lib/quant/indicators_source.rb', line 63

def attach(name:, indicator_class:)
  self.class.register(name:, indicator_class:)
  define_singleton_method(name) { indicator(indicator_class) }
end

#dominant_cycleObject



68
69
70
# File 'lib/quant/indicators_source.rb', line 68

def dominant_cycle
  indicator(dominant_cycle_indicator_class)
end