Prompt works in DB Browser SQLite but not in code?
Question:
I’m trying to filter a list of stocks based on price data. To be honest I don’t really know what I’m doing so any help is really appreciated.
I’ll get right to the point. Basically, this prompt
select * from (
select symbol, name, stock_id, max(close), date
from stock_price join stock on stock.id = stock_price.stock_id
group by stock_id
order by symbol
) where date = '2021-04-01'
works fine in DB Browser (SQLite). In the app I want it to work as a filter "New Closing High":
import sqlite3, config
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
from datetime import date
app = FastAPI()
templates = Jinja2Templates(directory="templates")
@app.get("/")
def index(request: Request):
stock_filter = request.query_params.get('filter', False)
connection = sqlite3.connect(config.DB_FILE)
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
if stock_filter == 'new_closing_highs':
cursor.execute("""
select * from (
select symbol, name, stock_id, max(close), date
from stock_price join stock on stock.id = stock_price.stock_id
group by stock_id
order by symbol
) where date = ?
""", (date.today().isoformat(),))
else:
cursor.execute("""
SELECT id, symbol, name FROM stock order by symbol
""")
rows = cursor.fetchall()
return templates.TemplateResponse("index.html", {"request": request, "stocks": rows})
@app.get("/stock/{symbol}")
def index(request: Request, symbol):
connection = sqlite3.connect(config.DB_FILE)
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
cursor.execute("""
SELECT id, symbol, name FROM stock WHERE symbol = ?
""", (symbol,))
row = cursor.fetchall()
cursor.execute("""
SELECT *FROM stock_price WHERE stock_ID = ? ORDER BY date DESC
""", (row['id'],))
prices = cursor.fetchall()
return templates.TemplateResponse("stock_detail.html", {"request": request, "stock": row, "bars": prices})
If the filter wasn’t defined in the code, it would just jump back to all stocks, but instead it shows an empty table and I can’t figure out why. Running this with uvicorn.
The database has two tables so far: stocks, containing all tradeable, active assets provided by Alpaca API:
id symbol name Exchange
65 AAA AAF First Priority CLO Bond ETF ARCA
66 AAAU Goldman Sachs Physical Gold ETF Shares ARCA
and stock_price:
id stock_id date open high low close volume
1 65 2020-10-02 24.9127 24.925 24.9127 24.92 2944
2 65 2020-10-05 24.91 24.94 24.91 24.92 29000
3 65 2020-10-06 24.89 24.91 24.89 24.91 4019
4 65 2020-10-07 24.9017 24.91 24.9 24.91 3800
5 65 2020-10-08 24.9 24.905 24.9 24.9 1534
6 65 2020-10-09 24.88 24.91 24.88 24.89 16273
Please let me know if I haven’t provided enough info here.
thanks so much for taking the time
Answers:
I don’t think that your query actually works.
Maybe it works coincidentally for the data you have and the specific date '2021-04-01'
.
If you want to get for a specific date the highest price of each stock, you should join the tables, group by stock and aggregate:
SELECT s.symbol, s.name, s.id, MAX(p.close) max_price, p.date
FROM stock s INNER JOIN stock_price p
ON p.stock_id = s.id
WHERE p.date = ?
GROUP BY s.symbol, s.name, s.id
MY BAD! I don’t have data for today yet! I thought I did, but the script didn’t run as scheduled. Should’ve just double-checked. So sorry if I wasted your time
I’m trying to filter a list of stocks based on price data. To be honest I don’t really know what I’m doing so any help is really appreciated.
I’ll get right to the point. Basically, this prompt
select * from (
select symbol, name, stock_id, max(close), date
from stock_price join stock on stock.id = stock_price.stock_id
group by stock_id
order by symbol
) where date = '2021-04-01'
works fine in DB Browser (SQLite). In the app I want it to work as a filter "New Closing High":
import sqlite3, config
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
from datetime import date
app = FastAPI()
templates = Jinja2Templates(directory="templates")
@app.get("/")
def index(request: Request):
stock_filter = request.query_params.get('filter', False)
connection = sqlite3.connect(config.DB_FILE)
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
if stock_filter == 'new_closing_highs':
cursor.execute("""
select * from (
select symbol, name, stock_id, max(close), date
from stock_price join stock on stock.id = stock_price.stock_id
group by stock_id
order by symbol
) where date = ?
""", (date.today().isoformat(),))
else:
cursor.execute("""
SELECT id, symbol, name FROM stock order by symbol
""")
rows = cursor.fetchall()
return templates.TemplateResponse("index.html", {"request": request, "stocks": rows})
@app.get("/stock/{symbol}")
def index(request: Request, symbol):
connection = sqlite3.connect(config.DB_FILE)
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
cursor.execute("""
SELECT id, symbol, name FROM stock WHERE symbol = ?
""", (symbol,))
row = cursor.fetchall()
cursor.execute("""
SELECT *FROM stock_price WHERE stock_ID = ? ORDER BY date DESC
""", (row['id'],))
prices = cursor.fetchall()
return templates.TemplateResponse("stock_detail.html", {"request": request, "stock": row, "bars": prices})
If the filter wasn’t defined in the code, it would just jump back to all stocks, but instead it shows an empty table and I can’t figure out why. Running this with uvicorn.
The database has two tables so far: stocks, containing all tradeable, active assets provided by Alpaca API:
id symbol name Exchange
65 AAA AAF First Priority CLO Bond ETF ARCA
66 AAAU Goldman Sachs Physical Gold ETF Shares ARCA
and stock_price:
id stock_id date open high low close volume
1 65 2020-10-02 24.9127 24.925 24.9127 24.92 2944
2 65 2020-10-05 24.91 24.94 24.91 24.92 29000
3 65 2020-10-06 24.89 24.91 24.89 24.91 4019
4 65 2020-10-07 24.9017 24.91 24.9 24.91 3800
5 65 2020-10-08 24.9 24.905 24.9 24.9 1534
6 65 2020-10-09 24.88 24.91 24.88 24.89 16273
Please let me know if I haven’t provided enough info here.
thanks so much for taking the time
I don’t think that your query actually works.
Maybe it works coincidentally for the data you have and the specific date '2021-04-01'
.
If you want to get for a specific date the highest price of each stock, you should join the tables, group by stock and aggregate:
SELECT s.symbol, s.name, s.id, MAX(p.close) max_price, p.date
FROM stock s INNER JOIN stock_price p
ON p.stock_id = s.id
WHERE p.date = ?
GROUP BY s.symbol, s.name, s.id
MY BAD! I don’t have data for today yet! I thought I did, but the script didn’t run as scheduled. Should’ve just double-checked. So sorry if I wasted your time