Spaces:
Runtime error
Runtime error
huggingface112
commited on
Commit
·
5fd74e2
1
Parent(s):
cb0a98c
update db with daily stocks price
Browse files- instance/local.db +2 -2
- pipeline.py +31 -15
- table_schema.py +0 -1
instance/local.db
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:554c882ab3dd2eb175ca2076ebf2458988340f165620dd03f487d3d23e6f13ea
|
3 |
+
size 6856704
|
pipeline.py
CHANGED
@@ -15,6 +15,7 @@ import api
|
|
15 |
import numpy as np
|
16 |
import pytz
|
17 |
import table_schema as ts
|
|
|
18 |
# fetch new stock price
|
19 |
stock_price_stream = Stream()
|
20 |
|
@@ -236,6 +237,28 @@ def update_portfolio_profile_to_db(portfolio_df):
|
|
236 |
return False
|
237 |
# TODO trigger recomputation of analysis
|
238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
239 |
|
240 |
def update_stock_price():
|
241 |
'''get daily stocks price until today'''
|
@@ -259,10 +282,10 @@ def update_stock_price():
|
|
259 |
return stock_df
|
260 |
|
261 |
|
262 |
-
def
|
263 |
'''
|
264 |
patch stock price db with all daily stock price within window
|
265 |
-
|
266 |
Parameters
|
267 |
----------
|
268 |
window: tuple
|
@@ -272,24 +295,17 @@ def patch_stock_prices_db(window):
|
|
272 |
-------
|
273 |
None
|
274 |
'''
|
275 |
-
start
|
276 |
-
|
277 |
-
with create_engine(db_url).connect() as conn:
|
278 |
-
all_stocks = pd.read_sql('stocks_details', con=conn)
|
279 |
-
|
280 |
selected_stocks = all_stocks[(all_stocks.start_date <= end) & (
|
281 |
all_stocks.end_date >= start)]
|
282 |
tickers = selected_stocks.ticker.to_list()
|
283 |
-
|
284 |
# fetch stock price and append to db
|
285 |
-
stock_price = api.fetch_stocks_price(
|
286 |
-
|
287 |
# drop where closing price is null
|
288 |
-
|
289 |
-
|
290 |
-
detailed_stock_df.to_sql(
|
291 |
-
'stocks_price', con=conn, if_exists='append', index=False)
|
292 |
-
return detailed_stock_df
|
293 |
|
294 |
|
295 |
def update():
|
|
|
15 |
import numpy as np
|
16 |
import pytz
|
17 |
import table_schema as ts
|
18 |
+
import db_operation as db
|
19 |
# fetch new stock price
|
20 |
stock_price_stream = Stream()
|
21 |
|
|
|
237 |
return False
|
238 |
# TODO trigger recomputation of analysis
|
239 |
|
240 |
+
def update_daily_stocks_price():
|
241 |
+
'''
|
242 |
+
update all stocks price until today. used for fetching new stock price
|
243 |
+
|
244 |
+
if no portfolio, terminate without warning
|
245 |
+
default start date is the most recent date in portfolio
|
246 |
+
'''
|
247 |
+
most_recent_portfolio = db.get_most_recent_portfolio_profile()
|
248 |
+
most_recent_stocks_price = db.get_most_recent_stocks_price()
|
249 |
+
|
250 |
+
# fetch all stocks price until today
|
251 |
+
stocks_dates = most_recent_stocks_price.time
|
252 |
+
portfolio_dates = most_recent_portfolio.date
|
253 |
+
if len(portfolio_dates) == 0:
|
254 |
+
return
|
255 |
+
start = stocks_dates[0] if len(stocks_dates) > 0 else portfolio_dates[0]
|
256 |
+
end = utils.time_in_beijing()
|
257 |
+
|
258 |
+
# frequency is set to daily
|
259 |
+
if end - start > dt.timedelta(days=1):
|
260 |
+
new_stocks_price = fetch_all_stocks_price_between(start, end)
|
261 |
+
db.append_to_stocks_price_table(new_stocks_price)
|
262 |
|
263 |
def update_stock_price():
|
264 |
'''get daily stocks price until today'''
|
|
|
282 |
return stock_df
|
283 |
|
284 |
|
285 |
+
def fetch_all_stocks_price_between(start, end):
|
286 |
'''
|
287 |
patch stock price db with all daily stock price within window
|
288 |
+
inclusive on both start and end date
|
289 |
Parameters
|
290 |
----------
|
291 |
window: tuple
|
|
|
295 |
-------
|
296 |
None
|
297 |
'''
|
298 |
+
# all trading stocks available between start day and end date
|
299 |
+
all_stocks = db.get_all_stocks()
|
|
|
|
|
|
|
300 |
selected_stocks = all_stocks[(all_stocks.start_date <= end) & (
|
301 |
all_stocks.end_date >= start)]
|
302 |
tickers = selected_stocks.ticker.to_list()
|
|
|
303 |
# fetch stock price and append to db
|
304 |
+
stock_price = api.fetch_stocks_price(
|
305 |
+
security=tickers, start_date=start, end_date=end, frequency='daily')
|
306 |
# drop where closing price is null
|
307 |
+
stock_price.dropna(subset=['close'], inplace=True)
|
308 |
+
return stock_price
|
|
|
|
|
|
|
309 |
|
310 |
|
311 |
def update():
|
table_schema.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
|
2 |
'''
|
3 |
create df schema for db
|
4 |
'''
|
|
|
|
|
1 |
'''
|
2 |
create df schema for db
|
3 |
'''
|