def __init__(self, batchDate = None): ''' Constructor ''' self.productionStartDate = Date(day=25,month=7,year=2013) self.logFile = settings.BATCH_LOGS + 'batchLog.txt' self.perfFile = settings.BATCH_LOGS + 'perf.txt' self.mtmFile = settings.BATCH_LOGS + 'mtm.txt' self.allocationFile = settings.BATCH_LOGS + 'allocation.txt' #Specify your email address to get notifications sent to self.email = 'XXX' self.marketId = 'EOD' self.maxBatchDate = Date() queryResult = Batch.objects.all().aggregate(Max('batchDate')) self.maxBatchDate.fromPythonDate(queryResult['batchDate__max']) if batchDate == None: today = datetime.date.today() self.batchDate = Date(month=today.month, day=today.day, year=today.year) else: self.batchDate = batchDate #prepare the batch so that we can save it if the batch finishes succesfully self.batch = Batch() self.batch.batchDate = self.batchDate print 'Running batch as of %s' % self.batchDate
def main(): print "Shifting Start" if len(sys.argv) != 4: print "Needs 3 arguments: Date, MarketId, Shift in decimal" exit(-1) else: fromDate = Date() fromDate.fromPythonDate(datetime.datetime.strptime(sys.argv[1],"%Y%m%d").date()) shifter = ShiftInterestRateCurve(date = fromDate, marketId = sys.argv[2], shift = float(sys.argv[3])) print "Copying Done"
def setUp(self): self._pricingDate = Date(month=9,day=12,year=2011) self._marketId = 'TEST1' QuantLib.Settings.instance().evaluationDate = self._pricingDate.ql() self._tcSwap = TCSwap(name='Dummy', ccy = Currency('USD'), startDate = date(month=9,day=14,year=2011), endDate = date(month=9,day=14,year=2016), fixedCoupon = 0.01, fixedBasis = Basis.createBasis('30360'), fixedPaymentFrequency = Frequency('S'), fixedPaymentRollRule = Roll('MF'), fixedPaymentCalendar = Calendar.createCalendar('US'), floatingIndex = Index('LIBOR'), floatingIndexTerm = TimePeriod('M'), floatingIndexNumTerms = 3, floatingSpread = 0.0, floatingBasis = Basis.createBasis('30360'), floatingPaymentFrequency = Frequency('Q'), floatingPaymentRollRule = Roll('MF'), floatingPaymentCalendar = Calendar.createCalendar('US'), floatingResetFrequency = Frequency('Q'), floatingResetRollRule = Roll('MF'), floatingResetCalendar = Calendar.createCalendar('US')) self._pos = SwapPosition(amount=1000000, tcSwap=self._tcSwap)
def main(): print 'Start' pricingDate = Date(month=9, day=12, year=2011) portfolio =Portfolio() QuantLib.Settings.instance().evaluationDate = pricingDate.ql() pos1 = BondPosition(100, 'TEST1') pos2 = EquityPosition(100,'TEST1') portfolio.addPosition(pos1) portfolio.addPosition(pos2) timePeriods = VARUtilities.VARTimePeriodsAndSteps() timePeriods.generate(start=Date(month=8,day=30,year=2011), end=Date(month=9,day=12,year=2011), num=1, term=TimePeriod('D').ql(), calendar=Calendar.US()) #print 'timePeriods: ' + str(timePeriods) analysis = HistoricalVAR(pricingDate=pricingDate, portfolio=portfolio, timeSteps=timePeriods, confidenceInterval=0.95, marketId='TEST1') print 'HVaR = %f' % analysis.run() print 'End'
def testCalculatePerformanceTEST1Portfolio1Y(self): #load portfolio as of start and value #load portfolio as of end and value #sum up all ADD and REMOVE Transaction #% return = {end - start + SUM(ADD) - SUM(REMOVE)} / Start startPortfolio =Portfolio.objects.get(name='TEST1') start = Date(month=8,day=30,year=2011) startDjangoPositions = ModelPosition.objects.filter(portfolio=startPortfolio, asOf=start) for djangoPosition in startDjangoPositions: position = CreatePosition(djangoPosition) startPortfolio.addPosition(position) marketDataContainer = MarketDataContainer() for position in startPortfolio.positions: marketDataContainer.add(position.marketData(pricingDate=start, marketId=self.marketId)) for position in startPortfolio.positions: position.marketDataContainer = marketDataContainer startValue = startPortfolio.NPV(pricingDate=start,marketId=self.marketId) print 'Start value = %f' % startValue endPortfolio =Portfolio.objects.get(name='TEST1') end = Date(month=9,day=12,year=2011) endDjangoPositions = ModelPosition.objects.filter(portfolio=endPortfolio, asOf=end) for djangoPosition in endDjangoPositions: position = CreatePosition(djangoPosition) endPortfolio.addPosition(position) marketDataContainer = MarketDataContainer() for position in endPortfolio.positions: marketDataContainer.add(position.marketData(pricingDate=end, marketId=self.marketId)) for position in endPortfolio.positions: position.marketDataContainer = marketDataContainer endValue = endPortfolio.NPV(pricingDate=end,marketId=self.marketId) print 'End value = %f' % endValue #Should add the looping over transactions that are ADD and REDUCE print 'Annualized Performance is %f' % (((endValue - startValue) / startValue) * 365.0 / (end.ql() - start.ql()))
def main(): if len(sys.argv) == 1: batch = BatchUtility() batch.run() batch.sendEmail() else: for dateString in sys.argv[1:]: batchDate = Date() batchDate.fromPythonDate(datetime.datetime.strptime(dateString,"%Y%m%d").date()) batch = BatchUtility(batchDate=batchDate) batch.run() batch.sendEmail() #Sample of hardcoding a specific date # batchDate = Date(month=8,day=2,year=2013) # batch = BatchUtility(batchDate=batchDate) # batch = BatchUtility() # batch.run() # batch.sendEmail() #Sample of catching up between dates # batchDate = Date(month=7,day=14,year=2014) # while batchDate < Date(month=1,day=25,year=2015): # batchDate.nextDay() # print 'Running batch as of ', batchDate # batch = BatchUtility(batchDate=batchDate) # batch.run() # batchDate = Date(month=8,day=2,year=2013) # batch = BatchUtility(batchDate=batchDate) # batch = BatchUtility() # batch.run() # batch.sendEmail() print "Batch done"
def main(): print "Copying Start" if len(sys.argv) != 5: print "Needs 4 arguments: FromDate, FromMarketId, toDate, toMarketId" exit(-1) else: fromDate = Date() toDate = Date() fromDate.fromPythonDate(datetime.datetime.strptime(sys.argv[1],"%Y%m%d").date()) toDate.fromPythonDate(datetime.datetime.strptime(sys.argv[3],"%Y%m%d").date()) copier = CopyMarketData(fromDate = fromDate, fromMarketId = sys.argv[2], toDate = toDate, toMarketId = sys.argv[4]) print "Copying Done"
def setUp(self): self.pricingDate = Date(month=9,day=12,year=2011) QuantLib.Settings.instance().evaluationDate = self.pricingDate.ql()
class TestInterestRateCurve(unittest.TestCase): def setUp(self): self.pricingDate = Date(month=9,day=12,year=2011) QuantLib.Settings.instance().evaluationDate = self.pricingDate.ql() def tearDown(self): pass 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 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 testLoadCurve(self): curve = InterestRateCurve(ccy='USD', index=Index('LIBOR'), term='M', numTerms=3, pricingDate=self.pricingDate, marketId='TEST1') curve.load() # curve.printCurve() 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 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 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 testShiftCurve(self): curve = InterestRateCurve(ccy='USD', index=Index('LIBOR'), term='M', numTerms=3, pricingDate=self.pricingDate, marketId='TEST1') curve.load() #curve.printCurve() cv1 = curve.buildZeroCurve() curve.shift(0.01) cv2 = curve.buildZeroCurve() for node in cv1.nodes(): a = node for node in cv2.nodes(): a = node 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 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() # def testBuildCurveOverManyDates(self): # timeSteps = VARUtilities.VARTimePeriodsAndSteps() # timeSteps.generate(start=Date(month=7,day=3,year=2000), # end=Date(month=7,day=4,year=2000), # num=10, term = TimePeriod('D'), # calendar=Calendar.US()) # for timeStep in timeSteps.timeSteps: # curve = InterestRateCurve(ccy='USD', index=Enum.Index('LIBOR'), term='M', # numTerms=3, pricingDate=timeStep, marketId='EOD') # curve.load() # #curve.printCurve() # zeroCurve = curve.buildZeroCurve() # for node in zeroCurve.nodes(): # a = node def testAddRate(self): pass #Load curve #add one rate #build curve #check zero rate that was added def testRemoveRate(self): pass
class TestSwapPosition(unittest.TestCase): '''Unit test of BondPosition ''' def setUp(self): self._pricingDate = Date(month=9,day=12,year=2011) self._marketId = 'TEST1' QuantLib.Settings.instance().evaluationDate = self._pricingDate.ql() self._tcSwap = TCSwap(name='Dummy', ccy = Currency('USD'), startDate = date(month=9,day=14,year=2011), endDate = date(month=9,day=14,year=2016), fixedCoupon = 0.01, fixedBasis = Basis.createBasis('30360'), fixedPaymentFrequency = Frequency('S'), fixedPaymentRollRule = Roll('MF'), fixedPaymentCalendar = Calendar.createCalendar('US'), floatingIndex = Index('LIBOR'), floatingIndexTerm = TimePeriod('M'), floatingIndexNumTerms = 3, floatingSpread = 0.0, floatingBasis = Basis.createBasis('30360'), floatingPaymentFrequency = Frequency('Q'), floatingPaymentRollRule = Roll('MF'), floatingPaymentCalendar = Calendar.createCalendar('US'), floatingResetFrequency = Frequency('Q'), floatingResetRollRule = Roll('MF'), floatingResetCalendar = Calendar.createCalendar('US')) self._pos = SwapPosition(amount=1000000, tcSwap=self._tcSwap) def testNPV(self): marketDataContainer = MarketDataContainer() marketData = self._pos.marketData(self._pricingDate, self._marketId) marketDataContainer.add(marketData) self._pos.setMarketDataContainer(marketDataContainer) # print round(self._pos.NPV(self._pricingDate, self._marketId),2) self.failIf(round(self._pos.NPV(self._pricingDate, self._marketId),2) <> -5044.78) # def test__str__(self): # self.failIf(str(self._pos) <> '<<class \'src.bo.instruments.BondPosition.BondPosition\'>,TEST1,100>') def testFairRate(self): marketDataContainer = MarketDataContainer() marketData = self._pos.marketData(self._pricingDate, self._marketId) marketDataContainer.add(marketData) self._pos.setMarketDataContainer(marketDataContainer) # print round(self._pos.fairRate(self._pricingDate, self._marketId),2) self.failIf(round(self._pos.fairRate(self._pricingDate, self._marketId),2) <> 0.01) def testFairSpread(self): marketDataContainer = MarketDataContainer() marketData = self._pos.marketData(self._pricingDate, self._marketId) marketDataContainer.add(marketData) self._pos.setMarketDataContainer(marketDataContainer) # print round(self._pos.fairSpread(self._pricingDate, self._marketId),2) self.failIf(round(self._pos.fairSpread(self._pricingDate, self._marketId),2) <> 0.0) def testExceptionMarketDataMissing(self): self.assertRaises(ErrorHandling.MarketDataMissing, self._pos.NPV, Date(1,1,2009)) def testPriceWith2Curves(self): marketDataContainer = MarketDataContainer() marketData = self._pos.marketData(self._pricingDate, self._marketId) # print marketData[0].buildZeroCurve().nodes() marketDataContainer.add(marketData) self._pos.setMarketDataContainer(marketDataContainer) price1 = round(self._pos.NPV(self._pricingDate, self._marketId),5) # print price1 marketData[0].shift(0.01) # print marketData[0].buildZeroCurve().nodes() marketDataContainer1 = MarketDataContainer() marketDataContainer1.add(marketData) self._pos.setMarketDataContainer(marketDataContainer1) #add dirty flag to position to run setupql again. add add marketdatacontainer method price2 = round(self._pos.NPV(self._pricingDate, self._marketId),5) # print price2 self.failIf(price1 == price2) def testLoadAndSaveMarketData_TODO(self): #curve should never exist self._pos.loadAndSaveMarketData(pricingDate=Date(1,1,2009), marketId=self._marketId) #now, curve should exist irCurve = InterestRateCurve() irCurve.ccy = Currency('USD') irCurve.index = Index('LIBOR') irCurve.term = 'M' irCurve.numTerms = 3 irCurve.pricingDate = Date(1,1,2009) irCurve.marketId = self._marketId irCurve.load() #now, delete again so that next test will work irCurve.delete()
class BatchUtility(object): ''' Daily Batch that should be executed in windows scheduler ''' def __init__(self, batchDate = None): ''' Constructor ''' self.productionStartDate = Date(day=25,month=7,year=2013) self.logFile = settings.BATCH_LOGS + 'batchLog.txt' self.perfFile = settings.BATCH_LOGS + 'perf.txt' self.mtmFile = settings.BATCH_LOGS + 'mtm.txt' self.allocationFile = settings.BATCH_LOGS + 'allocation.txt' #Specify your email address to get notifications sent to self.email = 'XXX' self.marketId = 'EOD' self.maxBatchDate = Date() queryResult = Batch.objects.all().aggregate(Max('batchDate')) self.maxBatchDate.fromPythonDate(queryResult['batchDate__max']) if batchDate == None: today = datetime.date.today() self.batchDate = Date(month=today.month, day=today.day, year=today.year) else: self.batchDate = batchDate #prepare the batch so that we can save it if the batch finishes succesfully self.batch = Batch() self.batch.batchDate = self.batchDate print 'Running batch as of %s' % self.batchDate 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() def sendEmail(self): print "Sending email" mtmFileHandle = open(self.mtmFile,'r') send_mail('CMT MTM and Positions as of %s' % self.batchDate, mtmFileHandle.read(), self.email,[self.email], fail_silently=False) mtmFileHandle.close() perfFileHandle = open(self.perfFile,'r') send_mail('CMT Performance as of %s' % self.batchDate, perfFileHandle.read(), self.email,[self.email], fail_silently=False) perfFileHandle.close() allocationFileHandle = open(self.allocationFile,'r') send_mail('CMT Allocations as of %s' % self.batchDate, allocationFileHandle.read(), self.email,[self.email], fail_silently=False) allocationFileHandle.close() logFileHandle = open(self.logFile,'r') send_mail('CMT Log as of %s' % self.batchDate, logFileHandle.read(), self.email,[self.email], fail_silently=False) logFileHandle.close()