def testBuildMMCurve(self):
        curve = InterestRateCurve(ccy='USD', index=Index('LIBOR'), term='M', 
                                  numTerms=3, pricingDate=self.pricingDate, marketId='TEST1')
        curve.load()
#        curve.printCurve()
        cv = curve.buildZeroCurve()
        for node in cv.nodes():
            a = node
 def testCreateCurveAndRates(self):
     curve = InterestRateCurve(ccy='USD', index=Index('LIBOR'), term='M', 
                               numTerms=3, pricingDate=self.pricingDate)
     rate = InterestRate(term='M', numTerms=1, type='Deposit', mid=0.01,
                         curve=curve)
     curve.addRate(rate)
     cv = curve.buildZeroCurve()
     for node in cv.nodes():
         a = node
def loadIRCurves():
    timePeriods = VARUtilities.VARTimePeriodsAndSteps()
    timePeriods.generate(LoadStartDate, LoadEndDate, 1, 
                       Enum.TimePeriod('D').ql(), Calendar.Target())
    for time in timePeriods.timeSteps:
        print time
        curve = InterestRateCurve()
        curve.loadDefault(time)
        curve.save()
 def testBuild1RateCurve(self):
     curve = InterestRateCurve(ccy='USD', index=Index('LIBOR'), term='M', 
                               numTerms=3, pricingDate=self.pricingDate, marketId='TEST1')
     rate = InterestRate(term='M', numTerms=1, type='Deposit', mid=0.01,
                         curve=curve)
     #TODO: Fix. this addRate adds the rate to the DB
     curve.addRate(rate)
     zeroCurve = curve.buildZeroCurve()
     for node in zeroCurve.nodes():
         a = node
 def testSave2Curves(self):
     date = Date(month=9,day=13,year=2011)
     curve = InterestRateCurve(ccy='USD', index=Index('LIBOR'), term='M', 
                               numTerms=3, pricingDate=date, marketId='TMP')
     rate = InterestRate(term='M', numTerms=1, type='Deposit', mid=0.01, curve=curve)
     curve.addRate(rate)
     curve.save()
     date1 = Date(month=9,day=14,year=2011)
     curve1 = InterestRateCurve(ccy='USD', index=Index('LIBOR'), term='M', 
                                numTerms=3, pricingDate=date1, marketId='TMP')
     rate1 = InterestRate(term='M', numTerms=1, type='Deposit', mid=0.01)
     curve1.addRate(rate1)
     curve1.save()
Example #6
0
 def copyRates(self, fromDate, toDate, fromMarketId, toMarketId):
     curves = InterestRateCurve.objects.filter(pricingDate = fromDate, marketId = fromMarketId)
     for curve in curves:
         curve.loadRates()
         
         newCurve = InterestRateCurve(ccy=curve.ccy, index=curve.index, 
                                      term=curve.term, numTerms=curve.numTerms, 
                                      pricingDate=toDate, marketId=toMarketId)
         
         for rate in curve.getRates():
             newRate = InterestRate(type=rate.type, term=rate.term, numTerms=rate.numTerms, mid=rate.mid, curve=newCurve)
             newCurve.addRate(newRate)
         newCurve.save()
Example #7
0
 def marketData(self, pricingDate, marketId=''):
     irCurve = InterestRateCurve()
     irCurve.ccy = self._tcSwap.ccy
     irCurve.index = Index('LIBOR')
     irCurve.term = 'M'
     irCurve.numTerms = 3
     irCurve.pricingDate = pricingDate
     irCurve.marketId = marketId
     irCurve.load()
     l = []
     l.append(irCurve)
     return l
Example #8
0
    def setupQL(self, pricingDate, marketId=''):
        QuantLib.Settings.instance().evaluationDate = pricingDate.ql()
        # Get market data curve based on Ccy/Index
        self.pricingDate = pricingDate
        curve = InterestRateCurve()
        curve.pricingDate = pricingDate
        curve.ccy = self.tCBond.ccy
        #TODO Fix term to make it TimePeriod
        curve.index = Enum.Index('LIBOR')
        curve.term = 'M'
        curve.numTerms = 3
        curve.marketId = marketId
#        print 'marketDataContainer in setupQL'
 #       print self.marketDataContainer
        if self.marketDataContainer == None:
            raise ErrorHandling.MarketDataMissing('marketDataContainer is None')
        newCurve = self.marketDataContainer.find(curve)
        #print 'newCurve in setupQL'
        #print newCurve
        if newCurve == None:
            raise ErrorHandling.MarketDataMissing('Cannot find market data %s' % curve)
        #load OAS and adjust discount curve
        bondOAS = self.marketDataContainer.find(BondOAS(marketId=marketId,pricingDate=pricingDate,
                                                        tCBond=self.tCBond))
        newCurve.shift(shiftAmount=bondOAS.mid)
        # Map curve to QuantLib deposit rates
        depositCurve = newCurve.buildZeroCurve()
        #print 'deposit curve in setupQL'
        #print depositCurve.nodes()
        #Pricing Engine
        discountTermStructure = QuantLib.RelinkableYieldTermStructureHandle()
        discountTermStructure.linkTo(depositCurve)

        fixedSchedule = QuantLib.Schedule(pricingDate.ql(), 
                                          Date.createQLDateFromPythonDate(self.tCBond.endDate),
                                          QuantLib.Period(1,QuantLib.Years),
                                          self.tCBond.paymentCalendar.ql(),
                                          self.tCBond.paymentRollRule.ql(), 
                                          self.tCBond.paymentRollRule.ql(),
                                          QuantLib.DateGeneration.Forward, 
                                          False)
                
        coupons = []
        coupons.append(float(self.tCBond.coupon))
        #print self.tCBond.coupon
        #print self.tCBond.coupon.__class__
#        coupons.append(0.04)
        self.qlBond = QuantLib.FixedRateBond(2, 
                                      100, 
                                      fixedSchedule, 
                                      coupons, 
                                      self.tCBond.basis.ql(), 
                                      self.tCBond.paymentRollRule.ql(), 
                                      100, 
                                      pricingDate.ql())
        
        bondEngine = \
            QuantLib.DiscountingBondEngine(discountTermStructure)
        self.qlBond.setPricingEngine(bondEngine)
        self.upToDate = True
Example #9
0
 def marketData(self, pricingDate, marketId=''):
     irCurve = InterestRateCurve()
     irCurve.ccy = self.tCBond.ccy
     irCurve.index = Enum.Index('LIBOR')
     irCurve.term = 'M'
     irCurve.numTerms = 3
     irCurve.pricingDate = pricingDate
     irCurve.marketId = marketId
     irCurve.load()
     l = []
     l.append(irCurve)
     ##print pricingDate
     bondOAS = BondOAS.objects.get(tCBond=self.tCBond, pricingDate=pricingDate,
                                   marketId=marketId)
     l.append(bondOAS)
     return l
 def testSaveCurve(self):
     curve = InterestRateCurve(ccy='USD', index=Index('LIBOR'), term='M', 
                               numTerms=3, pricingDate=self.pricingDate, marketId='TMP')
     rate = InterestRate(term='M', numTerms=1, type='Deposit', mid=0.01,
                         curve=curve)
     rate1 = InterestRate(term='M', numTerms=3, type='Deposit', mid=0.01,
                         curve=curve)
     curve.addRate(rate)
     curve.addRate(rate1)
     curve.save()
 def testSingleIRCurveScenario(self):
     irA = InterestRateCurve()
     irA.ccy = "USD"
     irA.index = "LIBOR"
     irA.term = 'M'
     irA.numTerms = 3
     irA.marketId = 'TEST1'
     irA.pricingDate = Date.Date(month=9, day=12, year=2011)
     irA.load()
     scenarioBase = MarketDataScenario.MarketDataScenario()
     scenarioBase.name = 'Base'
     scenarioBase.marketData = irA
     #print scenarioBase
     scenarioUp = MarketDataScenario.MarketDataScenario()
     scenarioUp.name = 'Up'
     irB = copy.copy(irA)
     for item in irB.rates:
         item.mid = item.mid * 1.01
     scenarioUp.marketData = irB
 def testUpdateRate(self):
     '''
     Tests the update of a rate value and save it to DB
     '''
     curve = InterestRateCurve(ccy='USD', index=Index('LIBOR'), term='M', 
                               numTerms=3, pricingDate=self.pricingDate, marketId='TMP')
     curve.addRate(InterestRate(term='M', numTerms=1, type='Deposit', 
                                mid=0.01, curve=curve))
     curve.addRate(InterestRate(term='M', numTerms=3, type='Deposit', 
                                mid=0.01, curve=curve))        
     curve.save()
     curve = InterestRateCurve(ccy='USD', index=Index('LIBOR'), term='M', 
                               numTerms=3, pricingDate=self.pricingDate, marketId='TMP')
     curve.load()
     self.failIf(curve.rates[1].mid <> 0.01, "Rate incorrect")
     self.failIf(len(curve.rates) <> 2, "Number of rates incorrect")
     curve.rates[1].mid = 0.02
     curve.save()
     curveAfterUpdate = InterestRateCurve(ccy='USD', index=Index('LIBOR'), term='M', 
                                          numTerms=3, pricingDate=self.pricingDate, marketId='TMP')
     curveAfterUpdate.load()
     self.failIf(curveAfterUpdate.rates[1].mid <> 0.02, "Updated rate incorrect")
     self.failIf(len(curveAfterUpdate.rates) <> 2, "Number of updated rates incorrect")
     curveAfterUpdate.rates[1].mid = 0.01
     curveAfterUpdate.save()
     curve = InterestRateCurve(ccy='USD', index=Index('LIBOR'), term='M', 
                               numTerms=3, pricingDate=self.pricingDate, marketId='TMP')
     curve.load()
     self.failIf(curve.rates[1].mid <> 0.01, "Rate incorrect")
     self.failIf(len(curve.rates) <> 2, "Number of rates incorrect")
 def testLoadAndSaveMarketData_TODO(self):
     #curve should never exist
     self.pos.loadAndSaveMarketData(pricingDate=Date.Date(1,1,2009), 
                                    marketId=self.marketId)
     #now, curve should exist
     irCurve = InterestRateCurve()
     irCurve.ccy = Enum.Currency('USD')
     irCurve.index = Enum.Index('LIBOR')
     irCurve.term = 'M'
     irCurve.numTerms = 3
     irCurve.pricingDate = Date.Date(1,1,2009)
     irCurve.marketId = self.marketId
     irCurve.load()
     #now, delete again so that next test will work
     irCurve.delete()
Example #14
0
    def createAndSaveCurve(self, pricingDate, marketId, r1m, r3m, r6m, r1y, r2y, r3y, r4y, r5y, r7y, r10y, r30y):
        ''' 
        creates specific Libor curve and saves it with
        ''' 
        curve = InterestRateCurve(ccy='USD', index=Enum.Index('LIBOR'), 
                                  term=Enum.TimePeriod('M'), numTerms=3,
                                  pricingDate=pricingDate, marketId=marketId)
        m1 = InterestRate(type='Deposit', term=Enum.TimePeriod('M'), numTerms=1, mid=float(r1m)/100.0, curve=curve)
        m3 = InterestRate(type='Deposit', term=Enum.TimePeriod('M'), numTerms=3, mid=float(r3m)/100.0, curve=curve)
        m6 = InterestRate(type='Deposit', term=Enum.TimePeriod('M'), numTerms=6, mid=float(r6m)/100.0, curve=curve)
        y1 = InterestRate(type='Swap', term=Enum.TimePeriod('Y'), numTerms=1, mid=float(r1y)/100.0, curve=curve)
        y2 = InterestRate(type='Swap', term=Enum.TimePeriod('Y'), numTerms=2, mid=float(r2y)/100.0, curve=curve)
        y3 = InterestRate(type='Swap', term=Enum.TimePeriod('Y'), numTerms=3, mid=float(r3y)/100.0, curve=curve)
        y4 = InterestRate(type='Swap', term=Enum.TimePeriod('Y'), numTerms=4, mid=float(r4y)/100.0, curve=curve)
        y5 = InterestRate(type='Swap', term=Enum.TimePeriod('Y'), numTerms=5, mid=float(r5y)/100.0, curve=curve)
        y7 = InterestRate(type='Swap', term=Enum.TimePeriod('Y'), numTerms=7, mid=float(r7y)/100.0, curve=curve)
        y10 = InterestRate(type='Swap', term=Enum.TimePeriod('Y'), numTerms=10, mid=float(r10y)/100.0, curve=curve)
        y30 = InterestRate(type='Swap', term=Enum.TimePeriod('Y'), numTerms=30, mid=float(r30y)/100.0, curve=curve)
        curve.addRate(m1)
        curve.addRate(m3)
        curve.addRate(m6)
        curve.addRate(y1)
        curve.addRate(y2)
        curve.addRate(y3)
        curve.addRate(y4)
        curve.addRate(y5)
        curve.addRate(y7)
        curve.addRate(y10)
        curve.addRate(y30)
#        curve.printCurve()
        curve.save()
    def testBuildMMAndSwapFullCurve(self):
        curve = InterestRateCurve(ccy='USD', index=Index('LIBOR'), term='M', 
                                  numTerms=3, pricingDate=self.pricingDate)
        curve.addRate(InterestRate(term='M', numTerms=1, type='Deposit', 
                                   mid=0.003, curve=curve))
        curve.addRate(InterestRate(term='M', numTerms=3, type='Deposit', 
                                   mid=0.0039, curve=curve))
        curve.addRate(InterestRate(term='M', numTerms=6, type='Deposit', 
                                   mid=0.0056, curve=curve))
        curve.addRate(InterestRate(term='Y', numTerms=1, type='Swap', 
                                   mid=0.0052, curve=curve))
        curve.addRate(InterestRate(term='Y', numTerms=2, type='Swap', 
                                   mid=0.0054, curve=curve))
        curve.addRate(InterestRate(term='Y', numTerms=3, type='Swap', 
                                   mid=0.0066, curve=curve))
        curve.addRate(InterestRate(term='Y', numTerms=4, type='Swap', 
                                   mid=0.0089, curve=curve))
        curve.addRate(InterestRate(term='Y', numTerms=5, type='Swap', 
                                   mid=0.0116, curve=curve))
        curve.addRate(InterestRate(term='Y', numTerms=7, type='Swap', 
                                   mid=0.0164, curve=curve))
        curve.addRate(InterestRate(term='Y', numTerms=10, type='Swap', 
                                   mid=0.0214, curve=curve))
        curve.addRate(InterestRate(term='Y', numTerms=30, type='Swap', 
                                   mid=0.0295, curve=curve))

        cv = curve.buildZeroCurve()
        for node in cv.nodes():
            a = node
 def testLoadCurve(self):
     curve = InterestRateCurve(ccy='USD', index=Index('LIBOR'), term='M', 
                               numTerms=3, pricingDate=self.pricingDate, marketId='TEST1')
     curve.load()
Example #17
0
 def loadAndSaveMarketData(self, pricingDate, marketId):
     ''' assumes that 'EOD' marketId exists '''
     eodCurve = InterestRateCurve()
     eodCurve.ccy = self.tCBond.ccy
     eodCurve.index = Enum.Index('LIBOR')
     eodCurve.term = 'M'
     eodCurve.numTerms = 3
     eodCurve.pricingDate = pricingDate
     eodCurve.marketId = 'EOD'
     eodCurve.load()
     irCurve = InterestRateCurve()
     irCurve.ccy = self.tCBond.ccy
     irCurve.index = Enum.Index('LIBOR')
     irCurve.term = 'M'
     irCurve.numTerms = 3
     irCurve.pricingDate = pricingDate
     irCurve.marketId = marketId
     eodRates = eodCurve.getRates()
     for rate in eodRates:
         irCurve.addRate(rate)
     irCurve.save()
     eodBondOAS = BondOAS.objects.get(tCBond=self.tCBond, pricingDate=pricingDate,
                                   marketId='EOD')
     bondOAS = BondOAS(tCBond=self.tCBond, pricingDate=pricingDate,
                       marketId=marketId,mid=eodBondOAS.mid)
     bondOAS.save()
Example #18
0
    def dataForSuccessfulTest(self):
        '''
        This saves all data so that system tests run successfully
        Pricing date is 9/12/2011 with market data id TEST1
        '''
        testDatePython = date(month=9,day=12,year=2011)
        testDate = Date(month=9,day=12,year=2011)
        testFirstDate = Date(month=8,day=30,year=2011)
        
        if not Location.objects.filter(name='Test1').exists():
            location = Location()
            location.name = 'Test1'
            location.pricingDate = date(month=9,day=12,year=2011)
            location.save()
        location = Location.objects.get(name='Test1')
            
        if not User.objects.filter(username='******').exists():
            user = User.objects.create_user(username='******',email='*****@*****.**',\
                                            password='******')
            user.is_staff = True
            user.is_superuser = True
            user.save()

        if not User.objects.filter(username='******').exists():
            User.objects.create_user(username='******',email='*****@*****.**',\
                                     password='******')

        if not User.objects.filter(username='******').exists():
            User.objects.create_user(username='******',email='*****@*****.**',\
                                     password='******')

        if not User.objects.filter(username='******').exists():
            User.objects.create_user(username='******',email='*****@*****.**',\
                                     password='******')

        user1 = User.objects.get(username='******')
        if not UserProfile.objects.filter(user=user1).exists():
            up1 = UserProfile()
            up1.user = user1
            up1.location = location
            up1.marketId = 'EOD'
            up1.save()
            
        user2 = User.objects.get(username='******')
        if not UserProfile.objects.filter(user=user2).exists():
            up2 = UserProfile()
            up2.user = user2
            up2.location = location
            up2.marketId = 'EOD'
            up2.save()

        user3 = User.objects.get(username='******')
        if not UserProfile.objects.filter(user=user3).exists():
            up3 = UserProfile()
            up3.user = user3
            up3.location = location
            up3.marketId = 'TEST1'
            up3.save()

        user4 = User.objects.get(username='******')
        if not UserProfile.objects.filter(user=user4).exists():
            up4 = UserProfile()
            up4.user = user4
            up4.location = location
            up4.marketId = 'DEMO'
            up4.save()
        
        if not TCBond.objects.filter(name='TEST1').exists():
            bond = TCBond()
            bond.name = 'TEST1'
            bond.ccy = 'USD'
            cusip = Enum.BondIdentifierType('CUSIP') 
            if not Identifier.objects.filter(name='123456789', type=cusip):
                identifier = Identifier()
                identifier.name='123456789'
                identifier.type=cusip
                identifier.save()
            identifier = Identifier.objects.get(name='123456789', type=cusip)  
            bond.identifiers = identifier
            bond.startDate = Date(month=9,day=12,year=2010).toPythonDate()
            bond.endDate = Date(month=9,day=12,year=2020).toPythonDate()
            bond.coupon = 0.01
            bond.basis = '30360'
            bond.paymentFrequency = Enum.Frequency('S')
            bond.paymentRollRule = Enum.Roll('MF')
            bond.paymentCalendar = Calendar.createCalendar('US')
            bond.assetType = Enum.AssetType('NYMUNIBOND')
            bond.save()

        if not Equity.objects.filter(ticker='TEST1').exists():
            equity = Equity()
            equity.ticker = 'TEST1'
            equity.assetType = Enum.AssetType('EQUITYUS')
            equity.save()
        equity = Equity.objects.get(ticker='TEST1')
        stockPrice = StockPrice()
        stockPrice.equity = equity
        stockPrice.pricingDate = testDate
        stockPrice.marketId = 'TEST1'
        stockPrice.mid = 123.45
        stockPrice.save()
        equity = Equity.objects.get(ticker='TEST1')
        stockPrice = StockPrice()
        stockPrice.equity = equity
        stockPrice.pricingDate = testFirstDate
        stockPrice.marketId = 'TEST1'
        stockPrice.mid = 123.44
        stockPrice.save()
        if not Equity.objects.filter(ticker='TEST2').exists():
            equity = Equity()
            equity.ticker = 'TEST2'
            equity.assetType = Enum.AssetType('EQUITYUS')
            equity.save()
        equity = Equity.objects.get(ticker='TEST2')
        stockPrice = StockPrice()
        stockPrice.equity = equity
        stockPrice.pricingDate = testDate
        stockPrice.marketId = 'TEST1'
        stockPrice.mid = 543.21
        stockPrice.save()
        equity = Equity.objects.get(ticker='TEST2')
        stockPrice = StockPrice()
        stockPrice.equity = equity
        stockPrice.pricingDate = testFirstDate
        stockPrice.marketId = 'TEST1'
        stockPrice.mid = 543.11
        stockPrice.save()
        
        if not Portfolio.objects.filter(name='TEST1', user='******').exists():
            portfolio =Portfolio()
            portfolio.name = 'TEST1'
            portfolio.user = '******'
            portfolio.save()
        portfolio =Portfolio.objects.get(name='TEST1', user='******')
        
        if not ModelPosition.objects.filter(asOf=testDate, portfolio=portfolio, 
                                       positionType = Enum.PositionType('EQUITY'),
                                       ticker = 'TEST1', amount = 100.0).exists():
            position = ModelPosition()
            position.asOf=testDate
            position.portfolio = portfolio
            position.positionType = Enum.PositionType('EQUITY')
            position.ticker = 'TEST1'
            position.amount = 100.0
            position.save()

        if not ModelPosition.objects.filter(asOf=testDate, portfolio=portfolio, 
                                       positionType = Enum.PositionType('EQUITY'),
                                       ticker = 'TEST2', amount = 100.0).exists():
            position = ModelPosition()
            position.asOf=testDate
            position.portfolio = portfolio
            position.positionType = Enum.PositionType('EQUITY')
            position.ticker = 'TEST2'
            position.amount = 100.0
            position.save()

        if not ModelPosition.objects.filter(asOf=testDate, portfolio=portfolio, 
                                       positionType = Enum.PositionType('BOND'),
                                       ticker = 'TEST1', amount = 100.0).exists():
            position = ModelPosition()
            position.asOf=testDate
            position.portfolio = portfolio
            position.positionType = Enum.PositionType('BOND')
            position.ticker = 'TEST1'
            position.amount = 100.0
            position.save()
       
        if not ModelPosition.objects.filter(asOf=testFirstDate, portfolio=portfolio, 
                                       positionType = Enum.PositionType('EQUITY'),
                                       ticker = 'TEST1', amount = 100.0).exists():
            position = ModelPosition()
            position.asOf=testFirstDate
            position.portfolio = portfolio
            position.positionType = Enum.PositionType('EQUITY')
            position.ticker = 'TEST1'
            position.amount = 100.0
            position.save()

        if not ModelPosition.objects.filter(asOf=testFirstDate, portfolio=portfolio, 
                                       positionType = Enum.PositionType('EQUITY'),
                                       ticker = 'TEST2', amount = 100.0).exists():
            position = ModelPosition()
            position.asOf=testFirstDate
            position.portfolio = portfolio
            position.positionType = Enum.PositionType('EQUITY')
            position.ticker = 'TEST2'
            position.amount = 100.0
            position.save()

        if not ModelPosition.objects.filter(asOf=testFirstDate, portfolio=portfolio, 
                                       positionType = Enum.PositionType('BOND'),
                                       ticker = 'TEST1', amount = 100.0).exists():
            position = ModelPosition()
            position.asOf=testFirstDate
            position.portfolio = portfolio
            position.positionType = Enum.PositionType('BOND')
            position.ticker = 'TEST1'
            position.amount = 100.0
            position.save()
            
        if not ModelPosition.objects.filter(asOf=testFirstDate, portfolio=portfolio, 
                                       positionType = Enum.PositionType('CASH'),
                                       ticker = 'Cash', amount = 1000.0).exists():
            position = ModelPosition()
            position.asOf=testFirstDate
            position.portfolio = portfolio
            position.positionType = Enum.PositionType('CASH')
            position.ticker = 'Cash'
            position.amount = 1000.0
            position.save()
            
        curve = InterestRateCurve()
        curve.ccy = 'USD'
        curve.index = Enum.Index('LIBOR')
        curve.term = Enum.TimePeriod('M')
        curve.numTerms = 3
        curve.pricingDate =testDate
        curve.marketId = 'TEST1'

        curve.addRate(InterestRate(type='Deposit', term=Enum.TimePeriod('M'),
                                   numTerms=1,mid=0.01,curve=curve))
        curve.addRate(InterestRate(type='Deposit', term=Enum.TimePeriod('M'),
                                   numTerms=3,mid=0.01,curve=curve))
        curve.addRate(InterestRate(type='Swap', term=Enum.TimePeriod('Y'),
                                   numTerms=1,mid=0.01,curve=curve))
        curve.addRate(InterestRate(type='Swap', term=Enum.TimePeriod('Y'),
                                   numTerms=5,mid=0.01,curve=curve))
        curve.addRate(InterestRate(type='Swap', term=Enum.TimePeriod('Y'),
                                   numTerms=10,mid=0.01,curve=curve))
        curve.addRate(InterestRate(type='Swap', term=Enum.TimePeriod('Y'),
                                   numTerms=30,mid=0.01,curve=curve))
        curve.save()

        curve = InterestRateCurve()
        curve.ccy = 'USD'
        curve.index = Enum.Index('LIBOR')
        curve.term = Enum.TimePeriod('M')
        curve.numTerms = 3
        curve.pricingDate = testFirstDate
        curve.marketId = 'TEST1'

        curve.addRate(InterestRate(type='Deposit', term=Enum.TimePeriod('M'),
                                   numTerms=1,mid=0.01,curve=curve))
        curve.addRate(InterestRate(type='Deposit', term=Enum.TimePeriod('M'),
                                   numTerms=3,mid=0.01,curve=curve))
        curve.addRate(InterestRate(type='Swap', term=Enum.TimePeriod('Y'),
                                   numTerms=1,mid=0.01,curve=curve))
        curve.addRate(InterestRate(type='Swap', term=Enum.TimePeriod('Y'),
                                   numTerms=5,mid=0.01,curve=curve))
        curve.addRate(InterestRate(type='Swap', term=Enum.TimePeriod('Y'),
                                   numTerms=10,mid=0.01,curve=curve))
        curve.addRate(InterestRate(type='Swap', term=Enum.TimePeriod('Y'),
                                   numTerms=30,mid=0.01,curve=curve))
        curve.save()
        
        if not SwaptionVolatilitySurface.objects.filter(ccy=Enum.Currency('USD'), index=Enum.Index('LIBOR'), term=Enum.TimePeriod('M'), numTerms=3, 
                                                        pricingDate=testDate, marketId='TEST1'):
            #Special case where I just append the vols. Should use a function
            vols = SwaptionVolatilitySurface(ccy=Enum.Currency('USD'), index=Enum.Index('LIBOR'), term=Enum.TimePeriod('M'), numTerms=3, 
                                             pricingDate=testDate, marketId='TEST1')
            volPoints = []
            volPoints.append(SwaptionVolatility(expiryTerm=Enum.TimePeriod('Y'), expiryNumTerms=1, underlyingTerm=Enum.TimePeriod('Y'),
                                                underlyingNumTerms=3, mid=0.40, surface=vols))
            volPoints.append(SwaptionVolatility(expiryTerm=Enum.TimePeriod('Y'), expiryNumTerms=3, underlyingTerm=Enum.TimePeriod('Y'),
                                                underlyingNumTerms=3, mid=0.45, surface=vols))
            volPoints.append(SwaptionVolatility(expiryTerm=Enum.TimePeriod('Y'), expiryNumTerms=1, underlyingTerm=Enum.TimePeriod('Y'),
                                                underlyingNumTerms=5, mid=0.5, surface=vols))
            volPoints.append(SwaptionVolatility(expiryTerm=Enum.TimePeriod('Y'), expiryNumTerms=3, underlyingTerm=Enum.TimePeriod('Y'),
                                                underlyingNumTerms=5, mid=0.55, surface=vols))
            vols.addVolatilities(volPoints)
            vols.save()
            
        if not BondOAS.objects.filter(tCBond=TCBond.objects.get(name='TEST1'),pricingDate=testDate,
                                   marketId='TEST1'):
            bondOAS = BondOAS(tCBond=TCBond.objects.get(name='TEST1'),pricingDate=testDate,
                              marketId='TEST1',mid=0.0012)
            bondOAS.save()
        #done for only one test BondPositionTest.testLoadAndSaveMarketData
        if not BondOAS.objects.filter(tCBond=TCBond.objects.get(name='TEST1'),pricingDate=Date(month=1,day=1,year=2009),
                                      marketId='EOD'):
            bondOAS = BondOAS(tCBond=TCBond.objects.get(name='TEST1'),pricingDate=Date(month=1,day=1,year=2009),
                              marketId='EOD',mid=0.01)
            bondOAS.save()
        #now load zero oas for all dates we do testing
        timePeriods = VARUtilities.VARTimePeriodsAndSteps()
        timePeriods.generate(start = Date(month=8,day=30,year=2011), end = Date(month=9,day=12,year=2011), 
                             num = 1, term = Enum.TimePeriod('D'), calendar = Calendar.US())
        for timeStep in timePeriods.timeSteps:
            if not BondOAS.objects.filter(tCBond=TCBond.objects.get(name='TEST1'), pricingDate=timeStep, marketId='TEST1'):
                bondOAS = BondOAS(tCBond=TCBond.objects.get(name='TEST1'), pricingDate=timeStep, marketId='TEST1',mid=0.0)
                bondOAS.save()
        
        fileLoader = MarketDataLoader.EquityPriceLoader()
        fileLoader.loadStockPriceFromCSVFile(ROOT_PATH+'/misc/data/StockPricesForHVaRTests.csv')
        fileLoader.loadInterestRateFromCSVFile(ROOT_PATH+'/misc/data/InterestRatesForHVaRTests.csv')

        if not HvarConfiguration.objects.filter(name='TEST1').exists():
            config = HvarConfiguration()
            config.name = 'TEST1'
            config.startDate = Date(month=8,day=30,year=2011).toPythonDate()
            config.endDate = Date(month=9,day=12,year=2011).toPythonDate()
            config.stepSize = 1
            config.stepUnit = Enum.TimePeriod('D')
            config.calendar = Calendar.US()
            config.confLevel = 0.95
            config.marketId = 'TEST1'
            config.save()
Example #19
0
 def loadAndSaveMarketData(self, pricingDate, marketId):
     ''' assumes that 'EOD' marketId exists
     algorithm is to load the EOD curve and save it with marketId
     '''
     eodCurve = InterestRateCurve()
     eodCurve.ccy = self._tcSwap.ccy
     eodCurve.index = Index('LIBOR')
     eodCurve.term = 'M'
     eodCurve.numTerms = 3
     eodCurve.pricingDate = pricingDate
     eodCurve.marketId = 'EOD'
     eodCurve.load()
     irCurve = InterestRateCurve()
     irCurve.ccy = self._tcSwap.ccy
     irCurve.index = Index('LIBOR')
     irCurve.term = 'M'
     irCurve.numTerms = 3
     irCurve.pricingDate = pricingDate
     irCurve.marketId = marketId
     eodRates = eodCurve.getRates()
     for rate in eodRates:
         irCurve.addRate(rate)
     irCurve.save()
 def loadDummyIRCurve(self, pricingDate):
     curve = InterestRateCurve()
     curve.ccy = 'USD'
     curve.index = Enum.Index('LIBOR')
     curve.term = 'M'
     curve.numTerms = 3
     curve.pricingDate = pricingDate
     curve.marketId = 'TEST1'
     r1 = InterestRate()
     r1.type = 'Deposit'
     r1.term = 'M'
     r1.numTerms = 1
     r1.mid = 0.01
     r1.curve = curve
     r2 = InterestRate()
     r2.type = 'Deposit'
     r2.term = 'M'
     r2.numTerms = 3
     r2.mid = 0.01
     r2.curve = curve
     r3 = InterestRate()
     r3.type = 'Swap'
     r3.term = 'Y'
     r3.numTerms = 1
     r3.mid = 0.01
     r3.curve = curve
     r4 = InterestRate()
     r4.type = 'Swap'
     r4.term = 'Y'
     r4.numTerms = 5
     r4.mid = 0.01
     r4.curve = curve
     r5 = InterestRate()
     r5.type = 'Swap'
     r5.term = 'Y'
     r5.numTerms = 10
     r5.mid = 0.01
     r5.curve = curve
     r6 = InterestRate()
     r6.type = 'Swap'
     r6.term = 'Y'
     r6.numTerms = 30
     r6.mid = 0.01
     r6.curve = curve
     curve.addRate(r1)
     curve.addRate(r2)
     curve.addRate(r3)
     curve.addRate(r4)
     curve.addRate(r5)
     curve.addRate(r6)
     curve.save()
 def loadInterestRateFromCSVFile(self, fileName):
     '''
     Downloads prices from fileName. fileName needs to include full path
     '''
     file = open(fileName,'r')
     lastDate = Date.Date(month=1,day=1,year=1972)
     for line in file:
         items = line.split(',')
         #skip first line which is headers
         if items[0] == 'date':
             continue
         #create new curve if date has changed
         curveDate = self.createDateFromString(items[0])
         if curveDate <> lastDate:
             newCurve = InterestRateCurve()
             newCurve.pricingDate = curveDate
             newCurve.ccy = items[1]
             newCurve.index = items[2]
             newCurve.marketId = items[3]
             newCurve.term = items[4]
             newCurve.numTerms = int(items[5])
             newCurve.save()
             newCurve.load()
             #Delete all rates in case the curve already existed
             rates = InterestRate.objects.filter(curve=newCurve)
             if rates:
                 rates.delete()
         curve = InterestRateCurve()
         curve.pricingDate = curveDate
         curve.ccy = items[1]
         curve.index = items[2]
         curve.marketId = items[3]
         curve.term = items[4]
         curve.numTerms = int(items[5])
         curve.load()
         rate = InterestRate()
         rate.curve = curve
         rate.type = items[6]
         rate.term = items[7]
         rate.numTerms = int(items[8])
         rate.mid = float(items[9])
         rate.save()
         lastDate = curveDate
Example #22
0
    def _setupQL(self, pricingDate, marketId=''):
        QuantLib.Settings.instance().evaluationDate = pricingDate.ql()
#        print 'in setupql'
        self._pricingDate = pricingDate
        # Get market data curve based on Ccy/Index
        curve = InterestRateCurve()
        curve.pricingDate = pricingDate
        curve.ccy = self._tcSwap.ccy
        curve.index = Index('LIBOR')
        curve.term = 'M'
        curve.numTerms = self._tcSwap.floatingIndexNumTerms
        curve.marketId = marketId
        if self.marketDataContainer == None:
            raise ErrorHandling.MarketDataMissing('marketDataContainer is None')
        newCurve = self.marketDataContainer.find(curve)
        if newCurve == None:
            raise ErrorHandling.MarketDataMissing('Cannot find market data %s' % curve)
        depositCurve = newCurve.buildZeroCurve()
        discountTermStructure = QuantLib.RelinkableYieldTermStructureHandle()
        discountTermStructure.linkTo(depositCurve)

        swapEngine = QuantLib.DiscountingSwapEngine(discountTermStructure)

        #TODO fix the discounting curve to make if projected fwd curve
        index = QuantLib.Euribor6M(discountTermStructure)

        #fixingDate = Date.Date(month=9,day=10,year=2012)
        #index.addFixing(fixingDate.ql(),0.02)
        
        #tcSwap dates are python dates
        fixedSchedule = QuantLib.Schedule(Date.createQLDateFromPythonDate(self._tcSwap.startDate),
                                          Date.createQLDateFromPythonDate(self._tcSwap.endDate),
                                          #fixedLegTenor is QuantLib.Period(n,unit)
                                          #Period is an integer (n) and TimePeriod (unit)
                                          #TODO fix payment freq
                                          QuantLib.Period(6,QuantLib.Months),
                                          self._tcSwap.fixedPaymentCalendar.ql(),
                                          self._tcSwap.fixedPaymentRollRule.ql(),
                                          self._tcSwap.fixedPaymentRollRule.ql(),
                                          QuantLib.DateGeneration.Forward, 
                                          False)
        
        floatingSchedule = QuantLib.Schedule(Date.createQLDateFromPythonDate(self._tcSwap.startDate),
                                          Date.createQLDateFromPythonDate(self._tcSwap.endDate),
                                          #fixedLegTenor is QuantLib.Period(n,unit)
                                          #Period is an integer (n) and TimePeriod (unit)
                                          #TODO fix payment freq
                                          QuantLib.Period(3,QuantLib.Months),
                                          self._tcSwap.floatingPaymentCalendar.ql(),
                                          self._tcSwap.floatingPaymentRollRule.ql(),
                                          self._tcSwap.floatingPaymentRollRule.ql(),
                                          QuantLib.DateGeneration.Forward, 
                                          False) 
    
    #TODO: FIx to decide if it's payer or receiver fixed
        self._qlSwap = QuantLib.VanillaSwap(QuantLib.VanillaSwap.Payer, 
                                            self.amount,
                                            fixedSchedule, 
                                            self._tcSwap.fixedCoupon, 
                                            self._tcSwap.fixedBasis.ql(),
                                            floatingSchedule, 
                                            #TODO fix index
                                            index,
                                            self._tcSwap.floatingSpread,
                                            self._tcSwap.floatingBasis.ql())
        
        self._qlSwap.setPricingEngine(swapEngine)
        self.upToDate = True
Example #23
0
    def run(self):
        #Log
        if os.path.isfile(self.logFile):
            os.remove(self.logFile)
        logFileHandle = open(self.logFile,'w')
        logFileHandle.write('Running batch as of %s\n' % self.batchDate)
        logFileHandle.write('Max batch is %s\n' % self.maxBatchDate)
#        if QuantLib.TARGET().isBusinessDay(self.batchDate.ql()) == False:
 #           logFileHandle.write('Not a good business date based on QuantLib TARGET calendar. Exiting ...')
  #          print('Not a good business date based on QuantLib TARGET calendar. Exiting ...')
        
        #Update locations with date
        logFileHandle.write('\nUpdating all locations except TEST1 with new date\n')
        locations = Location.objects.filter(~Q(name='TEST1'))
        for location in locations:
            logFileHandle.write('Updating location %s\n' % location.name)
            location.pricingDate = self.batchDate.toPythonDate()
            location.save()
        
        #Load Equity prices
        logFileHandle.write('\nLoading equity prices\n')
        equities = Equity.objects.all()
        equityLoader = EquityPriceLoader()
        for equity in equities:
            #skip TEST1 and TEST2 as they are only used for testing
            if equity.ticker in ['TEST1', 'TEST2']:
                continue
            if StockPrice.objects.filter(equity=equity, pricingDate=self.batchDate, marketId=self.marketId).exists() == True:
                logFileHandle.write('%s price exists\n' % equity.ticker)
                continue
            else:
                logFileHandle.write('Loading prices for equity %s\n' % equity.ticker)
                try:
                    equityLoader.loadCurrentPriceFromYahoo(secId=equity.ticker, 
                                                           today=self.batchDate,
                                                           marketId=self.marketId)
                except MarketDataMissing:
                    logFileHandle.write('No market data loaded for %s as of %s\n' % (equity.ticker, self.batchDate))
                    logFileHandle.write('As default loading price from last batch date\n')
                    lastPrice = StockPrice.objects.get(equity=equity, pricingDate=self.maxBatchDate, marketId=self.marketId)
                    lastPrice.pricingDate = self.batchDate
                    lastPrice.pk = None
                    lastPrice.save()
                   
        #Load Interest Rates        
        logFileHandle.write('\nLoading Interest Rates\n')
        if self.maxBatchDate != self.batchDate:
            logFileHandle.write('For now just copy rates from max batch date to current batch date\n')
            curve = InterestRateCurve.objects.get(ccy=Currency('USD'), index=Index('LIBOR'), 
                                                  term=TimePeriod('M'), numTerms=3, 
                                                  pricingDate=self.maxBatchDate, 
                                                  marketId='EOD')
            curve.loadRates()
            
            newCurve = InterestRateCurve(ccy=Currency('USD'), index=Index('LIBOR'), 
                                         term=TimePeriod('M'), numTerms=3, 
                                         pricingDate=self.batchDate, 
                                         marketId='EOD')
            
            for rate in curve.getRates():
                newRate = InterestRate(type=rate.type, term=rate.term, numTerms=rate.numTerms, mid=rate.mid, curve=newCurve)
                logFileHandle.write('Rate: %s/%d/%0.2f\n' % (rate.term, rate.numTerms, rate.mid*100))
                newCurve.addRate(newRate)
            newCurve.save()

        #Copy OAS forward and keep constant
        logFileHandle.write('\nCopying OAS from max batch date to batch date\n')
        if self.maxBatchDate != self.batchDate:
            bondOAS = BondOAS.objects.get(tCBond=TCBond.objects.get(name='PortAuth_4.00_JAN42'),
                                          marketId='EOD', pricingDate=self.maxBatchDate)
            bondOAS.pk = None
            bondOAS.pricingDate = self.batchDate
            bondOAS.save()
            
        #Process Positions
        logFileHandle.write('\nProcessing positions\n')
        #load all positions with the max date
        #if the batchDate greater than maxbatchdate then copy position to batchdate (roll position forward)
        if self.maxBatchDate < self.batchDate:
            positions = ModelPosition.objects.filter(asOf=self.maxBatchDate)
            for position in positions:
                logFileHandle.write('Copying position %s from max batch date to batch date\n' % position)
                position.pk = None
                position.asOf = self.batchDate
                position.save()

        #Update Positions based on Transactions
        logFileHandle.write('\nUpdating of positions based on transaction\n')
        transactions = Transaction.objects.filter(reflectedInPosition=False)
        if len(transactions) == 0:
            logFileHandle.write('No transactions to process\n')
        else:
            for transaction in transactions:
                logFileHandle.write('Processing transaction %s\n' % str(transaction))
                positions = transaction.relatedPositions()
                for position in positions:
                    logFileHandle.write('Processing position %s\n' % str(transaction))
                    transaction.updatePositionAmount(position=position)  
                    position.save() 
                transaction.reflectedInPosition = 'True'
                transaction.save()                 
            
        #Performance
        logFileHandle.write('\nRunning Performance Report\n')
        if os.path.isfile(self.perfFile):
            os.remove(self.perfFile)
        perfFileHandle = open(self.perfFile,'w')
        portfolios = Portfolio.objects.filter(user='******')
        startValue = 0
        endValue = 0
        for portfolio in portfolios:
            performanceCalculator = PerformanceCalculator(start=self.productionStartDate,
                                                          end=self.batchDate,
                                                          portfolio=portfolio,
                                                          marketId=self.marketId)
            perfFileHandle.write(performanceCalculator.report()+'\n')
            startValue += performanceCalculator.startValue
            endValue += performanceCalculator.endValue
            endValue += performanceCalculator.transactionValue
        
        if startValue == 0 or (self.batchDate.ql() - self.productionStartDate.ql()) == 0:
            overallPerformance = 0
        else:
            overallPerformance = ((endValue - startValue) / startValue) * 365.0 / (self.batchDate.ql() - self.productionStartDate.ql())
        perfFileHandle.write('Overall Period Performance = %.2f%%\n' % (overallPerformance*100.0/365.0*(self.batchDate.ql() - self.productionStartDate.ql())))
        perfFileHandle.write('Overall Annual Performance = %.2f%%' % (overallPerformance*100.0))
        perfFileHandle.close()
        
        #MTM and Positions
        logFileHandle.write('\nRunning MTM and Positions Report\n')
        totalValue = 0
        if os.path.isfile(self.mtmFile):
            os.remove(self.mtmFile)
        mtmFileHandle = open(self.mtmFile,'w')
        portfolios =Portfolio.objects.filter(user='******')
        mtmFileHandle.write('MTM Report as of %s\n' % self.batchDate)
        for portfolio in portfolios:
            mtmFileHandle.write('Portfolio=%s\n' % portfolio.name)
            modelPositions = ModelPosition.objects.filter(portfolio=portfolio, asOf=self.batchDate)
            for modelPosition in modelPositions:
                position = CreatePosition(modelPosition=modelPosition)
                portfolio.addPosition(position)
            marketDataContainer = MarketDataContainer()
            for position in portfolio.positions:
                marketDataContainer.add(position.marketData(pricingDate=self.batchDate, marketId=self.marketId))
            for position in portfolio.positions:
                position.marketDataContainer = marketDataContainer
            for position in portfolio.positions:
                positionValue = position.NPV(pricingDate=self.batchDate,marketId=self.marketId)
                mtmFileHandle.write('ModelPosition=%s with %.0f shares, asset type %s and value $%s\n' % (position.secId,
                                                                                                   position.amount,
                                                                                                   position.getAssetType(),
                                                                                                   fToDC(positionValue)))
            portfolioValue = portfolio.NPV(pricingDate=self.batchDate,marketId=self.marketId)
            mtmFileHandle.write('Portfolio value=$%s\n\n' % fToDC(portfolioValue))
            totalValue += portfolioValue
        mtmFileHandle.write('Total value=%s\n\n' % fToDC(totalValue))

        #Asset Allocation
        #allocations is associative array with value
        allocations = {}
        totalValue = 0
        for item in AssetType.choices:
            allocations[item[0]] = 0
        logFileHandle.write('\nRunning Allocation Report\n\n')
        if os.path.isfile(self.allocationFile):
            os.remove(self.allocationFile)
        allocationFileHandle = open(self.allocationFile,'w')
        portfolios =Portfolio.objects.filter(user='******')
        allocationFileHandle.write('Allocation Report as of %s\n\n' % self.batchDate)
        for portfolio in portfolios:
            if portfolio.name == 'TradeAug2013':
                continue
            modelPositions = ModelPosition.objects.filter(portfolio=portfolio, asOf=self.batchDate)
            for modelPosition in modelPositions:
                position = CreatePosition(modelPosition)
                portfolio.addPosition(position)
            marketDataContainer = MarketDataContainer()
            for position in portfolio.positions:
                marketDataContainer.add(position.marketData(pricingDate=self.batchDate, marketId=self.marketId))
            for position in portfolio.positions:
                position.marketDataContainer = marketDataContainer
            for position in portfolio.positions:
                npv = position.NPV(pricingDate=self.batchDate, marketId=self.marketId)
                allocations[str(position.getAssetType())] += npv
                totalValue += npv
        
        orderedAllocations = OrderedDict(sorted(allocations.items(),key=lambda t: t[1],reverse=True))
        for key in orderedAllocations.keys():
            try:
                allocationPercent = Allocation.objects.get(assetType=AssetType(key)).percent
            except:
                allocationPercent = 0.0
            try:
                actualAllocation = 100*orderedAllocations[key]/totalValue
            except:
                actualAllocation = 0.0
            allocationFileHandle.write('%s=%.0f%%\t\t\t\t\t\twith target %.0f%%\t and value $%s\n' % (key,
                                                                                           actualAllocation,
                                                                                           100*allocationPercent,
                                                                                           fToD(orderedAllocations[key])))
        
        logFileHandle.write('Batch completed\n')
        logFileHandle.close()
        
        #Every time I run the batch and it succeeds we need to save the batch info
        self.batch.save()