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
- #cancel_order(order_id, account_hash: nil) ⇒ Object
- #get_account(account_hash: nil, fields: nil, return_data_objects: true) ⇒ Object
- #get_account_numbers(return_data_objects: true) ⇒ Object
- #get_account_orders(account_hash: 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, 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, return_data_objects: true) ⇒ Object
- #get_transactions(account_hash: 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) ⇒ Object
- #preview_order(order_spec, account_hash: nil, return_data_objects: true) ⇒ Object
- #refresh! ⇒ Object
- #replace_order(order_id, order_spec, account_hash: 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
#cancel_order(order_id, account_hash: nil) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/schwab_rb/clients/base_client.rb', line 149 def cancel_order(order_id, account_hash: nil) # Cancel a specific order for a specific account. # # @param order_id [String] The order ID. # @param account_hash [String] The account hash. If nil, resolved from SCHWAB_ACCOUNT_NUMBER. resolved_hash = resolve_account_hash(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, 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 |
# File 'lib/schwab_rb/clients/base_client.rb', line 52 def get_account(account_hash: nil, fields: nil, return_data_objects: true) # Account balances, positions, and orders for a given account. # # @param account_hash [String] The account hash. If nil, resolved from SCHWAB_ACCOUNT_NUMBER. # @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 Hash resolved_hash = resolve_account_hash(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) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects SchwabRb::DataObjects::Account.build(data) else data end end end |
#get_account_numbers(return_data_objects: true) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/schwab_rb/clients/base_client.rb', line 107 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 Hash refresh_token_if_needed path = "/trader/v1/accounts/accountNumbers" response = get(path, {}) account_numbers_data = JSON.parse(response.body, symbolize_names: true) if return_data_objects SchwabRb::DataObjects::AccountNumbers.build(account_numbers_data) else account_numbers_data end end |
#get_account_orders(account_hash: nil, max_results: nil, from_entered_datetime: nil, to_entered_datetime: nil, status: nil, return_data_objects: true) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 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 |
# File 'lib/schwab_rb/clients/base_client.rb', line 164 def get_account_orders( account_hash: 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. If nil, resolved from SCHWAB_ACCOUNT_NUMBER. # @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 Hash resolved_hash = resolve_account_hash(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) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects data.map { |order_data| SchwabRb::DataObjects::Order.build(order_data) } else data end end end |
#get_accounts(fields: nil, return_data_objects: true) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/schwab_rb/clients/base_client.rb', line 81 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 Hash 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) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects data.map { |account_data| SchwabRb::DataObjects::Account.build(account_data) } else data 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
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/schwab_rb/clients/base_client.rb', line 210 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 Hash 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) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects data.map { |order_data| SchwabRb::DataObjects::Order.build(order_data) } else data end end |
#get_instrument_by_cusip(cusip, return_data_objects: true) ⇒ Object
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 |
# File 'lib/schwab_rb/clients/base_client.rb', line 885 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 Hash refresh_token_if_needed raise ArgumentError, "cusip must be passed as a string" unless cusip.is_a?(String) response = get("/marketdata/v1/instruments/#{cusip}", {}) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects SchwabRb::DataObjects::Instrument.build(data) else data end end |
#get_instruments(symbols, projection, return_data_objects: true) ⇒ Object
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 |
# File 'lib/schwab_rb/clients/base_client.rb', line 856 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 Hash 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) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects data.map do |instrument_data| SchwabRb::DataObjects::Instrument.build(instrument_data) end else data end end |
#get_market_hours(markets, date: nil, return_data_objects: true) ⇒ Object
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 |
# File 'lib/schwab_rb/clients/base_client.rb', line 832 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 Hash 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) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects SchwabRb::DataObjects::MarketHours.build(data) else data end end |
#get_movers(index, sort_order: nil, frequency: nil, return_data_objects: true) ⇒ Object
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 828 829 830 |
# File 'lib/schwab_rb/clients/base_client.rb', line 802 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) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects SchwabRb::DataObjects::MarketMoversFactory.build(data) else data 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
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 494 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 |
# File 'lib/schwab_rb/clients/base_client.rb', line 468 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 Hash 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) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects SchwabRb::DataObjects::OptionChain.build(data) else data end end |
#get_option_expiration_chain(symbol, return_data_objects: true) ⇒ Object
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 |
# File 'lib/schwab_rb/clients/base_client.rb', line 547 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 Hash refresh_token_if_needed path = "/marketdata/v1/expirationchain" response = get(path, { symbol: symbol }) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects SchwabRb::DataObjects::OptionExpirationChain.build(data) else data end end |
#get_order(order_id, account_hash: nil, return_data_objects: true) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/schwab_rb/clients/base_client.rb', line 126 def get_order(order_id, account_hash: 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. If nil, resolved from SCHWAB_ACCOUNT_NUMBER. # @param return_data_objects [Boolean] Whether to return data objects or Hash resolved_hash = resolve_account_hash(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, {}) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects SchwabRb::DataObjects::Order.build(data) else data 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
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 |
# File 'lib/schwab_rb/clients/base_client.rb', line 563 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 Hash 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) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects SchwabRb::DataObjects::PriceHistory.build(data) else data 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
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 772 |
# File 'lib/schwab_rb/clients/base_client.rb', line 746 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
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 716 |
# File 'lib/schwab_rb/clients/base_client.rb', line 690 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
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 660 |
# File 'lib/schwab_rb/clients/base_client.rb', line 634 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
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 632 |
# File 'lib/schwab_rb/clients/base_client.rb', line 606 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
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 688 |
# File 'lib/schwab_rb/clients/base_client.rb', line 662 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
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 744 |
# File 'lib/schwab_rb/clients/base_client.rb', line 718 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
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 800 |
# File 'lib/schwab_rb/clients/base_client.rb', line 774 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
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/schwab_rb/clients/base_client.rb', line 409 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 Hash 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) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects SchwabRb::DataObjects::QuoteFactory.build(data) else data end end |
#get_quotes(symbols, fields: nil, indicative: nil, return_data_objects: true) ⇒ Object
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/schwab_rb/clients/base_client.rb', line 431 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 Hash 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) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects data.map do |symbol, quote_data| SchwabRb::DataObjects::QuoteFactory.build({ symbol => quote_data }) end else data end end |
#get_transaction(activity_id, account_hash: nil, return_data_objects: true) ⇒ Object
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/schwab_rb/clients/base_client.rb', line 371 def get_transaction(activity_id, account_hash: nil, return_data_objects: true) # Transaction for a specific account. # # @param activity_id [String] ID of the transaction to retrieve # @param account_hash [String] The account hash. If nil, resolved from SCHWAB_ACCOUNT_NUMBER. # @param return_data_objects [Boolean] Whether to return data objects or Hash resolved_hash = resolve_account_hash(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, {}) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects SchwabRb::DataObjects::Transaction.build(data) else data end end end |
#get_transactions(account_hash: nil, start_date: nil, end_date: nil, transaction_types: nil, symbol: nil, return_data_objects: true) ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 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 |
# File 'lib/schwab_rb/clients/base_client.rb', line 311 def get_transactions( account_hash: 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. If nil, resolved from SCHWAB_ACCOUNT_NUMBER. # @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 Hash resolved_hash = resolve_account_hash(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) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects data.map do |transaction_data| SchwabRb::DataObjects::Transaction.build(transaction_data) end else data end end end |
#get_user_preferences(return_data_objects: true) ⇒ Object
394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/schwab_rb/clients/base_client.rb', line 394 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 Hash refresh_token_if_needed path = "/trader/v1/userPreference" response = get(path, {}) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects SchwabRb::DataObjects::UserPreferences.build(data) else data end end |
#place_order(order_spec, account_hash: nil) ⇒ Object
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 244 def place_order(order_spec, account_hash: nil) # Place an order for a specific account. If order creation is successful, # the response will contain the ID of the generated order. # # @param order_spec [Hash, SchwabRb::Orders::Builder] The order specification # @param account_hash [String] The account hash. If nil, resolved from SCHWAB_ACCOUNT_NUMBER. # # 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_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, return_data_objects: true) ⇒ Object
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/schwab_rb/clients/base_client.rb', line 285 def preview_order(order_spec, account_hash: 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 order_spec [Hash, SchwabRb::Orders::Builder] The order specification to preview # @param account_hash [String] The account hash. If nil, resolved from SCHWAB_ACCOUNT_NUMBER. # @param return_data_objects [Boolean] Whether to return data objects or Hash resolved_hash = resolve_account_hash(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) data = JSON.parse(response.body, symbolize_names: true) if return_data_objects SchwabRb::DataObjects::OrderPreview.build(data) else data 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) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/schwab_rb/clients/base_client.rb', line 265 def replace_order(order_id, order_spec, account_hash: 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 order_id [String] The order ID to replace # @param order_spec [Hash, SchwabRb::Orders::Builder] The new order specification # @param account_hash [String] The account hash. If nil, resolved from SCHWAB_ACCOUNT_NUMBER. resolved_hash = resolve_account_hash(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 |