Class: Quant::Ticks::Serializers::Tick Abstract

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

Overview

This class is abstract.

The Tick class serializes and deserializes Tick objects to and from various formats. These classes are wired into the Tick classes and are used to convert Tick objects to and from Ruby hashes, JSON strings, and CSV strings. They’re not typically used directly, but are extracted to make it easier to provide custom serialization and deserialization for Tick objects not shipped with the library.

Class Method Summary collapse

Class Method Details

.from(hash, tick_class:) ⇒ Quant::Ticks::Tick

Returns a Quant::Ticks::Tick from a Ruby Hash.

Examples:

hash = { "ct" => 2024-01-15 03:12:23 UTC", "cp" => 5.0, "iv" => "1d", "bv" => 0.0, "tv" => 0.0, "t" => 0}
Quant::Ticks::Serializers::Tick.from(hash, tick_class: Quant::Ticks::Spot)

Parameters:

Returns:

Raises:

  • (NotImplementedError)


59
60
61
# File 'lib/quant/ticks/serializers/tick.rb', line 59

def self.from(hash, tick_class:)
  raise NotImplementedError
end

.from_json(json, tick_class:) ⇒ Quant::Ticks::Tick

Returns a Quant::Ticks::Tick from a valid JSON String.

Examples:

json = "{\"ct\":\"2024-01-15 03:12:23 UTC\", \"cp\":5.0, \"iv\":\"1d\", \"bv\":0.0, \"tv\":0.0, \"t\":0}"
Quant::Ticks::Serializers::Tick.from_json(json, tick_class: Quant::Ticks::Spot)

Parameters:

Returns:



70
71
72
73
# File 'lib/quant/ticks/serializers/tick.rb', line 70

def self.from_json(json, tick_class:)
  hash = Oj.load(json)
  from(hash, tick_class:)
end

.to_csv(tick, headers: false) ⇒ String

Returns a String that is a valid CSV row

Examples:

Quant::Ticks::Serializers::Tick.to_csv(tick)
# => "1d,9999,5.0\n"
Quant::Ticks::Serializers::Tick.to_csv(tick, headers: true)
# => "iv,ct,cp\n1d,9999,5.0\n"

Parameters:

Returns:

  • (String)


23
24
25
26
27
28
29
30
# File 'lib/quant/ticks/serializers/tick.rb', line 23

def self.to_csv(tick, headers: false)
  hash = to_h(tick)
  row = CSV::Row.new(hash.keys, hash.values)
  return row.to_csv unless headers

  header_row = CSV::Row.new(hash.keys, hash.keys)
  header_row.to_csv << row.to_csv
end

.to_h(instance) ⇒ Hash

Returns a Ruby Hash comprised of the key properties for the tick.

Examples:

Quant::Ticks::Serializers::Tick.to_h(tick)
# => { "iv" => "1d", "ct" => 9999, "cp" => 5.0 }

Parameters:

Returns:

  • (Hash)

Raises:

  • (NotImplementedError)


48
49
50
# File 'lib/quant/ticks/serializers/tick.rb', line 48

def self.to_h(instance)
  raise NotImplementedError
end

.to_json(tick) ⇒ String

Returns a String that is a valid JSON representation of the tick’s key properties.

Examples:

Quant::Ticks::Serializers::Tick.to_json(tick)
# => "{\"iv\":\"1d\",\"ct\":9999,\"cp\":5.0}"

Parameters:

Returns:

  • (String)


38
39
40
# File 'lib/quant/ticks/serializers/tick.rb', line 38

def self.to_json(tick)
  Oj.dump to_h(tick)
end