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

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, #to_csv, #to_h, #to_json

Constructor Details

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

Returns a new instance of Spot.

Raises:

  • (ArgumentError)


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

def initialize(
  price: nil,
  timestamp: nil,
  close_price: nil,
  close_timestamp: nil,
  volume: nil,
  interval: 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

  @interval = Interval[interval]

  @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.



25
26
27
# File 'lib/quant/ticks/spot.rb', line 25

def base_volume
  @base_volume
end

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

Returns the value of attribute close_price.



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

def close_price
  @close_price
end

#close_timestampObject (readonly) Also known as: timestamp

Returns the value of attribute close_timestamp.



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

def close_timestamp
  @close_timestamp
end

#high_priceObject (readonly)

Returns the value of attribute high_price.



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

def high_price
  @high_price
end

#intervalObject (readonly)

Returns the value of attribute interval.



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

def interval
  @interval
end

#low_priceObject (readonly)

Returns the value of attribute low_price.



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

def low_price
  @low_price
end

#open_priceObject (readonly)

Returns the value of attribute open_price.



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

def open_price
  @open_price
end

#open_timestampObject (readonly)

Returns the value of attribute open_timestamp.



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

def open_timestamp
  @open_timestamp
end

#seriesObject (readonly)

Returns the value of attribute series.



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

def series
  @series
end

#target_volumeObject (readonly)

Returns the value of attribute target_volume.



25
26
27
# File 'lib/quant/ticks/spot.rb', line 25

def target_volume
  @target_volume
end

#tradesObject (readonly)

Returns the value of attribute trades.



25
26
27
# File 'lib/quant/ticks/spot.rb', line 25

def trades
  @trades
end

Instance Method Details

#==(other) ⇒ Object



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

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)


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

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

#inspectObject



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

def inspect
  "#<#{self.class.name} cp=#{close_price.to_f} ct=#{close_timestamp.strftime("%Y-%m-%d")} iv=#{interval.to_s} v=#{volume}>"
end