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

Parameters:

  • opts (Hash)

    a customizable set of options

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

Returns:



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

def results
  @results
end

Instance Method Details

#data_intervalInteger

Returns value of passed data_interval parameter on instantiation

Returns:

  • (Integer)

    value of passed data_interval parameter on instantiation



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

def data_interval
  @opts[:data_interval]
end

#data_periodSymbol

Returns value of passed data_period parameter on instantiation

Returns:

  • (Symbol)

    value of passed data_period parameter on instantiation



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

Returns List of normalized elements passed on instantiation

Returns:

  • (Array)

    List of normalized elements passed on instantiation



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

Returns value of passed end_time parameter in ISO8601 formatted Eastern Time

Returns:

  • (String)

    value of passed end_time parameter in ISO8601 formatted Eastern Time



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

Returns:

  • (StockMarkit::ChartResults)

    results object



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

def fetch
  @results || update
end

#label_intervalInteger

Returns value of passed label_interval parameter on instantiation

Returns:

  • (Integer)

    value of passed label_interval parameter on instantiation



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

def label_interval
  @opts[:label_interval]
end

#label_periodSymbol

Returns value of passed label_period parameter on instantiation

Returns:

  • (Symbol)

    value of passed label_period parameter on instantiation



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

Returns value of the passed normalized parameter on instantiation

Returns:

  • (Boolean)

    value of the passed normalized parameter on instantiation



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

Returns value of passed number_of_days parameter on instantiation

Returns:

  • (Integer)

    value of passed number_of_days parameter on instantiation



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

def number_of_days
  @opts[:number_of_days]
end

#offsetInteger

Returns value of passed offset parameter on instantiation

Returns:

  • (Integer)

    value of passed offset parameter on instantiation



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

def offset
  @opts[:offset]
end

#start_dateString

Returns value of passed start_time parameter in ISO8601 formatted Eastern Time

Returns:

  • (String)

    value of passed start_time parameter in ISO8601 formatted Eastern Time



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

Returns:



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

def update
  @results = lookup_with_api
end