{"id":13700072,"url":"https://github.com/alpacahq/alpaca-trade-api-python","last_synced_at":"2025-05-13T00:12:45.257Z","repository":{"id":38356640,"uuid":"124611126","full_name":"alpacahq/alpaca-trade-api-python","owner":"alpacahq","description":"Python client for Alpaca's trade API","archived":false,"fork":false,"pushed_at":"2024-12-02T13:49:20.000Z","size":884,"stargazers_count":1792,"open_issues_count":59,"forks_count":547,"subscribers_count":114,"default_branch":"master","last_synced_at":"2025-05-13T00:12:37.433Z","etag":null,"topics":["algo-trading","alpaca","api","crypto","hacktoberfest","market-data","python","rest-api","stock-market","trade","trading","websocket"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/alpaca-trade-api/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alpacahq.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-10T01:29:06.000Z","updated_at":"2025-05-09T21:11:08.000Z","dependencies_parsed_at":"2022-07-16T02:46:15.246Z","dependency_job_id":"2c9923ac-9a77-414f-9248-2fb6601bd721","html_url":"https://github.com/alpacahq/alpaca-trade-api-python","commit_stats":{"total_commits":692,"total_committers":66,"mean_commits":"10.484848484848484","dds":0.8251445086705202,"last_synced_commit":"dc3cf7a5b8ab2a786365f9cc120b2f97cc986ccf"},"previous_names":[],"tags_count":79,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alpacahq%2Falpaca-trade-api-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alpacahq%2Falpaca-trade-api-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alpacahq%2Falpaca-trade-api-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alpacahq%2Falpaca-trade-api-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alpacahq","download_url":"https://codeload.github.com/alpacahq/alpaca-trade-api-python/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253843221,"owners_count":21972874,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algo-trading","alpaca","api","crypto","hacktoberfest","market-data","python","rest-api","stock-market","trade","trading","websocket"],"created_at":"2024-08-02T20:00:48.120Z","updated_at":"2025-05-13T00:12:45.217Z","avatar_url":"https://github.com/alpacahq.png","language":"Python","readme":"[![PyPI version](https://badge.fury.io/py/alpaca-trade-api.svg)](https://badge.fury.io/py/alpaca-trade-api)\n[![CircleCI](https://circleci.com/gh/alpacahq/alpaca-trade-api-python.svg?style=shield)](https://circleci.com/gh/alpacahq/alpaca-trade-api-python)\n[![Updates](https://pyup.io/repos/github/alpacahq/alpaca-trade-api-python/shield.svg)](https://pyup.io/repos/github/alpacahq/alpaca-trade-api-python/)\n[![Python 3](https://pyup.io/repos/github/alpacahq/alpaca-trade-api-python/python-3-shield.svg)](https://pyup.io/repos/github/alpacahq/alpaca-trade-api-python/)\n\n# Deprecation Notice\n\nA new python SDK, [Alpaca-py](https://github.com/alpacahq/alpaca-py), is available. This SDK will be the primary python SDK starting in 2023. We recommend moving over your code to use the new SDK. Keep in mind, we will be maintaining this repo as usual until the end of 2022.\n\n# alpaca-trade-api-python\n\n`alpaca-trade-api-python` is a python library for the [Alpaca Commission Free Trading API](https://alpaca.markets).\nIt allows rapid trading algo development easily, with support for\nboth REST and streaming data interfaces. For details of each API behavior,\nplease see the online [API document](https://alpaca.markets/docs/api-documentation/api-v2/market-data/alpaca-data-api-v2/).\n\nNote that this package supports only python version 3.7 and above.\n\n## Install\nWe support python\u003e=3.7. If you want to work with python 3.6, please note that these package dropped support for python \u003c3.7 for the following versions:\n```\npandas \u003e= 1.2.0\nnumpy \u003e= 1.20.0\nscipy \u003e= 1.6.0\n```\nThe solution - manually install these packages before installing alpaca-trade-api. e.g:\n```bash\npip install pandas==1.1.5 numpy==1.19.4 scipy==1.5.4\n```\nAlso note that we do not limit the version of the websockets library, but we advise using\n```\nwebsockets\u003e=9.0\n```\n\nInstalling using pip\n```bash\n$ pip3 install alpaca-trade-api\n```\n \n## API Keys\nTo use this package you first need to obtain an API key. Go here to [signup](https://app.alpaca.markets/signup)\n\n# Services\nThese services are provided by Alpaca:\n* Data:\n  * [Historical](https://alpaca.markets/docs/api-documentation/api-v2/market-data/alpaca-data-api-v2/historical/)\n  * [Live Data Stream](https://alpaca.markets/docs/api-documentation/api-v2/market-data/alpaca-data-api-v2/real-time/)\n* [Account/Portfolio Management](https://alpaca.markets/docs/api-documentation/api-v2)\n\nThe free services are limited, please check the docs to see the differences between paid/free services.\n\n## Alpaca Environment Variables\n\nThe Alpaca SDK will check the environment for a number of variables that can be used rather than hard-coding these into your scripts.\u003cbr\u003e\nAlternatively you could pass the credentials directly to the SDK instances.\n\n\n| Environment                      | default                                                                                | Description                                                                                                            |\n| -------------------------------- | -------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |\n| APCA_API_KEY_ID=\u003ckey_id\u003e         |                                                                                        | Your API Key                                                                                                           |\n| APCA_API_SECRET_KEY=\u003csecret_key\u003e |                                                                                        | Your API Secret Key                                                                                                    |\n| APCA_API_BASE_URL=url            | https://api.alpaca.markets (for live) | Specify the URL for API calls, *Default is live, you must specify \u003cbr/\u003ehttps://paper-api.alpaca.markets to switch to paper endpoint!*                   |\n| APCA_API_DATA_URL=url            | https://data.alpaca.markets                                                            | Endpoint for data API                                                                                                  |\n| APCA_RETRY_MAX=3                 | 3                                                                                      | The number of subsequent API calls to retry on timeouts                                                                |\n| APCA_RETRY_WAIT=3                | 3                                                                                      | seconds to wait between each retry attempt                                                                             |\n| APCA_RETRY_CODES=429,504         | 429,504                                                                                | comma-separated HTTP status code for which retry is attempted                                                          |\n| DATA_PROXY_WS                    |                                                                                        | When using the alpaca-proxy-agent you need to set this environment variable as described ![here](https://github.com/shlomikushchi/alpaca-proxy-agent) |\n\n## Working with Data\n### Historic Data\nYou could get one of these historic data types:\n* Bars\n* Quotes\n* Trades\n\nYou now have 2 pythonic ways to retrieve historical data.\u003cbr\u003e\nOne using the traditional rest module and the other is to use the experimental asyncio module added lately.\u003cbr\u003e\nLet's have a look at both:\u003cbr\u003e\n\nThe first thing to understand is the new data polling mechanism. You could query up to 10000 items, and the API is using a pagination mechanism to provide you with the data.\u003cbr\u003e\nYou now have 2 options:\n* Working with data as it is received with a generator. (meaning it's faster but you need to process each item alone)\n* Wait for the entire data to be received, and then work with it as a list or dataframe.\nWe provide you with both options to choose from.\n\n#### Bars\noption 1: wait for the data\n```py\nfrom alpaca_trade_api.rest import REST, TimeFrame\napi = REST()\n\napi.get_bars(\"AAPL\", TimeFrame.Hour, \"2021-06-08\", \"2021-06-08\", adjustment='raw').df\n\n                              open      high       low     close    volume\ntimestamp\n2021-06-08 08:00:00+00:00  126.100  126.3000  125.9600  126.3000     42107\n2021-06-08 09:00:00+00:00  126.270  126.4000  126.2200  126.3800     21095\n2021-06-08 10:00:00+00:00  126.380  126.6000  125.8400  126.4900     54743\n2021-06-08 11:00:00+00:00  126.440  126.8700  126.4000  126.8500    206460\n2021-06-08 12:00:00+00:00  126.821  126.9500  126.7000  126.9300    385164\n2021-06-08 13:00:00+00:00  126.920  128.4600  126.4485  127.0250  18407398\n2021-06-08 14:00:00+00:00  127.020  127.6400  126.7800  127.1350  13446961\n2021-06-08 15:00:00+00:00  127.140  127.4700  126.2101  126.6100  10444099\n2021-06-08 16:00:00+00:00  126.610  126.8400  126.5300  126.8250   5289556\n2021-06-08 17:00:00+00:00  126.820  126.9300  126.4300  126.7072   4813459\n2021-06-08 18:00:00+00:00  126.709  127.3183  126.6700  127.2850   5338455\n2021-06-08 19:00:00+00:00  127.290  127.4200  126.6800  126.7400   9817083\n2021-06-08 20:00:00+00:00  126.740  126.8500  126.5400  126.6600   5525520\n2021-06-08 21:00:00+00:00  126.690  126.8500  126.6500  126.6600    156333\n2021-06-08 22:00:00+00:00  126.690  126.7400  126.6600  126.7300     49252\n2021-06-08 23:00:00+00:00  126.725  126.7600  126.6400  126.6400     41430\n```\noption 2: iterate over bars\n```py\ndef process_bar(bar):\n    # process bar\n    print(bar)\n\nbar_iter = api.get_bars_iter(\"AAPL\", TimeFrame.Hour, \"2021-06-08\", \"2021-06-08\", adjustment='raw')\nfor bar in bar_iter:\n    process_bar(bar)\n```\n\nAlternatively, you can decide on your custom timeframes by using the TimeFrame constructor:\n\n```py\nfrom alpaca_trade_api.rest import REST, TimeFrame, TimeFrameUnit\n\napi = REST()\napi.get_bars(\"AAPL\", TimeFrame(45, TimeFrameUnit.Minute), \"2021-06-08\", \"2021-06-08\", adjustment='raw').df\n\n                               open      high       low     close    volume  trade_count        vwap\ntimestamp\n2021-06-08 07:30:00+00:00  126.1000  126.1600  125.9600  126.0600     20951          304  126.049447\n2021-06-08 08:15:00+00:00  126.0500  126.3000  126.0500  126.3000     21181          349  126.231904\n2021-06-08 09:00:00+00:00  126.2700  126.3200  126.2200  126.2800     15955          308  126.284120\n2021-06-08 09:45:00+00:00  126.2900  126.4000  125.9000  125.9000     30179          582  126.196877\n2021-06-08 10:30:00+00:00  125.9000  126.7500  125.8400  126.7500    105380         1376  126.530863\n2021-06-08 11:15:00+00:00  126.7300  126.8500  126.5600  126.8300    129721         1760  126.738041\n2021-06-08 12:00:00+00:00  126.4101  126.9500  126.3999  126.8300    418107         3615  126.771889\n2021-06-08 12:45:00+00:00  126.8500  126.9400  126.6000  126.6200    428614         5526  126.802825\n2021-06-08 13:30:00+00:00  126.6200  128.4600  126.4485  127.4150  23065023       171263  127.425797\n2021-06-08 14:15:00+00:00  127.4177  127.6400  126.9300  127.1350   8535068        65753  127.342337\n2021-06-08 15:00:00+00:00  127.1400  127.4700  126.2101  126.7101   8447696        64616  126.789316\n2021-06-08 15:45:00+00:00  126.7200  126.8200  126.5300  126.6788   5084147        38366  126.712110\n2021-06-08 16:30:00+00:00  126.6799  126.8400  126.5950  126.5950   3205870        26614  126.718837\n2021-06-08 17:15:00+00:00  126.5950  126.9300  126.4300  126.7010   3908283        31922  126.665727\n2021-06-08 18:00:00+00:00  126.7072  127.0900  126.6700  127.0600   3923056        29114  126.939887\n2021-06-08 18:45:00+00:00  127.0500  127.4200  127.0000  127.0050   5051682        38235  127.214157\n2021-06-08 19:30:00+00:00  127.0150  127.0782  126.6800  126.7800  11665598        47146  126.813182\n2021-06-08 20:15:00+00:00  126.7700  126.7900  126.5400  126.6600     83725         1973  126.679259\n2021-06-08 21:00:00+00:00  126.6900  126.8500  126.6700  126.7200    145153          769  126.746457\n2021-06-08 21:45:00+00:00  126.7000  126.7400  126.6500  126.7100     38455          406  126.699544\n2021-06-08 22:30:00+00:00  126.7100  126.7600  126.6700  126.7100     30822          222  126.713892\n2021-06-08 23:15:00+00:00  126.7200  126.7600  126.6400  126.6400     32585          340  126.704131\n```\n\n#### Quotes\noption 1: wait for the data\n```py\nfrom alpaca_trade_api.rest import REST\napi = REST()\n\napi.get_quotes(\"AAPL\", \"2021-06-08\", \"2021-06-08\", limit=10).df\n\n                                    ask_exchange  ask_price  ask_size bid_exchange  bid_price  bid_size conditions\ntimestamp\n2021-06-08 08:00:00.070928640+00:00            P     143.00         1                    0.00         0        [Y]\n2021-06-08 08:00:00.070929408+00:00            P     143.00         1            P     102.51         1        [R]\n2021-06-08 08:00:00.070976768+00:00            P     143.00         1            P     116.50         1        [R]\n2021-06-08 08:00:00.070978816+00:00            P     143.00         1            P     118.18         1        [R]\n2021-06-08 08:00:00.071020288+00:00            P     143.00         1            P     120.00         1        [R]\n2021-06-08 08:00:00.071020544+00:00            P     134.18         1            P     120.00         1        [R]\n2021-06-08 08:00:00.071021312+00:00            P     134.18         1            P     123.36         1        [R]\n2021-06-08 08:00:00.071209984+00:00            P     131.11         1            P     123.36         1        [R]\n2021-06-08 08:00:00.071248640+00:00            P     130.13         1            P     123.36         1        [R]\n2021-06-08 08:00:00.071286016+00:00            P     129.80         1            P     123.36         1        [R]\n```\noption 2: iterate over quotes\n```py\ndef process_quote(quote):\n    # process quote\n    print(quote)\n\nquote_iter = api.get_quotes_iter(\"AAPL\", \"2021-06-08\", \"2021-06-08\", limit=10)\nfor quote in quote_iter:\n    process_quote(quote)\n```\n\n#### Trades\noption 1: wait for the data\n```py\nfrom alpaca_trade_api.rest import REST\napi = REST()\n\napi.get_trades(\"AAPL\", \"2021-06-08\", \"2021-06-08\", limit=10).df\n\n                                    exchange   price  size conditions  id tape\ntimestamp\n2021-06-08 08:00:00.069956608+00:00        P  126.10   179     [@, T]   1    C\n2021-06-08 08:00:00.207859+00:00           K  125.97     1  [@, T, I]   1    C\n2021-06-08 08:00:00.207859+00:00           K  125.97    12  [@, T, I]   2    C\n2021-06-08 08:00:00.207859+00:00           K  125.97     4  [@, T, I]   3    C\n2021-06-08 08:00:00.207859+00:00           K  125.97     4  [@, T, I]   4    C\n2021-06-08 08:00:00.207859+00:00           K  125.97     8  [@, T, I]   5    C\n2021-06-08 08:00:00.207859+00:00           K  125.97     1  [@, T, I]   6    C\n2021-06-08 08:00:00.207859+00:00           K  126.00    30  [@, T, I]   7    C\n2021-06-08 08:00:00.207859+00:00           K  126.00    10  [@, T, I]   8    C\n2021-06-08 08:00:00.207859+00:00           K  125.97    70  [@, T, I]   9    C\n\n```\noption 2: iterate over trades\n```py\ndef process_trade(trade):\n    # process trade\n    print(trade)\n\ntrades_iter = api.get_trades_iter(\"AAPL\", \"2021-06-08\", \"2021-06-08\", limit=10)\nfor trade in trades_iter:\n    process_trade(trade)\n```\n\n### Asyncio Rest module\nThe `rest_async.py` module now provides an asyncion approach to retrieving the historic data.\u003cbr\u003e\nThis module is, and thus may have expansions in the near future to support more endpoints.\u003cbr\u003e\nIt provides a much faster way to retrieve the historic data for multiple symbols.\u003cbr\u003e\nUnder the hood we use the [aiohttp](https://docs.aiohttp.org/en/stable/) library.\u003cbr\u003e\nWe provide a code sample to get you started with this new approach and it is located [here](examples/historic_async.py).\u003cbr\u003e\nFollow along with the example code to learn more, and utilize it for your own needs.\u003cbr\u003e\n\n### Live Stream Market Data\nThere are 2 streams available as described [here](https://alpaca.markets/docs/market-data/#subscription-plans).\n\nThe free plan is using the `iex` stream, while the paid subscription is using the `sip` stream.\n\nYou can subscribe to bars, trades, quotes, and trade updates for your account as well.\nUnder the example folder you can find different [code samples](https://github.com/alpacahq/alpaca-trade-api-python/tree/master/examples/websockets)\nto achieve different goals.\n\nHere in this basic example, We use the Stream class under `alpaca_trade_api.stream` for API V2 to subscribe to trade\nupdates for AAPL and quote updates for IBM.\n```py\nfrom alpaca_trade_api.common import URL\nfrom alpaca_trade_api.stream import Stream\n\nasync def trade_callback(t):\n    print('trade', t)\n\n\nasync def quote_callback(q):\n    print('quote', q)\n\n\n# Initiate Class Instance\nstream = Stream(\u003cALPACA_API_KEY\u003e,\n                \u003cALPACA_SECRET_KEY\u003e,\n                base_url=URL('https://paper-api.alpaca.markets'),\n                data_feed='iex')  # \u003c- replace to 'sip' if you have PRO subscription\n\n# subscribing to event\nstream.subscribe_trades(trade_callback, 'AAPL')\nstream.subscribe_quotes(quote_callback, 'IBM')\n\nstream.run()\n```\n\n#### Websockets Config For Live Data\nUnder the hood our SDK uses the [Websockets library](https://websockets.readthedocs.io/en/stable/index.html) to handle\nour websocket connections. Since different environments can have wildly differing requirements for resources we allow you\nto pass your own config options to the websockets lib via the `websocket_params` kwarg found on the Stream class.\n\nie:\n```python\n# Initiate Class Instance\nstream = Stream(\u003cALPACA_API_KEY\u003e,\n                \u003cALPACA_SECRET_KEY\u003e,\n                base_url=URL('https://paper-api.alpaca.markets'),\n                data_feed='iex', # \u003c- replace to 'sip' if you have PRO subscription\n                websocket_params =  {'ping_interval': 5}, #here we set ping_interval to 5 seconds \n                )\n```\n\nIf you're curious [this link to their docs](https://websockets.readthedocs.io/en/stable/reference/client.html#opening-a-connection)\nshows the values that websockets uses by default as well as any parameters they allow changing. Additionally, if you\ndon't specify any we set the following defaults on top of the ones the websockets library uses:\n```python\n{\n    \"ping_interval\": 10,\n    \"ping_timeout\": 180,\n    \"max_queue\": 1024,\n}\n```\n\n\n## Account \u0026 Portfolio Management\n\nThe HTTP API document is located at https://docs.alpaca.markets/\n\n### API Version\n\nAPI 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.\n\n### Authentication\n\nThe Alpaca API requires API key ID and secret key, which you can obtain from the\nweb console after you sign in.  You can pass `key_id` and `secret_key` to the initializers of\n`REST` or `Stream` as arguments, or set up environment variables as\noutlined below.\n\n### REST\n\nThe `REST` class is the entry point for the API request.  The instance of this\nclass provides all REST API calls such as account, orders, positions,\nand bars.\n\nEach returned object is wrapped by a subclass of the `Entity` class (or a list of it).\nThis helper class provides property access (the \"dot notation\") to the\njson object, backed by the original object stored in the `_raw` field.\nIt also converts certain types to the appropriate python object.\n\n```python\nimport alpaca_trade_api as tradeapi\n\napi = tradeapi.REST()\naccount = api.get_account()\naccount.status\n=\u003e 'ACTIVE'\n```\n\nThe `Entity` class also converts the timestamp string field to a pandas.Timestamp\nobject.  Its `_raw` property returns the original raw primitive data unmarshaled\nfrom the response JSON text.\n\nPlease 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.\n\nIf the retries are exceeded, or other API error is returned, `alpaca_trade_api.rest.APIError` is raised.\nYou can access the following information through this object.\n- the API error code: `.code` property\n- the API error message: `str(error)`\n- the original request object: `.request` property\n- the original response object: `.response` property\n- the HTTP status code: `.status_code` property\n\n#### API REST Methods\n\n| Rest Method                                                                                                                                                                                                                                      | End Point                          | Result                                                                                                                       |\n|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------|------------------------------------------------------------------------------------------------------------------------------|\n| get_account()                                                                                                                                                                                                                                    | `GET /account` and                 | `Account` entity.                                                                                                            |\n| get_order_by_client_order_id(client_order_id)                                                                                                                                                                                                    | `GET /orders` with client_order_id | `Order` entity.                                                                                                              |\n| list_orders(status=None, limit=None, after=None, until=None, direction=None, params=None,nested=None, symbols=None, side=None)                                                                                                                   | `GET /orders`                      | list of `Order` entities. `after` and `until` need to be string format, which you can obtain by `pd.Timestamp().isoformat()` |\n| submit_order(symbol, qty=None, side=\"buy\", type=\"market\", time_in_force=\"day\", limit_price=None, stop_price=None, client_order_id=None, order_class=None, take_profit=None, stop_loss=None, trail_price=None, trail_percent=None, notional=None) | `POST /orders`                     | `Order` entity.                                                                                                              |\n| get_order(order_id)                                                                                                                                                                                                                              | `GET /orders/{order_id}`           | `Order` entity.                                                                                                              |\n| cancel_order(order_id)                                                                                                                                                                                                                           | `DELETE /orders/{order_id}`        |                                                                                                                              |\n| cancel_all_orders()                                                                                                                                                                                                                              | `DELETE /orders`                   |                                                                                                                              |\n| list_positions()                                                                                                                                                                                                                                 | `GET /positions`                   | list of `Position` entities                                                                                                  |\n| get_position(symbol)                                                                                                                                                                                                                             | `GET /positions/{symbol}`          | `Position` entity.                                                                                                           |\n| list_assets(status=None, asset_class=None)                                                                                                                                                                                                       | `GET /assets`                      | list of `Asset` entities                                                                                                     |\n| get_asset(symbol)                                                                                                                                                                                                                                | `GET /assets/{symbol}`             | `Asset` entity                                                                                                               |\n| get_clock()                                                                                                                                                                                                                                      | `GET /clock`                       | `Clock` entity                                                                                                               |\n| get_calendar(start=None, end=None)                                                                                                                                                                                                               | `GET /calendar`                    | `Calendar` entity                                                                                                            |\n| get_portfolio_history(date_start=None, date_end=None, period=None, timeframe=None, extended_hours=None)                                                                                                                                          | `GET /account/portfolio/history`   | PortfolioHistory entity. PortfolioHistory.df can be used to get the results as a dataframe                                   |\n\n#### Rest Examples\n\nPlease see the `examples/` folder for some example scripts that make use of this API\n\n##### Using `submit_order()`\nBelow is an example of submitting a bracket order.\n```py\napi.submit_order(\n    symbol='SPY',\n    side='buy',\n    type='market',\n    qty='100',\n    time_in_force='day',\n    order_class='bracket',\n    take_profit=dict(\n        limit_price='305.0',\n    ),\n    stop_loss=dict(\n        stop_price='295.5',\n        limit_price='295.5',\n    )\n)\n```\n\nFor simple orders with `type='market'` and `time_in_force='day'`, you can pass a fractional amount (`qty`) or a `notional` amount (but not both). For instance, if the current market price for SPY is $300, the following calls are equivalent:\n\n```py\napi.submit_order(\n    symbol='SPY',\n    qty=1.5,  # fractional shares\n    side='buy',\n    type='market',\n    time_in_force='day',\n)\n```\n\n```py\napi.submit_order(\n    symbol='SPY',\n    notional=450,  # notional value of 1.5 shares of SPY at $300\n    side='buy',\n    type='market',\n    time_in_force='day',\n)\n```\n\n---\n\n## Logging\nYou should define a logger in your app in order to make sure you get all the messages from the different components.\u003cbr\u003e\nIt will help you debug, and make sure you don't miss issues when they occur.\u003cbr\u003e\nThe simplest way to define a logger, if you have no experience with the python logger - will be something like this:\n```py\nimport logging\nlogging.basicConfig(format='%(asctime)s %(message)s', level=logging.INFO)\n```\n\n## Websocket best practices\nUnder the examples folder you could find several examples to do the following:\n* Different subscriptions(channels) usage with the alpaca streams\n* pause / resume connection\n* change subscriptions/channels of existing connection\n* ws disconnections handler (make sure we reconnect when the internal mechanism fails)\n\n\n## Running Multiple Strategies\nThe base version of this library only allows running a single algorithm due to Alpaca's limit of one websocket connection per account. For those looking to run multiple strategies, there is [alpaca-proxy-agent project.](https://github.com/shlomikushchi/alpaca-proxy-agent)\n\nThe steps to execute this are:\n\n* Run the Alpaca Proxy Agent as described in the project's README\n* Define a new environment variable: `DATA_PROXY_WS` set to the address of the proxy agent. (e.g: `DATA_PROXY_WS=ws://127.0.0.1:8765`)\n* If you are using the Alpaca data stream, make sure to initiate the Stream object with the container's url: `data_url='http://127.0.0.1:8765'`\n* Execute your algorithm. It will connect to the Alpaca servers through the proxy agent, allowing you to execute multiple strategies\n\n\n## Raw Data vs Entity Data\nBy default the data returned from the api or streamed via Stream is wrapped with an Entity object for ease of use. Some users may prefer working with vanilla python objects (lists, dicts, ...). You have 2 options to get the raw data:\n\n* Each Entity object as a `_raw` property that extract the raw data from the object.\n* If you only want to work with raw data, and avoid casting to Entity (which may take more time, casting back and forth) you could pass `raw_data` argument to `Rest()` object or the `Stream()` object.\n\n## Support and Contribution\n\nFor technical issues particular to this module, please report the\nissue on this GitHub repository. Any API issues can be reported through\nAlpaca's customer support.\n\nNew features, as well as bug fixes, by sending a pull request is always\nwelcomed.\n","funding_links":[],"categories":["Python","Python：量化金融第一生态"],"sub_categories":["Data Sources","四、策略回测与交易执行"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falpacahq%2Falpaca-trade-api-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falpacahq%2Falpaca-trade-api-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falpacahq%2Falpaca-trade-api-python/lists"}