aboutsummaryrefslogtreecommitdiffstats
path: root/python/intex_scenarios.py
blob: bcac1966b171fe0e4bd9b3c3d709d145329e7636 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
import os
import os.path
import datetime
from datetime import date
from csv import reader
import json
import codecs
import re
import psycopg2

if os.name =='nt':
    root = "//WDsentinel/share/CorpCDOs/Scenarios"
elif os.name == 'posix':
    root = '/home/share/CorpCDOs/Scenarios'
input = os.path.join(root, "prometheus.sss")

pattern1  = re.compile("(REINVEST\[)\w+(::.*$)")
# maturity
pattern2 = re.compile("(STANDARD_VAR\[)\w+(::#REINV_END,\d+\]=)(\d*$)")
# reinvprices float
pattern3 = re.compile("(STANDARD_VAR\[)\w+(::#PRICE100_TBA1,\d+\]=)(.*$)")
# reinvprices fixed
pattern4 = re.compile("(STANDARD_VAR\[)\w+(::#PRICE100_TBA2,\d+\]=)(.*$)")
# reinv float percentage
pattern5 = re.compile("(STANDARD_VAR\[)\w+(::#REINVPCT_TBA1,\d+\]=)(.*$)")
# reinv fixed percentage
pattern6 = re.compile("(STANDARD_VAR\[)\w+(::#REINVPCT_TBA2,\d+\]=)(.*$)")
pattern7 = re.compile("GOLDL5")

reinvfloatpercentage = 85
reinvfixedpercentage = 15

conn = psycopg2.connect(database="ET",
                        user="et_user",
                        password="Serenitas1",
                        host="192.168.1.108")
cursor = conn.cursor()

for dealname in ["abcl071", "ammcclo5", "atr5cdo", "blumt3", "callid6"]:
    cursor.execute('SELECT \"Reinv End Date\" from latest_clo_universe where dealname=%s', (dealname,))
    reinvenddate = cursor.fetchone()[0]
    if reinvenddate:
       reinvenddate = reinvenddate.strftime("%Y%m%d")
    else:
        print "missing reinvstment end date"
        pdb.set_trace()

    with open(os.path.join(root, "Intex curves", "csv", dealname + "-reinvprices.csv"), "r") as fhreinv:
        floatreinvprices = fhreinv.readline().rstrip("\n").split(",")
        fixedreinvprices = fhreinv.readline().rstrip("\n").split(",")

    output = os.path.join(root, "Intex curves", "sss", dealname + ".sss")
    cdrscenarios = os.path.join(root, "Intex curves", "csv", dealname + "-cdr.csv")
    recoveryscenarios = os.path.join(root, "Intex curves", "csv", dealname + "-recovery.csv")
    fh2 = open(output, "w")
    fhcdr = open(cdrscenarios, "r")
    fhrecovery = open(recoveryscenarios, "r")
    csvcdr = reader(fhcdr)
    csvrecovery = reader(fhrecovery)
    cdrline = csvcdr.next()
    cdrline = "\t".join(["{0:.3f}".format(float(cdr)) for cdr in cdrline]) +"\n"
    recoveryline = csvrecovery.next()
    recoveryline = "\t".join(["{0:.3f}".format(float(recovery)) for recovery in recoveryline]) + "\n"

    i=1
    with open(input) as fh:
        for line in fh:
            if "DEAL_NAME" in line:
                newline = "DEAL_NAME=" +  dealname.upper()
                fh2.write(newline)
            if pattern1.match(line):
                line = re.sub(pattern1, r"\1{0}\2", line).format(dealname.upper())
                fh2.write(line)
                continue
            if pattern2.match(line):
                line = re.sub(pattern2, r"\1{0}\2{1}", line).format(dealname.upper(), reinvenddate)
                fh2.write(line)
                continue
            if pattern3.match(line):
                line = re.sub(pattern3, r"\1{0}\2{1}", line).format(dealname.upper(), " ".join(floatreinvprices))
                fh2.write(line)
                continue
            if pattern4.match(line):
                line = re.sub(pattern4, r"\1{0}\2{1}", line).format(dealname.upper(), " ".join(fixedreinvprices))
                fh2.write(line)
                continue
            if pattern5.match(line):
                line = re.sub(pattern5, r"\1{0}\2{1}", line).format(dealname.upper(), reinvfloatpercentage)
                fh2.write(line)
                continue
            if pattern6.match(line):
                line  = re.sub(pattern6, r"\1{0}\2{1}", line).format(dealname.upper(), reinvfixedpercentage)
                fh2.write(line)
                continue
            if pattern7.search(line):
                line = re.sub(pattern7, dealname.upper(), line)
                fh2.write(line)
                continue
            # if "STANDARD_VAR" in line:
            #     newline = "STANDARD_VAR[REINVEST_PRICE,1]=" + " ".join(reinvprices)
            #     fh2.write(newline)
            #     continue
            if "LOSS_RATE[DEAL,{0}]".format(i) in line:
                newcdrline = "LOSS_RATE[DEAL,{0}]=".format(i) + cdrline
                fh2.write(newcdrline)
                continue
            if "LOSS_SEVERITY[DEAL,{0}]".format(i) in line:
                newrecoveryline = "LOSS_SEVERITY[DEAL,{0}]=".format(i) + recoveryline
                fh2.write(newrecoveryline)
                i=i+1
                if i<=100:
                    cdrline = csvcdr.next()
                    cdrline = "\t".join(["{0:.3f}".format(float(cdr)) for cdr in cdrline]) + "\n"
                    recoveryline = csvrecovery.next()
                    recoveryline = "\t".join(["{0:.3f}".format(float(recovery)) \
                                                  for recovery in recoveryline]) + "\n"
                continue
            fh2.write(line)
    fh2.close()
    fhrecovery.close()
    fhcdr.close()
cursor.close()
conn.close()

# sed -i -e "s/\(LOSS_NONPERF_SEVERITY\\[DEAL,[0-9]*\\]\)=.*$/\1=mkt(70)/g" stonln1_100.sss