-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfastapi_api.py
112 lines (83 loc) · 3.03 KB
/
fastapi_api.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
104
105
106
107
108
109
110
111
112
import uvicorn
from datetime import date, timedelta
from utils import ta_calculations as ta_calcs
from utils import functions as fnc
from utils import ohlc_values as ohlc
from utils import dollar_price as dllp
from fastapi import FastAPI
from fastapi.encoders import jsonable_encoder
from fastapi.middleware.cors import CORSMiddleware
from typing import Optional
import typing
app = FastAPI(
title="Stock Market API",
description="Get Stocks Prices, Technical Analysis Indicators and Dollar Prices",
version="1.0.0"
)
origins = ['*']
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_methods=['GET'],
allow_headers=['Content-Type, Authorization'],
)
@app.get('/api/technical-analysis-between')
async def get_ta(
*,
ticker: str,
indicator: str,
start_date: Optional[str] = None,
end_date: Optional[str] = None
):
if not start_date:
# date params format: YYYY/MM/DD
start_date = date.today() - timedelta(days=365)
#start_date = start_date.strftime("%Y/%m/%d")
start_date = start_date.strftime("%m/%d/%Y")
#print({"q": q})
if not end_date:
# date params format: YYYY/MM/DD
end_date = date.today().strftime("%m/%d/%Y")
#start_date = start_date.strftime("%Y/%m/%d")
#start_date = start_date.strftime("%m/%d/%Y")
#print({"q": q})
print(f"indicator: {indicator}, ticker: {ticker}")
indicator_values, str_dates = ta_calcs.get_indicator_values(ticker, indicator, start_date, end_date)
ta = fnc.ta_json_format(ticker, indicator, indicator_values, str_dates)
return jsonable_encoder(ta)
@app.get('/api/simple-technical-analysis')
async def get_simple_ta(ticker: str):
simple_ta = ta_calcs.get_simple_ta(ticker)
return jsonable_encoder(simple_ta)
@app.get('/api/price-between')
async def get_price_between(ticker: str, start_date: str, end_date: str):
# date params format: YYYY/MM/DD
data = ohlc.get_ohlc_between(ticker, start_date, end_date)
return_json = {}
return_json['ticker'] = ticker
return_json['name'] = fnc.get_name(ticker)
return_json['data'] = data
return jsonable_encoder(return_json)
@app.get('/api/year-today-price')
async def get_year_today_prices(ticker: str):
data = ohlc.get_ohlc_year_today(ticker)
return_json = {}
return_json['ticker'] = ticker
return_json['name'] = fnc.get_name(ticker)
return_json['data'] = data
return jsonable_encoder(return_json)
@app.get('/api/data')
def get_docs():
return_json = {}
return_json['indicators'] = fnc.get_indicators_types()
return_json['cedears'] = fnc.getCedears()
# https://fastapi.tiangolo.com/advanced/response-directly/
# json_compatible_item_data = jsonable_encoder(return_json)
return jsonable_encoder(return_json)
@app.get('/api/ccl-cedear-dollar')
async def get_ccl_vs_cedear_dollar(ticker: str):
data = dllp.calculate_difference(ticker)
response = jsonable_encoder(data)
return response
if __name__ == '__main__':
uvicorn.run(app, port=5000)