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.

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
# File 'lib/quant/indicators_proxy.rb', line 18

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

Instance Attribute Details

#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.



38
39
40
# File 'lib/quant/indicators_proxy.rb', line 38

def <<(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.



54
55
56
# File 'lib/quant/indicators_proxy.rb', line 54

def attach(name:, indicator_class:)
  define_singleton_method(name) { indicator(indicator_class) }
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.



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

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

#maObject



58
# File 'lib/quant/indicators_proxy.rb', line 58

def ma; indicator(Indicators::Ma) end

#pingObject



59
# File 'lib/quant/indicators_proxy.rb', line 59

def ping; indicator(Indicators::Ping) end