Class: StockMarkit::Chart

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/stock-markit/chart.rb

Overview

Stock Chart Object

Copyright

Copyright © 2016 Michael Heijmans

License

MIT

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Chart

Returns a new instance of Chart

Options Hash (opts):

  • :normalized (Boolean)

    Show data in price units (false) or percentages (true)

  • :start_date (Time)

    The beginning date for the chart

  • :end_date (Time)

    The end date of the chart

  • :offset (Integer)

    Number of days back that chart should end. Defaults to 0 if not specified. May be used instead of :end_date for interday requests.

  • :number_of_days (Integer)

    Number of days that should be shown on the chart. Required for intraday requests. May be used instead of :start_date for interday requests.

  • :data_period (Symbol)

    The type of data requested. :Minute, :Hour, :Day, :Week, :Month, :Quarter, :Year

  • :data_interval (Integer)

    For intraday data, specifies the number of periods between data points. e.g. if DataPeriod is Minute and DataInterval is 5, you will get a chart with five minute intervals. Must be 0 or null for interday charts

  • :label_period (Symbol)

    The TimePeriod over which to create labels. Control how often you want labels by setting LabelInterval. :Minute, :Hour, :Day, :Week, :Month, :Quarter, :Year

  • :label_interval (Integer)

    How many LabelPeriods to skip between labels

  • :elements (Array<StockMarkit::Element>)

    An Array of 1 or more Elements.


29
30
31
# File 'lib/stock-markit/chart.rb', line 29

def initialize(opts)
  @opts = opts
end

Instance Attribute Details

#resultsStockMarkit::ChartResult (readonly)

The chart results


13
14
15
# File 'lib/stock-markit/chart.rb', line 13

def results
  @results
end

Instance Method Details

#data_intervalInteger


66
67
68
# File 'lib/stock-markit/chart.rb', line 66

def data_interval
  @opts[:data_interval]
end

#data_periodSymbol


60
61
62
63
# File 'lib/stock-markit/chart.rb', line 60

def data_period
  raise "valid data_periods are #{allowed_periods.join(", ")}" unless allowed_periods.include? @opts[:data_period]
  @opts[:data_period].to_s.capitalize
end

#elementsArray


83
84
85
# File 'lib/stock-markit/chart.rb', line 83

def elements
  @opts[:elements].map{ |element| {"Symbol" => element.symbol, "Type" => element.type, "Params" => [element.params]} }
end

#end_dateString


45
46
47
# File 'lib/stock-markit/chart.rb', line 45

def end_date
  format_time(@opts[:end_date])
end

#fetchStockMarkit::ChartResults

loads the @results on first call or returns results on subsequent calls


90
91
92
# File 'lib/stock-markit/chart.rb', line 90

def fetch
  @results || update
end

#label_intervalInteger


78
79
80
# File 'lib/stock-markit/chart.rb', line 78

def label_interval
  @opts[:label_interval]
end

#label_periodSymbol


71
72
73
74
75
# File 'lib/stock-markit/chart.rb', line 71

def label_period
  return nil unless @opts[:label_period]
  raise "valid label_periods are #{allowed_periods.join(", ")}" unless allowed_periods.include? @opts[:label_period]
  @opts[:label_period].to_s.capitalize
end

#normalized?Boolean


34
35
36
37
# File 'lib/stock-markit/chart.rb', line 34

def normalized?
  return true if @opts[:normalized].nil?
  @opts[:normalized]
end

#number_of_daysInteger


55
56
57
# File 'lib/stock-markit/chart.rb', line 55

def number_of_days
  @opts[:number_of_days]
end

#offsetInteger


50
51
52
# File 'lib/stock-markit/chart.rb', line 50

def offset
  @opts[:offset]
end

#start_dateString


40
41
42
# File 'lib/stock-markit/chart.rb', line 40

def start_date
  format_time(@opts[:start_date])
end

#updateStockMarkit::ChartResult

updates the data from the api


97
98
99
# File 'lib/stock-markit/chart.rb', line 97

def update
  @results = lookup_with_api
end