Module: Schwab::MarketData

Defined in:
lib/schwab/market_data.rb

Overview

Market Data API endpoints for retrieving quotes, price history, and market information

Class Method Summary collapse

Class Method Details

.get_market_hour(market_id, date: nil, client: nil) ⇒ Object

Get market hours for a single market Note: This appears to be the same endpoint as get_market_hours but with a single market instead of multiple



123
124
125
# File 'lib/schwab/market_data.rb', line 123

def get_market_hour(market_id, date: nil, client: nil)
  get_market_hours(market_id, date: date, client: client)
end

.get_market_hours(markets, date: nil, client: nil) ⇒ Hash

Get market hours for one or more markets

Examples:

Get equity market hours

Schwab::MarketData.get_market_hours("EQUITY")

Parameters:

  • markets (String, Array<String>)

    Market(s) to get hours for (e.g., “EQUITY”, “OPTION”)

  • date (Date, Time, String, nil) (defaults to: nil)

    Date to get market hours for

  • client (Schwab::Client, nil) (defaults to: nil)

    Optional client instance

Returns:

  • (Hash)

    Market hours information



110
111
112
113
114
115
116
117
118
# File 'lib/schwab/market_data.rb', line 110

def get_market_hours(markets, date: nil, client: nil)
  client ||= default_client
  params = {
    markets: normalize_markets(markets),
  }
  params[:date] = format_date(date) if date

  client.get("/marketdata/v1/markets", params)
end

.get_movers(index, direction: nil, change: nil, client: nil) ⇒ Hash

Get market movers for an index

Examples:

Get top movers for S&P 500

Schwab::MarketData.get_movers("$SPX", direction: "up", change: "percent")

Parameters:

  • index (String)

    The index symbol (e.g., “$SPX”, “$DJI”)

  • direction (String, nil) (defaults to: nil)

    Direction of movement (“up” or “down”)

  • change (String, nil) (defaults to: nil)

    Type of change (“percent” or “value”)

  • client (Schwab::Client, nil) (defaults to: nil)

    Optional client instance

Returns:

  • (Hash)

    Market movers data



91
92
93
94
95
96
97
98
99
100
# File 'lib/schwab/market_data.rb', line 91

def get_movers(index, direction: nil, change: nil, client: nil)
  client ||= default_client
  path = "/marketdata/v1/movers/#{URI.encode_www_form_component(index)}"

  params = {}
  params[:direction] = direction if direction
  params[:change] = change if change

  client.get(path, params)
end

.get_quote(symbol, fields: nil, client: nil) ⇒ Hash

Get detailed quote for a single symbol

Examples:

Get a single quote

Schwab::MarketData.get_quote("AAPL")

Parameters:

  • symbol (String)

    The symbol to get a quote for

  • fields (String, Array<String>, nil) (defaults to: nil)

    Quote fields to include

  • client (Schwab::Client, nil) (defaults to: nil)

    Optional client instance

Returns:

  • (Hash)

    Detailed quote data for the symbol



39
40
41
42
43
44
45
46
# File 'lib/schwab/market_data.rb', line 39

def get_quote(symbol, fields: nil, client: nil)
  client ||= default_client
  path = "/marketdata/v1/#{URI.encode_www_form_component(symbol)}/quotes"
  params = {}
  params[:fields] = normalize_fields(fields) if fields

  client.get(path, params)
end

.get_quote_history(symbol, period_type: nil, period: nil, frequency_type: nil, frequency: nil, start_date: nil, end_date: nil, need_extended_hours: true, need_previous_close: false, client: nil) ⇒ Hash

Get price history for a symbol

Examples:

Get 5 days of history

Schwab::MarketData.get_quote_history("AAPL", period_type: "day", period: 5)

Parameters:

  • symbol (String)

    The symbol to get price history for

  • period_type (String, nil) (defaults to: nil)

    The type of period (“day”, “month”, “year”, “ytd”)

  • period (Integer, nil) (defaults to: nil)

    The number of periods

  • frequency_type (String, nil) (defaults to: nil)

    The type of frequency (“minute”, “daily”, “weekly”, “monthly”)

  • frequency (Integer, nil) (defaults to: nil)

    The frequency value

  • start_date (Time, Date, String, Integer, nil) (defaults to: nil)

    Start date for history

  • end_date (Time, Date, String, Integer, nil) (defaults to: nil)

    End date for history

  • need_extended_hours (Boolean) (defaults to: true)

    Include extended hours data

  • need_previous_close (Boolean) (defaults to: false)

    Include previous close data

  • client (Schwab::Client, nil) (defaults to: nil)

    Optional client instance

Returns:

  • (Hash)

    Price history data with candles



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/schwab/market_data.rb', line 63

def get_quote_history(symbol, period_type: nil, period: nil, frequency_type: nil,
  frequency: nil, start_date: nil, end_date: nil,
  need_extended_hours: true, need_previous_close: false, client: nil)
  client ||= default_client
  path = "/marketdata/v1/pricehistory"

  params = { symbol: symbol }
  params[:periodType] = period_type if period_type
  params[:period] = period if period
  params[:frequencyType] = frequency_type if frequency_type
  params[:frequency] = frequency if frequency
  params[:startDate] = format_timestamp(start_date) if start_date
  params[:endDate] = format_timestamp(end_date) if end_date
  params[:needExtendedHoursData] = need_extended_hours
  params[:needPreviousClose] = need_previous_close

  client.get(path, params)
end

.get_quotes(symbols, fields: nil, indicative: false, client: nil) ⇒ Hash

Get quotes for one or more symbols

Examples:

Get quotes for multiple symbols

Schwab::MarketData.get_quotes(["AAPL", "MSFT"])

Get quotes with specific fields

Schwab::MarketData.get_quotes("AAPL", fields: ["quote", "fundamental"])

Parameters:

  • symbols (String, Array<String>)

    Symbol(s) to get quotes for

  • fields (String, Array<String>, nil) (defaults to: nil)

    Quote fields to include (e.g., “quote”, “fundamental”)

  • indicative (Boolean) (defaults to: false)

    Whether to include indicative quotes

  • client (Schwab::Client, nil) (defaults to: nil)

    Optional client instance (uses default if not provided)

Returns:

  • (Hash)

    Quote data for the requested symbols



20
21
22
23
24
25
26
27
28
29
# File 'lib/schwab/market_data.rb', line 20

def get_quotes(symbols, fields: nil, indicative: false, client: nil)
  client ||= default_client
  params = {
    symbols: normalize_symbols(symbols),
    indicative: indicative,
  }
  params[:fields] = normalize_fields(fields) if fields

  client.get("/marketdata/v1/quotes", params)
end