Ejemplo 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)
Ejemplo n.º 2
0
def test_SingleTermStructure(und,
                             startContract,
                             endContract,
                             Term='1m',
                             exceptionDateList=[]):
    plotList = []
    legendList = []
    mkt = hydra.db.read('/' + und)

    ContractList = mkt.ContractRange(startContract, endContract)

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

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

        ts = tsfns.TimeSeries(contString,
                              StartDate=startD,
                              EndDate=endD,
                              folder=TS_folder)
        if expiryT != endD:
            endD = expiryT = ts.Dates()[-1]

        IsCall = 1
        volTS = BSHistVol.BS_ATMVol_TermStr(
            IsCall,
            ts,
            expiryT,
            rd=0.0,
            rf=0.0,
            endVol=0.0,
            termTenor=Term,
            rehedgingTenor="1d",
            exceptionDateList=exceptionDateList)

        ToM = [(expiryT - x).days / 365. * 12.0 for x in volTS.Dates()]
        vol = [y * 100.0 for y in volTS.Values()]
        plotList.append(zip(ToM, vol))
        legendList.append(str(cont))

    plotxy.PlotXY(plotList,
                  Legends=legendList,
                  xlabel='Time to expiry [months]',
                  ylabel='BS Cumulative vol')
Ejemplo n.º 3
0
def test_ConstDelta_VolSurface(und, Contract, Term='1m', exceptionDateList=[]):
    plotList = []
    legendList = []
    mkt = hydra.db.read('/' + und)

    startD = datetime.date(2001, 1, 1)
    contString = und + '_' + str(Contract)
    expiryT = mkt.ExpirationDateByContract(Contract)
    if expiryT != 'N/A':
        endD = expiryT
    else:
        endD = datetime.date(2012, 1, 1)

    ts = tsfns.TimeSeries(contString, StartDate=startD, EndDate=endD)
    if expiryT != endD:
        endD = expiryT = ts.Dates()[-1]

    moneynessList = [0.95, 1.0, 1.05]
    volTS = BSHistVol.BS_ConstDelta_VolSurf(ts,
                                            moneynessList,
                                            expiryT,
                                            rd=0.0,
                                            rf=0.0,
                                            exceptionDateList=[])

    ToM = []
    crvList = [[]] * len(moneynessList)
    for d in volTS.Dates():
        ToM += [(expiryT - d).days / 365. * 12.0]
        for n in range(len(moneynessList)):
            crvList[n].append(volTS[d][n] * 100.0)

    for n in range(len(moneynessList)):
        plotList.append(zip(ToM, crvList[n]))
        legendList.append(str(moneynessList[n]))

    plotxy.PlotXY(plotList,
                  Legends=legendList,
                  xlabel='Time to expiry [months]',
                  ylabel='BS Cumulative vol')
Ejemplo n.º 4
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)
Ejemplo n.º 5
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)