Class: Finnhub::Timeseries

Inherits:
Object
  • Object
show all
Defined in:
lib/Timeseries.rb

Direct Known Subclasses

Crypto_Timeseries, Forex_Timeseries

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, stock:, resolution: "D", count: 100, from: nil, to: nil, format: nil, adjusted: false) ⇒ Timeseries

Returns a new instance of Timeseries.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/Timeseries.rb', line 3

def initialize(client:, stock:, resolution: "D", count: 100,
  from: nil, to: nil, format: nil, adjusted: false)
  url = "/stock/candle?symbol=#{stock.symbol}&resolution=#{resolution}"
  url += "&count=#{count}" unless count.nil?
  from = from.to_i if from.is_a?(Time)
  url += "&from=#{from}" unless from.nil?
  to = to.to_i if to.is_a?(Time)
  url += "&to=#{to}" unless to.nil?
  url += "&format=#{format}" unless format.nil?
  url += "&adjusted=#{adjusted}" if adjusted
  @output = client.request(url)
  if @output.is_a?(Hash) && @output[:s] == "ok"
    @timestamps = @output[:t]&.map{|t| Time.strptime(t.to_s,'%s')}
  else
    @timestamps = []
  end
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



21
22
23
# File 'lib/Timeseries.rb', line 21

def output
  @output
end

#timestampsObject (readonly)

Returns the value of attribute timestamps.



21
22
23
# File 'lib/Timeseries.rb', line 21

def timestamps
  @timestamps
end

Instance Method Details

#closeObject



35
36
37
# File 'lib/Timeseries.rb', line 35

def close
  operation(:c)
end

#highObject



27
28
29
# File 'lib/Timeseries.rb', line 27

def high
  operation(:h)
end

#lowObject



31
32
33
# File 'lib/Timeseries.rb', line 31

def low
  operation(:l)
end

#openObject



23
24
25
# File 'lib/Timeseries.rb', line 23

def open
  operation(:o)
end

#statusObject



43
44
45
46
# File 'lib/Timeseries.rb', line 43

def status
  raise Finnhub::Error message: "Output is not a hash" unless @output.is_a?(Hash)
  @output[:s]
end

#volumeObject



39
40
41
# File 'lib/Timeseries.rb', line 39

def volume
  operation(:v)
end