class TDAmeritrade::Operations::GetPriceHistory

Constants

FREQUENCY_TYPE

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

PERIOD_TYPE

Public Instance Methods

call( symbol, period_type: nil, period: nil, frequency_type: nil, frequency: nil, end_date: nil, start_date: nil, need_extended_hours_data: false ) click to toggle source
# 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