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
Copy file name to clipboardExpand all lines: README.md
+35-77Lines changed: 35 additions & 77 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@
6
6
# alpaca-trade-api-python
7
7
8
8
`alpaca-trade-api-python` is a python library for the [Alpaca Commission Free Trading API](https://alpaca.markets).
9
-
It allows rapid trading algo development easily, with support for the
9
+
It allows rapid trading algo development easily, with support for
10
10
both REST and streaming data interfaces. For details of each API behavior,
11
11
please see the online [API document](https://docs.alpaca.markets).
12
12
@@ -21,7 +21,7 @@ $ pip3 install alpaca-trade-api
21
21
22
22
## Example
23
23
24
-
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
+
In order to call Alpaca's trade API, you need to sign up for an account and obtain API key pairs. Replace <key_id> and <secret_key> with what you get from the web console.
25
25
26
26
### REST example
27
27
```python
@@ -42,7 +42,7 @@ The HTTP API document is located at https://docs.alpaca.markets/
42
42
43
43
## API Version
44
44
45
-
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.
45
+
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.
46
46
47
47
## Authentication
48
48
@@ -53,7 +53,7 @@ outlined below.
53
53
54
54
## Alpaca Environment Variables
55
55
56
-
The Alpaca SDK will check the environment for a number of variables which can be used rather than hard-coding these into your scripts.
56
+
The Alpaca SDK will check the environment for a number of variables that can be used rather than hard-coding these into your scripts.
@@ -66,15 +66,14 @@ The Alpaca SDK will check the environment for a number of variables which can be
66
66
| APCA_RETRY_CODES=429,504 | 429,504 | comma-separated HTTP status code for which retry is attempted |
67
67
| POLYGON_WS_URL | wss://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 |
68
68
| POLYGON_KEY_ID || Your Polygon key, if it's not the same as your Alpaca API key. Most users will not need to set this to access Polygon. |
69
-
| ALPHAVANTAGE_API_KEY=<key_id> || Your Alpha Vantage API key. You can get [one for free here](https://www.alphavantage.co/support/#api-key). |
70
69
71
70
## REST
72
71
73
72
The `REST` class is the entry point for the API request. The instance of this
74
73
class provides all REST API calls such as account, orders, positions,
75
74
and bars.
76
75
77
-
Each returned object is wrapped by a subclass of `Entity` class (or a list of it).
76
+
Each returned object is wrapped by a subclass of the `Entity` class (or a list of it).
78
77
This helper class provides property access (the "dot notation") to the
79
78
json object, backed by the original object stored in the `_raw` field.
80
79
It also converts certain types to the appropriate python object.
@@ -88,7 +87,7 @@ account.status
88
87
=>'ACTIVE'
89
88
```
90
89
91
-
The `Entity` class also converts timestamp string field to a pandas.Timestamp
90
+
The `Entity` class also converts the timestamp string field to a pandas.Timestamp
92
91
object. Its `_raw` property returns the original raw primitive data unmarshaled
93
92
from the response JSON text.
94
93
@@ -221,9 +220,9 @@ exception is raised, and each event handler is called asynchronously
221
220
upon the message arrivals.
222
221
223
222
The `run` method tries to reconnect to the server in the event of
224
-
connection failure. In this case you may want to reset your state
223
+
connection failure. In this case, you may want to reset your state
225
224
which is best in the `connect` event. The method still raises
226
-
exception in the case any other unknown error happens inside the
225
+
an exception in the case any other unknown error happens inside the
227
226
event loop.
228
227
229
228
The `msg` object passed to each handler is wrapped by the entity
@@ -285,8 +284,29 @@ same `channel_pat` will overwrite the old handler.
285
284
Deregisters the event handler function that was previously registered via `on` or
286
285
`register` method.
287
286
287
+
#### Debugging
288
+
Websocket exceptions may occur during execution.
289
+
It will usually happen during the `consume()` method, which basically is the
290
+
websocket steady-state.<br>
291
+
exceptions during the consume method may occur due to:
292
+
- server disconnections
293
+
- error while handling the response data
294
+
295
+
We handle the first issue by reconnecting the websocket every time there's a disconnection.
296
+
The second issue, is usually a user's code issue. To help you find it, we added a flag to the
297
+
StreamConn object called `debug`. It is set to False by default, but you can turn it on to get a more
298
+
verbose logs when this exception happens.
299
+
Turn it on like so `StreamConn(debug=True)`
300
+
301
+
## Logging
302
+
You should define a logger in your app in order to make sure you get all the messages from the different components.<br>
303
+
It will help you debug, and make sure you don't miss issues when they occur.<br>
304
+
The simplest way to define a logger, if you have no experience with the python logger - will be something like this:
-`to` is an Eastern Time timestamp string that filters the result for the upper bound, inclusive. we support the same formats as the _from field
@@ -406,72 +425,11 @@ dict[symbol -> `Financials`] if `symbol` is a list of string.
406
425
### polygon/REST.news(symbol)
407
426
Returns a `NewsList` entity for the symbol.
408
427
409
-
410
-
---
411
-
# Alpha Vantage API Service
412
-
413
-
In addition to Polygon is Alpha Vantage, for users without a live account (paper trading) or want to use the unique features of AV data. You can get a free key [here](https://www.alphavantage.co/support/#api-key) and the documentation is [here](https://www.alphavantage.co/documentation/). Premium keys are also available [here](https://www.alphavantage.co/premium/#intro)
414
-
This python SDK wraps their API service and seamlessly integrates it with the Alpaca
415
-
API. `alpaca_trade_api.REST.alpha_vantage` will be the `REST` object for Alpha Vantage.
416
-
417
-
The example below gives AAPL daily OHLCV data in a DataFrame format.
0 commit comments