Skip to content

Commit 4be49a5

Browse files
author
Shlomi Kushchi
authored
Merge branch 'master' into feature/trailing-stop
2 parents f7e6453 + 8e7bef4 commit 4be49a5

26 files changed

+126
-2521
lines changed

NOTICE

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Apache [PRODUCT_NAME]
2+
Copyright [XXXX-XXXX] The Apache Software Foundation
3+
4+
This product includes software developed at
5+
The Apache Software Foundation (http://www.apache.org/).

README.md

Lines changed: 35 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# alpaca-trade-api-python
77

88
`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
1010
both REST and streaming data interfaces. For details of each API behavior,
1111
please see the online [API document](https://docs.alpaca.markets).
1212

@@ -21,7 +21,7 @@ $ pip3 install alpaca-trade-api
2121

2222
## Example
2323

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.
2525

2626
### REST example
2727
```python
@@ -42,7 +42,7 @@ The HTTP API document is located at https://docs.alpaca.markets/
4242

4343
## API Version
4444

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.
4646

4747
## Authentication
4848

@@ -53,7 +53,7 @@ outlined below.
5353

5454
## Alpaca Environment Variables
5555

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.
5757

5858
| Environment | default | Description |
5959
| -------------------------------- | -------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
@@ -66,15 +66,14 @@ The Alpaca SDK will check the environment for a number of variables which can be
6666
| APCA_RETRY_CODES=429,504 | 429,504 | comma-separated HTTP status code for which retry is attempted |
6767
| 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 |
6868
| 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). |
7069

7170
## REST
7271

7372
The `REST` class is the entry point for the API request. The instance of this
7473
class provides all REST API calls such as account, orders, positions,
7574
and bars.
7675

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).
7877
This helper class provides property access (the "dot notation") to the
7978
json object, backed by the original object stored in the `_raw` field.
8079
It also converts certain types to the appropriate python object.
@@ -88,7 +87,7 @@ account.status
8887
=> 'ACTIVE'
8988
```
9089

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
9291
object. Its `_raw` property returns the original raw primitive data unmarshaled
9392
from the response JSON text.
9493

@@ -221,9 +220,9 @@ exception is raised, and each event handler is called asynchronously
221220
upon the message arrivals.
222221

223222
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
225224
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
227226
event loop.
228227

229228
The `msg` object passed to each handler is wrapped by the entity
@@ -285,8 +284,29 @@ same `channel_pat` will overwrite the old handler.
285284
Deregisters the event handler function that was previously registered via `on` or
286285
`register` method.
287286

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:
305+
```py
306+
import logging
307+
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)
308+
```
288309

289-
---
290310
# Polygon API Service
291311

292312
Alpaca's API key ID can be used to access Polygon API, the documentation for
@@ -321,7 +341,7 @@ df = api.polygon.historic_agg_v2('AAPL', 1, 'minute', _from=start, to=end).df
321341
```
322342

323343
## polygon/REST
324-
It is initialized through alpaca `REST` object.
344+
It is initialized through the alpaca `REST` object.
325345

326346
### polygon/REST.exchanges()
327347
Returns a list of `Exchange` entity.
@@ -332,7 +352,7 @@ Returns a `SymbolTypeMap` object.
332352
### polygon/REST.historic_trades_v2(symbol, date,timestamp=None, timestamp_limit=None, reverse=None, limit=None)
333353
Returns a `TradesV2` which is a list of `Trade` entities.
334354

335-
- `date` is a date string such as '2018-2-2'. The returned quotes are from this day onyl.
355+
- `date` is a date string such as '2018-2-2'. The returned quotes are from this day only.
336356
- `timestamp` is an integer in Unix Epoch nanoseconds as the lower bound filter, exclusive.
337357
- `timestamp_limit` is an integer in Unix Epoch nanoseconds as the maximum timestamp allowed in the results.
338358
- `limit` is an integer for the number of ticks to return. Default and max is 50000.
@@ -343,7 +363,7 @@ Returns a pandas DataFrame object with the ticks returned by `historic_trades_v2
343363
### polygon/REST.historic_quotes_v2(symbol, date,timestamp=None, timestamp_limit=None, reverse=None, limit=None)
344364
Returns a `QuotesV2` which is a list of `Quote` entities.
345365

346-
- `date` is a date string such as '2018-2-2'. The returned quotes are from this day onyl.
366+
- `date` is a date string such as '2018-2-2'. The returned quotes are from this day only.
347367
- `timestamp` is an integer in Unix Epoch nanoseconds as the lower bound filter, exclusive.
348368
- `timestamp_limit` is an integer in Unix Epoch nanoseconds as the maximum timestamp allowed in the results.
349369
- `limit` is an integer for the number of ticks to return. Default and max is 50000.
@@ -358,8 +378,7 @@ object.
358378
- `multiplier` is an integer affecting the amount of data contained in each Agg object.
359379
- `timespan` is a string affecting the length of time represented by each Agg object. It is one of the following values:
360380
- `minute`, `hour`, `day`, `week`, `month`, `quarter`, `year`
361-
- `_from` is an Eastern Time timestamp string/object that filters the result
362-
for the lower bound, inclusive. we accept the date in these formats:
381+
- `_from` is an Eastern Time timestamp string/object that filters the result for the lower bound, inclusive. we accept the date in these formats:
363382
datetime.datetime, datetime.date, pd.Timestamp, datetime.timestamp,
364383
isoformat string (YYYY-MM-DD)
365384
- `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.
406425
### polygon/REST.news(symbol)
407426
Returns a `NewsList` entity for the symbol.
408427

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.
418-
419-
```py
420-
import alpaca_trade_api as tradeapi
421-
422-
api = tradeapi.REST()
423-
aapl = api.alpha_vantage.historic_quotes('AAPL', adjusted=True, output_format='pandas')
424-
```
425-
426-
## alpha_vantage/REST
427-
It is initialized through alpaca `REST` object.
428-
429-
### alpha_vantage/REST.get(params=None)
430-
Customizable endpoint, where you can pass all keywords/paramters from the documentation:https://www.alphavantage.co/documentation/#
431-
432-
Returns the specific customized data.
433-
434-
### alpha_vantage/REST.historic_quotes(symbol, adjusted=False, outputsize='full', cadence='daily', output_format=None)
435-
Returns a `csv`, `json`, or `pandas` object of historical OHLCV data.
436-
437-
### alpha_vantage/REST.intraday_quotes(symbol, interval='5min', outputsize='full', output_format=None)
438-
Returns a `csv`, `json`, or `pandas` object of intraday OHLCV data.
439-
440-
### alpha_vantage/REST.current_quote(symbol)
441-
Returns a `json` object with the current OHLCV data of the selected symbol.
442-
443-
### alpha_vantage/REST.last_quote(symbol)
444-
Returns a `json` object with the current OHLCV data of the selected symbol (same as `current_quote`).
445-
446-
### alpha_vantage/REST.search_endpoint(keywords, datatype='json')
447-
Returns a `csv`, `json`, or `pandas` object that contains the best-matching symbols and market information based on keywords of your choice.
448-
449-
### alpha_vantage/REST.company(symbol, datatype='json')
450-
Same as `search_endpoint`.
451-
452-
### alpha_vantage/REST.historic_fx_quotes(from_symbol, to_symbol, outputsize='full', cadence='daily', output_format=None)
453-
Returns a `csv`, `json`, or `pandas` object of historical OHLCV data for the currency pair.
454-
455-
### alpha_vantage/REST.intraday_fx_quotes(from_symbol, to_symbol, interval='5min', outputsize='full', output_format=None)
456-
Returns a `csv`, `json`, or `pandas` object of intraday OHLCV data for the currency pair.
457-
458-
### alpha_vantage/REST.exchange_rate(from_currency, to_currency)
459-
Returns a `json` object with the current OHLCV data of the selected currency pair (digital or physical)
460-
461-
### alpha_vantage/REST.historic_cryptocurrency_quotes(self, symbol, market, cadence='daily', output_format=None)
462-
Returns a `csv`, `json`, or `pandas` object of historical OHLCV data for the cryptocurrency pair.
463-
464-
### alpha_vantage/REST.techindicators(self, techindicator='SMA', output_format='json', **kwargs)
465-
Returns a `csv`, `json`, or `pandas` object with the data from the techindicator of choice.
466-
467-
### alpha_vantage/REST.sector()
468-
Returns a `json` of the currrency sector performances.
469-
470428
## Support and Contribution
471429

472430
For technical issues particular to this module, please report the
473431
issue on this GitHub repository. Any API issues can be reported through
474432
Alpaca's customer support.
475433

476-
New features, as well as bug fixes, by sending pull request is always
434+
New features, as well as bug fixes, by sending a pull request is always
477435
welcomed.

alpaca_trade_api/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from .rest import REST # noqa
22
from .stream2 import StreamConn # noqa
33

4-
__version__ = '0.49.0'
4+
__version__ = '0.49.1'

alpaca_trade_api/alpha_vantage/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

alpaca_trade_api/alpha_vantage/rest.py

Lines changed: 0 additions & 155 deletions
This file was deleted.

0 commit comments

Comments
 (0)