forked from javiert2-dev/FullstackAlgoTrading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
103 lines (77 loc) · 3.11 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
100
101
102
103
from fastapi import FastAPI, Request, Form
import sqlite3, config
from fastapi.templating import Jinja2Templates
from fastapi.responses import RedirectResponse
from datetime import datetime, timedelta
app = FastAPI()
templates = Jinja2Templates(directory="templates")
@app.get("/")
def index(request: Request):
asset_filter = request.query_params.get('filter', False)
connection = sqlite3.connect(config.DB_FILE)
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
date_param = (datetime.today() - timedelta(hours=10)).strftime('%Y-%m-%d')
date = (datetime.today() - timedelta(days=13)).strftime('%Y-%m-%d')
if asset_filter == 'new_closing_highs':
cursor.execute("""
select * from (
SELECT symbol, name, a.id, max(close), date
FROM asset_prices ap JOIN assets a ON
ap.asset_id = a.id GROUP BY asset_id ORDER BY symbol
) AS subquery
where date = ?
""", ('2024-06-06',))
else:
cursor.execute("""
SELECT symbol, name FROM assets ORDER BY symbol LIMIT 50
""")
rows = cursor.fetchall()
return templates.TemplateResponse("index.html", {"request": request, "assets": rows})
@app.get("/asset/{symbol}")
def asset_price_info(request: Request, symbol):
connection = sqlite3.connect(config.DB_FILE)
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
cursor.execute("""
SELECT * FROM trading_strategies
""")
strategies = cursor.fetchall()
cursor.execute("""
SELECT id, symbol, name, exchange FROM assets WHERE symbol = ?
""", (symbol,))
row = cursor.fetchone()
cursor.execute("""
SELECT * FROM asset_prices WHERE asset_id = ? ORDER BY date DESC
""", (row['id'],))
prices = cursor.fetchall()
return templates.TemplateResponse("asset_details.html", {"request": request, "asset": row, "bars":prices, "strategies": strategies })
@app.post("/apply_strategy")
def apply_strategy(strategy_id: int = Form(...), asset_id: int = Form(...)):
connection = sqlite3.connect(config.DB_FILE)
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
cursor.execute("""
INSERT INTO asset_strategy (asset_id, strategy_id) VALUES (?, ?)
""", (asset_id, strategy_id))
connection.commit()
return RedirectResponse(url=f"/strategy/{strategy_id}", status_code = 303)
@app.get("/strategy/{strategy_id}")
def strategy(request: Request, strategy_id):
connection = sqlite3.connect(config.DB_FILE)
connection.row_factory = sqlite3.Row
cursor = connection.cursor()
cursor.execute("""
SELECT id, name
FROM trading_strategies
WHERE id = ?
""", (strategy_id))
strategy = cursor.fetchone()
cursor.execute("""
SELECT symbol, name
FROM assets ts JOIN asset_strategy ast ON ts.id = ast.asset_id
WHERE strategy_id = ?
""", (strategy_id))
assets = cursor.fetchall()
print(assets)
return templates.TemplateResponse("strategy.html", {"request": request, "assets": assets, "strategies": strategy })