Class: Quant::Ticks::Value

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

Overview

Value ticks are the most basic ticks and are used to represent a single price point (no open, high, low, close, etc.) and a single timestamp. Usually, these are best used in streaming data where ticks are flowing in every second or whatever interval that’s appropriate for the data source. Often indicators and charts still want a universal public interface (i.e. open_price, high_price, volume, etc.), so we add those methods here and inherit and redefine upstream as appropriate.

For Value ticks:

  • The price given is set for all *_price fields.

  • The volume is set for both base and target volume.

  • The timestamp is set for both open and close timestamps.

Direct Known Subclasses

OHLC, Spot

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

Constructor Details

#initialize(price:, timestamp: Quant.current_time, interval: nil, volume: 0, trades: 0) ⇒ Value

Returns a new instance of Value.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/quant/ticks/value.rb', line 24

def initialize(price:, timestamp: Quant.current_time, interval: nil, volume: 0, trades: 0)
  @interval = Interval[interval]

  @close_timestamp = extract_time(timestamp)
  @open_timestamp = @close_timestamp

  @close_price = price.to_f
  @open_price = close_price
  @high_price = close_price
  @low_price = close_price

  @base_volume = volume.to_i
  @target_volume = volume.to_i
  @trades = trades.to_i

  # Set the series by appending to the series
  @series = nil
end

Instance Attribute Details

#base_volumeObject (readonly) Also known as: volume

Returns the value of attribute base_volume.



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

def base_volume
  @base_volume
end

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

Returns the value of attribute close_price.



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

def close_price
  @close_price
end

#close_timestampObject (readonly)

Returns the value of attribute close_timestamp.



19
20
21
# File 'lib/quant/ticks/value.rb', line 19

def close_timestamp
  @close_timestamp
end

#dojiObject (readonly)

Returns the value of attribute doji.



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

def doji
  @doji
end

#greenObject (readonly)

Returns the value of attribute green.



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

def green
  @green
end

#high_priceObject (readonly)

Returns the value of attribute high_price.



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

def high_price
  @high_price
end

#intervalObject (readonly)

Returns the value of attribute interval.



18
19
20
# File 'lib/quant/ticks/value.rb', line 18

def interval
  @interval
end

#low_priceObject (readonly)

Returns the value of attribute low_price.



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

def low_price
  @low_price
end

#open_priceObject (readonly)

Returns the value of attribute open_price.



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

def open_price
  @open_price
end

#open_timestampObject (readonly)

Returns the value of attribute open_timestamp.



19
20
21
# File 'lib/quant/ticks/value.rb', line 19

def open_timestamp
  @open_timestamp
end

#seriesObject (readonly)

Returns the value of attribute series.



18
19
20
# File 'lib/quant/ticks/value.rb', line 18

def series
  @series
end

#target_volumeObject (readonly)

Returns the value of attribute target_volume.



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

def target_volume
  @target_volume
end

#tradesObject (readonly)

Returns the value of attribute trades.



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

def trades
  @trades
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  to_h == other.to_h
end

#assign_series(new_series) ⇒ Object

ticks are immutable across series so we can easily initialize 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
65
66
67
68
# File 'lib/quant/ticks/value.rb', line 61

def assign_series(new_series)
  assign_series!(new_series) if @series.nil?

  # dup.tap do |new_tick|
  #   # new_tick.instance_variable_set(:@series, new_series)
  #   new_tick.instance_variable_set(:@indicators, indicators)
  # end
end

#assign_series!(new_series) ⇒ Object



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

def assign_series!(new_series)
  @series = new_series
  self
end

#corresponding?(other) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/quant/ticks/value.rb', line 50

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

#inspectObject



75
76
77
# File 'lib/quant/ticks/value.rb', line 75

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

#to_hObject



79
80
81
# File 'lib/quant/ticks/value.rb', line 79

def to_h
  Quant::Ticks::Serializers::Value.to_h(self)
end

#to_json(*_args) ⇒ Object



83
84
85
# File 'lib/quant/ticks/value.rb', line 83

def to_json(*_args)
  Quant::Ticks::Serializers::Value.to_json(self)
end