FastAPI_stock_ticker https://www.youtube.com/watch?v=q8jaJ4Y3H7E
FastAPI - Stock Ticker - with Part Time Larry
pipenv install fastapi pipenv install uvicorn[standard] pipenv install jinja2 pipenv install yfinance pipenv install sqlalchemy
{"symbol":"APPL"}
{ "code": "success", "message": "stock" }
def get_db(): try: db = SessionLocal() yield db finally: db.close()
{"symbol":"MSFT"}
sqlite> delete from stocks; sqlite> select * from stocks; sqlite>
from fastapi import FastAPI, Request, Depends, BackgroundTasks
def create_stock(stock_request: StockRequest, background_tasks: BackgroundTasks, db: Session = Depends(get_db)):
background_tasks.add_task()
def fetch_stock_data(id: int): pass
background_tasks.add_task(fetch_stock_data, stock.id)
def fetch_stock_data(id: int): db = SessionLocal() stock = db.query(Stock).filter(Stock.id = id).first()
stock.forward_pe = 10
db.add(stock)
db.commit()
{"symbol":"PG"}
{ "code": "success", "message": "stock" }
sqlite> select * from stocks; 1|PG||10|||| sqlite>
async def create_stock(stock_request: StockRequest, background_tasks: BackgroundTasks, db: Session = Depends(get_db)):
{"symbol":"JNJ"}
{ "code": "success", "message": "stock" }
sqlite> select * from stocks; 1|PG||10|||| 2|JNJ||10|||| sqlite>
import yfinanace
testyfinance.py
import yfinance as yf
msft = yf.Ticker("MSFT")
#get stock inof print(msft.info)
{'zip': '98052-6399', 'sector': 'Technology', 'fullTimeEmployees': 163000, 'longBusinessSummary': 'Microsoft Corporation develops, licenses, and supports software, services, devices, and solutions worldwide. Its Productivity and Business Processes segment offers Office, Exchange, SharePoint, Microsoft Teams, Office 365 Security and Compliance, and Skype for Business, as well as related Client Access Licenses (CAL); Skype, Outlook.com, OneDrive, and LinkedIn; and Dynamics 365, a set of cloud-based and on-premises business solutions for small and medium businesses, large organizations, and divisions of enterprises. Its Intelligent Cloud segment licenses SQL and Windows Servers, Visual Studio, System Center, and related CALs; GitHub that provides a collaboration platform and code hosting service for developers; and Azure, a cloud platform. It also offers support services and Microsoft consulting services to assist customers in developing, deploying, and managing Microsoft server and desktop solutions; and training and certification to developers and IT professionals on various Microsoft products. Its More Personal Computing segment provides Windows original equipment manufacturer (OEM) licensing and other non-volume licensing of the Windows operating system; Windows Commercial, such as volume licensing of the Windows operating system, Windows cloud services, and other Windows commercial offerings; patent licensing; Windows Internet of Things; and MSN advertising. It also offers Surface, PC accessories, PCs, tablets, gaming and entertainment consoles, and other devices; Gaming, including Xbox hardware, and Xbox content and services; video games and third-party video game royalties; and Search, including Bing and Microsoft advertising. It sells its products through OEMs, distributors, and resellers; and directly through digital marketplaces, online stores, and retail stores. It has a strategic collaboration with DXC Technology. The company was founded in 1975 and is headquartered in Redmond, Washington.', 'city': 'Redmond', 'phone': '425-882-8080', 'state': 'WA', 'country': 'United States', 'companyOfficers': [], 'website': 'http://www.microsoft.com', 'maxAge': 1, 'address1': 'One Microsoft Way', 'industry': 'Software—Infrastructure', 'previousClose': 231.96, 'regularMarketOpen': 235.06, 'twoHundredDayAverage': 214.06598, 'trailingAnnualDividendYield': 0.009225729, 'payoutRatio': 0.31149998, 'volume24Hr': None, 'regularMarketDayHigh': 242.5, 'navPrice': None, 'averageDailyVolume10Day': 45657516, 'totalAssets': None, 'regularMarketPreviousClose': 231.96, 'fiftyDayAverage': 221.45813, 'trailingAnnualDividendRate': 2.14, 'open': 235.06, 'toCurrency': None, 'averageVolume10days': 45657516, 'expireDate': None, 'yield': None, 'algorithm': None, 'dividendRate': 2.24, 'exDividendDate': 1613520000, 'beta': 0.826155, 'circulatingSupply': None, 'startDate': None, 'regularMarketDayLow': 232.46, 'priceHint': 2, 'currency': 'USD', 'trailingPE': 35.731327, 'regularMarketVolume': 31762797, 'lastMarket': None, 'maxSupply': None, 'openInterest': None, 'marketCap': 1807492972544, 'volumeAllCurrencies': None, 'strikePrice': None, 'averageVolume': 29775703, 'priceToSalesTrailing12Months': 11.791791, 'dayLow': 232.46, 'ask': 240.42, 'ytdReturn': None, 'askSize': 800, 'volume': 31762797, 'fiftyTwoWeekHigh': 242.64, 'forwardPE': 29.62299, 'fromCurrency': None, 'fiveYearAvgDividendYield': 1.71, 'fiftyTwoWeekLow': 132.52, 'bid': 240.05, 'tradeable': False, 'dividendYield': 0.0097, 'bidSize': 800, 'dayHigh': 242.5, 'exchange': 'NMS', 'shortName': 'Microsoft Corporation', 'longName': 'Microsoft Corporation', 'exchangeTimezoneName': 'America/New_York', 'exchangeTimezoneShortName': 'EST', 'isEsgPopulated': False, 'gmtOffSetMilliseconds': '-18000000', 'quoteType': 'EQUITY', 'symbol': 'MSFT', 'messageBoardId': 'finmb_21835', 'market': 'us_market', 'annualHoldingsTurnover': None, 'enterpriseToRevenue': 11.092, 'beta3Year': None, 'profitMargins': 0.33473998, 'enterpriseToEbitda': 23.718, '52WeekChange': 0.3301984, 'morningStarRiskRating': None, 'forwardEps': 8.09, 'revenueQuarterlyGrowth': None, 'sharesOutstanding': 7560500224, 'fundInceptionDate': None, 'annualReportExpenseRatio': None, 'bookValue': 17.259, 'sharesShort': 41952779, 'sharesPercentSharesOut': 0.0056, 'fundFamily': None, 'lastFiscalYearEnd': 1593475200, 'heldPercentInstitutions': 0.71844, 'netIncomeToCommon': 51309998080, 'trailingEps': 6.707, 'lastDividendValue': 0.56, 'SandP52WeekChange': 0.14322305, 'priceToBook': 13.885508, 'heldPercentInsiders': 0.00059, 'nextFiscalYearEnd': 1656547200, 'mostRecentQuarter': 1609372800, 'shortRatio': 1.54, 'sharesShortPreviousMonthDate': 1607990400, 'floatShares': 7431722306, 'enterpriseValue': 1700285382656, 'threeYearAverageReturn': None, 'lastSplitDate': 1045526400, 'lastSplitFactor': '2:1', 'legalType': None, 'lastDividendDate': 1605657600, 'morningStarOverallRating': None, 'earningsQuarterlyGrowth': 0.327, 'dateShortInterest': 1610668800, 'pegRatio': 1.82, 'lastCapGain': None, 'shortPercentOfFloat': 0.0056, 'sharesShortPriorMonth': 39913925, 'impliedSharesOutstanding': None, 'category': None, 'fiveYearAverageReturn': None, 'regularMarketPrice': 235.06, 'logo_url': 'https://logo.clearbit.com/microsoft.com'}
stock.ma200 = yahoo_data.info['twoHundredDayAverage']
stock.ma50 = yahoo_data.info['fiftyDayAverage']
stock.price = yahoo_data.info['previousClose']
stock.forward_pe = yahoo_data.info['forwardPE']
stock.forward_eps = yahoo_data.info['forwardEps']
if yahoo_data.info['dividendYield'] is not None:
stock.dividend_yield = yahoo_data.info['dividendYield'] * 100
{"symbol":"AMZN"}
{ "code": "success", "message": "stock" }
sqlite> select * from stocks; 1|PG||10|||| 2|JNJ||10|||| 3|AMZN|3206.2|73.21244|45.66||3210.0344|3185.174 sqlite>