Skip to content

Commit

Permalink
#30 StockPriceDailyPct, StockPriceCumChange
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamonkey committed Nov 2, 2023
1 parent 76b838b commit f0eb808
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
45 changes: 44 additions & 1 deletion src/riskMonitoring/alertMonitor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from riskMonitoring import db_operation as db
class PNLDown():
def watch(self):
pass
Expand All @@ -19,4 +20,46 @@ def watch():

class IsShuWongOkay():
def watch():
pass
pass


class StockPriceDailyPct():
'''
return any latest stock in portfolio has a daily change over threshold
Parameters
----------
threshold: float
threshold to trigger alert, represent percentage
'''

def __init__(self, **parm):
self.latest_analytic_p = db.get_most_recent_analytic_p()
self.threshold = parm['threshold']

def run(self):
exceed = self.latest_analytic_p[self.latest_analytic_p['pct'] > self.threshold]
return exceed

class StockPriceCumChange():
'''
return any latest stock in portfolio has a accumulative change of average price over threshold
Parameters
----------
threshold: float
threshold to trigger alert, represent percentage
'''
def __init__(self, **param):
self.analytic_p = db.get_portfolio_analytic_df()
self.portfolio_profile = db.get_most_recent_portfolio_profile()
self.analytic_p = self.analytic_p[self.analytic_p.time == self.analytic_p.time.max()]
self.threshold = param['threshold']


def run(self):
result = self.portfolio_profile.merge(self.analytic_p[['ticker', 'close']], on='ticker', suffixes=('_profile', '_analytic'))
result['cum_pct'] = result['close'] / result['ave_price'] - 1
exceed = result[result['cum_pct'] > self.threshold]

return exceed
25 changes: 5 additions & 20 deletions src/riskMonitoring/db_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,6 @@ def get_most_recent_benchmark_profile():
return _get_most_recent(ts.BENCHMARK_TABLE)


def get_most_recent_profile(type):
table_name = 'benchmark_profile' if type == 'benchmark' else 'portfolio_profile'
query = f"SELECT * FROM {table_name} WHERE date = (SELECT MAX(date) FROM {table_name})"
with create_engine(db_url).connect() as conn:
df = pd.read_sql(query, con=conn)
# convert date to datetime object
df['date'] = pd.to_datetime(df['date'])
return df


def _get_oldest(table_name, ts_column='date'):
query = f"SELECT * FROM {table_name} WHERE {ts_column} = (SELECT MIN({ts_column}) FROM {table_name})"
with create_engine(db_url).connect() as conn:
Expand All @@ -102,13 +92,14 @@ def get_oldest_portfolio_profile():
return df


def get_oldest_stocks_proce():
df = _get_oldest(ts.STOCKS_PRICE_TABLE, ts_column='time')
def get_oldest_benchmark_profile():
df = _get_oldest(ts.BENCHMARK_TABLE)
return df


def get_oldest_benchmark_profile():
df = _get_oldest(ts.BENCHMARK_TABLE)
def get_most_recent_analytic_p():
table_name = 'analytic_p'
df = _get_most_recent(table_name, ts_column='time')
return df


Expand Down Expand Up @@ -217,12 +208,6 @@ def save_analytic_df(df):
df.to_sql(table_name, con=conn, if_exists='replace', index=False)


def get_analytic_df():
table_name = 'analytic'
with create_engine(db_url).connect() as conn:
df = pd.read_sql(table_name, con=conn)
return df


def _get_all_row(table_name, ts_column='date'):
with create_engine(db_url).connect() as conn:
Expand Down

0 comments on commit f0eb808

Please sign in to comment.