-
Notifications
You must be signed in to change notification settings - Fork 161
/
main.py
99 lines (72 loc) · 2.53 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import models
import yfinance
from fastapi import FastAPI, Request, Depends, BackgroundTasks
from fastapi.templating import Jinja2Templates
from database import SessionLocal, engine
from pydantic import BaseModel
from models import Stock
from sqlalchemy.orm import Session
app = FastAPI()
models.Base.metadata.create_all(bind=engine)
templates = Jinja2Templates(directory="templates")
class StockRequest(BaseModel):
symbol: str
def get_db():
try:
db = SessionLocal()
yield db
finally:
db.close()
@app.get("/")
def home(request: Request, forward_pe = None, dividend_yield = None, ma50 = None, ma200 = None, db: Session = Depends(get_db)):
"""
show all stocks in the database and button to add more
button next to each stock to delete from database
filters to filter this list of stocks
button next to each to add a note or save for later
"""
stocks = db.query(Stock)
if forward_pe:
stocks = stocks.filter(Stock.forward_pe < forward_pe)
if dividend_yield:
stocks = stocks.filter(Stock.dividend_yield > dividend_yield)
if ma50:
stocks = stocks.filter(Stock.price > Stock.ma50)
if ma200:
stocks = stocks.filter(Stock.price > Stock.ma200)
stocks = stocks.all()
return templates.TemplateResponse("home.html", {
"request": request,
"stocks": stocks,
"dividend_yield": dividend_yield,
"forward_pe": forward_pe,
"ma200": ma200,
"ma50": ma50
})
def fetch_stock_data(id: int):
db = SessionLocal()
stock = db.query(Stock).filter(Stock.id == id).first()
yahoo_data = yfinance.Ticker(stock.symbol)
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']
stock.dividend_yield = yahoo_data.info['dividendYield'] * 100
db.add(stock)
db.commit()
@app.post("/stock")
async def create_stock(stock_request: StockRequest, background_tasks: BackgroundTasks, db: Session = Depends(get_db)):
"""
add one or more tickers to the database
background task to use yfinance and load key statistics
"""
stock = Stock()
stock.symbol = stock_request.symbol
db.add(stock)
db.commit()
background_tasks.add_task(fetch_stock_data, stock.id)
return {
"code": "success",
"message": "stock was added to the database"
}