You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In order to call Alpaca's trade API, you need to obtain API key pairs.
23
-
Replace <key_id> and <secret_key> with what you get from the web console.
22
+
In order to call Alpaca's trade API, you need to sign up for a account and obtain API key pairs. Replace <key_id> and <secret_key> with what you get from the web console.
24
23
25
24
### REST example
26
25
```python
27
26
import alpaca_trade_api as tradeapi
28
27
29
-
api = tradeapi.REST('<key_id>', '<secret_key>')
28
+
api = tradeapi.REST('<key_id>', '<secret_key>', api_version='v2') # or use ENV Vars shown below
30
29
account = api.get_account()
31
30
api.list_positions()
32
31
```
33
32
34
33
## API Document
35
34
36
-
The HTTP API document is located in https://docs.alpaca.markets/
35
+
The HTTP API document is located at https://docs.alpaca.markets/
36
+
37
+
## API Version
38
+
39
+
API Version now defaults to 'v2', however if you still have a 'v1' account, you may need to specify api_version='v1' to properly use the API until you migrate.
37
40
38
41
## Authentication
39
42
40
43
The Alpaca API requires API key ID and secret key, which you can obtain from the
41
44
web console after you sign in. You can pass `key_id` and `secret_key` to the initializers of
42
45
`REST` or `StreamConn` as arguments, or set up environment variables as
43
-
follows.
46
+
outlined below.
44
47
45
-
- APCA_API_KEY_ID: key ID
46
-
- APCA_API_SECRET_KEY: secret key
48
+
## Alpaca Environment Variables
47
49
48
-
## Base URL
50
+
The Alpaca SDK will check the environment for a number of variables which can be used rather than hard-coding these into your scripts.
49
51
50
-
The base URL for API calls defaults to `https://api.alpaca.markets/`. This endpoint
51
-
is for live trading. You can change the base URL to `https://paper-api.alpaca.markets`
52
-
for paper trading. You can specify the API URL with the environment variable `APCA_API_BASE_URL`.
52
+
| Environment | default | Description |
53
+
| ----------- | ------- | ----------- |
54
+
| APCA_API_KEY_ID=<key_id> || Your API Key |
55
+
| APCA_API_SECRET_KEY=<secret_key> || Your API Secret Key |
56
+
| APCA_API_BASE_URL=url |https://api.alpaca.markets (for live)<br/>https://paper-api.alpaca.markets (for paper) | Specify the URL for API calls, *Default is live, you must specify this to switch to paper endpoint!*|
57
+
| APCA_API_DATA_URL=url |https://data.alpaca.markets| Endpoint for data API |
58
+
| APCA_RETRY_MAX=3 | 3 | The number of subsequent API calls to retry on timeouts |
59
+
| APCA_RETRY_WAIT=3 | 3 | seconds to wait between each retry attempt |
60
+
| APCA_RETRY_CODES=429,504 | 429,504 | comma-separated HTTP status code for which retry is attempted |
61
+
| POLYGON_WS_URL | wss://alpaca.socket.polygon.io/stocks | Endpoint for streaming polygon data. You likely don't need to change this unless you want to proxy it for example |
53
62
54
-
The environment variable `APCA_API_DATA_URL` can also be changed to configure the
55
-
endpoint for returning data from the `/bars` endpoint. By default, it will use
56
-
`https://data.alpaca.markets`.
57
63
58
64
## REST
59
65
@@ -79,15 +85,9 @@ The `Entity` class also converts timestamp string field to a pandas.Timestamp
79
85
object. Its `_raw` property returns the original raw primitive data unmarshaled
80
86
from the response JSON text.
81
87
82
-
When a REST API call sees the 429 or 504 status code, this library retries 3 times
83
-
by default, with 3 seconds apart between each call. These are configurable with
84
-
the following environment variables.
85
-
86
-
- APCA_RETRY_MAX: the number of subsequent API calls to retry, defaults to 3
87
-
- APCA_RETRY_WAIT: seconds to wait between each call, defaults to 3
88
-
- APCA_RETRY_CODES: comma-separated HTTP status code for which retry is attempted
88
+
Please note that the API is throttled, currently 200 requests per minute, per account. If your client exceeds this number, a 429 Too many requests status will be returned and this library will retry according to the retry environment variables as configured.
89
89
90
-
If the retry exceeds, or other API error is returned, `alpaca_trade_api.rest.APIError` is raised.
90
+
If the retries are exceeded, or other API error is returned, `alpaca_trade_api.rest.APIError` is raised.
91
91
You can access the following information through this object.
92
92
93
93
- the API error code: `.code` property
@@ -146,7 +146,7 @@ Calls `GET /calendar` and returns a `Calendar` entity.
146
146
147
147
## StreamConn
148
148
149
-
The `StreamConn` class provides WebSocket/NATS-based event-driven
149
+
The `StreamConn` class provides WebSocket-based event-driven
150
150
interfaces. Using the `on` decorator of the instance, you can
151
151
define custom event handlers that are called when the pattern
152
152
is matched on the channel name. Once event handlers are set up,
0 commit comments