Class: Quant::Indicators::Indicator
- Inherits:
-
Object
- Object
- Quant::Indicators::Indicator
- Includes:
- Enumerable, Mixins::Filters, Mixins::FisherTransform, Mixins::Functions, Mixins::HilbertTransform, Mixins::MovingAverages, Mixins::Stochastic, Mixins::SuperSmoother
- Defined in:
- lib/quant/indicators/indicator.rb
Overview
The Indicator class is the abstract ancestor for all Indicators.
Direct Known Subclasses
Adx, Atr, Cci, Decycler, DominantCycles::DominantCycle, Ema, Frama, Mama, Mesa, Ping, Pivots::Pivot, RocketRsi, Roofing, Rsi, Snr
Constant Summary collapse
- PRIORITIES =
The priority drives the order of computations when iterating over each tick in a series. Generally speaking, indicators that feed values to another indicator must have a lower priority value than the indicator that consumes the values.
* Most indicators will have a default priority of 1000. * Dominant Cycle indicators will have a priority of 100. * Some indicators will have a "high priority" of 500.
Priority values are arbitrary and purposefully gapping so that new indicators introduced outside the core library can be slotted in between.
NOTE: Priority is well-managed by the library and should not require overriding for a custom indicator developed outside the library. If you find yourself needing to override this method, please open an issue on the library’s GitHub page.
[ DOMINANT_CYCLES_PRIORITY = 100, DEPENDENCY_PRIORITY = 500, DEFAULT_PRIORITY = 1000 ].freeze
Constants included from Mixins::UniversalFilters
Instance Attribute Summary collapse
-
#p0 ⇒ Object
readonly
Returns the value of attribute p0.
-
#p1 ⇒ Object
readonly
Returns the value of attribute p1.
-
#p2 ⇒ Object
readonly
Returns the value of attribute p2.
-
#p3 ⇒ Object
readonly
Returns the value of attribute p3.
-
#series ⇒ Object
readonly
Returns the value of attribute series.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#t0 ⇒ Object
readonly
Returns the value of attribute t0.
-
#t1 ⇒ Object
readonly
Returns the value of attribute t1.
-
#t2 ⇒ Object
readonly
Returns the value of attribute t2.
-
#t3 ⇒ Object
readonly
Returns the value of attribute t3.
Class Method Summary collapse
-
.dependent_indicator_classes ⇒ Object
Provides a registry of dependent indicators for each indicator class.
-
.depends_on(*indicator_classes) ⇒ Object
Use the Indicator.depends_on method to declare dependencies for an indicator.
-
.register(name:) ⇒ Object
include Mixins::Direction.
Instance Method Summary collapse
- #<<(tick) ⇒ Object
- #[](index) ⇒ Object
- #adaptive_half_period ⇒ Object (also: #dc_half_period, #dominant_half_cycle_period)
-
#adaptive_period ⇒ Object
(also: #dc_period, #dominant_cycle_period)
The adaptive period is the full dominant cycle period.
- #compute ⇒ Object
- #dominant_cycle ⇒ Object
- #dominant_cycle_indicator_class ⇒ Object
- #dominant_cycle_kind ⇒ Object
- #each(&block) ⇒ Object
- #half_period ⇒ Object
- #indicator_name ⇒ Object
-
#initialize(series:, source:) ⇒ Indicator
constructor
A new instance of Indicator.
-
#input ⇒ Numeric
The input is the value derived from the source for the indicator for the current tick.
- #inspect ⇒ Object
- #max_period ⇒ Object
- #micro_period ⇒ Object
- #min_period ⇒ Object
- #p(offset) ⇒ Object
- #period_points(max_period) ⇒ Object
- #pivot_kind ⇒ Object
- #points_class ⇒ Object
- #priority ⇒ Object
- #size ⇒ Object
- #t(offset) ⇒ Object
- #ticks ⇒ Object
- #values ⇒ Object
- #warmed_up? ⇒ Boolean
Methods included from Mixins::FisherTransform
#fisher_transform, #inverse_fisher_transform, #relative_fisher_transform
Methods included from Mixins::Stochastic
Methods included from Mixins::SuperSmoother
#three_pole_super_smooth, #two_pole_super_smooth
Methods included from Mixins::HilbertTransform
Methods included from Mixins::ExponentialMovingAverage
Methods included from Mixins::SimpleMovingAverage
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, #hpf2, #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.
40 41 42 43 44 45 |
# File 'lib/quant/indicators/indicator.rb', line 40 def initialize(series:, source:) @series = series @source = source @points = {} series.each { |tick| self << tick } end |
Instance Attribute Details
#p0 ⇒ Object (readonly)
Returns the value of attribute p0.
135 136 137 |
# File 'lib/quant/indicators/indicator.rb', line 135 def p0 @p0 end |
#p1 ⇒ Object (readonly)
Returns the value of attribute p1.
135 136 137 |
# File 'lib/quant/indicators/indicator.rb', line 135 def p1 @p1 end |
#p2 ⇒ Object (readonly)
Returns the value of attribute p2.
135 136 137 |
# File 'lib/quant/indicators/indicator.rb', line 135 def p2 @p2 end |
#p3 ⇒ Object (readonly)
Returns the value of attribute p3.
135 136 137 |
# File 'lib/quant/indicators/indicator.rb', line 135 def p3 @p3 end |
#series ⇒ Object (readonly)
Returns the value of attribute series.
38 39 40 |
# File 'lib/quant/indicators/indicator.rb', line 38 def series @series end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
38 39 40 |
# File 'lib/quant/indicators/indicator.rb', line 38 def source @source end |
#t0 ⇒ Object (readonly)
Returns the value of attribute t0.
136 137 138 |
# File 'lib/quant/indicators/indicator.rb', line 136 def t0 @t0 end |
#t1 ⇒ Object (readonly)
Returns the value of attribute t1.
136 137 138 |
# File 'lib/quant/indicators/indicator.rb', line 136 def t1 @t1 end |
#t2 ⇒ Object (readonly)
Returns the value of attribute t2.
136 137 138 |
# File 'lib/quant/indicators/indicator.rb', line 136 def t2 @t2 end |
#t3 ⇒ Object (readonly)
Returns the value of attribute t3.
136 137 138 |
# File 'lib/quant/indicators/indicator.rb', line 136 def t3 @t3 end |
Class Method Details
.dependent_indicator_classes ⇒ Object
Provides a registry of dependent indicators for each indicator class. NOTE: Internal use only.
24 25 26 |
# File 'lib/quant/indicators/indicator.rb', line 24 def self.dependent_indicator_classes @dependent_indicator_classes ||= Set.new end |
.depends_on(*indicator_classes) ⇒ Object
Use the depends_on method to declare dependencies for an indicator.
34 35 36 |
# File 'lib/quant/indicators/indicator.rb', line 34 def self.depends_on(*indicator_classes) Array(indicator_classes).each{ |dependency| dependent_indicator_classes << dependency } end |
.register(name:) ⇒ Object
include Mixins::Direction
18 19 20 |
# File 'lib/quant/indicators/indicator.rb', line 18 def self.register(name:) Quant::IndicatorsSource.register(name:, indicator_class: self) end |
Instance Method Details
#<<(tick) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/quant/indicators/indicator.rb', line 138 def <<(tick) @t0 = tick @p0 = points_class.new(indicator: self, 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
118 119 120 |
# File 'lib/quant/indicators/indicator.rb', line 118 def [](index) values[index] end |
#adaptive_half_period ⇒ Object Also known as: dc_half_period, dominant_half_cycle_period
108 109 110 |
# File 'lib/quant/indicators/indicator.rb', line 108 def adaptive_half_period adaptive_period / 2 end |
#adaptive_period ⇒ Object Also known as: dc_period, dominant_cycle_period
The adaptive period is the full dominant cycle period
102 103 104 |
# File 'lib/quant/indicators/indicator.rb', line 102 def adaptive_period dominant_cycle.points[t0].period end |
#compute ⇒ Object
162 163 164 |
# File 'lib/quant/indicators/indicator.rb', line 162 def compute raise NotImplementedError end |
#dominant_cycle ⇒ Object
97 98 99 |
# File 'lib/quant/indicators/indicator.rb', line 97 def dominant_cycle series.indicators[source][dominant_cycle_indicator_class] end |
#dominant_cycle_indicator_class ⇒ Object
47 48 49 |
# File 'lib/quant/indicators/indicator.rb', line 47 def dominant_cycle_indicator_class Quant.config.indicators.dominant_cycle_indicator_class end |
#dominant_cycle_kind ⇒ Object
89 90 91 |
# File 'lib/quant/indicators/indicator.rb', line 89 def dominant_cycle_kind Quant.config.indicators.dominant_cycle_kind end |
#each(&block) ⇒ Object
154 155 156 |
# File 'lib/quant/indicators/indicator.rb', line 154 def each(&block) @points.each_value(&block) end |
#half_period ⇒ Object
81 82 83 |
# File 'lib/quant/indicators/indicator.rb', line 81 def half_period Quant.config.indicators.half_period end |
#indicator_name ⇒ Object
166 167 168 |
# File 'lib/quant/indicators/indicator.rb', line 166 def indicator_name self.class.name.split("::").last end |
#input ⇒ Numeric
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
201 202 203 |
# File 'lib/quant/indicators/indicator.rb', line 201 def input t0.send(source) end |
#inspect ⇒ Object
158 159 160 |
# File 'lib/quant/indicators/indicator.rb', line 158 def inspect "#<#{self.class.name} symbol=#{series.symbol} source=#{source} ticks=#{ticks.size}>" end |
#max_period ⇒ Object
77 78 79 |
# File 'lib/quant/indicators/indicator.rb', line 77 def max_period Quant.config.indicators.max_period end |
#micro_period ⇒ Object
85 86 87 |
# File 'lib/quant/indicators/indicator.rb', line 85 def micro_period Quant.config.indicators.micro_period end |
#min_period ⇒ Object
73 74 75 |
# File 'lib/quant/indicators/indicator.rb', line 73 def min_period Quant.config.indicators.min_period end |
#p(offset) ⇒ Object
178 179 180 181 182 183 |
# File 'lib/quant/indicators/indicator.rb', line 178 def p(offset) raise ArgumentError, "offset must be a positive value" if offset < 0 index = offset + 1 values[[-index, -size].max] end |
#period_points(max_period) ⇒ Object
130 131 132 133 |
# File 'lib/quant/indicators/indicator.rb', line 130 def period_points(max_period) extent = [values.size, max_period].min values[-extent, extent] end |
#pivot_kind ⇒ Object
93 94 95 |
# File 'lib/quant/indicators/indicator.rb', line 93 def pivot_kind Quant.config.indicators.pivot_kind end |
#points_class ⇒ Object
170 171 172 |
# File 'lib/quant/indicators/indicator.rb', line 170 def points_class Object.const_get "Quant::Indicators::#{indicator_name}Point" end |
#priority ⇒ Object
69 70 71 |
# File 'lib/quant/indicators/indicator.rb', line 69 def priority DEFAULT_PRIORITY end |
#size ⇒ Object
126 127 128 |
# File 'lib/quant/indicators/indicator.rb', line 126 def size @points.size end |
#t(offset) ⇒ Object
189 190 191 192 193 194 |
# File 'lib/quant/indicators/indicator.rb', line 189 def t(offset) raise ArgumentError, "offset must be a positive value" if offset < 0 index = offset + 1 ticks[[-index, -size].max] end |
#ticks ⇒ Object
114 115 116 |
# File 'lib/quant/indicators/indicator.rb', line 114 def ticks @points.keys end |
#values ⇒ Object
122 123 124 |
# File 'lib/quant/indicators/indicator.rb', line 122 def values @points.values end |
#warmed_up? ⇒ Boolean
205 206 207 |
# File 'lib/quant/indicators/indicator.rb', line 205 def warmed_up? ticks.size > min_period end |