Class: Quant::Ticks::Tick

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

Overview

Tick is the abstract ancestor for all Ticks and holds the logic for interacting with series and indicators. The public interface is devoid of properties around price, volume, and timestamp, etc. Descendant classes are responsible for defining the properties and how they are represented.

The Tick class is designed to be immutable and is intended to be used as a value object. This means that once a Tick is created, it cannot be changed. This is important for the integrity of the series and indicators that depend on the ticks within the series.

When a tick is added to a series, it is locked into the series and ownership cannot be changed. This is important for the integrity of the series and indicators that depend on the ticks within the series. This is a key design to being able to being able to not only compute indicators on the ticks just once, but also avoid recomputing indicators when series are limited/sliced/filtered into subsets of the original series.

Ticks can be serialized to and from Ruby Hash, JSON strings, and CSV strings.

Direct Known Subclasses

OHLC, Serializers::Spot, Spot

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTick

Returns a new instance of Tick.



47
48
49
50
51
52
# File 'lib/quant/ticks/tick.rb', line 47

def initialize
  # Set the series by appending to the series or calling #assign_series method
  @series = nil
  @interval = nil
  @indicators = {}
end

Instance Attribute Details

#indicatorsObject (readonly)

Returns the value of attribute indicators.



45
46
47
# File 'lib/quant/ticks/tick.rb', line 45

def indicators
  @indicators
end

#seriesObject (readonly)

Returns the value of attribute series.



45
46
47
# File 'lib/quant/ticks/tick.rb', line 45

def series
  @series
end

Class Method Details

.default_serializer_classObject

Note:

internal use only.

Reflects the serializer class from the tick’s class name.



117
118
119
# File 'lib/quant/ticks/tick.rb', line 117

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.

Examples:

hash = { "timestamp" => "2018-01-01 12:00:00 UTC", "price" => 100.0, "volume" => 1000 }
Quant::Ticks::Tick.from(hash)
# => #<Quant::Ticks::Spot:0x00007f9e3b8b3e08 @timestamp=2018-01-01 12:00:00 UTC, @price=100.0, @volume=1000>

Parameters:

  • hash (Hash)
  • serializer_class (Class) (defaults to: nil)

    The serializer class to use for the conversion.

Returns:



28
29
30
31
# File 'lib/quant/ticks/tick.rb', line 28

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.

Examples:

json = "{\"timestamp\":\"2018-01-01 12:00:00 UTC\",\"price\":100.0,\"volume\":1000}"
Quant::Ticks::Tick.from_json(json)
# => #<Quant::Ticks::Spot:0x00007f9e3b8b3e08 @timestamp=2018-01-01 12:00:00 UTC, @price=100.0, @volume=1000>

Parameters:

  • json (String)
  • serializer_class (Class) (defaults to: default_serializer_class)

    The serializer class to use for the conversion.

Returns:



41
42
43
# File 'lib/quant/ticks/tick.rb', line 41

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



61
62
63
64
# File 'lib/quant/ticks/tick.rb', line 61

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.



77
78
79
80
81
# File 'lib/quant/ticks/tick.rb', line 77

def assign_series!(new_series)
  @series = new_series
  @interval ||= new_series.interval
  self
end

#default_serializer_classObject

Note:

internal use only.

Reflects the serializer class from the tick’s class name.



123
124
125
# File 'lib/quant/ticks/tick.rb', line 123

def default_serializer_class
  self.class.default_serializer_class
end

#intervalObject



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

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.

Returns:

  • (Boolean)


68
69
70
# File 'lib/quant/ticks/tick.rb', line 68

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.

Examples:

tick.to_csv(headers: true)
# => "timestamp,price,volume\n2018-01-01 12:00:00 UTC,100.0,1000\n"

Parameters:

  • serializer_class (Class) (defaults to: default_serializer_class)

    the serializer class to use for the conversion.



111
112
113
# File 'lib/quant/ticks/tick.rb', line 111

def to_csv(serializer_class: default_serializer_class, headers: false)
  serializer_class.to_csv(self, headers: 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.

Examples:

tick.to_h
# => { timestamp: "2018-01-01 12:00:00 UTC", price: 100.0, volume: 1000 }

Parameters:

  • serializer_class (Class) (defaults to: default_serializer_class)

    the serializer class to use for the conversion.



89
90
91
# File 'lib/quant/ticks/tick.rb', line 89

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.

Examples:

tick.to_json
# => "{\"timestamp\":\"2018-01-01 12:00:00 UTC\",\"price\":100.0,\"volume\":1000}"

Parameters:

  • serializer_class (Class) (defaults to: default_serializer_class)

    the serializer class to use for the conversion.



99
100
101
# File 'lib/quant/ticks/tick.rb', line 99

def to_json(serializer_class: default_serializer_class)
  serializer_class.to_json(self)
end