Class: Finnhub::Indicator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client:, stock:, resolution: "D", from:, to:, indicator:, args: {}) ⇒ Indicator

Returns a new instance of Indicator.



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

def initialize(client:, stock:, resolution: "D", from:, to:, indicator:, args: {})
  url = "/indicator?symbol=#{stock.symbol}&resolution=#{resolution}"
  from = from.to_i if from.is_a?(Time)
  url += "&from=#{from}"
  to = to.to_i if to.is_a?(Time)
  url += "&to=#{to}"
  url += "&indicator=#{indicator}"
  args.each do |attribute, value|
    url += "&#{attribute}=#{value}"
  end
  @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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



48
49
50
# File 'lib/Indicator.rb', line 48

def method_missing(name, *args, &block)
  operation(name.to_sym)
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



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

def output
  @output
end

#timestampsObject (readonly)

Returns the value of attribute timestamps.



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

def timestamps
  @timestamps
end

Instance Method Details

#closeObject



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

def close
  operation(:c)
end

#highObject



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

def high
  operation(:h)
end

#lowObject



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

def low
  operation(:l)
end

#openObject



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

def open
  operation(:o)
end

#statusObject



43
44
45
46
# File 'lib/Indicator.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/Indicator.rb', line 39

def volume
  operation(:v)
end