Class: Schwab::Operations::GetPriceHistory

Inherits:
BaseOperation show all
Defined in:
lib/schwab/operations/get_price_history.rb

Constant Summary collapse

FREQUENCY_TYPE =

Not used right now, but can be used later on for validation

[:minute, :daily, :weekly, :monthly]
PERIOD_TYPE =
[:day, :month, :year, :ytd]

Constants inherited from BaseOperation

BaseOperation::HTTP_DEBUG_OUTPUT

Instance Attribute Summary

Attributes inherited from BaseOperation

#client

Instance Method Summary collapse

Methods inherited from BaseOperation

#initialize

Methods included from Util

response_success?

Methods included from Error

raise_error

Constructor Details

This class inherits a constructor from Schwab::Operations::BaseOperation

Instance Method Details

#call(symbol, period_type: nil, period: nil, frequency_type: nil, frequency: nil, end_date: nil, start_date: nil, need_extended_hours_data: false) ⇒ Object

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/schwab/operations/get_price_history.rb', line 10

def call(
  symbol,
  period_type: nil,
  period: nil,
  frequency_type: nil,
  frequency: nil,
  end_date: nil, # should be a Date
  start_date: nil, # should be a Date
  need_extended_hours_data: false
)
  raise(ArgumentError, "Can't use period if using start_date/end_date") if start_date.present? && period.present?

  params = {
    needExtendedHoursData: need_extended_hours_data,
    symbol: symbol
  }

  params.merge!(frequencyType: frequency_type) if frequency_type
  params.merge!(frequency: frequency) if frequency

  params.merge!(periodType: period_type) if period_type
  params.merge!(period: period) if period
  params.merge!(startDate: "#{start_date.strftime('%s')}000") if start_date
  params.merge!(endDate: "#{end_date.strftime('%s')}000") if end_date

  response = perform_api_get_request(
    url: 'https://api.schwabapi.com/marketdata/v1/pricehistory',
    query: params,
  )

  if response["candles"]
    response["candles"].map do |candle|
      if candle["datetime"].is_a? Numeric
        candle["datetime"] = Time.at(candle["datetime"] / 1000)
      end
    end
  end

  response
end