def createHistory(portfolioFile = None, forceReload = False): # Read all transactions from disk transactions = transaction.readTransactions(portfolioFile) startDate = transactions[0].date # And all investments investments = investment.learn_investments(transactions) # Build a list of all mentioned tickers tickerList = [] for trans in transactions: if trans.ticker not in tickerList: tickerList.append(trans.ticker) # Hard code currency list. !! Should pick these out of investments really. currencyList = ["USD", "Euro", "NOK"] # Build a history of our transactions history = History(transactions) # Load what we've got from disk prices = {} Price.loadHistoricalPricesFromDisk(prices) # Start reading all the HTML we're going to need now. urlCache, currencyInfo, tickerInfo = cacheUrls(tickerList, currencyList, investments, history, startDate, prices, forceReload) # Load currency histories for currency in currencyInfo: Price.getCurrencyHistory(currency[0], currency[1], date.today(), prices, urlCache) # Load current prices from the Web Price.loadCurrentPricesFromWeb(history.currentTickers(), prices, urlCache) # Now load historical prices from the Web for ticker in tickerInfo: Price.loadHistoricalPricesFromWeb(ticker[0], ticker[1], ticker[2], prices, urlCache) # Fill in any gaps Price.fixPriceGaps(prices) # Now save any new data to disk Price.savePricesToDisk(prices) # And fill in any gaps between the last noted price and today Price.fixLastPrices(prices, history.currentTickers()) # Give the prices to our history history.notePrices(prices) # Done with all the HTML that we read #urlCache.clean_urls() return (history, investments)
def compareShare(ticker): global newHistory global newInvestments global newPortfolio # Get price info about the ticker newPrices = {} Price.loadHistoricalPricesFromDisk(newPrices) urlCache, currencyInfo, tickerInfoList = cacheUrls([ticker], [], [], None, history.transactions[0].date, newPrices, forceReload = True) Price.loadCurrentPricesFromWeb([ticker], newPrices, urlCache) for tickerInfo in tickerInfoList: Price.loadHistoricalPricesFromWeb(tickerInfo[0], tickerInfo[1], tickerInfo[2], newPrices, urlCache) Price.fixPriceGaps(newPrices) #urlCache.clean_urls() # Generate a new portfolio file with all tickers replaced with this one. newTransactions = [] for tran in history.transactions: if tran.action == "BUY" or tran.action == "SELL": newTran = copy.copy(tran) newTran.ticker = ticker value = newTran.number * newTran.price newTran.price = newPrices[(ticker, tran.date)] newTran.number = value / newTran.price newTransactions.append(newTran) transaction.writeTransactions(newTransactions, TEMP_PORTFOLIO) # Now build an alternate history based on these new transactions print "Building portfolio history..." newHistory, newInvestments = createHistory(TEMP_PORTFOLIO, forceReload = True) print "Done" print "" newPortfolio = newHistory.getPortfolio(date.today()) # And print some info print u"Hypothetical / real capital gain: \N{pound sign}%.2f / \N{pound sign}%.2f"%(newPortfolio.capitalGain() / 100, portfolio.capitalGain() / 100) print u"Real portfolio dividends received: \N{pound sign}%.2f"%(portfolio.totalDividends() / 100) print "Real portfolio yield: %.2f%%"%(((portfolio.totalDividends() * 365.0 / (history.endDate() - history.startDate()).days)) * 100 / history.averageValue(history.startDate(), history.endDate()))