-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
76 lines (61 loc) · 2.46 KB
/
app.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
# package imports
from flask import Flask, request
import configparser
import sqlite3
import json
from .src.engine import Engine, Covered_Calls_Strat
from .src.data.utils import sql_queries as queries
# admin
config = configparser.ConfigParser()
with open('config.ini') as f:
config.read_file(f)
PROJECT_DB_PATH = config['DB Path']['PROJECT_DB_PATH']
# FUTURE: switch flask -> https://github.com/python-restx/flask-restx ?
# -------------------------------------------
app = Flask(__name__)
app.debug = True
engine = Engine()
# -------------------------------------------
@app.route('/portfolio/strategy/<ticker>', methods = ['GET'])
def portfolio_strategy(ticker):
"""
return all trades related to <ticker> for some given strategy
:param ticker: some stock ticker
body parameters:
strategy -> e.g. covered_call
start_time -> e.g. YYYY-MM-DD HH:MM:SS
end_time -> e.g. YYYY-MM-DD HH:MM:SS
"""
data = request.form
with sqlite3.connect(PROJECT_DB_PATH) as conn:
try:
start_time = data['start_time']
except KeyError:
start_time = None
try:
end_time = data['end_time']
except KeyError:
end_time = None
if data['strategy'] == 'covered_call':
covered_call = Covered_Calls_Strat(engine)
stock_trades, call_trades = covered_call.get_trades(conn, ticker, start_time, end_time)
cumulative_positions = covered_call.calculate_cumulative_positions(stock_trades, call_trades)
return cumulative_positions.to_dict(), 200, {'schema': '*insert schema here'}
@app.route('/portfolio/stocks', methods = ['GET'])
def portfolio_stocks():
with sqlite3.connect(PROJECT_DB_PATH) as conn:
result = queries.execute_sql(conn, queries.sql_get_all_stocks_traded)
return {row[0]: row[1] for row in result}, 200
@app.route('/info/<ticker>', methods = ['GET'])
def ticker_info(ticker):
with sqlite3.connect(PROJECT_DB_PATH) as conn:
result = queries.execute_sql(conn, queries.sql_get_ticker_currency.format(ticker=ticker))[0]
return {'name': result[0], 'currency': result[1]}, 200
@app.route('/historical/price/<ticker>', methods = ['GET'])
def get_historical_price(ticker):
with sqlite3.connect(PROJECT_DB_PATH) as conn:
#result = queries.execute_sql(conn, queries.sql_get_ticker_currency.format(ticker=ticker))[0]
result = {}
return result, 200
if __name__ == '__main__':
app.run()