Class: SchwabRb::BaseClient
- Inherits:
-
Object
- Object
- SchwabRb::BaseClient
- Includes:
- EnumEnforcer
- Defined in:
- lib/schwab_rb/clients/base_client.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#app_secret ⇒ Object
readonly
Returns the value of attribute app_secret.
-
#enforce_enums ⇒ Object
readonly
Returns the value of attribute enforce_enums.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
-
#token_manager ⇒ Object
readonly
Returns the value of attribute token_manager.
Instance Method Summary collapse
- #available_account_names ⇒ Object
- #cancel_order(order_id, account_hash = nil, account_name: nil) ⇒ Object
- #get_account(account_hash = nil, account_name: nil, fields: nil, return_data_objects: true) ⇒ Object
- #get_account_numbers(return_data_objects: true) ⇒ Object
- #get_account_orders(account_hash = nil, account_name: nil, max_results: nil, from_entered_datetime: nil, to_entered_datetime: nil, status: nil, return_data_objects: true) ⇒ Object
- #get_accounts(fields: nil, return_data_objects: true) ⇒ Object
- #get_all_linked_account_orders(max_results: nil, from_entered_datetime: nil, to_entered_datetime: nil, status: nil, return_data_objects: true) ⇒ Object
- #get_instrument_by_cusip(cusip, return_data_objects: true) ⇒ Object
- #get_instruments(symbols, projection, return_data_objects: true) ⇒ Object
- #get_market_hours(markets, date: nil, return_data_objects: true) ⇒ Object
- #get_movers(index, sort_order: nil, frequency: nil, return_data_objects: true) ⇒ Object
- #get_option_chain(symbol, contract_type: nil, strike_count: nil, include_underlying_quote: nil, strategy: nil, interval: nil, strike: nil, strike_range: nil, from_date: nil, to_date: nil, volatility: nil, underlying_price: nil, interest_rate: nil, days_to_expiration: nil, exp_month: nil, option_type: nil, entitlement: nil, return_data_objects: true) ⇒ Object
- #get_option_expiration_chain(symbol, return_data_objects: true) ⇒ Object
- #get_order(order_id, account_hash = nil, account_name: nil, return_data_objects: true) ⇒ Object
- #get_price_history(symbol, period_type: nil, period: nil, frequency_type: nil, frequency: nil, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true) ⇒ Object
- #get_price_history_every_day(symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true) ⇒ Object
- #get_price_history_every_fifteen_minutes(symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true) ⇒ Object
- #get_price_history_every_five_minutes(symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true) ⇒ Object
- #get_price_history_every_minute(symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true) ⇒ Object
- #get_price_history_every_ten_minutes(symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true) ⇒ Object
- #get_price_history_every_thirty_minutes(symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true) ⇒ Object
- #get_price_history_every_week(symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true) ⇒ Object
- #get_quote(symbol, fields: nil, return_data_objects: true) ⇒ Object
- #get_quotes(symbols, fields: nil, indicative: nil, return_data_objects: true) ⇒ Object
- #get_transaction(activity_id, account_hash = nil, account_name: nil, return_data_objects: true) ⇒ Object
- #get_transactions(account_hash = nil, account_name: nil, start_date: nil, end_date: nil, transaction_types: nil, symbol: nil, return_data_objects: true) ⇒ Object
- #get_user_preferences(return_data_objects: true) ⇒ Object
-
#initialize(api_key, app_secret, session, token_manager:, enforce_enums: true) ⇒ BaseClient
constructor
A new instance of BaseClient.
- #place_order(order_spec, account_hash = nil, account_name: nil) ⇒ Object
- #preview_order(order_spec, account_hash = nil, account_name: nil, return_data_objects: true) ⇒ Object
- #refresh! ⇒ Object
- #replace_order(order_id, order_spec, account_hash = nil, account_name: nil) ⇒ Object
- #set_timeout(timeout) ⇒ Object
- #timeout ⇒ Object
- #token_age ⇒ Object
Methods included from EnumEnforcer
#convert_enum, #convert_enum_iterable, #enforce_enums=, #enforce_enums?, #get_valid_enum_values, #set_enforce_enums, #type_error
Constructor Details
#initialize(api_key, app_secret, session, token_manager:, enforce_enums: true) ⇒ BaseClient
Returns a new instance of BaseClient.
23 24 25 26 27 28 29 |
# File 'lib/schwab_rb/clients/base_client.rb', line 23 def initialize(api_key, app_secret, session, token_manager:, enforce_enums: true) @api_key = api_key @app_secret = app_secret @session = session @token_manager = token_manager @enforce_enums = enforce_enums end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
21 22 23 |
# File 'lib/schwab_rb/clients/base_client.rb', line 21 def api_key @api_key end |
#app_secret ⇒ Object (readonly)
Returns the value of attribute app_secret.
21 22 23 |
# File 'lib/schwab_rb/clients/base_client.rb', line 21 def app_secret @app_secret end |
#enforce_enums ⇒ Object (readonly)
Returns the value of attribute enforce_enums.
21 22 23 |
# File 'lib/schwab_rb/clients/base_client.rb', line 21 def enforce_enums @enforce_enums end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
21 22 23 |
# File 'lib/schwab_rb/clients/base_client.rb', line 21 def session @session end |
#token_manager ⇒ Object (readonly)
Returns the value of attribute token_manager.
21 22 23 |
# File 'lib/schwab_rb/clients/base_client.rb', line 21 def token_manager @token_manager end |
Instance Method Details
#available_account_names ⇒ Object
134 135 136 137 138 139 140 141 |
# File 'lib/schwab_rb/clients/base_client.rb', line 134 def available_account_names # Returns a list of available account names from account_names.json # Returns empty array if account_names.json doesn't exist # # @return [Array<String>] List of account names hash_manager = SchwabRb::AccountHashManager.new hash_manager.available_account_names end |
#cancel_order(order_id, account_hash = nil, account_name: nil) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/schwab_rb/clients/base_client.rb', line 167 def cancel_order(order_id, account_hash = nil, account_name: nil) # Cancel a specific order for a specific account. # # @param order_id [String] The order ID. # @param account_hash [String] The account hash (optional if account_name provided) # @param account_name [String] The account name from account_names.json (takes priority) resolved_hash = resolve_account_hash(account_name: account_name, account_hash: account_hash) with_account_hash_retry(resolved_hash) do refresh_token_if_needed path = "/trader/v1/accounts/#{resolved_hash}/orders/#{order_id}" delete(path) end end |
#get_account(account_hash = nil, account_name: nil, fields: nil, return_data_objects: true) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/schwab_rb/clients/base_client.rb', line 52 def get_account(account_hash = nil, account_name: nil, fields: nil, return_data_objects: true) # Account balances, positions, and orders for a given account. # # @param account_hash [String] The account hash (optional if account_name provided) # @param account_name [String] The account name from account_names.json (takes priority) # @param fields [Array] Balances displayed by default, additional fields can be # added here by adding values from Account.fields. # @param return_data_objects [Boolean] Whether to return data objects or raw JSON resolved_hash = resolve_account_hash(account_name: account_name, account_hash: account_hash) with_account_hash_retry(resolved_hash) do refresh_token_if_needed fields = convert_enum_iterable(fields, SchwabRb::Account::Statuses) if fields params = {} params[:fields] = fields.join(",") if fields path = "/trader/v1/accounts/#{resolved_hash}" response = get(path, params) if return_data_objects account_data = JSON.parse(response.body, symbolize_names: true) SchwabRb::DataObjects::Account.build(account_data) else response end end end |
#get_account_numbers(return_data_objects: true) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/schwab_rb/clients/base_client.rb', line 108 def get_account_numbers(return_data_objects: true) # Returns a mapping from account IDs available to this token to the # account hash that should be passed whenever referring to that account # in API calls. # @param return_data_objects [Boolean] Whether to return data objects or raw JSON refresh_token_if_needed path = "/trader/v1/accounts/accountNumbers" response = get(path, {}) account_numbers_data = JSON.parse(response.body, symbolize_names: true) begin hash_manager = SchwabRb::AccountHashManager.new hash_manager.update_hashes_from_api_response(account_numbers_data) rescue SchwabRb::AccountHashManager::AccountNamesFileNotFoundError # Silently skip if account names file doesn't exist - not all users will use this feature end if return_data_objects SchwabRb::DataObjects::AccountNumbers.build(account_numbers_data) else response end end |
#get_account_orders(account_hash = nil, account_name: nil, max_results: nil, from_entered_datetime: nil, to_entered_datetime: nil, status: nil, return_data_objects: true) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/schwab_rb/clients/base_client.rb', line 183 def get_account_orders( account_hash = nil, account_name: nil, max_results: nil, from_entered_datetime: nil, to_entered_datetime: nil, status: nil, return_data_objects: true ) # Orders for a specific account. Optionally specify a single status on which to filter. # # @param account_hash [String] The account hash (optional if account_name provided) # @param account_name [String] The account name from account_names.json (takes priority) # @param max_results [Integer] The maximum number of orders to retrieve. # @param from_entered_datetime [DateTime] Start of the query date range (default: 60 days ago). # @param to_entered_datetime [DateTime] End of the query date range (default: now). # @param status [String] Restrict query to orders with this status. # @param return_data_objects [Boolean] Whether to return data objects or raw JSON resolved_hash = resolve_account_hash(account_name: account_name, account_hash: account_hash) with_account_hash_retry(resolved_hash) do refresh_token_if_needed from_entered_datetime = DateTime.now.new_offset(0) - 60 if from_entered_datetime.nil? to_entered_datetime = DateTime.now if to_entered_datetime.nil? status = convert_enum(status, SchwabRb::Order::Statuses) if status path = "/trader/v1/accounts/#{resolved_hash}/orders" params = make_order_query( max_results: max_results, from_entered_datetime: from_entered_datetime, to_entered_datetime: to_entered_datetime, status: status ) response = get(path, params) if return_data_objects orders_data = JSON.parse(response.body, symbolize_names: true) orders_data.map { |order_data| SchwabRb::DataObjects::Order.build(order_data) } else response end end end |
#get_accounts(fields: nil, return_data_objects: true) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/schwab_rb/clients/base_client.rb', line 82 def get_accounts(fields: nil, return_data_objects: true) # Account balances, positions, and orders for all linked accounts. # # Note: This method does not return account hashes. # # @param fields [Array] Balances displayed by default, additional fields can be # added here by adding values from Account.fields. # @param return_data_objects [Boolean] Whether to return data objects or raw JSON refresh_token_if_needed fields = convert_enum_iterable(fields, SchwabRb::Account::Statuses) if fields params = {} params[:fields] = fields.join(",") if fields path = "/trader/v1/accounts" response = get(path, params) if return_data_objects accounts_data = JSON.parse(response.body, symbolize_names: true) accounts_data.map { |account_data| SchwabRb::DataObjects::Account.build(account_data) } else response end end |
#get_all_linked_account_orders(max_results: nil, from_entered_datetime: nil, to_entered_datetime: nil, status: nil, return_data_objects: true) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/schwab_rb/clients/base_client.rb', line 231 def get_all_linked_account_orders( max_results: nil, from_entered_datetime: nil, to_entered_datetime: nil, status: nil, return_data_objects: true ) # Orders for all linked accounts. Optionally specify a single status on which to filter. # # @param max_results [Integer] The maximum number of orders to retrieve. # @param from_entered_datetime [DateTime] Start of the query date range (default: 60 days ago). # @param to_entered_datetime [DateTime] End of the query date range (default: now). # @param status [String] Restrict query to orders with this status. # @param return_data_objects [Boolean] Whether to return data objects or raw JSON refresh_token_if_needed path = "/trader/v1/orders" params = make_order_query( max_results: max_results, from_entered_datetime: from_entered_datetime, to_entered_datetime: to_entered_datetime, status: status ) response = get(path, params) if return_data_objects orders_data = JSON.parse(response.body, symbolize_names: true) orders_data.map { |order_data| SchwabRb::DataObjects::Order.build(order_data) } else response end end |
#get_instrument_by_cusip(cusip, return_data_objects: true) ⇒ Object
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 |
# File 'lib/schwab_rb/clients/base_client.rb', line 912 def get_instrument_by_cusip(cusip, return_data_objects: true) # Get instrument information for a single instrument by CUSIP. # # @param cusip [String] CUSIP of the instrument to fetch. Leading zeroes must be preserved. # @param return_data_objects [Boolean] Whether to return data objects or raw JSON refresh_token_if_needed raise ArgumentError, "cusip must be passed as a string" unless cusip.is_a?(String) response = get("/marketdata/v1/instruments/#{cusip}", {}) if return_data_objects instrument_data = JSON.parse(response.body, symbolize_names: true) SchwabRb::DataObjects::Instrument.build(instrument_data) else response end end |
#get_instruments(symbols, projection, return_data_objects: true) ⇒ Object
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 |
# File 'lib/schwab_rb/clients/base_client.rb', line 883 def get_instruments(symbols, projection, return_data_objects: true) # Get instrument details by using different search methods. Also used # to get fundamental instrument data using the "FUNDAMENTAL" projection. # # @param symbols [String, Array] For "FUNDAMENTAL" projection, the symbols to fetch. # For other projections, a search term. # @param projection [String] Search mode or "FUNDAMENTAL" for instrument fundamentals. # @param return_data_objects [Boolean] Whether to return data objects or raw JSON refresh_token_if_needed symbols = [symbols] unless symbols.is_a?(Array) projection = convert_enum(projection, SchwabRb::Orders::Instrument::Projections) params = { "symbol" => symbols.join(","), "projection" => projection } response = get("/marketdata/v1/instruments", params) if return_data_objects instruments_data = JSON.parse(response.body, symbolize_names: true) instruments_data.map do |instrument_data| SchwabRb::DataObjects::Instrument.build(instrument_data) end else response end end |
#get_market_hours(markets, date: nil, return_data_objects: true) ⇒ Object
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 |
# File 'lib/schwab_rb/clients/base_client.rb', line 859 def get_market_hours(markets, date: nil, return_data_objects: true) # Retrieve market hours for specified markets. # # @param markets [Array, String] Markets for which to return trading hours. # @param date [Date] Date for which to return market hours. Accepts values up to # one year from today. # @param return_data_objects [Boolean] Whether to return data objects or raw JSON refresh_token_if_needed markets = convert_enum_iterable(markets, SchwabRb::MarketHours::Markets) params = { "markets" => markets.join(",") } params["date"] = format_date_as_day("date", date) if date response = get("/marketdata/v1/markets", params) if return_data_objects market_hours_data = JSON.parse(response.body, symbolize_names: true) SchwabRb::DataObjects::MarketHours.build(market_hours_data) else response end end |
#get_movers(index, sort_order: nil, frequency: nil, return_data_objects: true) ⇒ Object
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 |
# File 'lib/schwab_rb/clients/base_client.rb', line 829 def get_movers(index, sort_order: nil, frequency: nil, return_data_objects: true) # Get a list of the top ten movers for a given index. # # @param index [String] Category of mover. See Movers::Index for valid values. # @param sort_order [String] Order in which to return values. See Movers::SortOrder for valid values. # @param frequency [String] Only return movers that saw this magnitude or greater. # See Movers::Frequency for valid values. # @param return_data_objects [Boolean] Whether to return data objects or raw JSON refresh_token_if_needed index = convert_enum(index, SchwabRb::Movers::Indexes) sort_order = convert_enum(sort_order, SchwabRb::Movers::SortOrders) if sort_order frequency = convert_enum(frequency, SchwabRb::Movers::Frequencies) if frequency path = "/marketdata/v1/movers/#{index}" params = {} params["sort"] = sort_order if sort_order params["frequency"] = frequency.to_s if frequency response = get(path, params) if return_data_objects movers_data = JSON.parse(response.body, symbolize_names: true) SchwabRb::DataObjects::MarketMoversFactory.build(movers_data) else response end end |
#get_option_chain(symbol, contract_type: nil, strike_count: nil, include_underlying_quote: nil, strategy: nil, interval: nil, strike: nil, strike_range: nil, from_date: nil, to_date: nil, volatility: nil, underlying_price: nil, interest_rate: nil, days_to_expiration: nil, exp_month: nil, option_type: nil, entitlement: nil, return_data_objects: true) ⇒ Object
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 |
# File 'lib/schwab_rb/clients/base_client.rb', line 495 def get_option_chain( symbol, contract_type: nil, strike_count: nil, include_underlying_quote: nil, strategy: nil, interval: nil, strike: nil, strike_range: nil, from_date: nil, to_date: nil, volatility: nil, underlying_price: nil, interest_rate: nil, days_to_expiration: nil, exp_month: nil, option_type: nil, entitlement: nil, return_data_objects: true ) # Get option chain for an optionable symbol. # # @param symbol [String] The symbol for the option chain. # @param contract_type [String] Type of contracts to return in the chain. # @param strike_count [Integer] Number of strikes above and below the ATM price. # @param include_underlying_quote [Boolean] Include a quote for the underlying. # @param strategy [String] Strategy type for the option chain. # @param interval [Float] Strike interval for spread strategy chains. # @param strike [Float] Specific strike price for the option chain. # @param strike_range [String] Range of strikes to include. # @param from_date [Date] Filter expirations after this date. # @param to_date [Date] Filter expirations before this date. # @param volatility [Float] Volatility for analytical calculations. # @param underlying_price [Float] Underlying price for analytical calculations. # @param interest_rate [Float] Interest rate for analytical calculations. # @param days_to_expiration [Integer] Days to expiration for analytical calculations. # @param exp_month [String] Filter options by expiration month. # @param option_type [String] Type of options to include in the chain. # @param entitlement [String] Client entitlement. # @param return_data_objects [Boolean] Whether to return data objects or raw JSON refresh_token_if_needed contract_type = convert_enum(contract_type, SchwabRb::Option::ContractTypes) strategy = convert_enum(strategy, SchwabRb::Option::Strategies) strike_range = convert_enum(strike_range, SchwabRb::Option::StrikeRanges) option_type = convert_enum(option_type, SchwabRb::Option::Types) exp_month = convert_enum(exp_month, SchwabRb::Option::ExpirationMonths) entitlement = convert_enum(entitlement, SchwabRb::Option::Entitlements) params = { "symbol" => symbol } params["contractType"] = contract_type if contract_type params["strikeCount"] = strike_count if strike_count params["includeUnderlyingQuote"] = if params["strategy"] = strategy if strategy params["interval"] = interval if interval params["strike"] = strike if strike params["range"] = strike_range if strike_range params["fromDate"] = format_date_as_day("from_date", from_date) if from_date params["toDate"] = format_date_as_day("to_date", to_date) if to_date params["volatility"] = volatility if volatility params["underlyingPrice"] = if params["interestRate"] = interest_rate if interest_rate params["daysToExpiration"] = days_to_expiration if days_to_expiration params["expMonth"] = exp_month if exp_month params["optionType"] = option_type if option_type params["entitlement"] = entitlement if entitlement path = "/marketdata/v1/chains" response = get(path, params) if return_data_objects option_chain_data = JSON.parse(response.body, symbolize_names: true) SchwabRb::DataObjects::OptionChain.build(option_chain_data) else response end end |
#get_option_expiration_chain(symbol, return_data_objects: true) ⇒ Object
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 |
# File 'lib/schwab_rb/clients/base_client.rb', line 574 def get_option_expiration_chain(symbol, return_data_objects: true) # Get option expiration chain for a symbol. # @param symbol [String] The symbol for which to get option expiration dates. # @param return_data_objects [Boolean] Whether to return data objects or raw JSON refresh_token_if_needed path = "/marketdata/v1/expirationchain" response = get(path, { symbol: symbol }) if return_data_objects expiration_data = JSON.parse(response.body, symbolize_names: true) SchwabRb::DataObjects::OptionExpirationChain.build(expiration_data) else response end end |
#get_order(order_id, account_hash = nil, account_name: nil, return_data_objects: true) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/schwab_rb/clients/base_client.rb', line 143 def get_order(order_id, account_hash = nil, account_name: nil, return_data_objects: true) # Get a specific order for a specific account by its order ID. # # @param order_id [String] The order ID. # @param account_hash [String] The account hash (optional if account_name provided) # @param account_name [String] The account name from account_names.json (takes priority) # @param return_data_objects [Boolean] Whether to return data objects or raw JSON resolved_hash = resolve_account_hash(account_name: account_name, account_hash: account_hash) with_account_hash_retry(resolved_hash) do refresh_token_if_needed path = "/trader/v1/accounts/#{resolved_hash}/orders/#{order_id}" response = get(path, {}) if return_data_objects order_data = JSON.parse(response.body, symbolize_names: true) SchwabRb::DataObjects::Order.build(order_data) else response end end end |
#get_price_history(symbol, period_type: nil, period: nil, frequency_type: nil, frequency: nil, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true) ⇒ Object
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 |
# File 'lib/schwab_rb/clients/base_client.rb', line 590 def get_price_history( symbol, period_type: nil, period: nil, frequency_type: nil, frequency: nil, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true ) # Get price history for a symbol. # @param return_data_objects [Boolean] Whether to return data objects or raw JSON refresh_token_if_needed period_type = convert_enum(period_type, SchwabRb::PriceHistory::PeriodTypes) if period_type period = convert_enum(period, SchwabRb::PriceHistory::Periods) if period frequency_type = convert_enum(frequency_type, SchwabRb::PriceHistory::FrequencyTypes) if frequency_type frequency = convert_enum(frequency, SchwabRb::PriceHistory::Frequencies) if frequency 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_date_as_millis("start_datetime", start_datetime) if start_datetime params["endDate"] = format_date_as_millis("end_datetime", end_datetime) if end_datetime params["needExtendedHoursData"] = need_extended_hours_data unless need_extended_hours_data.nil? params["needPreviousClose"] = need_previous_close unless need_previous_close.nil? path = "/marketdata/v1/pricehistory" response = get(path, params) if return_data_objects price_history_data = JSON.parse(response.body, symbolize_names: true) SchwabRb::DataObjects::PriceHistory.build(price_history_data) else response end end |
#get_price_history_every_day(symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true) ⇒ Object
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 |
# File 'lib/schwab_rb/clients/base_client.rb', line 773 def get_price_history_every_day( symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true ) refresh_token_if_needed start_datetime, end_datetime = normalize_start_and_end_datetimes( start_datetime, end_datetime ) get_price_history( symbol, period_type: SchwabRb::PriceHistory::PeriodTypes::YEAR, period: SchwabRb::PriceHistory::Periods::TWENTY_YEARS, frequency_type: SchwabRb::PriceHistory::FrequencyTypes::DAILY, frequency: SchwabRb::PriceHistory::FrequencyTypes::EVERY_MINUTE, start_datetime: start_datetime, end_datetime: end_datetime, need_extended_hours_data: need_extended_hours_data, need_previous_close: need_previous_close, return_data_objects: return_data_objects ) end |
#get_price_history_every_fifteen_minutes(symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true) ⇒ Object
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 |
# File 'lib/schwab_rb/clients/base_client.rb', line 717 def get_price_history_every_fifteen_minutes( symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true ) refresh_token_if_needed start_datetime, end_datetime = normalize_start_and_end_datetimes( start_datetime, end_datetime ) get_price_history( symbol, period_type: SchwabRb::PriceHistory::PeriodTypes::DAY, period: SchwabRb::PriceHistory::Periods::ONE_DAY, frequency_type: SchwabRb::PriceHistory::FrequencyTypes::MINUTE, frequency: SchwabRb::PriceHistory::Frequencies::EVERY_FIFTEEN_MINUTES, start_datetime: start_datetime, end_datetime: end_datetime, need_extended_hours_data: need_extended_hours_data, need_previous_close: need_previous_close, return_data_objects: return_data_objects ) end |
#get_price_history_every_five_minutes(symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true) ⇒ Object
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 |
# File 'lib/schwab_rb/clients/base_client.rb', line 661 def get_price_history_every_five_minutes( symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true ) refresh_token_if_needed start_datetime, end_datetime = normalize_start_and_end_datetimes( start_datetime, end_datetime ) get_price_history( symbol, period_type: SchwabRb::PriceHistory::PeriodTypes::DAY, period: SchwabRb::PriceHistory::Periods::ONE_DAY, frequency_type: SchwabRb::PriceHistory::FrequencyTypes::MINUTE, frequency: SchwabRb::PriceHistory::Frequencies::EVERY_FIVE_MINUTES, start_datetime: start_datetime, end_datetime: end_datetime, need_extended_hours_data: need_extended_hours_data, need_previous_close: need_previous_close, return_data_objects: return_data_objects ) end |
#get_price_history_every_minute(symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true) ⇒ Object
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 |
# File 'lib/schwab_rb/clients/base_client.rb', line 633 def get_price_history_every_minute( symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true ) refresh_token_if_needed start_datetime, end_datetime = normalize_start_and_end_datetimes( start_datetime, end_datetime ) get_price_history( symbol, period_type: SchwabRb::PriceHistory::PeriodTypes::DAY, period: SchwabRb::PriceHistory::Periods::ONE_DAY, frequency_type: SchwabRb::PriceHistory::FrequencyTypes::MINUTE, frequency: SchwabRb::PriceHistory::Frequencies::EVERY_MINUTE, start_datetime: start_datetime, end_datetime: end_datetime, need_extended_hours_data: need_extended_hours_data, need_previous_close: need_previous_close, return_data_objects: return_data_objects ) end |
#get_price_history_every_ten_minutes(symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true) ⇒ Object
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 |
# File 'lib/schwab_rb/clients/base_client.rb', line 689 def get_price_history_every_ten_minutes( symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true ) refresh_token_if_needed start_datetime, end_datetime = normalize_start_and_end_datetimes( start_datetime, end_datetime ) get_price_history( symbol, period_type: SchwabRb::PriceHistory::PeriodTypes::DAY, period: SchwabRb::PriceHistory::Periods::ONE_DAY, frequency_type: SchwabRb::PriceHistory::FrequencyTypes::MINUTE, frequency: SchwabRb::PriceHistory::Frequencies::EVERY_TEN_MINUTES, start_datetime: start_datetime, end_datetime: end_datetime, need_extended_hours_data: need_extended_hours_data, need_previous_close: need_previous_close, return_data_objects: return_data_objects ) end |
#get_price_history_every_thirty_minutes(symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true) ⇒ Object
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 |
# File 'lib/schwab_rb/clients/base_client.rb', line 745 def get_price_history_every_thirty_minutes( symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true ) refresh_token_if_needed start_datetime, end_datetime = normalize_start_and_end_datetimes( start_datetime, end_datetime ) get_price_history( symbol, period_type: SchwabRb::PriceHistory::PeriodTypes::DAY, period: SchwabRb::PriceHistory::Periods::ONE_DAY, frequency_type: SchwabRb::PriceHistory::FrequencyTypes::MINUTE, frequency: SchwabRb::PriceHistory::Frequencies::EVERY_THIRTY_MINUTES, start_datetime: start_datetime, end_datetime: end_datetime, need_extended_hours_data: need_extended_hours_data, need_previous_close: need_previous_close, return_data_objects: return_data_objects ) end |
#get_price_history_every_week(symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true) ⇒ Object
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 |
# File 'lib/schwab_rb/clients/base_client.rb', line 801 def get_price_history_every_week( symbol, start_datetime: nil, end_datetime: nil, need_extended_hours_data: nil, need_previous_close: nil, return_data_objects: true ) refresh_token_if_needed start_datetime, end_datetime = normalize_start_and_end_datetimes( start_datetime, end_datetime ) get_price_history( symbol, period_type: SchwabRb::PriceHistory::PeriodTypes::YEAR, period: SchwabRb::PriceHistory::Periods::TWENTY_YEARS, frequency_type: SchwabRb::PriceHistory::FrequencyTypes::WEEKLY, frequency: SchwabRb::PriceHistory::FrequencyTypes::EVERY_MINUTE, start_datetime: start_datetime, end_datetime: end_datetime, need_extended_hours_data: need_extended_hours_data, need_previous_close: need_previous_close, return_data_objects: return_data_objects ) end |
#get_quote(symbol, fields: nil, return_data_objects: true) ⇒ Object
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/schwab_rb/clients/base_client.rb', line 436 def get_quote(symbol, fields: nil, return_data_objects: true) # Get quote for a symbol. # # @param symbol [String] Single symbol to fetch. # @param fields [Array] Fields to request. If unset, return all available # data (i.e., all fields). See `GetQuote::Field` for options. # @param return_data_objects [Boolean] Whether to return data objects or raw JSON refresh_token_if_needed fields = convert_enum_iterable(fields, SchwabRb::Quote::Types) if fields params = fields ? { "fields" => fields.join(",") } : {} path = "/marketdata/v1/#{symbol}/quotes" response = get(path, params) if return_data_objects quote_data = JSON.parse(response.body, symbolize_names: true) SchwabRb::DataObjects::QuoteFactory.build(quote_data) else response end end |
#get_quotes(symbols, fields: nil, indicative: nil, return_data_objects: true) ⇒ Object
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
# File 'lib/schwab_rb/clients/base_client.rb', line 458 def get_quotes(symbols, fields: nil, indicative: nil, return_data_objects: true) # Get quotes for symbols. This method supports all symbols, including those # containing non-alphanumeric characters like `/ES`. # # @param symbols [Array, String] Symbols to fetch. Can be a single symbol or an array of symbols. # @param fields [Array] Fields to request. If unset, return all available data. # See `GetQuote::Field` for options. # @param indicative [Boolean] If set, fetch indicative quotes. Must be true or false. # @param return_data_objects [Boolean] Whether to return data objects or raw JSON refresh_token_if_needed symbols = [symbols] if symbols.is_a?(String) params = { "symbols" => symbols.join(",") } fields = convert_enum_iterable(fields, SchwabRb::Quote::Types) if fields params["fields"] = fields.join(",") if fields unless indicative.nil? unless [true, false].include?(indicative) raise ArgumentError, "value of 'indicative' must be either true or false" end params["indicative"] = indicative ? "true" : "false" end path = "/marketdata/v1/quotes" response = get(path, params) if return_data_objects quotes_data = JSON.parse(response.body, symbolize_names: true) quotes_data.map do |symbol, quote_data| SchwabRb::DataObjects::QuoteFactory.build({ symbol => quote_data }) end else response end end |
#get_transaction(activity_id, account_hash = nil, account_name: nil, return_data_objects: true) ⇒ Object
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/schwab_rb/clients/base_client.rb', line 397 def get_transaction(activity_id, account_hash = nil, account_name: nil, return_data_objects: true) # Transaction for a specific account. # # @param account_hash [String] The account hash (optional if account_name provided) # @param activity_id [String] ID of the transaction to retrieve # @param account_name [String] The account name from account_names.json (takes priority) # @param return_data_objects [Boolean] Whether to return data objects or raw JSON resolved_hash = resolve_account_hash(account_name: account_name, account_hash: account_hash) with_account_hash_retry(resolved_hash) do refresh_token_if_needed path = "/trader/v1/accounts/#{resolved_hash}/transactions/#{activity_id}" response = get(path, {}) if return_data_objects transaction_data = JSON.parse(response.body, symbolize_names: true) SchwabRb::DataObjects::Transaction.build(transaction_data) else response end end end |
#get_transactions(account_hash = nil, account_name: nil, start_date: nil, end_date: nil, transaction_types: nil, symbol: nil, return_data_objects: true) ⇒ Object
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/schwab_rb/clients/base_client.rb', line 335 def get_transactions( account_hash = nil, account_name: nil, start_date: nil, end_date: nil, transaction_types: nil, symbol: nil, return_data_objects: true ) # Transactions for a specific account. # # @param account_hash [String] The account hash (optional if account_name provided) # @param account_name [String] The account name from account_names.json (takes priority) # @param start_date [Date, DateTime] Start date for transactions (default: 60 days ago). # @param end_date [Date, DateTime] End date for transactions (default: now). # @param transaction_types [Array] List of transaction types to filter by. # @param symbol [String] Filter transactions by the specified symbol. # @param return_data_objects [Boolean] Whether to return data objects or raw JSON resolved_hash = resolve_account_hash(account_name: account_name, account_hash: account_hash) with_account_hash_retry(resolved_hash) do refresh_token_if_needed transaction_types = if transaction_types convert_enum_iterable(transaction_types, SchwabRb::Transaction::Types) else get_valid_enum_values(SchwabRb::Transaction::Types) end start_date = if start_date.nil? format_date_as_iso("start_date", DateTime.now.new_offset(0) - 60) else format_date_as_iso("start_date", start_date) end end_date = if end_date.nil? format_date_as_iso("end_date", DateTime.now.new_offset(0)) else format_date_as_iso("end_date", end_date) end params = { "types" => transaction_types.sort.join(","), "startDate" => start_date, "endDate" => end_date } params["symbol"] = symbol unless symbol.nil? path = "/trader/v1/accounts/#{resolved_hash}/transactions" response = get(path, params) if return_data_objects transactions_data = JSON.parse(response.body, symbolize_names: true) transactions_data.map do |transaction_data| SchwabRb::DataObjects::Transaction.build(transaction_data) end else response end end end |
#get_user_preferences(return_data_objects: true) ⇒ Object
421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/schwab_rb/clients/base_client.rb', line 421 def get_user_preferences(return_data_objects: true) # Get user preferences for the authenticated user. # @param return_data_objects [Boolean] Whether to return data objects or raw JSON refresh_token_if_needed path = "/trader/v1/userPreference" response = get(path, {}) if return_data_objects preferences_data = JSON.parse(response.body, symbolize_names: true) SchwabRb::DataObjects::UserPreferences.build(preferences_data) else response end end |
#place_order(order_spec, account_hash = nil, account_name: nil) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/schwab_rb/clients/base_client.rb', line 265 def place_order(order_spec, account_hash = nil, account_name: nil) # Place an order for a specific account. If order creation is successful, # the response will contain the ID of the generated order. # # @param account_hash [String] The account hash (optional if account_name provided) # @param order_spec [Hash, SchwabRb::Orders::Builder] The order specification # @param account_name [String] The account name from account_names.json (takes priority) # # Note: Unlike most methods in this library, successful responses typically # do not contain JSON data, and attempting to extract it may raise an exception. resolved_hash = resolve_account_hash(account_name: account_name, account_hash: account_hash) with_account_hash_retry(resolved_hash) do refresh_token_if_needed order_spec = order_spec.build if order_spec.is_a?(SchwabRb::Orders::Builder) path = "/trader/v1/accounts/#{resolved_hash}/orders" post(path, order_spec) end end |
#preview_order(order_spec, account_hash = nil, account_name: nil, return_data_objects: true) ⇒ Object
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/schwab_rb/clients/base_client.rb', line 308 def preview_order(order_spec, account_hash = nil, account_name: nil, return_data_objects: true) # Preview an order, i.e., test whether an order would be accepted by the # API and see the structure it would result in. # # @param account_hash [String] The account hash (optional if account_name provided) # @param order_spec [Hash, SchwabRb::Orders::Builder] The order specification to preview # @param account_name [String] The account name from account_names.json (takes priority) # @param return_data_objects [Boolean] Whether to return data objects or raw JSON resolved_hash = resolve_account_hash(account_name: account_name, account_hash: account_hash) with_account_hash_retry(resolved_hash) do refresh_token_if_needed order_spec = order_spec.build if order_spec.is_a?(SchwabRb::Orders::Builder) path = "/trader/v1/accounts/#{resolved_hash}/previewOrder" response = post(path, order_spec) if return_data_objects preview_data = JSON.parse(response.body, symbolize_names: true) SchwabRb::DataObjects::OrderPreview.build(preview_data) else response end end end |
#refresh! ⇒ Object
31 32 33 |
# File 'lib/schwab_rb/clients/base_client.rb', line 31 def refresh! refresh_token_if_needed end |
#replace_order(order_id, order_spec, account_hash = nil, account_name: nil) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/schwab_rb/clients/base_client.rb', line 287 def replace_order(order_id, order_spec, account_hash = nil, account_name: nil) # Replace an existing order for an account. # The existing order will be replaced by the new order. # Once replaced, the old order will be canceled and a new order will be created. # # @param account_hash [String] The account hash (optional if account_name provided) # @param order_id [String] The order ID to replace # @param order_spec [Hash, SchwabRb::Orders::Builder] The new order specification # @param account_name [String] The account name from account_names.json (takes priority) resolved_hash = resolve_account_hash(account_name: account_name, account_hash: account_hash) with_account_hash_retry(resolved_hash) do refresh_token_if_needed order_spec = order_spec.build if order_spec.is_a?(SchwabRb::Orders::Builder) path = "/trader/v1/accounts/#{resolved_hash}/orders/#{order_id}" put(path, order_spec) end end |
#set_timeout(timeout) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/schwab_rb/clients/base_client.rb', line 39 def set_timeout(timeout) # Sets the timeout for the client session. # # @param timeout [Integer] The timeout value in seconds. @session.[:connection_opts] ||= {} @session.[:connection_opts][:request] ||= {} @session.[:connection_opts][:request][:timeout] = timeout end |
#timeout ⇒ Object
35 36 37 |
# File 'lib/schwab_rb/clients/base_client.rb', line 35 def timeout @session.[:connection_opts][:request][:timeout] end |
#token_age ⇒ Object
48 49 50 |
# File 'lib/schwab_rb/clients/base_client.rb', line 48 def token_age @token_manager.token_age end |