Quickstart

Installation

> # Using Poetry
> poetry add py-alpaca-api
>
> # Using Pip
> pip install py-alpaca-api

Create Instance

[6]:
import os
from pprint import pprint

from py_alpaca_api import PyAlpacaAPI

# Load environment variables
api_key = os.environ.get("ALPACA_API_KEY")
api_secret = os.environ.get("ALPACA_SECRET_KEY")

# Create an instance of PyAlpacaAPI
api = PyAlpacaAPI(api_key=api_key, api_secret=api_secret, api_paper=True)

Using Stock Module

[7]:
# Create instance of Stock Module
stock = api.stock

Get Stock Asset Information

[8]:
asset = stock.assets.get("AAPL")
pprint(asset)
AssetModel(id='b0b6dd9d-8b9b-48a9-ba46-b9d54906e415',
           asset_class='us_equity',
           easy_to_borrow=True,
           exchange='NASDAQ',
           fractionable=True,
           maintenance_margin_requirement=30.0,
           marginable=True,
           name='Apple Inc. Common Stock',
           shortable=True,
           status='active',
           symbol='AAPL',
           tradable=True)

Get Stock Historical Data

[10]:
asset_history = stock.history.get_stock_data(
    symbol=asset.symbol, start="2024-06-01", end="2024-06-07", timeframe="1d"
)
pprint(asset_history)
  symbol   close    high       low  trade_count     open                date  \
0   AAPL  194.03  194.99  192.5200       700957  192.900 2024-06-03 04:00:00
1   AAPL  194.35  195.32  193.0342       575318  194.635 2024-06-04 04:00:00
2   AAPL  195.87  196.90  194.8700       648283  195.400 2024-06-05 04:00:00
3   AAPL  194.48  196.50  194.1700       550123  195.685 2024-06-06 04:00:00
4   AAPL  196.89  196.94  194.1400       505960  194.650 2024-06-07 04:00:00

     volume        vwap
0  50080539  193.789113
1  47471445  194.301392
2  54156785  195.869929
3  41181753  195.314869
4  53103912  195.904156

Screener Previous Day Stock Losers and Gainers

[11]:
# Get the 10 biggest losers from previous trading day
losers = stock.screener.losers(total_losers_returned=10)
pprint(losers)
  symbol  change  price     volume   trades
0    GME  -39.38  28.22  279054397  3119769
1   ARCT  -25.31  31.82    2306862    28269
2   GDXU  -20.36  33.33    2931194    26075
3   SMTC  -17.90  31.18   11935475   114968
4   MEXX  -16.40  17.89     428983     3514
5   JNUG  -14.45  37.66    2583802    23668
6   RENT  -13.39  23.22     174117     3395
7    AGQ  -13.26  37.76    3485566    17129
8   NUGT  -13.24  37.28    3595586    33645
9    IOT  -12.34  30.56   19097586   150608
[12]:
# Get the 10 biggest gainers from previous trading day
gainers = stock.screener.gainers(total_gainers_returned=10)
pprint(gainers)
  symbol  change  price    volume  trades
0    ODD   20.54  44.31   3562308   42637
1   GDXD   20.37  24.17   1295452    5310
2   MBLY   15.62  31.68  17413414  121828
3   BDTX   11.61   5.19    827546    5897
4   REPL   11.11   7.90   2622480   22565
5    AGX   10.43  76.35    616683    9318
6   PHAT    9.50  10.60    513061    7301
7   BOIL    9.30  21.04  10453994   59408
8   YEXT    9.11   5.15   6744492   35172
9   RMAX    9.04   8.56    700893    6203

Use Predictor to predict the most likely gainers

[17]:
# Use Meta Prophet to guess the future gainers
# returns a list of symbols that are predicted to gain in the next 14 days
# default is set to 10% gain, but can be changed
# This function is experimental and may not be accurate
# It is fairly slow, because it does a lot. Pulls 4 years of historical data,
# then uses Meta Prophet to predict future prices. Then it calculates the
# percentage gain and returns a list of symbols that are predicted to gain.
future_gainers = stock.predictor.get_losers_to_gainers(losers_to_scan=100)
print(
    "This function uses Meta Prophet to predict future gainers. There is no guarantee that these stocks will gain."
)
print(
    f"Out of 100 previous loser stocks, there are {len(future_gainers)} predicted to gain 10% in the next 14 days:"
)
print(future_gainers)
• Predicting 100 future gainers with Prophet: : 100it [01:03,  1.58it/s]
This function uses Meta Prophet to predict future gainers. There is no guarantee that these stocks will gain.
Out of 100 previous loser stocks, there are 41 predicted to gain 10% in the next 14 days:
['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']