Class: Quant::IndicatorsProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/quant/indicators_proxy.rb

Overview

The IndicatorsProxy 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 IndicatorsProxy class.

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

Direct Known Subclasses

DominantCycleIndicators, Indicators

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(series:, source:) ⇒ IndicatorsProxy

Returns a new instance of IndicatorsProxy.



18
19
20
21
22
23
# File 'lib/quant/indicators_proxy.rb', line 18

def initialize(series:, source:)
  @series = series
  @source = source
  @indicators = {}
  @dominant_cycle = dominant_cycle_indicator
end

Instance Attribute Details

#dominant_cycleObject (readonly)

Returns the value of attribute dominant_cycle.



16
17
18
# File 'lib/quant/indicators_proxy.rb', line 16

def dominant_cycle
  @dominant_cycle
end

#indicatorsObject (readonly)

Returns the value of attribute indicators.



16
17
18
# File 'lib/quant/indicators_proxy.rb', line 16

def indicators
  @indicators
end

#seriesObject (readonly)

Returns the value of attribute series.



16
17
18
# File 'lib/quant/indicators_proxy.rb', line 16

def series
  @series
end

#sourceObject (readonly)

Returns the value of attribute source.



16
17
18
# File 'lib/quant/indicators_proxy.rb', line 16

def source
  @source
end

Instance Method Details

#<<(tick) ⇒ Object

Adds the tick to all active indicators, triggering them to compute new values against the latest tick.

NOTE: Dominant cycle indicators must be computed first as many indicators are adaptive and require the dominant cycle period. The IndicatorsProxy class is not responsible for enforcing this order of events.



47
48
49
50
# File 'lib/quant/indicators_proxy.rb', line 47

def <<(tick)
  dominant_cycle << tick
  indicators.each_value { |indicator| indicator << tick }
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.

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.



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

def attach(name:, indicator_class:)
  define_singleton_method(name) { indicator(indicator_class) }
end

#dominant_cycle_indicatorObject



25
26
27
28
29
30
31
# File 'lib/quant/indicators_proxy.rb', line 25

def dominant_cycle_indicator
  kind = Quant.config.indicators.dominant_cycle_kind.to_s
  base_class_name = kind.split("_").map(&:capitalize).join
  class_name = "Quant::Indicators::DominantCycles::#{base_class_name}"
  indicator_class = Object.const_get(class_name)
  indicator_class.new(series:, source:)
end

#indicator(indicator_class) ⇒ Object

Instantiates the indicator class and stores it in the indicators hash. Once prepared, the indicator becomes active and all ticks pushed into the series are sent to the indicator for processing.



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

def indicator(indicator_class)
  indicators[indicator_class] ||= indicator_class.new(series:, source:)
end