Class: Quant::Ticks::Spot

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

Overview

A Spot is a single price point in time. It is the most basic form of a Tick and is usually used to represent a continuously streaming tick that just has a single price point at a given point in time.

Examples:

spot = Quant::Ticks::Spot.new(price: 100.0, timestamp: Time.now)
spot.price # => 100.0
spot.timestamp # => 2018-01-01 12:00:00 UTC
spot = Quant::Ticks::Spot.from({ "p" => 100.0, "t" => "2018-01-01 12:00:00 UTC", "bv" => 1000 })
spot.price # => 100.0
spot.timestamp # => 2018-01-01 12:00:00 UTC
spot.volume # => 1000

Constant Summary

Constants included from Quant::TimeMethods

Quant::TimeMethods::EPOCH_DATE, Quant::TimeMethods::EPOCH_TIME

Instance Attribute Summary collapse

Attributes inherited from Tick

#indicators

Instance Method Summary collapse

Methods included from Quant::TimeMethods

epoch_date, epoch_time, #extract_time

Methods inherited from Tick

#assign_series, #assign_series!, #default_serializer_class, default_serializer_class, from, from_json, #interval, #series?, #to_csv, #to_h, #to_json

Constructor Details

#initialize(price: nil, timestamp: nil, close_price: nil, close_timestamp: nil, volume: nil, base_volume: nil, target_volume: nil, trades: nil) ⇒ Spot

Returns a new instance of Spot.

Raises:

  • (ArgumentError)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/quant/ticks/spot.rb', line 25

def initialize(
  price: nil,
  timestamp: nil,
  close_price: nil,
  close_timestamp: nil,
  volume: nil,
  base_volume: nil,
  target_volume: nil,
  trades: nil
)
  raise ArgumentError, "Must supply a spot price as either :price or :close_price" unless price || close_price

  @close_price = (close_price || price).to_f

  @close_timestamp = extract_time(timestamp || close_timestamp || Quant.current_time)
  @open_timestamp = @close_timestamp

  @base_volume = (volume || base_volume).to_i
  @target_volume = (target_volume || @base_volume).to_i

  @trades = trades.to_i
  super()
end

Instance Attribute Details

#base_volumeObject (readonly) Also known as: volume

Returns the value of attribute base_volume.



23
24
25
# File 'lib/quant/ticks/spot.rb', line 23

def base_volume
  @base_volume
end

#close_priceObject (readonly) Also known as: price, high_price, low_price, open_price, oc2, hl2, hlc3, ohlc4, delta

Returns the value of attribute close_price.



22
23
24
# File 'lib/quant/ticks/spot.rb', line 22

def close_price
  @close_price
end

#close_timestampObject (readonly) Also known as: timestamp

Returns the value of attribute close_timestamp.



21
22
23
# File 'lib/quant/ticks/spot.rb', line 21

def close_timestamp
  @close_timestamp
end

#open_timestampObject (readonly)

Returns the value of attribute open_timestamp.



21
22
23
# File 'lib/quant/ticks/spot.rb', line 21

def open_timestamp
  @open_timestamp
end

#seriesObject (readonly)

Returns the value of attribute series.



20
21
22
# File 'lib/quant/ticks/spot.rb', line 20

def series
  @series
end

#target_volumeObject (readonly)

Returns the value of attribute target_volume.



23
24
25
# File 'lib/quant/ticks/spot.rb', line 23

def target_volume
  @target_volume
end

#tradesObject (readonly)

Returns the value of attribute trades.



23
24
25
# File 'lib/quant/ticks/spot.rb', line 23

def trades
  @trades
end

Instance Method Details

#==(other) ⇒ Object

Two ticks are equal if they have the same close price and close timestamp.



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

def ==(other)
  [close_price, close_timestamp] == [other.close_price, other.close_timestamp]
end

#corresponding?(other) ⇒ Boolean

The corresponding? method helps determine that the other tick’s timestamp is the same as this tick’s timestamp, which is useful when aligning ticks between two separate series where one starts or ends at a different time, or when there may be gaps in the data between the two series.

Returns:

  • (Boolean)


69
70
71
# File 'lib/quant/ticks/spot.rb', line 69

def corresponding?(other)
  close_timestamp == other.close_timestamp
end

#inspectObject



73
74
75
# File 'lib/quant/ticks/spot.rb', line 73

def inspect
  "#<#{self.class.name} ct=#{close_timestamp} c=#{close_price.to_f} v=#{volume}>"
end