alpaca-trade-api 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7b11b95c51e70df9f8dede233caff85b1f6ba320ef48223f352fb9aad667a7be
4
- data.tar.gz: 34ec4d4d1d1a7e30cdae40bfcff47a672f604776b3165ec5b58ec837d5b37eed
3
+ metadata.gz: 4226e3207279468a4ccafa2fd3bb303bf1d3695446a9e245f42437c6a5209614
4
+ data.tar.gz: a54c2fbe8f5cd7b1f8c0a2a161122b7ef9d4088be73309649c9898a421c75095
5
5
  SHA512:
6
- metadata.gz: 6c6e449816b441014290897abb54fec95c555f8a2d505ae7ca2e61bd11b234660fd72ea1705bd3b6c16d2aff2b55de6d2a349269dcf08bd39c5b7a0e39f251ad
7
- data.tar.gz: 6237db054e3a43eb502be7676d6d13addc1f8d20dd52977297ee454081791360b7e987be4b6bfca5c5b86b4c0cc1e0edaf0acc7922350fbbca283a24ab14787d
6
+ metadata.gz: fc02ec91bf7d1f41adf99142665f0c2d690379afb2e064832453eb49e4afc3ca0fbc5714ce746a011fbac878c2d9d8b95a1235a2c0b9d8a88d68c9137f47d934
7
+ data.tar.gz: 02b4c968b277f98a6d2a017d4917326b96744790611641cbb02066abc0c0f90b23b1c927232a4ca92f3a0dd21c01b33a345fa39fc5bf34cec6078a48897fc16e
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.4.0] - 2020-02-16
8
+ ### Added
9
+ - Supporting [Bracket Orders](https://docs.alpaca.markets/trading-on-alpaca/orders/#bracket-orders)
10
+ - Validating time frame in Client#bars
11
+
7
12
  ## [0.3.0] - 2019-09-19
8
13
  ### Added
9
14
  - Implemented new endpoints:
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- alpaca-trade-api (0.3.0)
4
+ alpaca-trade-api (0.4.0)
5
5
  faraday (~> 0.15)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -26,23 +26,54 @@ By default, the library is configured to use the Paper Trading host - `https://p
26
26
 
27
27
  ```ruby
28
28
  Alpaca::Trade::Api.configure do |config|
29
- config.endpoint = 'https://api.alapca.markets'
29
+ config.endpoint = 'https://api.alpaca.markets'
30
30
  config.key_id = 'A_KEY_ID'
31
31
  config.key_secret = 'A_S3CRET'
32
32
  end
33
33
  ```
34
34
 
35
- ### Account
36
35
 
37
- TODO: Write usage instructions here
36
+ ### Example
38
37
 
39
- ### Asset
38
+ Here's an example on how to use the library to read details of an account's orders in paper trading.
40
39
 
41
- TODO: Write usage instructions here
40
+ ```ruby
41
+ require 'alpaca/trade/api'
42
42
 
43
- ### Trading
43
+ Alpaca::Trade::Api.configure do |config|
44
+ config.endpoint = 'https://paper-api.alpaca.markets'
45
+ config.key_id = 'xxxxxxxx'
46
+ config.key_secret = 'xxxxx'
47
+ end
48
+
49
+ client = Alpaca::Trade::Api::Client.new
50
+ puts client.orders.last.inspect
51
+ ```
44
52
 
45
- TODO: Write usage instructions here
53
+ ### Supported endpoints
54
+
55
+ Here's a table with all currently supported endpoints in this library:
56
+
57
+ | Object | Action | Method |
58
+ |------------------------------------------------------------------------------|---------------------------------------|--------------------------------|
59
+ | [Account](https://docs.alpaca.markets/api-documentation/api-v2/account/) | [GET] Get the account | Client#account |
60
+ | [Orders](https://docs.alpaca.markets/api-documentation/api-v2/orders/) | [GET] Get a list of orders | Client#order |
61
+ | | [POST] Request a new order | Client#new_order |
62
+ | | [GET] Get an order | Client#order(id:) |
63
+ | | [GET] Get an order by client order id | Client#order(id:) |
64
+ | | [PATCH] Replace an order | Client#replace_order |
65
+ | | [DELETE] Cancel all orders | Client#cancel_orders |
66
+ | | [DELETE] Cancel an order | Client#cancel_order(id:) |
67
+ | [Positions](https://docs.alpaca.markets/api-documentation/api-v2/positions/) | [GET] Get open positions | Client#positions |
68
+ | | [GET] Get an open position | Client#position(symbol:) |
69
+ | | [DELETE] Close all positions | Client#close_positions |
70
+ | | [DELETE] Close a position | Client#close_position(symbol:) |
71
+ | [Assets](https://docs.alpaca.markets/api-documentation/api-v2/assets/) | [GET] Get assets | Client#assets |
72
+ | | [GET] Get assets/:id | Client#asset(symbol:) |
73
+ | | [GET] Get an asset | Client#asset(symbol:) |
74
+ | [Calendar](https://docs.alpaca.markets/api-documentation/api-v2/calendar/) | [GET] Get the calendar | Client#calendar(start_date:, end_date:) |
75
+ | [Clock](https://docs.alpaca.markets/api-documentation/api-v2/clock/) | [GET] Get the clock | Client#clock |
76
+ | [Bars](https://docs.alpaca.markets/api-documentation/api-v2/market-data/bars/) | [GET] Get a list of bars | Client#bars(timeframe, symbols, limit:) |
46
77
 
47
78
  ## Development
48
79
 
@@ -9,6 +9,8 @@ module Alpaca
9
9
  class Client
10
10
  attr_reader :data_endpoint, :endpoint, :key_id, :key_secret
11
11
 
12
+ TIMEFRAMES = ['minute', '1Min', '5Min', '15Min', 'day', '1D']
13
+
12
14
  def initialize(endpoint: Alpaca::Trade::Api.configuration.endpoint,
13
15
  key_id: Alpaca::Trade::Api.configuration.key_id,
14
16
  key_secret: Alpaca::Trade::Api.configuration.key_secret)
@@ -35,6 +37,7 @@ module Alpaca
35
37
  end
36
38
 
37
39
  def bars(timeframe, symbols, limit: 100)
40
+ validate_timeframe(timeframe)
38
41
  response = get_request(data_endpoint, "v1/bars/#{timeframe}", symbols: symbols.join(','), limit: limit)
39
42
  json = JSON.parse(response.body)
40
43
  json.keys.each_with_object({}) do |symbol, hash|
@@ -43,6 +46,7 @@ module Alpaca
43
46
  end
44
47
 
45
48
  def calendar(start_date: Date.today, end_date: (Date.today + 30))
49
+ # FIX, use start_date.strftime('%F')
46
50
  params = { "start" => start_date.iso8601, "end" => end_date.iso8601 }
47
51
  response = get_request(endpoint, "v2/calendar", params)
48
52
  json = JSON.parse(response.body)
@@ -94,7 +98,8 @@ module Alpaca
94
98
  end
95
99
 
96
100
  def new_order(symbol:, qty:, side:, type:, time_in_force:, limit_price: nil,
97
- stop_price: nil, extended_hours: false, client_order_id: nil)
101
+ stop_price: nil, extended_hours: false, client_order_id: nil, order_class: nil,
102
+ take_profit: nil, stop_loss: nil)
98
103
 
99
104
  params = {
100
105
  symbol: symbol,
@@ -103,7 +108,10 @@ module Alpaca
103
108
  type: type,
104
109
  time_in_force: time_in_force,
105
110
  limit_price: limit_price,
111
+ order_class: order_class,
106
112
  stop_price: stop_price,
113
+ take_profit: take_profit,
114
+ stop_loss: stop_loss,
107
115
  extended_hours: extended_hours,
108
116
  client_order_id: client_order_id
109
117
  }
@@ -219,6 +227,12 @@ module Alpaca
219
227
  raise InternalServerError, JSON.parse(response.body)['message']
220
228
  end
221
229
  end
230
+
231
+ def validate_timeframe(timeframe)
232
+ unless TIMEFRAMES.include?(timeframe)
233
+ raise ArgumentError, "Invalid timeframe: #{timeframe}. Valid arguments are: #{TIMEFRAMES}"
234
+ end
235
+ end
222
236
  end
223
237
  end
224
238
  end
@@ -4,34 +4,41 @@ module Alpaca
4
4
  module Trade
5
5
  module Api
6
6
  class Order
7
- attr_reader :id, :client_order_id, :created_at, :updated_at, :submitted_at,
8
- :filled_at, :expired_at, :canceled_at, :failed_at, :asset_id, :symbol,
9
- :asset_class, :qty, :filled_qty, :type, :side, :time_in_force, :limit_price,
10
- :stop_price, :filled_avg_price, :status, :extended_hours
7
+ attr_reader :id, :asset_class, :asset_id, :canceled_at, :client_order_id,
8
+ :created_at, :expired_at, :extended_hours, :failed_at, :filled_at, :filled_avg_price,
9
+ :filled_qty, :legs, :limit_price, :order_class, :qty, :replaced_at, :replaced_by,
10
+ :replaces, :side, :status, :stop_price, :submitted_at, :symbol, :time_in_force,
11
+ :type, :updated_at
11
12
 
12
13
  def initialize(json)
13
14
  @id = json['id']
15
+
16
+ @asset_class = json['asset_class']
17
+ @asset_id = json['asset_id']
18
+ @canceled_at = json['canceled_at']
14
19
  @client_order_id = json['client_order_id']
15
20
  @created_at = json['created_at']
16
- @updated_at = json['updated_at']
17
- @submitted_at = json['submitted_at']
18
- @filled_at = json['filled_at']
19
21
  @expired_at = json['expired_at']
20
- @canceled_at = json['canceled_at']
22
+ @extended_hours = json['extended_hours']
21
23
  @failed_at = json['failed_at']
22
- @asset_id = json['asset_id']
23
- @symbol = json['symbol']
24
- @asset_class = json['asset_class']
25
- @qty = json['qty']
24
+ @filled_at = json['filled_at']
25
+ @filled_avg_price = json['filled_avg_price']
26
26
  @filled_qty = json['filled_qty']
27
- @type = json['type']
28
- @side = json['side']
29
- @time_in_force = json['time_in_force']
27
+ @legs = (json['legs'] || []).map {|leg| Order.new(leg)}
30
28
  @limit_price = json['limit_price']
31
- @stop_price = json['stop_price']
32
- @filled_avg_price = json['filled_avg_price']
29
+ @order_class = json['order_class']
30
+ @qty = json['qty']
31
+ @replaced_at = json['replaced_at']
32
+ @replaced_by = json['replaced_by']
33
+ @replaces = json['replaces']
34
+ @side = json['side']
33
35
  @status = json['status']
34
- @extended_hours = json['extended_hours']
36
+ @stop_price = json['stop_price']
37
+ @submitted_at = json['submitted_at']
38
+ @symbol = json['symbol']
39
+ @time_in_force = json['time_in_force']
40
+ @type = json['type']
41
+ @updated_at = json['updated_at']
35
42
  end
36
43
  end
37
44
  end
@@ -3,7 +3,7 @@
3
3
  module Alpaca
4
4
  module Trade
5
5
  module Api
6
- VERSION = '0.3.0'
6
+ VERSION = '0.4.0'
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alpaca-trade-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cloves Carneiro Jr
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-20 00:00:00.000000000 Z
11
+ date: 2020-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler