schwab-api-ruby 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/CHANGELOG.md +32 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +65 -0
- data/Rakefile +8 -0
- data/lib/schwab/authentication.rb +87 -0
- data/lib/schwab/client.rb +37 -0
- data/lib/schwab/error.rb +20 -0
- data/lib/schwab/operations/base_operation.rb +67 -0
- data/lib/schwab/operations/get_instrument_fundamentals.rb +23 -0
- data/lib/schwab/operations/get_price_history.rb +54 -0
- data/lib/schwab/operations/get_quotes.rb +23 -0
- data/lib/schwab/operations/support/build_watchlist_items.rb +22 -0
- data/lib/schwab/util.rb +12 -0
- data/lib/schwab/version.rb +3 -0
- data/lib/schwab-api-ruby.rb +1 -0
- data/lib/schwab.rb +7 -0
- data/schwab-api-ruby.gemspec +29 -0
- data/spec/spec_helper.rb +110 -0
- data/spec/support/authenticated_client.rb +12 -0
- data/spec/support/spec/mocks/mock_get_instrument_fundamentals.rb +18 -0
- data/spec/support/spec/mocks/mock_get_price_history.rb +18 -0
- data/spec/support/spec/mocks/mock_get_quotes.rb +18 -0
- data/spec/support/spec/mocks/mock_watchlists.rb +44 -0
- data/spec/support/spec/mocks/tdameritrade_api_mock_base.rb +22 -0
- data/spec/support/webmock_off.rb +7 -0
- data/spec/tdameritrade/operations/error_spec.rb +75 -0
- data/spec/tdameritrade/operations/get_instrument_fundamentals_spec.rb +182 -0
- data/spec/tdameritrade/operations/get_price_history_spec.rb +160 -0
- data/spec/tdameritrade/operations/get_quotes_spec.rb +351 -0
- metadata +171 -0
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'pry'
|
2
|
+
require 'webmock/rspec'
|
3
|
+
|
4
|
+
Dir[File.join(File.dirname(__FILE__), 'support', '/**/*.rb')].each { |f| require(f) }
|
5
|
+
|
6
|
+
WebMock.disable_net_connect!
|
7
|
+
|
8
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
9
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
10
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
11
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
12
|
+
# files.
|
13
|
+
#
|
14
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
15
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
16
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
17
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
18
|
+
# a separate helper file that requires the additional dependencies and performs
|
19
|
+
# the additional setup, and require it from the spec files that actually need
|
20
|
+
# it.
|
21
|
+
#
|
22
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
23
|
+
# users commonly want.
|
24
|
+
#
|
25
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
26
|
+
RSpec.configure do |config|
|
27
|
+
# rspec-expectations config goes here. You can use an alternate
|
28
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
29
|
+
# assertions if you prefer.
|
30
|
+
config.expect_with :rspec do |expectations|
|
31
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
32
|
+
# and `failure_message` of custom matchers include text for helper methods
|
33
|
+
# defined using `chain`, e.g.:
|
34
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
35
|
+
# # => "be bigger than 2 and smaller than 4"
|
36
|
+
# ...rather than:
|
37
|
+
# # => "be bigger than 2"
|
38
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
39
|
+
end
|
40
|
+
|
41
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
42
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
43
|
+
config.mock_with :rspec do |mocks|
|
44
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
45
|
+
# a real object. This is generally recommended, and will default to
|
46
|
+
# `true` in RSpec 4.
|
47
|
+
mocks.verify_partial_doubles = true
|
48
|
+
end
|
49
|
+
|
50
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
51
|
+
# have no way to turn it off -- the option exists only for backwards
|
52
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
53
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
54
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
55
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
56
|
+
|
57
|
+
# The settings below are suggested to provide a good initial experience
|
58
|
+
# with RSpec, but feel free to customize to your heart's content.
|
59
|
+
=begin
|
60
|
+
# This allows you to limit a spec run to individual examples or groups
|
61
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
62
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
63
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
64
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
65
|
+
config.filter_run_when_matching :focus
|
66
|
+
|
67
|
+
# Allows RSpec to persist some state between runs in order to support
|
68
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
69
|
+
# you configure your source control system to ignore this file.
|
70
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
71
|
+
|
72
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
73
|
+
# recommended. For more details, see:
|
74
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
75
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
76
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
77
|
+
config.disable_monkey_patching!
|
78
|
+
|
79
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
80
|
+
# be too noisy due to issues in dependencies.
|
81
|
+
config.warnings = true
|
82
|
+
|
83
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
84
|
+
# file, and it's useful to allow more verbose output when running an
|
85
|
+
# individual spec file.
|
86
|
+
if config.files_to_run.one?
|
87
|
+
# Use the documentation formatter for detailed output,
|
88
|
+
# unless a formatter has already been configured
|
89
|
+
# (e.g. via a command-line flag).
|
90
|
+
config.default_formatter = 'doc'
|
91
|
+
end
|
92
|
+
|
93
|
+
# Print the 10 slowest examples and example groups at the
|
94
|
+
# end of the spec run, to help surface which specs are running
|
95
|
+
# particularly slow.
|
96
|
+
config.profile_examples = 10
|
97
|
+
|
98
|
+
# Run specs in random order to surface order dependencies. If you find an
|
99
|
+
# order dependency and want to debug it, you can fix the order by providing
|
100
|
+
# the seed, which is printed after each run.
|
101
|
+
# --seed 1234
|
102
|
+
config.order = :random
|
103
|
+
|
104
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
105
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
106
|
+
# test failures related to randomization by passing the same `--seed` value
|
107
|
+
# as the one that triggered the failure.
|
108
|
+
Kernel.srand config.seed
|
109
|
+
=end
|
110
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
shared_context 'authenticated client' do
|
2
|
+
let!(:client) do
|
3
|
+
# If you turn off WebMock by including the 'webmock_off' shared context in your tests, you can hit the
|
4
|
+
# live API with a real ping. Just be sure to replace these values here with actual connection tokens.
|
5
|
+
Schwab::Client.new(
|
6
|
+
client_id: 'RUBYAPITEST@AMER.OAUTHAP',
|
7
|
+
redirect_uri: 'http://localhost:3000',
|
8
|
+
access_token: 'test_access_token',
|
9
|
+
refresh_token: 'test_refresh_token'
|
10
|
+
)
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'tdameritrade_api_mock_base.rb')
|
2
|
+
|
3
|
+
module Schwab
|
4
|
+
; module Spec; module Mocks;
|
5
|
+
class MockGetInstrumentFundamentals < TDAmeritradeMockBase
|
6
|
+
|
7
|
+
def self.mock_find(request: { query: {}, headers: {} }, response: { status: 200, body: '' })
|
8
|
+
return if webmock_off?
|
9
|
+
|
10
|
+
url_params = build_url_params(request)
|
11
|
+
WebMock
|
12
|
+
.stub_request(:get, "#{API_BASE_URL}/instruments#{url_params}")
|
13
|
+
.with(request)
|
14
|
+
.to_return(response)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end; end; end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'tdameritrade_api_mock_base.rb')
|
2
|
+
|
3
|
+
module Schwab
|
4
|
+
; module Spec; module Mocks;
|
5
|
+
class MockGetPriceHistory < TDAmeritradeMockBase
|
6
|
+
|
7
|
+
def self.mock_find(symbol: nil, request: { query: {}, headers: {} }, response: { status: 200, body: '' })
|
8
|
+
return if webmock_off?
|
9
|
+
|
10
|
+
url_params = build_url_params(request)
|
11
|
+
WebMock
|
12
|
+
.stub_request(:get, "#{API_BASE_URL}/marketdata/#{symbol}/pricehistory#{url_params}")
|
13
|
+
.with(request)
|
14
|
+
.to_return(response)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end; end; end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'tdameritrade_api_mock_base.rb')
|
2
|
+
|
3
|
+
module Schwab
|
4
|
+
; module Spec; module Mocks;
|
5
|
+
class MockGetQuotes < TDAmeritradeMockBase
|
6
|
+
|
7
|
+
def self.mock_find(request: { query: {}, headers: {} }, response: { status: 200, body: '' })
|
8
|
+
return if webmock_off?
|
9
|
+
|
10
|
+
url_params = build_url_params(request)
|
11
|
+
WebMock
|
12
|
+
.stub_request(:get, "#{API_BASE_URL}/marketdata/quotes#{url_params}")
|
13
|
+
.with(request)
|
14
|
+
.to_return(response)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end; end; end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'tdameritrade_api_mock_base.rb')
|
2
|
+
|
3
|
+
module Schwab
|
4
|
+
; module Spec; module Mocks;
|
5
|
+
class MockWatchlists < TDAmeritradeMockBase
|
6
|
+
|
7
|
+
def self.mock_create(account_id, request: { headers: {} }, response: { status: 200, body: '' } )
|
8
|
+
return if webmock_off?
|
9
|
+
|
10
|
+
WebMock
|
11
|
+
.stub_request(:post, "#{API_BASE_URL}/accounts/#{account_id}/watchlists")
|
12
|
+
.with(request)
|
13
|
+
.to_return(response)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.mock_find_for_account(account_id, request: { headers: {} }, response: { status: 200, body: '' })
|
17
|
+
return if webmock_off?
|
18
|
+
|
19
|
+
WebMock
|
20
|
+
.stub_request(:get, "#{API_BASE_URL}/accounts/#{account_id}/watchlists")
|
21
|
+
.with(request)
|
22
|
+
.to_return(response)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.mock_replace(account_id, watchlist_id, request: { headers: {} }, response: { status: 200, body: '' })
|
26
|
+
return if webmock_off?
|
27
|
+
|
28
|
+
WebMock
|
29
|
+
.stub_request(:put, "#{API_BASE_URL}/accounts/#{account_id}/watchlists/#{watchlist_id}")
|
30
|
+
.with(request)
|
31
|
+
.to_return(response)
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.mock_update(account_id, watchlist_id, request: { headers: {} }, response: { status: 200, body: '' })
|
35
|
+
return if webmock_off?
|
36
|
+
|
37
|
+
WebMock
|
38
|
+
.stub_request(:patch, "#{API_BASE_URL}/accounts/#{account_id}/watchlists/#{watchlist_id}")
|
39
|
+
.with(request)
|
40
|
+
.to_return(response)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end; end; end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Schwab
|
2
|
+
; module Spec; module Mocks
|
3
|
+
class TDAmeritradeMockBase
|
4
|
+
|
5
|
+
API_BASE_URL = 'https://api.tdameritrade.com/v1'
|
6
|
+
|
7
|
+
def self.build_url_params(request)
|
8
|
+
# Need to do this to get around a WebMock encoding bug. It's looking for 'APIKEY@AMER.OAUTHAP' when it
|
9
|
+
# should be looking for 'APIKEY%40AMER.OAUTHAP'
|
10
|
+
if request[:query]
|
11
|
+
"?" + request.delete(:query).map { |k,v| "#{k}=#{CGI::escape(v)}" }.join('&')
|
12
|
+
else
|
13
|
+
''
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.webmock_off?
|
18
|
+
WebMock.net_connect_allowed?
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end; end; end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'support/authenticated_client'
|
3
|
+
require 'schwab'
|
4
|
+
|
5
|
+
describe Schwab::Error do
|
6
|
+
include_context 'authenticated client'
|
7
|
+
# include_context 'webmock off'
|
8
|
+
|
9
|
+
pending 'rate limit error' do
|
10
|
+
subject do
|
11
|
+
client.get_quotes(symbols)
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:symbols) { %w(PG MSFT CVX) }
|
15
|
+
|
16
|
+
let!(:expected_request) do
|
17
|
+
Schwab::Spec::Mocks::MockGetQuotes.mock_find(
|
18
|
+
request: {
|
19
|
+
headers: { 'Authorization': "Bearer #{client.access_token}" },
|
20
|
+
query: {
|
21
|
+
apikey: client.client_id,
|
22
|
+
symbol: symbols.join(','),
|
23
|
+
}.map {|k,v| [k, v.to_s] }.to_h,
|
24
|
+
},
|
25
|
+
response: {
|
26
|
+
status: 429,
|
27
|
+
body: <<~RESPONSE
|
28
|
+
{"error":"Rate limit error"}
|
29
|
+
RESPONSE
|
30
|
+
}
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
34
|
+
let(:expected_result) do
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'raises a RateLimitError' do
|
38
|
+
expect { subject }.to raise_error(Schwab::Error::RateLimitError)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
pending 'access token is invalid or expired' do
|
43
|
+
subject do
|
44
|
+
client.get_quotes(symbols)
|
45
|
+
end
|
46
|
+
|
47
|
+
let(:symbols) { %w(PG MSFT CVX) }
|
48
|
+
|
49
|
+
let!(:expected_request) do
|
50
|
+
Schwab::Spec::Mocks::MockGetQuotes.mock_find(
|
51
|
+
request: {
|
52
|
+
headers: { 'Authorization': "Bearer #{client.access_token}" },
|
53
|
+
query: {
|
54
|
+
apikey: client.client_id,
|
55
|
+
symbol: symbols.join(','),
|
56
|
+
}.map {|k,v| [k, v.to_s] }.to_h,
|
57
|
+
},
|
58
|
+
response: {
|
59
|
+
status: 401,
|
60
|
+
body: <<~RESPONSE
|
61
|
+
{"error":"Not Authorized."}
|
62
|
+
RESPONSE
|
63
|
+
}
|
64
|
+
)
|
65
|
+
end
|
66
|
+
|
67
|
+
let(:expected_result) do
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'raises a NotAuthorizedError' do
|
71
|
+
expect { subject }.to raise_error(Schwab::Error::NotAuthorizedError)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
@@ -0,0 +1,182 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'support/authenticated_client'
|
3
|
+
require 'schwab'
|
4
|
+
|
5
|
+
describe Schwab::Operations::GetInstrumentFundamentals do
|
6
|
+
include_context 'authenticated client'
|
7
|
+
# include_context 'webmock off'
|
8
|
+
|
9
|
+
pending 'valid symbol' do
|
10
|
+
subject do
|
11
|
+
client.get_instrument_fundamentals(symbol)
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:symbol) { 'PG' }
|
15
|
+
|
16
|
+
let!(:expected_request) do
|
17
|
+
Schwab::Spec::Mocks::MockGetInstrumentFundamentals.mock_find(
|
18
|
+
request: {
|
19
|
+
headers: { 'Authorization': "Bearer #{client.access_token}" },
|
20
|
+
query: {
|
21
|
+
apikey: client.client_id,
|
22
|
+
symbol: symbol,
|
23
|
+
projection: 'fundamental'
|
24
|
+
}.map {|k,v| [k, v.to_s] }.to_h,
|
25
|
+
},
|
26
|
+
response: {
|
27
|
+
body: <<~RESPONSE
|
28
|
+
{
|
29
|
+
"PG": {
|
30
|
+
"fundamental": {
|
31
|
+
"symbol": "PG",
|
32
|
+
"high52": 121.76,
|
33
|
+
"low52": 78.49,
|
34
|
+
"dividendAmount": 2.9836,
|
35
|
+
"dividendYield": 2.54,
|
36
|
+
"dividendDate": "2019-07-18 00:00:00.000",
|
37
|
+
"peRatio": 79.14674,
|
38
|
+
"pegRatio": 0.0,
|
39
|
+
"pbRatio": 6.35135,
|
40
|
+
"prRatio": 4.33729,
|
41
|
+
"pcfRatio": 44.97703,
|
42
|
+
"grossMarginTTM": 49.84339,
|
43
|
+
"grossMarginMRQ": 50.80145,
|
44
|
+
"netProfitMarginTTM": 5.85958,
|
45
|
+
"netProfitMarginMRQ": 0.0,
|
46
|
+
"operatingMarginTTM": 8.61208,
|
47
|
+
"operatingMarginMRQ": 0.0,
|
48
|
+
"returnOnEquity": 7.44733,
|
49
|
+
"returnOnAssets": 3.39838,
|
50
|
+
"returnOnInvestment": 4.55386,
|
51
|
+
"quickRatio": 0.58165,
|
52
|
+
"currentRatio": 0.74883,
|
53
|
+
"interestCoverage": 467.625,
|
54
|
+
"totalDebtToCapital": 38.7429,
|
55
|
+
"ltDebtToEquity": 43.21524,
|
56
|
+
"totalDebtToEquity": 63.76234,
|
57
|
+
"epsTTM": 1.48231,
|
58
|
+
"epsChangePercentTTM": 0.0,
|
59
|
+
"epsChangeYear": 0.0,
|
60
|
+
"epsChange": 0.0,
|
61
|
+
"revChangeYear": 0.0,
|
62
|
+
"revChangeTTM": 1.27484,
|
63
|
+
"revChangeIn": 3.83914,
|
64
|
+
"sharesOutstanding": 2502.26,
|
65
|
+
"marketCapFloat": 2499.515,
|
66
|
+
"marketCap": 293565.1,
|
67
|
+
"bookValuePerShare": 0.0,
|
68
|
+
"shortIntToFloat": 0.0,
|
69
|
+
"shortIntDayToCover": 0.0,
|
70
|
+
"divGrowthRate3Year": 0.0,
|
71
|
+
"dividendPayAmount": 0.7459,
|
72
|
+
"dividendPayDate": "2019-11-15 00:00:00.000",
|
73
|
+
"beta": 0.40382,
|
74
|
+
"vol1DayAvg": 6650680.0,
|
75
|
+
"vol10DayAvg": 6548680.0,
|
76
|
+
"vol3MonthAvg": 161420290.0
|
77
|
+
},
|
78
|
+
"cusip": "742718109",
|
79
|
+
"symbol": "PG",
|
80
|
+
"description": "Procter & Gamble Company (The) Common Stock",
|
81
|
+
"exchange": "NYSE",
|
82
|
+
"assetType": "EQUITY"
|
83
|
+
}
|
84
|
+
}
|
85
|
+
RESPONSE
|
86
|
+
}
|
87
|
+
)
|
88
|
+
end
|
89
|
+
|
90
|
+
let(:expected_result) do
|
91
|
+
{"PG"=>
|
92
|
+
{"fundamental"=>
|
93
|
+
{"symbol"=>"PG",
|
94
|
+
"high52"=>121.76,
|
95
|
+
"low52"=>78.49,
|
96
|
+
"dividendAmount"=>2.9836,
|
97
|
+
"dividendYield"=>2.54,
|
98
|
+
"dividendDate"=>"2019-07-18 00:00:00.000",
|
99
|
+
"peRatio"=>79.14674,
|
100
|
+
"pegRatio"=>0.0,
|
101
|
+
"pbRatio"=>6.35135,
|
102
|
+
"prRatio"=>4.33729,
|
103
|
+
"pcfRatio"=>44.97703,
|
104
|
+
"grossMarginTTM"=>49.84339,
|
105
|
+
"grossMarginMRQ"=>50.80145,
|
106
|
+
"netProfitMarginTTM"=>5.85958,
|
107
|
+
"netProfitMarginMRQ"=>0.0,
|
108
|
+
"operatingMarginTTM"=>8.61208,
|
109
|
+
"operatingMarginMRQ"=>0.0,
|
110
|
+
"returnOnEquity"=>7.44733,
|
111
|
+
"returnOnAssets"=>3.39838,
|
112
|
+
"returnOnInvestment"=>4.55386,
|
113
|
+
"quickRatio"=>0.58165,
|
114
|
+
"currentRatio"=>0.74883,
|
115
|
+
"interestCoverage"=>467.625,
|
116
|
+
"totalDebtToCapital"=>38.7429,
|
117
|
+
"ltDebtToEquity"=>43.21524,
|
118
|
+
"totalDebtToEquity"=>63.76234,
|
119
|
+
"epsTTM"=>1.48231,
|
120
|
+
"epsChangePercentTTM"=>0.0,
|
121
|
+
"epsChangeYear"=>0.0,
|
122
|
+
"epsChange"=>0.0,
|
123
|
+
"revChangeYear"=>0.0,
|
124
|
+
"revChangeTTM"=>1.27484,
|
125
|
+
"revChangeIn"=>3.83914,
|
126
|
+
"sharesOutstanding"=>2502.26,
|
127
|
+
"marketCapFloat"=>2499.515,
|
128
|
+
"marketCap"=>293565.1,
|
129
|
+
"bookValuePerShare"=>0.0,
|
130
|
+
"shortIntToFloat"=>0.0,
|
131
|
+
"shortIntDayToCover"=>0.0,
|
132
|
+
"divGrowthRate3Year"=>0.0,
|
133
|
+
"dividendPayAmount"=>0.7459,
|
134
|
+
"dividendPayDate"=>"2019-11-15 00:00:00.000",
|
135
|
+
"beta"=>0.40382,
|
136
|
+
"vol1DayAvg"=>6650680.0,
|
137
|
+
"vol10DayAvg"=>6548680.0,
|
138
|
+
"vol3MonthAvg"=>161420290.0},
|
139
|
+
"cusip"=>"742718109",
|
140
|
+
"symbol"=>"PG",
|
141
|
+
"description"=>"Procter & Gamble Company (The) Common Stock",
|
142
|
+
"exchange"=>"NYSE",
|
143
|
+
"assetType"=>"EQUITY"}
|
144
|
+
}
|
145
|
+
end
|
146
|
+
|
147
|
+
it { is_expected.to eql(expected_result) }
|
148
|
+
end
|
149
|
+
|
150
|
+
context 'invalid symbol' do
|
151
|
+
subject do
|
152
|
+
client.get_instrument_fundamentals(invalid_symbol)
|
153
|
+
end
|
154
|
+
|
155
|
+
let(:invalid_symbol) { 'XXX' }
|
156
|
+
|
157
|
+
let!(:expected_request) do
|
158
|
+
Schwab::Spec::Mocks::MockGetInstrumentFundamentals.mock_find(
|
159
|
+
request: {
|
160
|
+
headers: { 'Authorization': "Bearer #{client.access_token}" },
|
161
|
+
query: {
|
162
|
+
apikey: client.client_id,
|
163
|
+
symbol: invalid_symbol,
|
164
|
+
projection: 'fundamental'
|
165
|
+
}.map {|k,v| [k, v.to_s] }.to_h,
|
166
|
+
},
|
167
|
+
response: {
|
168
|
+
body: <<~RESPONSE
|
169
|
+
{}
|
170
|
+
RESPONSE
|
171
|
+
}
|
172
|
+
)
|
173
|
+
end
|
174
|
+
|
175
|
+
let(:expected_result) do
|
176
|
+
{}
|
177
|
+
end
|
178
|
+
|
179
|
+
it { is_expected.to eql(expected_result) }
|
180
|
+
end
|
181
|
+
|
182
|
+
end
|
@@ -0,0 +1,160 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'support/authenticated_client'
|
3
|
+
require 'schwab'
|
4
|
+
|
5
|
+
describe Schwab::Operations::GetPriceHistory do
|
6
|
+
include_context 'authenticated client'
|
7
|
+
# include_context 'webmock off'
|
8
|
+
|
9
|
+
let(:single_ticker) { 'PG' }
|
10
|
+
let(:multiple_tickers) { ['PG', 'MSFT', 'BA'] }
|
11
|
+
|
12
|
+
pending '5 days of 5-min candles' do
|
13
|
+
context 'single ticker' do
|
14
|
+
subject do
|
15
|
+
client.get_price_history(single_ticker, period_type: :day, period: 10, frequency: 5, frequency_type: :minute)
|
16
|
+
end
|
17
|
+
|
18
|
+
let!(:expected_request) do
|
19
|
+
Schwab::Spec::Mocks::MockGetPriceHistory.mock_find(
|
20
|
+
symbol: single_ticker,
|
21
|
+
request: {
|
22
|
+
headers: { 'Authorization': "Bearer #{client.access_token}" },
|
23
|
+
query: {
|
24
|
+
apikey: client.client_id,
|
25
|
+
periodType: 'day',
|
26
|
+
period: 10,
|
27
|
+
frequencyType: 'minute',
|
28
|
+
frequency: 5,
|
29
|
+
needExtendedHoursData: 'false'
|
30
|
+
}.map {|k,v| [k, v.to_s] }.to_h,
|
31
|
+
},
|
32
|
+
response: {
|
33
|
+
body: <<~RESPONSE
|
34
|
+
{
|
35
|
+
"candles": [
|
36
|
+
{
|
37
|
+
"open": 119.44,
|
38
|
+
"high": 119.54,
|
39
|
+
"low": 119.44,
|
40
|
+
"close": 119.54,
|
41
|
+
"volume": 208,
|
42
|
+
"datetime": 1566558000000
|
43
|
+
},
|
44
|
+
{
|
45
|
+
"open": 119.54,
|
46
|
+
"high": 119.54,
|
47
|
+
"low": 119.54,
|
48
|
+
"close": 119.54,
|
49
|
+
"volume": 104,
|
50
|
+
"datetime": 1566559800000
|
51
|
+
}
|
52
|
+
],
|
53
|
+
"symbol": "PG",
|
54
|
+
"empty": false
|
55
|
+
}
|
56
|
+
RESPONSE
|
57
|
+
}
|
58
|
+
)
|
59
|
+
end
|
60
|
+
|
61
|
+
let(:expected_result) do
|
62
|
+
{
|
63
|
+
"candles"=>
|
64
|
+
[{"open"=>119.44, "high"=>119.54, "low"=>119.44, "close"=>119.54, "volume"=>208, "datetime"=>Time.at(1566558000000 / 1000)},
|
65
|
+
{"open"=>119.54, "high"=>119.54, "low"=>119.54, "close"=>119.54, "volume"=>104, "datetime"=>Time.at(1566559800000 / 1000)}],
|
66
|
+
"symbol"=>"PG",
|
67
|
+
"empty"=>false
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
it { is_expected.to eql(expected_result) }
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'multiple tickers' do
|
75
|
+
pending 'does not appear to be supported yet by the API'
|
76
|
+
end
|
77
|
+
|
78
|
+
pending 'invalid ticker or one with no data' do
|
79
|
+
subject do
|
80
|
+
client.get_price_history(invalid_ticker, period: 1, period_type: :month, frequency: 5, frequency_type: :minute)
|
81
|
+
end
|
82
|
+
let(:invalid_ticker) { 'XXX' }
|
83
|
+
|
84
|
+
let!(:expected_request) do
|
85
|
+
Schwab::Spec::Mocks::MockGetPriceHistory.mock_find(
|
86
|
+
symbol: invalid_ticker,
|
87
|
+
request: {
|
88
|
+
headers: { 'Authorization': "Bearer #{client.access_token}" },
|
89
|
+
query: {
|
90
|
+
apikey: client.client_id,
|
91
|
+
periodType: 'month',
|
92
|
+
period: 1,
|
93
|
+
frequencyType: 'minute',
|
94
|
+
frequency: 5,
|
95
|
+
needExtendedHoursData: 'false'
|
96
|
+
}.map {|k,v| [k, v.to_s] }.to_h,
|
97
|
+
},
|
98
|
+
response: {
|
99
|
+
body: <<~RESPONSE
|
100
|
+
{"candles":[],"symbol":"XXX","empty":true}
|
101
|
+
RESPONSE
|
102
|
+
}
|
103
|
+
)
|
104
|
+
end
|
105
|
+
|
106
|
+
let(:expected_result) do
|
107
|
+
{
|
108
|
+
"candles"=> [],
|
109
|
+
"symbol"=>"XXX",
|
110
|
+
"empty"=>true
|
111
|
+
}
|
112
|
+
end
|
113
|
+
|
114
|
+
it { is_expected.to eql(expected_result) }
|
115
|
+
end
|
116
|
+
|
117
|
+
pending 'invalid request due to mismatched frequency type and interval' do
|
118
|
+
subject do
|
119
|
+
# Can't use minute with the monthly period type
|
120
|
+
client.get_price_history(
|
121
|
+
single_ticker,
|
122
|
+
period_type: :month,
|
123
|
+
period: 10,
|
124
|
+
frequency: 5,
|
125
|
+
frequency_type: :minute,
|
126
|
+
need_extended_hours_data: true
|
127
|
+
)
|
128
|
+
end
|
129
|
+
|
130
|
+
let!(:expected_request) do
|
131
|
+
Schwab::Spec::Mocks::MockGetPriceHistory.mock_find(
|
132
|
+
symbol: single_ticker,
|
133
|
+
request: {
|
134
|
+
headers: { 'Authorization': "Bearer #{client.access_token}" },
|
135
|
+
query: {
|
136
|
+
apikey: client.client_id,
|
137
|
+
periodType: 'month',
|
138
|
+
period: 10,
|
139
|
+
frequencyType: 'minute',
|
140
|
+
frequency: 5,
|
141
|
+
needExtendedHoursData: 'true'
|
142
|
+
}.map {|k,v| [k, v.to_s] }.to_h,
|
143
|
+
},
|
144
|
+
response: {
|
145
|
+
status: 400,
|
146
|
+
body: <<~RESPONSE
|
147
|
+
{"error":"Bad request."}
|
148
|
+
RESPONSE
|
149
|
+
}
|
150
|
+
)
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'raises an error' do
|
154
|
+
expect { subject }.to raise_error(Schwab::Error::SchwabAPIError, "400: Bad request.")
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
|
160
|
+
end
|