Class: Quant::Series

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/quant/series.rb

Overview

Ticks belong to the first series they’re associated with always. There are no provisions for series merging their ticks to one series! Indicators will be computed against the parent series of a list of ticks, so we can safely work with subsets of a series and indicators will compute just once.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(symbol:, interval:) ⇒ Series

Returns a new instance of Series.



44
45
46
47
48
# File 'lib/quant/series.rb', line 44

def initialize(symbol:, interval:)
  @symbol = symbol
  @interval = interval
  @ticks = []
end

Instance Attribute Details

#intervalObject (readonly)

Returns the value of attribute interval.



42
43
44
# File 'lib/quant/series.rb', line 42

def interval
  @interval
end

#symbolObject (readonly)

Returns the value of attribute symbol.



42
43
44
# File 'lib/quant/series.rb', line 42

def symbol
  @symbol
end

#ticksObject (readonly)

Returns the value of attribute ticks.



42
43
44
# File 'lib/quant/series.rb', line 42

def ticks
  @ticks
end

Class Method Details

.from_file(filename:, symbol:, interval:, folder: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/quant/series.rb', line 12

def self.from_file(filename:, symbol:, interval:, folder: nil)
  symbol = symbol.to_s.upcase
  interval = Interval[interval]

  filename = Rails.root.join("historical", folder, "#{symbol.upcase}.txt") if filename.nil?
  raise "File #{filename} does not exist" unless File.exist?(filename)

  lines = File.read(filename).split("\n")
  ticks = lines.map{ |line| Quant::Ticks::OHLC.from_json(line) }

  from_ticks(symbol: symbol, interval: interval, ticks: ticks)
end

.from_hash(symbol:, interval:, hash:) ⇒ Object



29
30
31
32
# File 'lib/quant/series.rb', line 29

def self.from_hash(symbol:, interval:, hash:)
  ticks = hash.map { |tick_hash| Quant::Ticks::OHLC.from(tick_hash) }
  from_ticks(symbol: symbol, interval: interval, ticks: ticks)
end

.from_json(symbol:, interval:, json:) ⇒ Object



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

def self.from_json(symbol:, interval:, json:)
  from_hash symbol: symbol, interval: interval, hash: Oj.load(json)
end

.from_ticks(symbol:, interval:, ticks:) ⇒ Object



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

def self.from_ticks(symbol:, interval:, ticks:)
  ticks = ticks.sort_by(&:close_timestamp)

  new(symbol: symbol, interval: interval).tap do |series|
    ticks.each { |tick| series << tick }
  end
end

Instance Method Details

#<<(tick) ⇒ Object



94
95
96
# File 'lib/quant/series.rb', line 94

def <<(tick)
  @ticks << tick.assign_series(self)
end

#==(other) ⇒ Object



82
83
84
# File 'lib/quant/series.rb', line 82

def ==(other)
  [symbol, interval, ticks] == [other.symbol, other.interval, other.ticks]
end

#dupObject



86
87
88
# File 'lib/quant/series.rb', line 86

def dup
  self.class.from_ticks(symbol: symbol, interval: interval, ticks: ticks)
end

#highestObject



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

def highest
  ticks.max_by(&:high_price)
end

#inspectObject



90
91
92
# File 'lib/quant/series.rb', line 90

def inspect
  "#<#{self.class.name} symbol=#{symbol} interval=#{interval} ticks=#{ticks.size}>"
end

#limit(period) ⇒ Object



57
58
59
60
61
62
# File 'lib/quant/series.rb', line 57

def limit(period)
  selected_ticks = ticks.select{ |tick| period.cover?(tick.close_timestamp) }
  return self if selected_ticks.size == ticks.size

  self.class.from_ticks(symbol: symbol, interval: interval, ticks: selected_ticks)
end

#limit_iterations(start_iteration, stop_iteration) ⇒ Object



50
51
52
53
54
55
# File 'lib/quant/series.rb', line 50

def limit_iterations(start_iteration, stop_iteration)
  selected_ticks = ticks[start_iteration..stop_iteration]
  return self if selected_ticks.size == ticks.size

  self.class.from_ticks(symbol: symbol, interval: interval, ticks: selected_ticks)
end

#lowestObject



78
79
80
# File 'lib/quant/series.rb', line 78

def lowest
  ticks.min_by(&:low_price)
end

#to_hObject



98
99
100
101
102
# File 'lib/quant/series.rb', line 98

def to_h
  { "symbol" => symbol,
    "interval" => interval,
    "ticks" => ticks.map(&:to_h) }
end

#to_json(*args) ⇒ Object



104
105
106
# File 'lib/quant/series.rb', line 104

def to_json(*args)
  Oj.dump(to_h, *args)
end