Class: Quant::Ticks::Tick
- Inherits:
-
Object
- Object
- Quant::Ticks::Tick
- Defined in:
- lib/quant/ticks/tick.rb
Overview
Ticks can be serialized to and from Ruby Hash, JSON strings, and CSV strings.
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.
Class Method Summary collapse
-
.default_serializer_class ⇒ Object
Reflects the serializer class from the tick’s class name.
-
.from(hash, serializer_class: nil) ⇒ Quant::Ticks::Tick
Returns a Tick from a Ruby
Hash
. -
.from_json(json, serializer_class: default_serializer_class) ⇒ Quant::Ticks::Tick
Returns a Tick from a JSON string.
Instance Method Summary collapse
-
#assign_series(new_series) ⇒ Object
Ticks always belong to the first series they’re assigned so we can easily spin off sub-sets or new series with the same ticks while allowing each series to have its own state and full control over the ticks within its series.
-
#assign_series!(new_series) ⇒ Object
Ticks always belong to the first series they’re assigned so we can easily spin off sub-sets or new series with the same ticks.
-
#default_serializer_class ⇒ Object
Reflects the serializer class from the tick’s class name.
-
#initialize ⇒ Tick
constructor
A new instance of Tick.
- #interval ⇒ Object
-
#series? ⇒ Boolean
Returns true if the tick is assigned to a series.
-
#to_csv(serializer_class: default_serializer_class, headers: false) ⇒ Object
Returns a CSV row as a String for the Tick.
-
#to_h(serializer_class: default_serializer_class) ⇒ Object
Returns a Ruby hash for the Tick.
-
#to_json(serializer_class: default_serializer_class) ⇒ Object
Returns a JSON string for the Tick.
Constructor Details
#initialize ⇒ Tick
Returns a new instance of Tick.
55 56 57 58 59 60 |
# File 'lib/quant/ticks/tick.rb', line 55 def initialize # Set the series by appending to the series or calling #assign_series method @series = nil @interval = nil @indicators = {} end |
Instance Attribute Details
#indicators ⇒ Object (readonly)
Returns the value of attribute indicators.
53 54 55 |
# File 'lib/quant/ticks/tick.rb', line 53 def indicators @indicators end |
#series ⇒ Object (readonly)
Returns the value of attribute series.
53 54 55 |
# File 'lib/quant/ticks/tick.rb', line 53 def series @series end |
Class Method Details
.default_serializer_class ⇒ Object
internal use only.
Reflects the serializer class from the tick’s class name.
128 129 130 |
# File 'lib/quant/ticks/tick.rb', line 128 def self.default_serializer_class Object.const_get "Quant::Ticks::Serializers::#{name.split("::").last}" end |
.from(hash, serializer_class: nil) ⇒ Quant::Ticks::Tick
Returns a Quant::Ticks::Tick from a Ruby Hash
. The default serializer is used to generate the Quant::Ticks::Tick.
35 36 37 38 |
# File 'lib/quant/ticks/tick.rb', line 35 def self.from(hash, serializer_class: nil) serializer_class ||= default_serializer_class serializer_class.from(hash, tick_class: self) end |
.from_json(json, serializer_class: default_serializer_class) ⇒ Quant::Ticks::Tick
Returns a Quant::Ticks::Tick from a JSON string. The default serializer is used to generate the Quant::Ticks::Tick.
49 50 51 |
# File 'lib/quant/ticks/tick.rb', line 49 def self.from_json(json, serializer_class: default_serializer_class) serializer_class.from_json(json, tick_class: self) end |
Instance Method Details
#assign_series(new_series) ⇒ Object
Ticks always belong to the first series they’re assigned so we can easily spin off sub-sets or new series with the same ticks while allowing each series to have its own state and full control over the ticks within its series
69 70 71 72 |
# File 'lib/quant/ticks/tick.rb', line 69 def assign_series(new_series) assign_series!(new_series) unless series? self end |
#assign_series!(new_series) ⇒ Object
Ticks always belong to the first series they’re assigned so we can easily spin off sub-sets or new series with the same ticks. However, if you need to reassign the series, you can use this method to force the change of series ownership.
The series interval is also assigned to the tick if it is not already set.
85 86 87 88 89 |
# File 'lib/quant/ticks/tick.rb', line 85 def assign_series!(new_series) @series = new_series @interval ||= new_series.interval self end |
#default_serializer_class ⇒ Object
internal use only.
Reflects the serializer class from the tick’s class name.
134 135 136 |
# File 'lib/quant/ticks/tick.rb', line 134 def default_serializer_class self.class.default_serializer_class end |
#interval ⇒ Object
62 63 64 |
# File 'lib/quant/ticks/tick.rb', line 62 def interval @series&.interval || Interval[nil] end |
#series? ⇒ Boolean
Returns true if the tick is assigned to a series. The first series a tick is assigned to is the series against which the indicators compute.
76 77 78 |
# File 'lib/quant/ticks/tick.rb', line 76 def series? !!@series end |
#to_csv(serializer_class: default_serializer_class, headers: false) ⇒ Object
Returns a CSV row as a String for the Tick. The default serializer is used to generate the CSV string. If headers is true, two lines returned separated by newline. The first line is the header row and the second line is the data row.
122 123 124 |
# File 'lib/quant/ticks/tick.rb', line 122 def to_csv(serializer_class: default_serializer_class, headers: false) serializer_class.to_csv(self, headers:) end |
#to_h(serializer_class: default_serializer_class) ⇒ Object
Returns a Ruby hash for the Tick. The default serializer is used to generate the hash.
98 99 100 |
# File 'lib/quant/ticks/tick.rb', line 98 def to_h(serializer_class: default_serializer_class) serializer_class.to_h(self) end |
#to_json(serializer_class: default_serializer_class) ⇒ Object
Returns a JSON string for the Tick. The default serializer is used to generate the JSON string.
109 110 111 |
# File 'lib/quant/ticks/tick.rb', line 109 def to_json(serializer_class: default_serializer_class) serializer_class.to_json(self) end |