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.



32
33
34
# File 'lib/stock-markit/chart.rb', line 32

def initialize(opts)
  @opts = opts
end

Instance Attribute Details

#resultsStockMarkit::ChartResult (readonly)

The chart results

Returns:



16
17
18
# File 'lib/stock-markit/chart.rb', line 16

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



69
70
71
# File 'lib/stock-markit/chart.rb', line 69

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



63
64
65
66
# File 'lib/stock-markit/chart.rb', line 63

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



86
87
88
# File 'lib/stock-markit/chart.rb', line 86

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



48
49
50
# File 'lib/stock-markit/chart.rb', line 48

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



93
94
95
# File 'lib/stock-markit/chart.rb', line 93

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



81
82
83
# File 'lib/stock-markit/chart.rb', line 81

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



74
75
76
77
78
# File 'lib/stock-markit/chart.rb', line 74

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



37
38
39
40
# File 'lib/stock-markit/chart.rb', line 37

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



58
59
60
# File 'lib/stock-markit/chart.rb', line 58

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



53
54
55
# File 'lib/stock-markit/chart.rb', line 53

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



43
44
45
# File 'lib/stock-markit/chart.rb', line 43

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

#updateStockMarkit::ChartResult

updates the data from the api

Returns:



100
101
102
# File 'lib/stock-markit/chart.rb', line 100

def update
  @results = lookup_with_api
end