Skip to main content

Stocks

The Stocks module provides the functionality of the Stocks menu from the OpenBB Terminal. The list below are the SDK functions within the Stocks module and a short description:

PathTypeDescription
openbb.stocks.baSub-ModuleBehavioural Analysis
openbb.stocks.caSub-ModuleComparison Analysis
openbb.stocks.candleFunctionOHLC + Volume + Moving Averages Chart
openbb.stocks.discSub-ModuleStock Discovery
openbb.stocks.dpsSub-ModuleDark Pools & Shorts
openbb.stocks.faSub-ModuleFundamental Analysis & Future Estimations
openbb.stocks.govSub-ModuleUS Government, Lobbying & Representative Activity
openbb.stocks.insSub-ModuleCorporate Insider Activity (SEC Form 4)
openbb.stocks.loadFunctionLoad Historical OHLC+V Data
openbb.stocks.optionsSub-ModuleOptions
openbb.stocks.qaSub-ModuleStocks-Only Quantitative Analysis
openbb.stocks.quoteFunctionLast Price and Performance Data (FinancialModelingPrep)
openbb.stocks.screenerSub-ModuleStock Screener
openbb.stocks.searchFunctionFind Stocks
openbb.stocks.siaSub-ModuleSector & Industry Analysis
openbb.stocks.taSub-ModuleStocks-Only Technical Analysis
openbb.stocks.thSub-ModuleTrading Hours (Market Status)
openbb.stocks.tobFunctionTop of Book (CBOE)

How to use

help(openbb.stocks)

Most items in openbb.stocks are sub-modules. The objective of this guide is to introduce the functions at the top-level and to demonstrate a selection of functions from the sub-modules.

Examples

The code snippets in the following section will assume that the import block contains:

import pandas as pd
from datetime import datetime
from openbb_terminal.sdk import openbb

Load

The first step in a workflow might be to collect historical price data. The load function has the ability to request data from multiple sources. The choices for source are currently:

  • YahooFinance (default)
  • AlphaVantage
  • Polygon
  • EODHD
spy_monthly = openbb.stocks.load(
symbol = 'SPY',
start_date = '1990-01-01',
interval = 1440,
prepost = False,
source = 'YahooFinance',
weekly = False,
monthly = True,
)
spy_monthly.head(3)
dateOpenHighLowCloseAdj CloseVolume
1993-02-01 00:00:0043.968845.12542.812544.406225.60435.4176e+06
1993-03-01 00:00:0044.562545.843844.218845.187526.05483.0192e+06
1993-04-01 00:00:0045.2545.2543.281244.031225.5082.6972e+06

There are source-dependent differences to the DataFrame returned. For example, compare the above with below:

spy_monthly = openbb.stocks.load(
symbol = 'SPY',
start_date = '1990-01-01',
interval = 1440,
prepost = False,
source = 'Polygon',
weekly = False,
monthly = True,
)
spy_monthly.head(3)
dateVolumevwOpenAdj CloseHighLowTransactionsClose
2022-09-01 04:00:001.99492e+09382.507392.89357.18411.73357.041.35653e+07357.18
2022-10-01 04:00:002.02389e+09370.918361.08386.21389.52348.111.38047e+07386.21
2022-11-01 04:00:001.17559e+09386.119390.14394.28402.31368.798.34067e+06394.28

Candle

OHLC DataFrames can be passed through the Candle function to display a chart.

openbb.stocks.candle(data = spy_monthly, symbol = 'SPY - Monthly Candles')

openbb.stocks.candle

The candle function is also aware if a ticker is being passed. This makes it unnecessary to create a DataFrame if the objective is only to display the chart.

openbb.stocks.candle('SPY')

openbb.stocks.candle

Add moving averages to the chart with:

openbb.stocks.candle('SPY', ma = [50,150])

openbb.stocks.candle

Search for companies by name with the search function, with optional filters for sector, industry, and region:

openbb.stocks.search(sector = 'Energy', country = 'United Kingdom', query = 'oil')
long_namecountrysectorindustryexchange
03NO.FNostrum Oil & Gas PLCUnited KingdomEnergyOil & Gas E&Pnan
1BOIL.LBaron Oil PlcUnited KingdomEnergyOil & Gas E&Pnan
2EGN.FEuropa Oil & Gas (Holdings) plcUnited KingdomEnergyOil & Gas E&Pnan
3EOG.LEuropa Oil & Gas (Holdings) plcUnited KingdomEnergyOil & Gas E&Pnan
4GHA.FBaron Oil PlcUnited KingdomEnergyOil & Gas E&Pnan

...continued

Quote

Watchlists are easy to make. Get the last price and performance data for multiple tickers by looping the quote function:

spdr_sectors = ['SPY', 'XLE', 'XLB', 'XLI', 'XLP', 'XLY', 'XLV', 'XLF', 'XLK', 'XLC', 'XLU', 'XLRE']

quotes: object = []
symbols = spdr_sectors
for symbols in spdr_sectors:
quote = openbb.stocks.quote(symbols).transpose()
quotes.append(quote)

quotes = pd.concat(quotes)

quotes
SymbolNamePriceChanges percentageChangeDay lowDay highYear highYear lowMarket capPrice avg50Price avg200ExchangeVolumeAvg volumeOpenPrevious closeEpsPeEarnings announcementShares outstandingTimestamp
0SPYSPDR S&P 500 ETF Trust393.740.96163.75390.08394.17462.07348.11361.367 B399.841392.797AMEX88.857 M89590689390.8389.9919.936519.75917.782 M1679342401
0XLEEnergy Select Sector SPDR Fund77.72.00241.525376.178.294.7165.4814.485 B86.658482.6697AMEX25.791 M1990616676.176.174710.867.15186.424 M1679342400
0XLBMaterials Select Sector SPDR Fund76.732.05811.547375.7776.7791.4966.855.519 B81.718677.8203AMEX7.327 M583064575.7775.18275.1625614.8671.924 M1679342400
0XLIIndustrial Select Sector SPDR Fund97.551.32421.274996.7197.79105.2382.7513.328 B101.10395.1877AMEX14.078 M1195747796.7196.27514.697420.77136.626 M1679342400
0XLPConsumer Staples Select Sector SPDR Fund72.761.3860.994772.0172.8381.3466.1815.292 B73.09272.9574AMEX14.127 M1095044972.0471.76532.8867525.2210.172 M1679342400
0XLYConsumer Discretionary Select Sector SPDR Fund141.520.4330.6102139.75142.39192.1912617.018 B143.901146.454AMEX5.863 M5416313140.74140.916.2720322.56120.253 M1679342400
0XLVHealth Care Select Sector SPDR Fund126.961.26751.5891125.58127.145143.42118.7525.064 B131.092130.376AMEX10.584 M8826462125.58125.3715.9201721.45197.415 M1679342400
0XLFFinancial Select Sector SPDR Fund31.171.11140.342631.02531.4440.0129.5927.537 B35.406833.8593AMEX74.917 M5001718831.0730.82742.5707512.12883.445 M1679342400
0XLKTechnology Select Sector SPDR Fund143.530.26930.3855141.82143.7163.65112.9739.048 B136.542132.924AMEX5.924 M7283266142.89143.1455.111128.08272.056 M1679342400
0XLCCommunication Services Select Sector SPDR Fund55.270.74740.4154.7155.4171.5744.86054.111852.9782AMEX7.189 M611729854.9154.862.8711419.2501679342400
0XLUUtilities Select Sector SPDR Fund670.78180.519766.5667.2278.2260.3510.939 B68.015869.9207AMEX19.265 M1230402266.5866.48032.8829523.24163.274 M1679342400
0XLREThe Real Estate Select Sector SPDR Fund36.361.07270.385935.8736.493250.9733.125038.95339.4885AMEX4.963 M603945536.0635.97411.3093827.7701679342400

TOB

Top of Book gets the size and price at the top of the order book.

bid,ask = openbb.stocks.tob('SPY')
quote_tob = bid.join(ask, lsuffix= ': Bid', rsuffix = ': Ask')

quote_tob
Size: BidPrice: BidSize: AskPrice: Ask
0100394.85100395
1100394.8300395.05
2100394.7100395.07
3100394.68200395.25
4100394.65100395.29

Filings

Get the most-recent form submissions to the SEC.

filings = openbb.stocks.disc.filings()
filings.head(3)
DateTickerCIKForm TypeTitleURL
2023-03-20 17:30:26TLGA182787110-K10-K - TLG Acquisition One Corp. (0001827871) (Filer)https://www.sec.gov/Archives/edgar/data/1827871/000119312523074903/0001193125-23-074903-index.htm
2023-03-20 17:30:26TLGA182787110-K10-K - TLG Acquisition One Corp. (0001827871) (Filer)https://www.sec.gov/Archives/edgar/data/1827871/000119312523074903/0001193125-23-074903-index.htm
2023-03-20 17:30:26TLGA-UN182787110-K10-K - TLG Acquisition One Corp. (0001827871) (Filer)https://www.sec.gov/Archives/edgar/data/1827871/000119312523074903/0001193125-23-074903-index.htm
2023-03-20 17:30:26TLGA-UN182787110-K10-K - TLG Acquisition One Corp. (0001827871) (Filer)https://www.sec.gov/Archives/edgar/data/1827871/000119312523074903/0001193125-23-074903-index.htm
2023-03-20 17:30:26TLGA-WT182787110-K10-K - TLG Acquisition One Corp. (0001827871) (Filer)https://www.sec.gov/Archives/edgar/data/1827871/000119312523074903/0001193125-23-074903-index.htm

Filter them to be from the current day only:

today = filings.filter(like = datetime.now().strftime("%Y-%m-%d"), axis = 0)

Screener

Grab the list of filtered tickers and put them through the comparison analysis screener and get an overview:

tickers = today['Ticker'].to_list()
screener_results = openbb.stocks.ca.screener(similar = tickers, data_type = 'overview')
screener_results = screener_results.sort_values(by = ['Market Cap'], ascending = False).convert_dtypes()

screener_results.head(5)
TickerCompanySectorIndustryCountryMarket CapP/EPriceChangeVolume
72NVONovo Nordisk A/SHealthcareBiotechnologyDenmark2.4814e+1141.42143.590.02981242167
34EQNREquinor ASAEnergyOil & Gas IntegratedNorway8.591e+103.0227.340.00964154740
46GSKGSK plcHealthcareDrug Manufacturers - GeneralUnited Kingdom7.026e+1012.2934.930.02522973805
105UBSUBS Group AGFinancialBanks - DiversifiedSwitzerland6.783e+108.3918.80.03340598414
83RELXRELX PLCCommunication ServicesPublishingUnited Kingdom5.97e+1030.5231.310.0205620949

This type of framework can be used to create any type of custom screener. For example, the most popular tickers on Stocktwits:

stocktwits = openbb.stocks.ba.trending()
stocktwits = pd.DataFrame(stocktwits).sort_values(by = 'Watchlist Count', ascending = False)
tickers = stocktwits['Ticker'].to_list()
stocktwits.head(10)
TickerWatchlist CountName
18NVDA409301NVIDIA Corp
12AMC406952AMC Entertainment Holdings Inc
5WMT109864Walmart Inc
6MRNA100705Moderna Inc
21PFE87229Pfizer Inc.
4DWAC66122Digital World Acquisition Corp
29CSCO54458Cisco Systems, Inc.
0SAVA49063Cassava Sciences Inc
8TDOC38657Teladoc Health Inc
16PENN38277Penn National Gaming, Inc.

Filter the results by market cap:

screener_results = openbb.stocks.ca.screener(similar = tickers, data_type = 'overview')
screener_results = screener_results.sort_values(by = ['Market Cap'], ascending = False)

screener_results.head(5)
TickerCompanySectorIndustryCountryMarket CapP/EPriceChangeVolume
15NVDANVIDIA CorporationTechnologySemiconductorsUSA3.9217e+1153.53163.660.00243.16641e+07
26WMTWalmart Inc.Consumer DefensiveDiscount StoresUSA3.864e+1128.46140.5-0.01465.33372e+06
18PFEPfizer Inc.HealthcareDrug Manufacturers - GeneralUSA2.6591e+119.0349.360.0371.07892e+07
4BLKBlackRock, Inc.FinancialAsset ManagementUSA1.1412e+1121.34747.95-0.0346742602
25TGTTarget CorporationConsumer DefensiveDiscount StoresUSA7.566e+1019.68175.670.01362.45293e+06

...continued

SEC

Get the links for SEC filings belonging to a company:

openbb.stocks.fa.sec(symbol = 'WMT')
Filing DateDocument DateTypeCategoryAmendedLink
09/09/202209/06/20228-KSpecial Eventshttps://www.marketwatch.com/investing/stock/wmt/financials/secfilings?docid=16074059
09/02/202207/31/202210-QQuarterly Reportshttps://www.marketwatch.com/investing/stock/wmt/financials/secfilings?docid=16064659
08/17/202208/17/20228-KSpecial Eventshttps://www.marketwatch.com/investing/stock/wmt/financials/secfilings?docid=16026629
08/17/202208/17/20228-KSpecial Eventshttps://www.marketwatch.com/investing/stock/wmt/financials/secfilings?docid=16025172
08/16/202208/16/20228-KSpecial Eventshttps://www.marketwatch.com/investing/stock/wmt/financials/secfilings?docid=16021891
07/25/202207/25/20228-KSpecial Eventshttps://www.marketwatch.com/investing/stock/wmt/financials/secfilings?docid=15964414
07/21/2022N/ASC 13D/AN/A*https://www.marketwatch.com/investing/stock/wmt/financials/secfilings?docid=15959675
06/28/202206/28/20228-KSpecial Eventshttps://www.marketwatch.com/investing/stock/wmt/financials/secfilings?docid=15918852
06/21/2022N/ASC 13DN/Ahttps://www.marketwatch.com/investing/stock/wmt/financials/secfilings?docid=15900809

...continued

Insiders Trading

View insider activity on the stock:

openbb.stocks.ins.lins(symbol = 'WMT')
DateRelationshipTransaction#SharesCostValue ($)#Shares TotalInsider TradingSEC Form 4
Oct 27President and CEOSale9,708141.181,370,6171,478,337McMillon C DouglasOct 28 06:14 PM
Oct 27Executive Vice PresidentSale4,375140.94616,612263,809Furner John R.Oct 28 06:11 PM
Sep 22Executive Vice PresidentSale4,375134.38587,912268,183Furner John R.Sep 23 05:21 PM
Sep 22President and CEOSale9,708134.041,301,3091,488,043McMillon C DouglasSep 23 05:18 PM
Aug 25DirectorSale347,542135.6647,145,880282,330,635WALTON S ROBSONAug 26 06:36 PM

...continued

Income Statement Comparison

Income statements from multiple companies can be easily referenced:

openbb.stocks.ca.income(similar = ['WMT', 'TGT', 'AMZN'], quarter = True)
Item31-Jul-2022: WMT31-Jul-2022: TGT30-Sep-2022: AMZN
Sales/Revenue152.86B26.04B127.1B
Sales Growth7.97%3.44%4.84%
Cost of Goods Sold (COGS) incl. D&A115.84B20.71B70.27B
COGS Growth8.41%8.67%5.79%
COGS excluding D&A113.14B20.06B60.06B
Depreciation & Amortization Expense2.7B650M10.2B
Depreciation2.7B--
Amortization of Intangibles---
Gross Income37.02B5.32B56.83B
Gross Income Growth6.62%-12.85%3.69%
Gross Profit Margin24.22%20.44%44.71%
SG&A Expense30.17B4.98B54.14B
SGA Growth3.30%4.98%5.33%
Research & Development--19.49B
Other SG&A---
Other Operating Expense---
Unusual Expense(238M)27M(2.04B)
EBIT after Unusual Expense238M(27M)2.04B
Non Operating Income/Expense-8M(1.45B)
Non-Operating Interest Income31M-277M

...continued

Ratios

Get historical fundamental ratios for a company:

openbb.stocks.fa.ratios(symbol = 'WMT')
20222021202020192018
PeriodFYFYFYFYFY
Current ratio0.9280.9720.7950.7990.760
Quick ratio0.2640.2620.2020.1810.158
Cash ratio0.1690.1910.1220.1000.086
Days of sales outstanding5.2774.2534.3784.4584.095
Days of inventory outstanding48.08039.03441.10141.93742.799
Operating cycle53.35743.28745.47946.39546.894
Days of payables outstanding47.01742.67443.44944.58045.056
Cash conversion cycle6.3400.6132.0301.8141.838
Gross profit margin0.2510.2480.2470.2510.254
Operating profit margin0.0450.0400.0390.0430.041
Pretax profit margin0.0330.0370.0380.0220.030
Net profit margin0.0240.0240.0280.0130.020

...continued

Take just the ratio needed by filtering the index:

ratios = openbb.stocks.fa.ratios(symbol = 'WMT', limit = 20)
ratios.filter(like = 'Price earnings to growth ratio', axis = 0)
20222021202020192018201720162015201420132012201120102009200820072006200520042003
Price earnings to growth ratio10.202-3.1630.16-1.401-1.069-4.092-1.5014.777-5.4871.2939.680.5591.5851.6371.02215.7611.4981.3581.9531.233

Be sure to check out the introduction guides for each sub-module as well.