{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Quickstart\n", "\n", "### Installation\n", "```bash\n", "> # Using Poetry\n", "> poetry add py-alpaca-api\n", ">\n", "> # Using Pip\n", "> pip install py-alpaca-api\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create Instance" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "import os\n", "from pprint import pprint\n", "\n", "from py_alpaca_api import PyAlpacaAPI\n", "\n", "# Load environment variables\n", "api_key = os.environ.get(\"ALPACA_API_KEY\")\n", "api_secret = os.environ.get(\"ALPACA_SECRET_KEY\")\n", "\n", "# Create an instance of PyAlpacaAPI\n", "api = PyAlpacaAPI(api_key=api_key, api_secret=api_secret, api_paper=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using Stock Module" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "# Create instance of Stock Module\n", "stock = api.stock" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get Stock Asset Information" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "AssetModel(id='b0b6dd9d-8b9b-48a9-ba46-b9d54906e415',\n", " asset_class='us_equity',\n", " easy_to_borrow=True,\n", " exchange='NASDAQ',\n", " fractionable=True,\n", " maintenance_margin_requirement=30.0,\n", " marginable=True,\n", " name='Apple Inc. Common Stock',\n", " shortable=True,\n", " status='active',\n", " symbol='AAPL',\n", " tradable=True)\n" ] } ], "source": [ "asset = stock.assets.get(\"AAPL\")\n", "pprint(asset)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Get Stock Historical Data" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " symbol close high low trade_count open date \\\n", "0 AAPL 194.03 194.99 192.5200 700957 192.900 2024-06-03 04:00:00 \n", "1 AAPL 194.35 195.32 193.0342 575318 194.635 2024-06-04 04:00:00 \n", "2 AAPL 195.87 196.90 194.8700 648283 195.400 2024-06-05 04:00:00 \n", "3 AAPL 194.48 196.50 194.1700 550123 195.685 2024-06-06 04:00:00 \n", "4 AAPL 196.89 196.94 194.1400 505960 194.650 2024-06-07 04:00:00 \n", "\n", " volume vwap \n", "0 50080539 193.789113 \n", "1 47471445 194.301392 \n", "2 54156785 195.869929 \n", "3 41181753 195.314869 \n", "4 53103912 195.904156 \n" ] } ], "source": [ "asset_history = stock.history.get_stock_data(\n", " symbol=asset.symbol, start=\"2024-06-01\", end=\"2024-06-07\", timeframe=\"1d\"\n", ")\n", "pprint(asset_history)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Screener Previous Day Stock Losers and Gainers" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " symbol change price volume trades\n", "0 GME -39.38 28.22 279054397 3119769\n", "1 ARCT -25.31 31.82 2306862 28269\n", "2 GDXU -20.36 33.33 2931194 26075\n", "3 SMTC -17.90 31.18 11935475 114968\n", "4 MEXX -16.40 17.89 428983 3514\n", "5 JNUG -14.45 37.66 2583802 23668\n", "6 RENT -13.39 23.22 174117 3395\n", "7 AGQ -13.26 37.76 3485566 17129\n", "8 NUGT -13.24 37.28 3595586 33645\n", "9 IOT -12.34 30.56 19097586 150608\n" ] } ], "source": [ "# Get the 10 biggest losers from previous trading day\n", "losers = stock.screener.losers(total_losers_returned=10)\n", "pprint(losers)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " symbol change price volume trades\n", "0 ODD 20.54 44.31 3562308 42637\n", "1 GDXD 20.37 24.17 1295452 5310\n", "2 MBLY 15.62 31.68 17413414 121828\n", "3 BDTX 11.61 5.19 827546 5897\n", "4 REPL 11.11 7.90 2622480 22565\n", "5 AGX 10.43 76.35 616683 9318\n", "6 PHAT 9.50 10.60 513061 7301\n", "7 BOIL 9.30 21.04 10453994 59408\n", "8 YEXT 9.11 5.15 6744492 35172\n", "9 RMAX 9.04 8.56 700893 6203\n" ] } ], "source": [ "# Get the 10 biggest gainers from previous trading day\n", "gainers = stock.screener.gainers(total_gainers_returned=10)\n", "pprint(gainers)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Use Predictor to predict the most likely gainers" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "• Predicting 100 future gainers with Prophet: : 100it [01:03, 1.58it/s]" ] }, { "name": "stdout", "output_type": "stream", "text": [ "This function uses Meta Prophet to predict future gainers. There is no guarantee that these stocks will gain.\n", "Out of 100 previous loser stocks, there are 41 predicted to gain 10% in the next 14 days:\n", "['ARCT', 'SMTC', 'MEXX', 'IOT', 'MTN', 'PI', 'KOLD', 'NVRI', 'UPST', 'SEZL', 'HOV', 'AG', 'RETL', 'MAG', 'MARA', 'CRMT', 'ENPH', 'SDGR', 'YINN', 'ESTA', 'BVS', 'KRUS', 'BAND', 'SYM', 'SSYS', 'UEC', 'SG', 'AZUL', 'CWEB', 'AMRC', 'RDDT', 'SBS', 'CDLX', 'GOGO', 'EWW', 'RXRX', 'ACB', 'ASR', 'CENX', 'LFMD', 'SANA']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "# Use Meta Prophet to guess the future gainers\n", "# returns a list of symbols that are predicted to gain in the next 14 days\n", "# default is set to 10% gain, but can be changed\n", "# This function is experimental and may not be accurate\n", "# It is fairly slow, because it does a lot. Pulls 4 years of historical data,\n", "# then uses Meta Prophet to predict future prices. Then it calculates the\n", "# percentage gain and returns a list of symbols that are predicted to gain.\n", "future_gainers = stock.predictor.get_losers_to_gainers(losers_to_scan=100)\n", "print(\n", " \"This function uses Meta Prophet to predict future gainers. There is no guarantee that these stocks will gain.\"\n", ")\n", "print(\n", " f\"Out of 100 previous loser stocks, there are {len(future_gainers)} predicted to gain 10% in the next 14 days:\"\n", ")\n", "print(future_gainers)" ] } ], "metadata": { "kernelspec": { "display_name": "py-alpaca-api-CfDuMe8Y-py3.12", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }