class Finnhub::Tick

Attributes

output[R]
timestamps[R]

Public Class Methods

new(client:, stock:, from:, to:) click to toggle source
# File lib/Tick.rb, line 3
def initialize(client:, stock:, from:, to:)
  url = "/stock/candle?symbol=#{stock.symbol}"
  from = from.to_i if from.is_a?(Time)
  url += "&from=#{from}"
  to = to.to_i if to.is_a?(Time)
  @output = client.request(url)
  if @output.is_a?(Hash) && @output[:s] == "ok"
    @timestamps = @output[:trades][:t]&.map{|t| Time.strptime(t.to_s,'%L')}
  else
    @timestamps = []
  end
end

Public Instance Methods

price() click to toggle source
# File lib/Tick.rb, line 18
def price
  operation(:p)
end
status() click to toggle source
# File lib/Tick.rb, line 27
def status
  raise Finnhub::Error message: "Output is not a hash" unless @output.is_a?(Hash)
  @output[:s]
end
volume() click to toggle source
# File lib/Tick.rb, line 22
def volume
  operation(:v)
end

Private Instance Methods

operation(type) click to toggle source
# File lib/Tick.rb, line 34
def operation(type)
  raise Finnhub::Error message: "Output is not a hash" unless @output.is_a?(Hash)
  return [] if @output[:trades][0][type].nil?
  @timestamps.zip(@output[:trades].map{|t| t[type]})
end