Class: Quant::IndicatorsProxy
- Inherits:
-
Object
- Object
- Quant::IndicatorsProxy
- 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
Instance Attribute Summary collapse
-
#indicators ⇒ Object
readonly
Returns the value of attribute indicators.
-
#series ⇒ Object
readonly
Returns the value of attribute series.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Instance Method Summary collapse
-
#<<(tick) ⇒ Object
Adds the tick to all active indicators, triggering them to compute new values against the latest tick.
-
#attach(name:, indicator_class:) ⇒ Object
Attaches a given Indicator class and defines the method for accessing it using the given name.
-
#indicator(indicator_class) ⇒ Object
Instantiates the indicator class and stores it in the indicators hash.
-
#initialize(series:, source:) ⇒ IndicatorsProxy
constructor
A new instance of IndicatorsProxy.
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
#indicators ⇒ Object (readonly)
Returns the value of attribute indicators.
16 17 18 |
# File 'lib/quant/indicators_proxy.rb', line 16 def indicators @indicators end |
#series ⇒ Object (readonly)
Returns the value of attribute series.
16 17 18 |
# File 'lib/quant/indicators_proxy.rb', line 16 def series @series end |
#source ⇒ Object (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.
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:, source:) end |