Class: Quant::Ticks::Serializers::Spot

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

Instance Attribute Summary

Attributes inherited from Tick

#indicators, #series

Class Method Summary collapse

Methods inherited from Tick

#assign_series, #assign_series!, default_serializer_class, #default_serializer_class, #initialize, #interval, #series?, #to_csv, #to_h, #to_json

Constructor Details

This class inherits a constructor from Quant::Ticks::Tick

Class Method Details

.from(hash, tick_class:) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/quant/ticks/serializers/spot.rb', line 42

def self.from(hash, tick_class:)
  tick_class.new(
    close_timestamp: hash["ct"],
    close_price: hash["cp"],
    base_volume: hash["bv"],
    target_volume: hash["tv"],
    trades: hash["t"]
  )
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:



14
15
16
17
# File 'lib/quant/ticks/serializers/spot.rb', line 14

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

.to_h(tick) ⇒ Hash

Returns a Hash of the Spot tick’s key properties

Serialized Keys:

  • ct: close timestamp

  • cp: close price

  • bv: base volume

  • tv: target volume

  • t: trades

Examples:

Quant::Ticks::Serializers::Tick.to_h(tick)
# => { "ct" => "2024-02-13 03:12:23 UTC", "cp" => 5.0, "bv" => 0.0, "tv" => 0.0, "t" => 0 }

Parameters:

Returns:

  • (Hash)


34
35
36
37
38
39
40
# File 'lib/quant/ticks/serializers/spot.rb', line 34

def self.to_h(tick)
  { "ct" => tick.close_timestamp,
    "cp" => tick.close_price,
    "bv" => tick.base_volume,
    "tv" => tick.target_volume,
    "t" => tick.trades }
end