tradier 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.yardopts +8 -0
- data/LICENSE.md +20 -0
- data/README.md +114 -0
- data/Rakefile +19 -0
- data/lib/tradier.rb +31 -0
- data/lib/tradier/account.rb +30 -0
- data/lib/tradier/api/accounts.rb +122 -0
- data/lib/tradier/api/markets.rb +113 -0
- data/lib/tradier/api/orders.rb +84 -0
- data/lib/tradier/api/utils.rb +47 -0
- data/lib/tradier/api/utils/account.rb +15 -0
- data/lib/tradier/api/utils/balance.rb +15 -0
- data/lib/tradier/api/utils/base.rb +46 -0
- data/lib/tradier/api/utils/event.rb +15 -0
- data/lib/tradier/api/utils/expiration.rb +19 -0
- data/lib/tradier/api/utils/gainloss.rb +15 -0
- data/lib/tradier/api/utils/history.rb +15 -0
- data/lib/tradier/api/utils/option_quote.rb +15 -0
- data/lib/tradier/api/utils/order.rb +15 -0
- data/lib/tradier/api/utils/position.rb +15 -0
- data/lib/tradier/api/utils/quote.rb +15 -0
- data/lib/tradier/api/utils/strike.rb +15 -0
- data/lib/tradier/api/utils/timesales.rb +15 -0
- data/lib/tradier/api/utils/watchlist.rb +15 -0
- data/lib/tradier/api/watchlists.rb +139 -0
- data/lib/tradier/balance.rb +17 -0
- data/lib/tradier/base.rb +76 -0
- data/lib/tradier/calendar.rb +23 -0
- data/lib/tradier/client.rb +89 -0
- data/lib/tradier/clock.rb +21 -0
- data/lib/tradier/configurable.rb +76 -0
- data/lib/tradier/core_ext/enumerable.rb +9 -0
- data/lib/tradier/default.rb +75 -0
- data/lib/tradier/error.rb +34 -0
- data/lib/tradier/error/bad_gateway.rb +11 -0
- data/lib/tradier/error/bad_request.rb +10 -0
- data/lib/tradier/error/client_error.rb +28 -0
- data/lib/tradier/error/configuration_error.rb +6 -0
- data/lib/tradier/error/decode_error.rb +9 -0
- data/lib/tradier/error/forbidden.rb +10 -0
- data/lib/tradier/error/gateway_timeout.rb +11 -0
- data/lib/tradier/error/internal_server_error.rb +11 -0
- data/lib/tradier/error/not_acceptable.rb +10 -0
- data/lib/tradier/error/not_found.rb +10 -0
- data/lib/tradier/error/raise_error.rb +31 -0
- data/lib/tradier/error/server_error.rb +28 -0
- data/lib/tradier/error/service_unavailable.rb +11 -0
- data/lib/tradier/error/too_many_requests.rb +12 -0
- data/lib/tradier/error/unauthorized.rb +10 -0
- data/lib/tradier/error/unprocessable_entity.rb +10 -0
- data/lib/tradier/event.rb +8 -0
- data/lib/tradier/history.rb +20 -0
- data/lib/tradier/option_quote.rb +37 -0
- data/lib/tradier/order.rb +14 -0
- data/lib/tradier/position.rb +7 -0
- data/lib/tradier/profile.rb +14 -0
- data/lib/tradier/quote.rb +12 -0
- data/lib/tradier/response/parse_json.rb +25 -0
- data/lib/tradier/response/raise_error.rb +31 -0
- data/lib/tradier/symbol.rb +147 -0
- data/lib/tradier/timesales.rb +11 -0
- data/lib/tradier/version.rb +3 -0
- data/lib/tradier/watchlist.rb +16 -0
- data/lib/tradier/watchlist_item.rb +17 -0
- data/spec/fixtures/account_balances.json +28 -0
- data/spec/fixtures/account_gainloss.json +18 -0
- data/spec/fixtures/account_history.json +96 -0
- data/spec/fixtures/account_orders.json +833 -0
- data/spec/fixtures/account_positions.json +22 -0
- data/spec/fixtures/calendar.json +400 -0
- data/spec/fixtures/chain.json +2972 -0
- data/spec/fixtures/clock.json +10 -0
- data/spec/fixtures/expirations.json +18 -0
- data/spec/fixtures/history.json +2086 -0
- data/spec/fixtures/option_quote.json +14 -0
- data/spec/fixtures/option_quotes.json +26 -0
- data/spec/fixtures/order.json +1 -0
- data/spec/fixtures/order_with_warnings.json +17 -0
- data/spec/fixtures/placed_order.json +6 -0
- data/spec/fixtures/quote.json +45 -0
- data/spec/fixtures/quotes.json +88 -0
- data/spec/fixtures/session.json +6 -0
- data/spec/fixtures/strikes.json +173 -0
- data/spec/fixtures/timesales.json +2956 -0
- data/spec/fixtures/user_balances.json +118 -0
- data/spec/fixtures/user_gainloss.json +6775 -0
- data/spec/fixtures/user_history.json +101 -0
- data/spec/fixtures/user_orders.json +57 -0
- data/spec/fixtures/user_positions.json +50 -0
- data/spec/fixtures/user_profile.json +103 -0
- data/spec/fixtures/watchlist.json +30 -0
- data/spec/fixtures/watchlist_item.json +6 -0
- data/spec/fixtures/watchlists.json +18 -0
- data/spec/spec_helper.rb +64 -0
- data/spec/tradier/account_spec.rb +76 -0
- data/spec/tradier/api/accounts_spec.rb +195 -0
- data/spec/tradier/api/markets_spec.rb +219 -0
- data/spec/tradier/api/orders_spec.rb +94 -0
- data/spec/tradier/api/utils/expiration_spec.rb +8 -0
- data/spec/tradier/api/watchlists_spec.rb +164 -0
- data/spec/tradier/balance_spec.rb +4 -0
- data/spec/tradier/base_spec.rb +11 -0
- data/spec/tradier/calendar_spec.rb +27 -0
- data/spec/tradier/client_spec.rb +125 -0
- data/spec/tradier/clock_spec.rb +73 -0
- data/spec/tradier/error/client_error_spec.rb +22 -0
- data/spec/tradier/error/server_error_spec.rb +22 -0
- data/spec/tradier/error_spec.rb +26 -0
- data/spec/tradier/option_quote_spec.rb +61 -0
- data/spec/tradier/order_spec.rb +43 -0
- data/spec/tradier/position_spec.rb +4 -0
- data/spec/tradier/profile_spec.rb +28 -0
- data/spec/tradier/quote_spec.rb +29 -0
- data/spec/tradier/symbol_spec.rb +54 -0
- data/spec/tradier/watchlist_item_spec.rb +48 -0
- data/spec/tradier/watchlist_spec.rb +35 -0
- data/spec/tradier_spec.rb +105 -0
- data/tradier.gemspec +30 -0
- metadata +302 -0
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'tradier/api/utils'
|
2
|
+
|
3
|
+
module Tradier
|
4
|
+
module API
|
5
|
+
module Orders
|
6
|
+
include Tradier::API::Utils
|
7
|
+
|
8
|
+
# @see https://developer.tradier.com/documentation/trading/create-order
|
9
|
+
# @rate_limited Yes
|
10
|
+
# @authentication Requires user context
|
11
|
+
# Place an order.
|
12
|
+
# @param [Hash] options
|
13
|
+
# @option options [String] :account The account number.
|
14
|
+
# @return [Tradier::Order] An order.
|
15
|
+
# @raise [Tradier::Error::BadRequest] Error raised when supplied order is not valid.
|
16
|
+
# @raise [Tradier::Error::Unauthorized] Error raised when supplied user credentials are not valid.
|
17
|
+
def create_order(options={})
|
18
|
+
account = options.delete(:account)
|
19
|
+
object_from_response(Tradier::Order, :post, "/accounts/#{account}/orders", options)
|
20
|
+
end
|
21
|
+
alias place_order create_order
|
22
|
+
alias submit_order create_order
|
23
|
+
|
24
|
+
# @see https://developer.tradier.com/documentation/trading/preview-order
|
25
|
+
# @rate_limited Yes
|
26
|
+
# @authentication Requires user context
|
27
|
+
# Preview an order.
|
28
|
+
# @return [Tradier::Order] An order.
|
29
|
+
# @raise [Tradier::Error::BadRequest] Error raised when supplied order is not valid.
|
30
|
+
# @raise [Tradier::Error::Unauthorized] Error raised when supplied user credentials are not valid.
|
31
|
+
def preview_order(options={})
|
32
|
+
create_order(options.merge(preview: true))
|
33
|
+
end
|
34
|
+
|
35
|
+
# @see https://developer.tradier.com/documentation/trading/cancel-order
|
36
|
+
# @rate_limited Yes
|
37
|
+
# @authentication Requires user context
|
38
|
+
# Cancel an order.
|
39
|
+
# @param [Hash] options
|
40
|
+
# @option options [String] :account The account number.
|
41
|
+
# @option options [String] :id The id of the order.
|
42
|
+
# @return [Tradier::Order] An order.
|
43
|
+
# @raise [Tradier::Error::BadRequest] Error raised when supplied order is not valid.
|
44
|
+
# @raise [Tradier::Error::Unauthorized] Error raised when supplied user credentials are not valid.
|
45
|
+
def cancel_order(options={})
|
46
|
+
account = options.delete(:account)
|
47
|
+
id = options.delete(:id)
|
48
|
+
object_from_response(Tradier::Order, :delete, "/accounts/#{account}/orders/#{id}", options)
|
49
|
+
end
|
50
|
+
|
51
|
+
# @see https://developer.tradier.com/documentation/accounts/get-account-order
|
52
|
+
# @rate_limited Yes
|
53
|
+
# @authentication Requires user context
|
54
|
+
# Retrieve an order.
|
55
|
+
# @param [Hash] options
|
56
|
+
# @option options [String] :account The account number.
|
57
|
+
# @option options [String] :id The id of the order.
|
58
|
+
# @return [Tradier::Order] An order.
|
59
|
+
# @raise [Tradier::Error::Unauthorized] Error raised when supplied user credentials are not valid.
|
60
|
+
def order(options={})
|
61
|
+
account = options.delete(:account)
|
62
|
+
id = options.delete(:id)
|
63
|
+
object_from_response(Tradier::Order, :get, "/accounts/#{account}/orders/#{id}", options)
|
64
|
+
end
|
65
|
+
|
66
|
+
# @see https://developer.tradier.com/documentation/trading/change-order
|
67
|
+
# @rate_limited Yes
|
68
|
+
# @authentication Requires user context
|
69
|
+
# Update an order.
|
70
|
+
# @param [Hash] options
|
71
|
+
# @option options [String] :account The account number.
|
72
|
+
# @option options [String] :id The id of the order.
|
73
|
+
# @return [Tradier::Order] An order.
|
74
|
+
# @raise [Tradier::Error::BadRequest] Error raised when supplied order is not valid.
|
75
|
+
# @raise [Tradier::Error::Unauthorized] Error raised when supplied user credentials are not valid.
|
76
|
+
def update_order(options={})
|
77
|
+
account = options.delete(:account)
|
78
|
+
id = options.delete(:id)
|
79
|
+
object_from_response(Tradier::Order, :put, "/accounts/#{account}/orders/#{id}", options)
|
80
|
+
end
|
81
|
+
alias change_order update_order
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'tradier/account'
|
2
|
+
require 'tradier/balance'
|
3
|
+
require 'tradier/calendar'
|
4
|
+
require 'tradier/clock'
|
5
|
+
require 'tradier/history'
|
6
|
+
require 'tradier/option_quote'
|
7
|
+
require 'tradier/order'
|
8
|
+
require 'tradier/position'
|
9
|
+
require 'tradier/profile'
|
10
|
+
require 'tradier/quote'
|
11
|
+
require 'tradier/timesales'
|
12
|
+
require 'tradier/watchlist'
|
13
|
+
require 'tradier/event'
|
14
|
+
|
15
|
+
require 'tradier/api/utils/account'
|
16
|
+
require 'tradier/api/utils/balance'
|
17
|
+
require 'tradier/api/utils/expiration'
|
18
|
+
require 'tradier/api/utils/gainloss'
|
19
|
+
require 'tradier/api/utils/history'
|
20
|
+
require 'tradier/api/utils/option_quote'
|
21
|
+
require 'tradier/api/utils/order'
|
22
|
+
require 'tradier/api/utils/position'
|
23
|
+
require 'tradier/api/utils/quote'
|
24
|
+
require 'tradier/api/utils/strike'
|
25
|
+
require 'tradier/api/utils/timesales'
|
26
|
+
require 'tradier/api/utils/watchlist'
|
27
|
+
require 'tradier/api/utils/event'
|
28
|
+
|
29
|
+
module Tradier
|
30
|
+
module API
|
31
|
+
module Utils
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
# @param klass [Class]
|
36
|
+
# @param request_method [Symbol]
|
37
|
+
# @param path [String]
|
38
|
+
# @param options [Hash]
|
39
|
+
# @return [klass instance]
|
40
|
+
def object_from_response(klass, request_method, path, options={})
|
41
|
+
response_body = send(request_method.to_sym, path, options)[:body]
|
42
|
+
klass.from_response(response_body)
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'tradier/api/utils/base'
|
2
|
+
|
3
|
+
module Tradier
|
4
|
+
module API
|
5
|
+
module Utils
|
6
|
+
class Base
|
7
|
+
|
8
|
+
def self.from_response(body)
|
9
|
+
self.new(body)
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(attrs)
|
13
|
+
@attrs = attrs
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def nested_array(klass, response_path)
|
19
|
+
begin
|
20
|
+
nested_attrs = nested_response_body(response_path)
|
21
|
+
if nested_attrs.is_a?(Array)
|
22
|
+
nested_attrs.map do |element|
|
23
|
+
klass.from_response(element)
|
24
|
+
end
|
25
|
+
else
|
26
|
+
[klass.from_response(nested_attrs)]
|
27
|
+
end
|
28
|
+
rescue TypeError => e
|
29
|
+
[]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def nested_response_body(path)
|
34
|
+
body = @attrs.dup
|
35
|
+
|
36
|
+
path.each do |p|
|
37
|
+
body = body[p]
|
38
|
+
end
|
39
|
+
|
40
|
+
body
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'tradier/api/utils/base'
|
2
|
+
|
3
|
+
module Tradier
|
4
|
+
module API
|
5
|
+
module Utils
|
6
|
+
class Expiration < Tradier::API::Utils::Base
|
7
|
+
|
8
|
+
def body
|
9
|
+
return [] unless @attrs[:expirations].kind_of?(Hash)
|
10
|
+
|
11
|
+
@attrs[:expirations].fetch(:date, []).map do |element|
|
12
|
+
Date.parse(element)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,139 @@
|
|
1
|
+
require 'tradier/api/utils'
|
2
|
+
|
3
|
+
module Tradier
|
4
|
+
module API
|
5
|
+
module Watchlists
|
6
|
+
include Tradier::API::Utils
|
7
|
+
|
8
|
+
# @see https://developer.tradier.com/documentation/watchlists/get-watchlists
|
9
|
+
# @rate_limited Yes
|
10
|
+
# @authentication Requires user context
|
11
|
+
# Get all watchlists.
|
12
|
+
# @return [Array<Tradier::Watchlist>] An array of watchlists.
|
13
|
+
# @raise [Tradier::Error::Unauthorized] Error raised when supplied user credentials are not valid.
|
14
|
+
def watchlists(options={})
|
15
|
+
response = object_from_response(Tradier::API::Utils::Watchlist, :get, '/watchlists', options).body
|
16
|
+
end
|
17
|
+
|
18
|
+
# @see https://developer.tradier.com/documentation/watchlists/get-specific-watchlist
|
19
|
+
# @rate_limited Yes
|
20
|
+
# @authentication Requires user context
|
21
|
+
# Get a watchlist.
|
22
|
+
# @param [String] id The watchlist id.
|
23
|
+
# @return [Tradier::Watchlist] A watchlist.
|
24
|
+
# @raise [Tradier::Error::NotFound] Error raised when watchlist is not found.
|
25
|
+
# @raise [Tradier::Error::Unauthorized] Error raised when supplied user credentials are not valid.
|
26
|
+
def watchlist(id, options={})
|
27
|
+
object_from_response(Tradier::Watchlist, :get, "/watchlists/#{id}", options)
|
28
|
+
end
|
29
|
+
|
30
|
+
# @see https://developer.tradier.com/documentation/watchlists/delete-watchlist
|
31
|
+
# @rate_limited Yes
|
32
|
+
# @authentication Requires user context
|
33
|
+
# Delete a watchlist.
|
34
|
+
# @param [String] id The watchlist id.
|
35
|
+
# @return [Boolean] Result of watchlist removal.
|
36
|
+
# @raise [Tradier::Error::NotFound] Error raised when watchlist is not found.
|
37
|
+
# @raise [Tradier::Error::Unauthorized] Error raised when supplied user credentials are not valid.
|
38
|
+
def delete_watchlist(id, options={})
|
39
|
+
send(:delete, "/watchlists/#{id}", options)[:status] == 200
|
40
|
+
end
|
41
|
+
|
42
|
+
# @see https://developer.tradier.com/documentation/watchlists/create-watchlist
|
43
|
+
# @rate_limited Yes
|
44
|
+
# @authentication Requires user context
|
45
|
+
# Create a new watchlist.
|
46
|
+
# @param [Hash] options
|
47
|
+
# @option options [String] :name The watchlist name.
|
48
|
+
# @return [Tradier::Watchlist] A watchlist.
|
49
|
+
# @raise [Tradier::Error::BadRequest] Error raised when request is not valid.
|
50
|
+
# @raise [Tradier::Error::Unauthorized] Error raised when supplied user credentials are not valid.
|
51
|
+
def create_watchlist(options={})
|
52
|
+
object_from_response(Tradier::Watchlist, :post, '/watchlists', options)
|
53
|
+
end
|
54
|
+
|
55
|
+
# @see https://developer.tradier.com/documentation/watchlists/update-watchlist
|
56
|
+
# @rate_limited Yes
|
57
|
+
# @authentication Requires user context
|
58
|
+
# Update a watchlist.
|
59
|
+
# @param [String] id The watchlist id.
|
60
|
+
# @param [Hash] options
|
61
|
+
# @option options [String] :name The watchlist name.
|
62
|
+
# @return [Tradier::Watchlist] A watchlist.
|
63
|
+
# @raise [Tradier::Error::BadRequest] Error raised when request is not valid.
|
64
|
+
# @raise [Tradier::Error::Unauthorized] Error raised when supplied user credentials are not valid.
|
65
|
+
def update_watchlist(id, options={})
|
66
|
+
object_from_response(Tradier::Watchlist, :put, "/watchlists/#{id}", options)
|
67
|
+
end
|
68
|
+
|
69
|
+
# @see https://developer.tradier.com/documentation/watchlists/update-watchlist
|
70
|
+
# @rate_limited Yes
|
71
|
+
# @authentication Requires user context
|
72
|
+
# Retrieve a watchlist symbol.
|
73
|
+
# @param [String] id The watchlist id.
|
74
|
+
# @param [String] symbol The watchlist symbol.
|
75
|
+
# @return [Tradier::WatchlistItem] A watchlist item.
|
76
|
+
# @raise [Tradier::Error::NotFound] Error raised when watchlist or watchlist item is not found.
|
77
|
+
# @raise [Tradier::Error::Unauthorized] Error raised when supplied user credentials are not valid.
|
78
|
+
def watchlist_item(id, symbol, options={})
|
79
|
+
object_from_response(Tradier::WatchlistItem, :get, "/watchlists/#{id}/symbols/#{symbol}", options)
|
80
|
+
end
|
81
|
+
alias watchlist_symbol watchlist_item
|
82
|
+
|
83
|
+
# @see https://developer.tradier.com/documentation/watchlists/add-watchlist-symbol
|
84
|
+
# @rate_limited Yes
|
85
|
+
# @authentication Requires user context
|
86
|
+
# Update a watchlist symbol.
|
87
|
+
# @param [String] id The watchlist id.
|
88
|
+
# @param [Hash] options
|
89
|
+
# @option options [String] :symbols A comma delimited list of symbols.
|
90
|
+
# @return [Tradier::Watchlist] A watchlist.
|
91
|
+
# @raise [Tradier::Error::BadRequest] Error raised when request is not valid.
|
92
|
+
# @raise [Tradier::Error::NotFound] Error raised when watchlist is not found.
|
93
|
+
# @raise [Tradier::Error::Unauthorized] Error raised when supplied user credentials are not valid.
|
94
|
+
def add_watchlist_item(id, options={})
|
95
|
+
object_from_response(Tradier::WatchlistItem, :post, "/watchlists/#{id}/symbols", options)
|
96
|
+
end
|
97
|
+
alias create_watchlist_item add_watchlist_item
|
98
|
+
alias create_watchlist_symbol add_watchlist_item
|
99
|
+
alias add_watchlist_symbol add_watchlist_item
|
100
|
+
|
101
|
+
# @see https://developer.tradier.com/documentation/watchlists/remove-watchlist-symbol
|
102
|
+
# @rate_limited Yes
|
103
|
+
# @authentication Requires user context
|
104
|
+
# Remove a watchlist symbol.
|
105
|
+
# @param [String] id The watchlist id.
|
106
|
+
# @param [String] symbol The watchlist symbol.
|
107
|
+
# @return [Tradier::Watchlist] A watchlist.
|
108
|
+
# @raise [Tradier::Error::NotFound] Error raised when watchlist or watchlist item is not found.
|
109
|
+
# @raise [Tradier::Error::BadRequest] Error raised when request is not valid.
|
110
|
+
# @raise [Tradier::Error::Unauthorized] Error raised when supplied user credentials are not valid.
|
111
|
+
def remove_watchlist_item(id, symbol, options={})
|
112
|
+
send(:delete, "/watchlists/#{id}/symbols/#{symbol}", options)[:status] == 200
|
113
|
+
end
|
114
|
+
alias delete_watchlist_item remove_watchlist_item
|
115
|
+
alias remove_watchlist_symbol remove_watchlist_item
|
116
|
+
alias delete_watchlist_symbol remove_watchlist_item
|
117
|
+
|
118
|
+
# @see https://developer.tradier.com/documentation/watchlists/update-watchlist-symbol
|
119
|
+
# @rate_limited Yes
|
120
|
+
# @authentication Requires user context
|
121
|
+
# Update a watchlist symbol.
|
122
|
+
# @param [String] id The watchlist id.
|
123
|
+
# @param [String] symbol The watchlist symbol.
|
124
|
+
# @param [Hash] options
|
125
|
+
# @option options [String] :shares The number of shares.
|
126
|
+
# @option options [String] :purchase_date The purchase date.
|
127
|
+
# @option options [String] :purchase_price The purchase price.
|
128
|
+
# @return [Tradier::WatchlistItem] A watchlist item.
|
129
|
+
# @raise [Tradier::Error::NotFound] Error raised when watchlist or watchlist item is not found.
|
130
|
+
# @raise [Tradier::Error::BadRequest] Error raised when request is not valid.
|
131
|
+
# @raise [Tradier::Error::Unauthorized] Error raised when supplied user credentials are not valid.
|
132
|
+
def update_watchlist_item(id, symbol, options={})
|
133
|
+
object_from_response(Tradier::WatchlistItem, :put, "/watchlists/#{id}/symbols/#{symbol}", options)
|
134
|
+
end
|
135
|
+
alias update_watchlist_symbol update_watchlist_item
|
136
|
+
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|