Skip to content

Instantly share code, notes, and snippets.

@QuantTraderEd
Last active August 29, 2015 14:06
Show Gist options
  • Save QuantTraderEd/193d2ba17f9753175125 to your computer and use it in GitHub Desktop.
Save QuantTraderEd/193d2ba17f9753175125 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 26 13:35:55 2014
@author: assa
"""
import time
import numpy as np
import pandas as pd
import pandas.io.sql as psql
import cx_Oracle as odb
def make_signal()
ip = '10.10.1.50'
port = '1521'
SID = 'fndb2'
tns_REALDB = odb.makedsn(ip, port, SID)
conn = odb.connect('UFNGDBA','venus2002',tns_REALDB)
curs = conn.cursor()
sqltext = """
SELECT A.TRD_DT
FROM FNC_CALENDAR A
WHERE A.TRD_DT > '20060101'
AND A.OPEN_GB_STOCK = '0'
ORDER BY A.TRD_DT
"""
df_date = psql.frame_query(sqltext, conn)
sqltext = """
SELECT TRD_dT, AMOUNT KORCDS5YR FROM FNE_ECO_DATA
WHERE ECO_CD = '11.02.009.001.001' -- KOR CDS 5YR
AND TERM = 'D'
AND TRD_DT > '20060101'
ORDER BY TRD_DT
"""
df_KRCDS5YR = psql.frame_query(sqltext, conn)
sqltext = """
SELECT TRD_dT, AMOUNT KRWUSD FROM FNE_ECO_DATA
WHERE ECO_CD = '13.03.006.001.001' -- KRWUSD
AND TERM = 'D'
AND TRD_DT > '20060101'
ORDER BY TRD_DT
"""
df_KRWUSD = psql.frame_query(sqltext, conn)
sqltext = """
SELECT TRD_DT, CLS_PRC VIX FROM FNS_FIDX_D
WHERE U_CD = 'I.VIX' -- SnP500 VIX
AND TRD_DT > '20060101'
ORDER BY TRD_DT
"""
df_VIX = psql.frame_query(sqltext, conn)
sqltext = """
SELECT TRD_DT ,CLS_PRC SSE FROM FNS_FIDX_D
WHERE U_CD = 'I.SSEC' -- Shanghai composite index
AND TRD_DT > '20060101'
ORDER BY TRD_DT
"""
df_SSE = psql.frame_query(sqltext, conn)
sqltext = """
SELECT TRD_DT, CLS_PRC KOSPI FROM FNS_UD
WHERE U_CD = 'I.001' -- KOSPI
AND TRD_DT > '20060101'
ORDER BY TRD_DT
"""
df_KOSPI = psql.frame_query(sqltext, conn)
df_TED = pd.io.parsers.read_csv('./Oracle/TEDSpread.csv')
df_TED['TRD_DT'] = df_TED['date'].str.replace('-','')
del df_TED['date']
df_TED.columns = ['TED','TRD_DT']
df = pd.merge(df_KRCDS5YR,df_KRWUSD,on='TRD_DT')
df = pd.merge(df,df_VIX,on='TRD_DT')
df = pd.merge(df,df_SSE,on='TRD_DT')
df = pd.merge(df,df_TED,on='TRD_DT')
df = pd.merge(df,df_KOSPI,on='TRD_DT')
df['TED'] = df['TED'].fillna(method='pad')
print df.corr()
# SSE +, CDS -, KRWUSD -, VIX -, TED -
# Get AVG, STD from series
df['KORCDS5YR_AVG'] = pd.rolling_mean(df['KORCDS5YR'],20)
df['KRWUSD_AVG'] = pd.rolling_mean(df['KRWUSD'],20)
df['VIX_AVG'] = pd.rolling_mean(df['VIX'],20)
df['SSE_AVG'] = pd.rolling_mean(df['SSE'],20)
df['TED_AVG'] = pd.rolling_mean(df['TED'],20)
df['KOSPI_AVG'] = pd.rolling_mean(df['KOSPI'],20)
df['KORCDS5YR_STD'] = pd.rolling_std(df['KORCDS5YR'],20)
df['KRWUSD_STD'] = pd.rolling_std(df['KRWUSD'],20)
df['VIX_STD'] = pd.rolling_std(df['VIX'],20)
df['SSE_STD'] = pd.rolling_std(df['SSE'],20)
df['TED_STD'] = pd.rolling_std(df['TED'],20)
df['KOSPI_STD'] = pd.rolling_std(df['KOSPI'],20)
df['KORCDS5YR_LMT'] = df['KORCDS5YR_AVG'] + df['KORCDS5YR_STD'] * 1.64
df['KRWUSD_LMT'] = df['KRWUSD_AVG'] + df['KRWUSD_STD'] * 1.64
df['VIX_LMT'] = df['VIX_AVG'] + df['VIX_STD'] * 1.64
df['SSE_LMT'] = df['SSE_AVG'] - df['SSE_STD'] * 1.64
df['TED_LMT'] = df['TED_AVG'] + df['TED_STD'] * 1.64
df['KOSPI_LMT'] = df['KOSPI_AVG'] - df['KOSPI_STD'] * 1.64
#df = df[df['TRD_DT'] > '20060313']
df['KORCDS5YR_SIG'] = np.where((df['KORCDS5YR'] > df['KORCDS5YR_LMT']),1,0)
df['KRWUSD_SIG'] = np.where((df['KRWUSD'] > df['KRWUSD_LMT']),1,0)
df['VIX_SIG'] = np.where((df['VIX'] > df['VIX_LMT']),1,0)
df['SSE_SIG'] = np.where((df['SSE'] < df['SSE_LMT']),1,0)
df['TED_SIG'] = np.where((df['TED'] > df['TED_LMT']),1,0)
df['KOSPI_SIG'] = np.where(df['KOSPI'] < df['KOSPI_LMT'],1,0)
df['KORCDS5YR_SIG_A'] = np.where((df['KORCDS5YR'] > df['KORCDS5YR_LMT']) & (df['KOSPI'] < df['KOSPI_LMT']),1,0)
df['KRWUSD_SIG_A'] = np.where((df['KRWUSD'] > df['KRWUSD_LMT']) & (df['KOSPI'] < df['KOSPI_LMT']),1,0)
df['VIX_SIG_A'] = np.where((df['VIX'] > df['VIX_LMT']) & (df['KOSPI'] < df['KOSPI_LMT']),1,0)
df['SSE_SIG_A'] = np.where((df['SSE'] < df['SSE_LMT']) & (df['KOSPI'] < df['KOSPI_LMT']),1,0)
df['TED_SIG_A'] = np.where((df['TED'] > df['TED_LMT']) & (df['KOSPI'] < df['KOSPI_LMT']),1,0)
df['KORCDS5YR_SIG_B'] = np.where((df['KORCDS5YR'] < df['KORCDS5YR_LMT']) & (df['KOSPI'] < df['KOSPI_LMT']),1,0)
df['KRWUSD_SIG_B'] = np.where((df['KRWUSD'] < df['KRWUSD_LMT']) & (df['KOSPI'] < df['KOSPI_LMT']),1,0)
df['VIX_SIG_B'] = np.where((df['VIX'] < df['VIX_LMT']) & (df['KOSPI'] < df['KOSPI_LMT']),1,0)
df['SSE_SIG_B'] = np.where((df['SSE'] > df['SSE_LMT']) & (df['KOSPI'] < df['KOSPI_LMT']),1,0)
df['TED_SIG_B'] = np.where((df['TED'] < df['TED_LMT']) & (df['KOSPI'] < df['KOSPI_LMT']),1,0)
df['KORCDS5YR_SIGSUM_A'] = pd.rolling_sum(df['KORCDS5YR_SIG_A'],60)
df['KRWUSD_SIGSUM_A'] = pd.rolling_sum(df['KRWUSD_SIG_A'],60)
df['VIX_SIGSUM_A'] = pd.rolling_sum(df['VIX_SIG_A'],60)
df['SSE_SIGSUM_A'] = pd.rolling_sum(df['SSE_SIG_A'],60)
df['TED_SIGSUM_A'] = pd.rolling_sum(df['TED_SIG_A'],60)
df['KORCDS5YR_SIGSUM_B'] = pd.rolling_sum(df['KORCDS5YR_SIG_B'],60)
df['KRWUSD_SIGSUM_B'] = pd.rolling_sum(df['KRWUSD_SIG_B'],60)
df['VIX_SIGSUM_B'] = pd.rolling_sum(df['VIX_SIG_B'],60)
df['SSE_SIGSUM_B'] = pd.rolling_sum(df['SSE_SIG_B'],60)
df['TED_SIGSUM_B'] = pd.rolling_sum(df['TED_SIG_B'],60)
df['KOSPI_SIGSUM'] = pd.rolling_sum(df['KOSPI_SIG'],60)
df['KORCDS5YR_NTSSUB'] = ( df['KORCDS5YR_SIGSUM_B'] / (60-df['KOSPI_SIGSUM']) ) / (df['KORCDS5YR_SIGSUM_A'] / df['KOSPI_SIGSUM'] )
df['KRWUSD_NTSSUB'] = ( df['KRWUSD_SIGSUM_B'] / (60-df['KOSPI_SIGSUM']) ) / (df['KRWUSD_SIGSUM_A'] / df['KOSPI_SIGSUM'] )
df['VIX_NTSSUB'] = ( df['VIX_SIGSUM_B'] / (60-df['KOSPI_SIGSUM']) ) / (df['VIX_SIGSUM_A'] / df['KOSPI_SIGSUM'] )
df['SSE_NTSSUB'] = ( df['SSE_SIGSUM_B'] / (60-df['KOSPI_SIGSUM']) ) / (df['SSE_SIGSUM_A'] / df['KOSPI_SIGSUM'] )
df['TED_NTSSUB'] = ( df['TED_SIGSUM_B'] / (60-df['KOSPI_SIGSUM']) ) / (df['TED_SIGSUM_A'] / df['KOSPI_SIGSUM'] )
df['KORCDS5YR_NTS_WEIGHT'] = np.where(df['KORCDS5YR_NTSSUB'] > 1,0,df['KORCDS5YR_NTSSUB'] * df['KORCDS5YR_SIG'])
df['KRWUSD_NTS_WEIGHT'] = np.where(df['KRWUSD_NTSSUB'] > 1,0,df['KRWUSD_NTSSUB'] * df['KRWUSD_SIG'])
df['VIX_NTS_WEIGHT'] = np.where(df['VIX_NTSSUB'] > 1,0,df['VIX_NTSSUB'] * df['VIX_SIG'])
df['SSE_NTS_WEIGHT'] = np.where(df['SSE_NTSSUB'] > 1,0,df['SSE_NTSSUB'] * df['SSE_SIG'])
df['TED_NTS_WEIGHT'] = np.where(df['TED_NTSSUB'] > 1,0,df['TED_NTSSUB'] * df['TED_SIG'])
df['EWI'] = (df['KORCDS5YR_NTS_WEIGHT'] + df['KRWUSD_NTS_WEIGHT'] + df['VIX_NTS_WEIGHT'] + df['SSE_NTS_WEIGHT'] + df['TED_NTS_WEIGHT']) / 5 * 10
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 19 08:56:17 2015
@author: assa
"""
#import pdb
import time
import pandas as pd
import pandas.io.sql as psql
import cx_Oracle as odb
from openpyxl import workbook
# REALDB
ip = '10.10.1.50'
port = '1521'
SID = 'fndb2'
tns_REALDB = odb.makedsn(ip, port, SID)
conn = odb.connect('UFNGDBA','venus2002',tns_REALDB)
curs = conn.cursor()
sqltext = """
SELECT START_DT
FROM RES_DATE
WHERE END_DT < '20150115'
ORDER BY END_DT
"""
df_date = psql.frame_query(sqltext, conn)
u_cd_lst = ['FI00.PVA','FI00.PGR','FI00.LAR.PVA', 'FI00.LAR.PGR','FI00.MLA.PVA','FI00.MLA.PGR']
#u_cd_lst = ['FI00.MLA.PGR']
for u_cd in u_cd_lst:
print u_cd
df_date_lst = []
for i in xrange(len(df_date)):
start_dt = df_date.ix[i]['START_DT']
print start_dt
sql_text = """
SELECT A.TRD_DT,A.U_CD,A.GICODE,B.ITEMABBRNM, ROUND(A.CAP*100/SUM(A.CAP) OVER (PARTITION BY A.TRD_DT),2) CAP_WEIGHT
FROM JISUDEV.RES_J_CAP_HIST A, FNS_J_MAST_HIST B
WHERE A.U_CD = '%s'
AND A.UNIV_CD = '001'
AND A.TRD_DT = '%s'
AND B.TRD_DT = A.TRD_DT
AND B.GICODE = A.GICODE
GROUP BY A.GICODE, A.CAP, A.TRD_DT, B.ITEMABBRNM, A.U_CD
ORDER BY CAP_WEIGHT DESC
""" %(u_cd, start_dt)
df = psql.frame_query(sql_text, conn)
df_date_lst.append(df)
#pdb.set_trace()
df_index = pd.concat(df_date_lst)
df_index.to_csv('%s.txt'%(u_cd),index=False)
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 1 17:45:17 2014
@author: assa
"""
import time
import calendar
import pandas as pd
import pandas.io.sql as psql
import cx_Oracle as odb
ip = '10.10.1.50'
port = '1521'
SID = 'fndb2'
tns_REALDB = odb.makedsn(ip, port, SID)
conn = odb.connect('UFNGDBA','venus2002',tns_REALDB)
curs = conn.cursor()
sqltext = """
SELECT DISTINCT TRD_DT
FROM FNS_JD A
WHERE A.TRD_DT < '20010101'
order by A.TRD_DT
"""
df = psql.frame_query(sqltext, conn)
dbdate = list(df['TRD_DT'])
df['TRD_DT'] = pd.to_datetime(df['TRD_DT'])
# print Eval date
prev_month = 1
for i in xrange(1,len(df.index)):
trd_dt = df.ix[i]['TRD_DT']
if trd_dt.month != prev_month:
prev_month = trd_dt.month
if trd_dt.month == 6 or trd_dt.month == 12:
print df.ix[i-1]['TRD_DT'].strftime('%Y-%m-%d')
# print Start date
for i in range(1980,2001,1):
for j in [6,12]:
c = calendar.monthcalendar(i, j)
first_week = c[0]
second_week = c[1]
third_week = c[2]
fourth_week = c[3]
if first_week[calendar.THURSDAY]:
meeting_date = second_week[calendar.THURSDAY]
end_date = second_week[calendar.FRIDAY]
start_date = third_week[calendar.MONDAY]
else:
meeting_date = third_week[calendar.THURSDAY]
end_date = third_week[calendar.FRIDAY]
start_date = fourth_week[calendar.MONDAY]
start_date = "%d%.2d%.2d" %(i,j,int(start_date))
end_date = "%d%.2d%.2d" %(i,j,int(end_date))
print start_date, start_date in dbdate, end_date, end_date in dbdate
conn.close()
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 29 09:45:17 2014
@author: assa
"""
import time
import pandas as pd
import pandas.io.sql as psql
import cx_Oracle as odb
ip = '10.10.1.50'
port = '1521'
SID = 'fndb2'
tns_REALDB = odb.makedsn(ip, port, SID)
conn = odb.connect('UFNGDBA','venus2002',tns_REALDB)
curs = conn.cursor()
sqltext = """
SELECT A.TRD_DT, A.TRD_DT_PDAY
FROM FNC_CALENDAR A,JISUDEV.RES_DATE B
WHERE A.TRD_DT = B.START_DT
"""
df = psql.frame_query(sqltext, conn)
df = df[1:]
trd_dt_1 = '20010102'
for i in xrange(1,len(df.index)+1):
trd_dt_pday = df.ix[i]['TRD_DT_PDAY']
print trd_dt_1, trd_dt_pday
sql_text_pday = """
SELECT A.GICODE, B.ADJ_PRC, ROUND(A.CAP*100/SUM(A.CAP) OVER (PARTITION BY A.TRD_DT),2) CAP_WEIGHT
FROM JISUDEV.RES_J_CAP_HIST A, FNS_JD B
WHERE A.U_CD = 'FI00.WLT.FNK'
AND A.UNIV_CD = '001'
AND A.TRD_DT = '%s'
AND A.GICODE = B.GICODE
AND A.TRD_DT = B.TRD_DT
GROUP BY A.GICODE, A.CAP, A.TRD_DT, B.ADJ_PRC
ORDER BY A.GICODE
""" %trd_dt_pday
sql_text_day_1 = """
SELECT A.GICODE, B.ADJ_PRC, C.UCD, C.RN , ROUND(A.CAP*100/SUM(A.CAP) OVER (PARTITION BY A.TRD_DT),2) CAP_WEIGHT
FROM JISUDEV.RES_J_CAP_HIST A, FNS_JD B, bc50_simul_tmp C
WHERE A.U_CD = 'FI00.WLT.FNK'
AND A.UNIV_CD = '001'
AND A.TRD_DT = '%s'
AND A.GICODE = B.GICODE
AND A.TRD_DT = B.TRD_DT
AND A.GICODE = C.GICODE
AND A.TRD_DT = C.START_DT
GROUP BY A.GICODE, A.CAP, A.TRD_DT, B.ADJ_PRC, C.UCD, C.RN
ORDER BY A.GICODE
""" %trd_dt_1
df_pday = psql.frame_query(sql_text_pday, conn)
df_day = psql.frame_query(sql_text_day_1, conn)
df_balance = df_day.merge(df_pday,left_on='GICODE',right_on='GICODE',how='outer')
df_balance['Ret'] = (df_balance['ADJ_PRC_y'] - df_balance['ADJ_PRC_x']) / df_balance['ADJ_PRC_x']
print df_balance[['GICODE', 'UCD', 'RN']]
#print trd_dt_1, trd_dt_pday, df_balance['Ret'].mean(axis=1)
trd_dt_1 = df.ix[i]['TRD_DT']
conn.close()
# -*- coding: utf-8 -*-
"""
Created on Tue Sep 16 16:45:17 2014
@author: assa
"""
import time
import pandas as pd
import pandas.io.sql as psql
import cx_Oracle as odb
ip = '10.10.1.50'
port = '1521'
SID = 'fndb2'
tns_REALDB = odb.makedsn(ip, port, SID)
conn = odb.connect('UFNGDBA','venus2002',tns_REALDB)
curs = conn.cursor()
sqltext = """
SELECT A.TRD_DT, A.TRD_DT_PDAY
FROM FNC_CALENDAR A,JISUDEV.RES_DATE B
WHERE A.TRD_DT = B.START_DT
"""
#curs.execute(sqltext)
#print curs.description
#for row in curs:
# print row
df = psql.frame_query(sqltext, conn)
df = df[1:]
for i in xrange(1,28):
trd_dt = df.ix[i]['TRD_DT']
trd_dt_pday = df.ix[i]['TRD_DT_PDAY']
sql_text_pday = """
SELECT A.GICODE, ROUND(A.CAP*100/SUM(A.CAP) OVER (PARTITION BY A.TRD_DT),2) CAP_WEIGHT
FROM JISUDEV.RES_J_CAP_HIST A
WHERE A.U_CD = 'FI00.WLT.FNK'
AND A.UNIV_CD = '001'
AND A.TRD_DT = '%s'
GROUP BY A.GICODE, A.CAP, A.TRD_DT
ORDER BY A.GICODE
""" %trd_dt_pday
sql_text_day = """
SELECT A.GICODE, ROUND(A.CAP*100/SUM(A.CAP) OVER (PARTITION BY A.TRD_DT),2) CAP_WEIGHT
FROM JISUDEV.RES_J_CAP_HIST A
WHERE A.U_CD = 'FI00.WLT.FNK'
AND A.UNIV_CD = '001'
AND A.TRD_DT = '%s'
GROUP BY A.GICODE, A.CAP, A.TRD_DT
ORDER BY A.GICODE
""" %trd_dt
df_pday = psql.frame_query(sql_text_pday, conn)
df_day = psql.frame_query(sql_text_day, conn)
df_balance = df_pday.merge(df_day,left_on='GICODE',right_on='GICODE',how='outer')
df_balance = df_balance.fillna(0)
df_balance['DiffWeight'] = abs(df_balance['CAP_WEIGHT_x'] - df_balance['CAP_WEIGHT_y'])
#print
#print trd_dt_pday
#print df_balance
turnover = sum(df_balance['DiffWeight']) / 2
trd_dt = time.strftime('%Y-%m-%d',time.strptime(trd_dt,'%Y%m%d'))
print trd_dt, turnover
conn.close()
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 16 16:45:17 2015
@author: assa
"""
import time
import pandas as pd
import pandas.io.sql as psql
import cx_Oracle as odb
# REAL_DB_DEV
ip = '10.10.1.23'
port = '1521'
SID = 'fndb2'
tns_REALDB_DEV = odb.makedsn(ip, port, SID)
conn = odb.connect('UFNGDBA','venus2002',tns_REALDB_DEV)
curs = conn.cursor()
sqltext = """
SELECT DISTINCT A.UPDATE_DT, B.TRD_DT_PDAY
FROM FNS_U_MAP_HIST A, FNC_CALENDAR B
WHERE A.U_CD = 'I.001'
--AND A.GICODE LIKE 'A%0'
--AND A.UPDATE_DT = '20140819'
--AND A.IN_OUT_GB IN ('0', '1', '3')
AND A.UPDATE_DT > '20001222'
AND A.UPDATE_DT = B.TRD_DT
ORDER BY A.UPDATE_DT
"""
df_date = psql.frame_query(sqltext, conn)
update_dt_prev = df_date.ix[0]['UPDATE_DT']
u_cd = 'FI00.WLT.002'
for i in xrange(len(df_date)-1):
update_dt = df_date.ix[i+1]['UPDATE_DT']
trd_dt_pday = df_date.ix[i+1]['TRD_DT_PDAY']
sql_text_pday = """
SELECT A.GICODE, A.WEIGHT IIF, B.STK_QTY, C.CLS_PRC, B.FF_RT_BAND,
A.WEIGHT * B.STK_QTY * C.CLS_PRC * B.FF_RT_BAND CAP,
A.WEIGHT * B.STK_QTY * C.CLS_PRC * B.FF_RT_BAND /
SUM(A.WEIGHT * B.STK_QTY * C.CLS_PRC * B.FF_RT_BAND) OVER (PARTITION BY A.START_DT) CAP_WEIGHT
FROM RES_STYLE_UNIV A, RES_J_MAST_HIST B, FNS_JD C
WHERE A.SIMUL_ID = 'KHJ01'
AND A.U_CD = '%s'
AND A.START_DT = '%s'
AND A.GICODE = B.GICODE
AND B.TRD_DT = '%s'
AND A.GICODE = C.GICODE
AND C.TRD_DT = '%s' """ %(u_cd, update_dt_prev, trd_dt_pday, trd_dt_pday)
sql_text_day = """
SELECT A.GICODE, A.WEIGHT IIF, B.STK_QTY, C.CLS_PRC, B.FF_RT_BAND,
A.WEIGHT * B.STK_QTY * C.CLS_PRC * B.FF_RT_BAND CAP,
A.WEIGHT * B.STK_QTY * C.CLS_PRC * B.FF_RT_BAND /
SUM(A.WEIGHT * B.STK_QTY * C.CLS_PRC * B.FF_RT_BAND) OVER (PARTITION BY A.START_DT) CAP_WEIGHT
FROM RES_STYLE_UNIV A, RES_J_MAST_HIST B, FNS_JD C
WHERE A.SIMUL_ID = 'KHJ01'
AND A.U_CD = '%s'
AND A.START_DT = '%s'
AND A.GICODE = B.GICODE
AND B.TRD_DT = '%s'
AND A.GICODE = C.GICODE
AND C.TRD_DT = '%s' """ %(u_cd,update_dt, update_dt, update_dt)
df_pday = psql.frame_query(sql_text_pday, conn)
df_day = psql.frame_query(sql_text_day, conn)
df_balance = df_pday.merge(df_day,left_on='GICODE',right_on='GICODE',how='outer')
df_balance = df_balance.fillna(0)
df_balance['DiffWeight'] = abs(df_balance['CAP_WEIGHT_x'] - df_balance['CAP_WEIGHT_y'])
turnover = sum(df_balance['DiffWeight']) / 2
str_update_dt = time.strftime('%Y-%m-%d',time.strptime(update_dt,'%Y%m%d'))
print str_update_dt, turnover
update_dt_prev = update_dt
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment