Class: TDAmeritrade::Operations::GetPriceHistory

Inherits:
BaseOperation show all
Defined in:
lib/tdameritrade/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

handle_api_error, parse_json_response, response_success?

Constructor Details

This class inherits a constructor from TDAmeritrade::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



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
50
# File 'lib/tdameritrade/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
)
  params = {
    apikey: client.client_id,
    needExtendedHoursData: need_extended_hours_data,
  }

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

  # NOTE: can't use period if using start and end dates
  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.tdameritrade.com/v1/marketdata/#{symbol}/pricehistory",
    query: params,
  )

  parsed_response = parse_api_response(response)

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

  parsed_response
end