diff options
Diffstat (limited to 'python')
| -rw-r--r-- | python/position.py | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/python/position.py b/python/position.py index 0ef35db7..f7df233a 100644 --- a/python/position.py +++ b/python/position.py @@ -2,7 +2,6 @@ from bbg_helpers import init_bbg_session, retrieve_data, BBG_IP import datetime import numpy as np import pandas as pd -import psycopg2 from pandas.tseries.offsets import BDay from pandas import bdate_range from sqlalchemy import create_engine @@ -11,13 +10,21 @@ import os import logging import sys -def get_list(engine, workdate=None, asset_class=None, include_unsettled=True): +def get_list(engine, workdate: datetime.datetime=None, asset_class=None, + include_unsettled: bool=True, + fund="SERCGMAST"): if workdate: - positions = pd.read_sql_query("select identifier, bbg_type from list_positions(%s, %s, %s)", + positions = pd.read_sql_query("SELECT identifier, bbg_type FROM " + "list_positions(%s, %s, %s, %s)", engine, - params=(workdate.date(), asset_class, include_unsettled)) - positions.loc[positions.identifier.str.len() <= 11, 'cusip'] = positions.identifier.str.slice(stop=9) - positions.loc[positions.identifier.str.len() == 12, 'isin'] = positions.identifier + params=(workdate.date(), + asset_class, + include_unsettled, + fund)) + positions.loc[positions.identifier.str.len() <= 11, 'cusip'] = \ + positions.identifier.str.slice(stop=9) + positions.loc[positions.identifier.str.len() == 12, 'isin'] = \ + positions.identifier else: positions = pd.read_sql_table("securities", engine) positions['bbg_id'] = positions.cusip.where(positions.cusip.notnull(), positions['isin']) + \ @@ -217,8 +224,8 @@ def update_swap_rates(conn, session, c.execute(sql_str, data[ticker]) conn.commit() -def populate_cashflow_history(engine, session, workdate=None): - securities = get_list(engine, workdate) +def populate_cashflow_history(engine, session, workdate=None, fund="SERCGMAST"): + securities = get_list(engine, workdate, fund=fund) data = retrieve_data(session, securities.index.tolist(), ['HIST_CASH_FLOW', 'MTG_HIST_CPN', 'FLT_CPN_HIST', 'HIST_INTEREST_DISTRIBUTED']) @@ -263,22 +270,22 @@ def populate_cashflow_history(engine, session, workdate=None): conn.close() if __name__ == "__main__": - serenitas_conn = psycopg2.connect(database="serenitasdb", - user="serenitas_user", - host="debian") - dawn_engine = create_engine('postgresql://dawn_user@debian/dawndb') + from db import serenitas_pool, dawn_engine dawn_conn = dawn_engine.raw_connection() + serenitas_conn = serenitas_pool.getconn() if len(sys.argv) > 1: workdate = pd.Timestamp(sys.argv[1]) else: workdate = pd.Timestamp.now().normalize() with init_bbg_session(BBG_IP) as session: update_securities(dawn_engine, session, workdate) - populate_cashflow_history(dawn_engine, session, workdate) + populate_cashflow_history(dawn_engine, session, workdate, "SERCGMAST") + populate_cashflow_history(dawn_engine, session, workdate, "BRINKER") update_fx(dawn_conn, session, ['EURUSD', 'CADUSD']) update_swap_rates(serenitas_conn, session) for vol_type in ["N", "V"]: update_swaption_vol(serenitas_conn, session, vol_type=vol_type) + serenitas_pool.putconn(serenitas_conn) # with init_bbg_session(BBG_IP) as session: # init_fx(session, engine, pd.datetime(2013, 1, 1)) # with init_bbg_session(BBG_IP) as session: |
