Esempio n. 1
0
def test_CrackTermStructure(undList, weights, startContract, endContract, Term,
                            exceptionDateList):
    undVolList = []
    CrkVolList = []
    legendList = []

    mkt = hydra.db.read('/' + undList[0])
    ContractList = mkt.ContractRange(startContract, endContract)

    for cont in ContractList:
        startD = datetime.date(2004, 7, 1)
        minDate = datetime.date(2050, 1, 1)
        tsList = []
        for und in undList:
            contString = und + '_' + str(cont)
            mkt = hydra.db.read('/' + und)
            endD = min(mkt.ExpirationDateByContract(cont), cont)

            if endD == 'N/A':
                endD = minDate

            ts = tsfns.TimeSeries(contString, StartDate=startD, EndDate=endD)
            tsList.append(ts)

            minDate = min(minDate, ts.Dates()[-1])

        endD = expiryT = minDate
        CrkVol, undVol = BSHistVol.Crack_ATMVol_TermStr(
            tsList,
            weights,
            expiryT,
            termTenor=Term,
            exceptionDateList=exceptionDateList)

        ToM = [(expiryT - x).days / 365. * 12.0 for x in CrkVol.Dates()]
        crk_vol = [y * 100.0 for y in CrkVol.Values()]
        und_vol = [y * 100.0 for y in undVol.Values()]

        legendList.append(str(cont))
        CrkVolList.append(zip(ToM, crk_vol))
        undVolList.append(zip(ToM, und_vol))

    gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    plotxy.PlotXY(undVolList,
                  Legends=legendList,
                  xlabel='Time to expiry [months]',
                  ylabel='Underlier Cumulative Vol',
                  figure=gridfig)

    gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    plotxy.PlotXY(CrkVolList,
                  Legends=legendList,
                  xlabel='Time to expiry [months]',
                  ylabel='Crack Vol',
                  figure=gridfig)
Esempio n. 2
0
def test_CCYCommodTermStr(und, ccyName, startContract, endContract, Term = '1m', exceptionDateList= []):
    HRPlotList =[]
    HRLegendList = []
    CorrPlotList =[]
    CorrLegendList = []

    ccyPair = ccyName+'-USD'
    pairObj = hydra.db[ '/' + ccyPair ]
    op = '*'

    mkt = hydra.db.read('/' + und)
    ContractList = mkt.ContractRange(startContract, endContract)

    for cont in ContractList:
        startD = datetime.date(1950,1,1)
        contString = und +'_'+ str(cont)
        expiryT = min(mkt.ExpirationDateByContract(cont), cont)

        if expiryT !='N/A':
            endD = expiryT
        else:
            endD = datetime.date(2012,1,1)

        ts1 = tsfns.TimeSeries(contString, StartDate = startD, EndDate =endD)

        if expiryT != endD:
            endD = expiryT = ts1.Dates()[-1]

        startD =ts1.Dates()[0]

        tsexprfns.StartDate = startD
        tsexprfns.EndDate = endD
        ts2 = tsexprfns.fxforwarddate(ccyPair, cont.strftime('%d%b%y'))

        HRVol, HRcorr, VolF1, VolF2 = BSHistVol.Spread_ATMVolCorr_TermStr(ts1 =ts1, ts2 =ts2, op =op, expiryT =expiryT, r1 = 0.0, r2 = 0.0, termTenor=Term, exceptionDateList= exceptionDateList)

        ToM = [ (expiryT - x).days/365.*12.0 for x in HRVol.Dates()]
        cross_vol = [ y*100.0 for y in HRVol.Values()]
        corr = [ y*100.0 for y in HRcorr.Values()]

        HRPlotList.append(zip(ToM,cross_vol))
        HRLegendList.append(str(cont))
        CorrPlotList.append(zip(ToM,corr))
        CorrLegendList.append(str(cont))

    gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    plotxy.PlotXY(HRPlotList,Legends = HRLegendList, xlabel='Time to expiry [months]', ylabel='HR BS Cumulative vol', figure=gridfig)

    gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    plotxy.PlotXY(CorrPlotList,Legends = CorrLegendList, xlabel='Time to expiry [months]', ylabel='HR BS correlation', figure=gridfig)
Esempio n. 3
0
def test_SpreadTermStructure(undList, operator, startContract, endContract, Term, exceptionDateList):
    HRPlotList =[]
    HRLegendList = []
    CorrPlotList =[]
    CorrLegendList = []

    und1 = undList[0]
    und2 = undList[1]
    mkt1 = hydra.db.read('/' + und1)
    mkt2 = hydra.db.read('/' + und2)

    Op = operator
    ContractList = mkt2.ContractRange(startContract, endContract)

    for cont in ContractList:
        startD = datetime.date(1950,1,1)
        contString1 = und1 +'_'+ str(cont)
        contString2 = und2 +'_'+ str(cont)

        expiryT = min(mkt1.ExpirationDateByContract(cont), mkt2.ExpirationDateByContract(cont), cont)

        if expiryT !='N/A':
            endD = expiryT
        else:
            endD = datetime.date(2050,1,1)

        ts1 = tsfns.TimeSeries(contString1, StartDate = startD, EndDate =endD)
        ts2 = tsfns.TimeSeries(contString2, StartDate = startD, EndDate =endD)

        if expiryT != endD:
            endD = expiryT = min( ts1.Dates()[-1], ts2.Dates()[-1])


        HRVol, HRcorr, VolF1, VolF2 = BSHistVol.Spread_ATMVolCorr_TermStr(ts1 =ts1, ts2 =ts2, op =Op, expiryT =expiryT, r1 = 0.0, r2 = 0.0, termTenor=Term, exceptionDateList=exceptionDateList)

        ToM = [ (expiryT - x).days/365.*12.0 for x in HRVol.Dates()]
        cross_vol = [ y*100.0 for y in HRVol.Values()]
        corr = [ y*100.0 for y in HRcorr.Values()]

        HRPlotList.append(zip(ToM,cross_vol))
        HRLegendList.append(str(cont))
        CorrPlotList.append(zip(ToM,corr))
        CorrLegendList.append(str(cont))

    gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    plotxy.PlotXY(HRPlotList,Legends = HRLegendList, xlabel='Time to expiry [months]', ylabel='HR BS Cumulative vol', figure=gridfig)

    gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    plotxy.PlotXY(CorrPlotList,Legends = CorrLegendList, xlabel='Time to expiry [months]', ylabel='HR BS correlation', figure=gridfig)
Esempio n. 4
0
def test_CrackTermStructure(undList, weights, startContract, endContract, Term, exceptionDateList):
    undVolList = []
    CrkVolList = []
    legendList = []

    mkt = hydra.db.read('/' + undList[0])
    ContractList = mkt.ContractRange(startContract, endContract)

    for cont in ContractList:
        startD = datetime.date(2004,7,1)
        minDate = datetime.date(2050, 1, 1)
        tsList = []
        for und in undList:
            contString = und +'_'+ str(cont)
            mkt = hydra.db.read('/' + und)
            endD = min(mkt.ExpirationDateByContract(cont), cont)

            if endD =='N/A':
                endD = minDate

            ts = tsfns.TimeSeries(contString, StartDate = startD, EndDate =endD)
            tsList.append(ts)

            minDate = min(minDate, ts.Dates()[-1])

        endD= expiryT = minDate
        CrkVol, undVol = BSHistVol.Crack_ATMVol_TermStr(tsList, weights, expiryT, termTenor=Term, exceptionDateList=exceptionDateList)

        ToM = [ (expiryT - x).days/365.*12.0 for x in CrkVol.Dates()]
        crk_vol = [ y*100.0 for y in CrkVol.Values()]
        und_vol = [ y*100.0 for y in undVol.Values()]

        legendList.append(str(cont))
        CrkVolList.append(zip(ToM, crk_vol))
        undVolList.append(zip(ToM,und_vol))

    gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    plotxy.PlotXY(undVolList, Legends = legendList, xlabel='Time to expiry [months]', ylabel='Underlier Cumulative Vol', figure=gridfig)

    gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    plotxy.PlotXY(CrkVolList,Legends = legendList, xlabel='Time to expiry [months]', ylabel='Crack Vol', figure=gridfig)
Esempio n. 5
0
def test_SpreadTermStructure(undList, operator, startContract, endContract,
                             Term, exceptionDateList):
    HRPlotList = []
    HRLegendList = []
    CorrPlotList = []
    CorrLegendList = []

    und1 = undList[0]
    und2 = undList[1]
    mkt1 = hydra.db.read('/' + und1)
    mkt2 = hydra.db.read('/' + und2)

    Op = operator
    ContractList = mkt2.ContractRange(startContract, endContract)

    for cont in ContractList:
        startD = datetime.date(1950, 1, 1)
        contString1 = und1 + '_' + str(cont)
        contString2 = und2 + '_' + str(cont)

        expiryT = min(mkt1.ExpirationDateByContract(cont),
                      mkt2.ExpirationDateByContract(cont), cont)

        if expiryT != 'N/A':
            endD = expiryT
        else:
            endD = datetime.date(2050, 1, 1)

        ts1 = tsfns.TimeSeries(contString1, StartDate=startD, EndDate=endD)
        ts2 = tsfns.TimeSeries(contString2, StartDate=startD, EndDate=endD)

        if expiryT != endD:
            endD = expiryT = min(ts1.Dates()[-1], ts2.Dates()[-1])

        HRVol, HRcorr, VolF1, VolF2 = BSHistVol.Spread_ATMVolCorr_TermStr(
            ts1=ts1,
            ts2=ts2,
            op=Op,
            expiryT=expiryT,
            r1=0.0,
            r2=0.0,
            termTenor=Term,
            exceptionDateList=exceptionDateList)

        ToM = [(expiryT - x).days / 365. * 12.0 for x in HRVol.Dates()]
        cross_vol = [y * 100.0 for y in HRVol.Values()]
        corr = [y * 100.0 for y in HRcorr.Values()]

        HRPlotList.append(zip(ToM, cross_vol))
        HRLegendList.append(str(cont))
        CorrPlotList.append(zip(ToM, corr))
        CorrLegendList.append(str(cont))

    gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    plotxy.PlotXY(HRPlotList,
                  Legends=HRLegendList,
                  xlabel='Time to expiry [months]',
                  ylabel='HR BS Cumulative vol',
                  figure=gridfig)

    gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    plotxy.PlotXY(CorrPlotList,
                  Legends=CorrLegendList,
                  xlabel='Time to expiry [months]',
                  ylabel='HR BS correlation',
                  figure=gridfig)
Esempio n. 6
0
def test_CorrTestComp():
    IsCall = 1
    exceptionDateList = []
    op = '/'

    startD = datetime.date(2005, 1, 1)
    endD = datetime.date(2008, 12, 31)

    trueCorr = curve.Curve()
    avgCorr = curve.Curve()
    varCorr = curve.Curve()

    trueVol = curve.Curve()
    avgVol = curve.Curve()
    varVol = curve.Curve()

    expiryT = endD
    F = [1.0, 1.0]
    Sig = [0.8, 0.5]
    Kappa = [1.2, 0.8]
    rho = 0.9
    numPath = 200

    HRPlotList = []
    LegendList = []
    CorrPlotList = []

    for i in range(numPath):
        ts1, ts2 = rand2DTSGen(F,
                               Sig,
                               Kappa,
                               rho,
                               StartDate=startD,
                               EndDate=endD)
        HRVol, HRcorr, VolF1, VolF2 = BSHistVol.Spread_ATMVolCorr_TermStr(
            ts1=ts1,
            ts2=ts2,
            op=op,
            expiryT=expiryT,
            r1=0.0,
            r2=0.0,
            termTenor="1m",
            exceptionDateList=exceptionDateList)

        ToM = [(expiryT - x).days / 365. * 12.0 for x in HRVol.Dates()]
        cross_vol = [y * 100.0 for y in HRVol.Values()]
        corr = [y * 100.0 for y in HRcorr.Values()]

        HRPlotList.append(zip(ToM, cross_vol))
        LegendList.append(str(i))
        CorrPlotList.append(zip(ToM, corr))

        if i == 0:
            for d in HRVol.Dates():
                tau = (expiryT - d).days / 365.0
                s1 = Sig[0]**2 * (1 - math.exp(-2 * Kappa[0] * tau)) / (
                    2 * Kappa[0] * tau)
                s2 = Sig[1]**2 * (1 - math.exp(-2 * Kappa[1] * tau)) / (
                    2 * Kappa[1] * tau)
                s12 = Sig[0] * Sig[1] * rho * (
                    1 - math.exp(-(Kappa[0] + Kappa[1]) * tau)) / (
                        (Kappa[0] + Kappa[1]) * tau)
                if op == '/':
                    s = math.sqrt(s1 + s2 - 2 * s12)
                else:
                    s = math.sqrt(s1 + s2 + 2 * s12)

                r = s12 / math.sqrt(s1 * s2)

                trueVol[d] = s
                trueCorr[d] = r
                avgVol[d] = 0.0
                varVol[d] = 0.0
                avgCorr[d] = 0.0
                varCorr[d] = 0.0

        for d in HRVol.Dates():
            avgVol[d] += HRVol[d] / (numPath * 1.0)
            varVol[d] += (HRVol[d] - trueVol[d])**2 / (numPath * 1.0)
            avgCorr[d] += HRcorr[d] / (numPath * 1.0)
            varCorr[d] += (HRcorr[d] - trueCorr[d])**2 / (numPath * 1.0)

    #gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    #plotxy.PlotXY(HRPlotList,Legends = LegendList, figure=gridfig)
    #gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    #plotxy.PlotXY(CorrPlotList,Legends = LegendList, figure=gridfig)

    plotList = [avgVol, trueVol]
    legendList = ['Vol', 'True Vol']
    gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    plotxy.PlotCurves(plotList, Legends=legendList, figure=gridfig)

    plotList = [avgCorr, trueCorr]
    legendList = ['Corr', 'True Corr']
    gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    plotxy.PlotCurves(plotList, Legends=legendList, figure=gridfig)

    diff = curve.Curve()
    rcorr = curve.Curve()
    Texp = curve.Curve()
    for d in trueVol.Dates():
        diff[d] = avgCorr[d] - trueCorr[d]
        varCorr[d] = math.sqrt(varCorr[d])
        rcorr[d] = varCorr[d] / trueCorr[d]
        Texp[d] = (expiryT - d).days / 365.0 * 12.0

    DiffRatio = [(Texp[d], diff[d], varCorr[d], rcorr[d])
                 for d in trueCorr.Dates()]
    print DiffRatio
Esempio n. 7
0
def test_CCYCommodTermStr(und,
                          ccyName,
                          startContract,
                          endContract,
                          Term='1m',
                          exceptionDateList=[]):
    HRPlotList = []
    HRLegendList = []
    CorrPlotList = []
    CorrLegendList = []

    ccyPair = ccyName + '-USD'
    pairObj = hydra.db['/' + ccyPair]
    op = '*'

    mkt = hydra.db.read('/' + und)
    ContractList = mkt.ContractRange(startContract, endContract)

    for cont in ContractList:
        startD = datetime.date(1950, 1, 1)
        contString = und + '_' + str(cont)
        expiryT = min(mkt.ExpirationDateByContract(cont), cont)

        if expiryT != 'N/A':
            endD = expiryT
        else:
            endD = datetime.date(2012, 1, 1)

        ts1 = tsfns.TimeSeries(contString, StartDate=startD, EndDate=endD)

        if expiryT != endD:
            endD = expiryT = ts1.Dates()[-1]

        startD = ts1.Dates()[0]

        tsexprfns.StartDate = startD
        tsexprfns.EndDate = endD
        ts2 = tsexprfns.fxforwarddate(ccyPair, cont.strftime('%d%b%y'))

        HRVol, HRcorr, VolF1, VolF2 = BSHistVol.Spread_ATMVolCorr_TermStr(
            ts1=ts1,
            ts2=ts2,
            op=op,
            expiryT=expiryT,
            r1=0.0,
            r2=0.0,
            termTenor=Term,
            exceptionDateList=exceptionDateList)

        ToM = [(expiryT - x).days / 365. * 12.0 for x in HRVol.Dates()]
        cross_vol = [y * 100.0 for y in HRVol.Values()]
        corr = [y * 100.0 for y in HRcorr.Values()]

        HRPlotList.append(zip(ToM, cross_vol))
        HRLegendList.append(str(cont))
        CorrPlotList.append(zip(ToM, corr))
        CorrLegendList.append(str(cont))

    gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    plotxy.PlotXY(HRPlotList,
                  Legends=HRLegendList,
                  xlabel='Time to expiry [months]',
                  ylabel='HR BS Cumulative vol',
                  figure=gridfig)

    gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    plotxy.PlotXY(CorrPlotList,
                  Legends=CorrLegendList,
                  xlabel='Time to expiry [months]',
                  ylabel='HR BS correlation',
                  figure=gridfig)
Esempio n. 8
0
def test_CorrTestComp():
    IsCall = 1
    exceptionDateList = []
    op= '/'

    startD = datetime.date(2005,1,1)
    endD = datetime.date(2008,12,31)

    trueCorr = curve.Curve()
    avgCorr = curve.Curve()
    varCorr = curve.Curve()

    trueVol = curve.Curve()
    avgVol = curve.Curve()
    varVol = curve.Curve()

    expiryT = endD
    F = [1.0, 1.0]
    Sig = [0.8, 0.5]
    Kappa = [1.2, 0.8]
    rho = 0.9
    numPath = 200

    HRPlotList = []
    LegendList = []
    CorrPlotList = []

    for i in range(numPath):
        ts1, ts2 = rand2DTSGen(F, Sig, Kappa, rho, StartDate = startD, EndDate = endD)
        HRVol, HRcorr, VolF1, VolF2 = BSHistVol.Spread_ATMVolCorr_TermStr(ts1 =ts1, ts2 =ts2, op =op, expiryT =expiryT, r1 = 0.0, r2 = 0.0, termTenor="1m", exceptionDateList= exceptionDateList)

        ToM = [ (expiryT - x).days/365.*12.0 for x in HRVol.Dates()]
        cross_vol = [ y*100.0 for y in HRVol.Values()]
        corr = [ y*100.0 for y in HRcorr.Values()]

        HRPlotList.append(zip(ToM,cross_vol))
        LegendList.append(str(i))
        CorrPlotList.append(zip(ToM,corr))

        if i == 0:
            for d in HRVol.Dates():
                tau = (expiryT-d).days/365.0
                s1 = Sig[0]**2 * (1 - math.exp(-2 * Kappa[0] * tau))/(2 * Kappa[0] * tau)
                s2 = Sig[1]**2 * (1 - math.exp(-2 * Kappa[1] * tau))/(2 * Kappa[1] * tau)
                s12 = Sig[0] * Sig[1]* rho * (1 - math.exp(-(Kappa[0] + Kappa[1]) * tau))/((Kappa[0] + Kappa[1]) * tau)
                if op == '/':
                    s = math.sqrt(s1 + s2 - 2 * s12)
                else:
                    s = math.sqrt(s1 + s2 + 2 * s12)

                r = s12 / math.sqrt(s1 * s2)

                trueVol[d] = s
                trueCorr[d] = r
                avgVol[d] = 0.0
                varVol[d] = 0.0
                avgCorr[d] = 0.0
                varCorr[d] = 0.0

        for d in HRVol.Dates():
            avgVol[d] += HRVol[d]/(numPath*1.0)
            varVol[d] += (HRVol[d] - trueVol[d])**2/(numPath*1.0)
            avgCorr[d] += HRcorr[d]/(numPath*1.0)
            varCorr[d] += (HRcorr[d] - trueCorr[d])**2/(numPath*1.0)


    #gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    #plotxy.PlotXY(HRPlotList,Legends = LegendList, figure=gridfig)
    #gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    #plotxy.PlotXY(CorrPlotList,Legends = LegendList, figure=gridfig)

    plotList = [avgVol, trueVol]
    legendList = ['Vol', 'True Vol']
    gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    plotxy.PlotCurves(plotList,Legends = legendList, figure=gridfig)

    plotList = [avgCorr, trueCorr]
    legendList = ['Corr', 'True Corr']
    gridfig, ax, axF = plotxy.createFigure(pylab.figure(), GridEnabled=True)
    plotxy.PlotCurves(plotList,Legends = legendList, figure=gridfig)

    diff = curve.Curve()
    rcorr = curve.Curve()
    Texp = curve.Curve()
    for d in trueVol.Dates():
        diff[d] = avgCorr[d] - trueCorr[d]
        varCorr[d] =math.sqrt(varCorr[d])
        rcorr[d] = varCorr[d]/trueCorr[d]
        Texp[d] = (expiryT-d).days/365.0*12.0

    DiffRatio = [ (Texp[d], diff[d], varCorr[d], rcorr[d]) for d in trueCorr.Dates()]
    print DiffRatio