/** * Queries a transaction feed and prints feed and entry details. * * @param service authenticated client connection to a Finance GData service * @param feedUrl resource URL for the feed, including GData query parameters e.g. * http://finance.google.com/finance/feeds/default/ * portfolios/1/positions/NYSE:IBM/transactions * @throws IOException If there is a problem communicating with the server. * @throws MalformedURLException If the URL is invalid. * @throws ServiceException If the service is unable to handle the request. */ private static void queryTransactionFeed(FinanceService service, String feedUrl) throws IOException, MalformedURLException, ServiceException { System.out.println("Requesting Feed at location " + feedUrl); TransactionFeed transactionFeed = service.getFeed(new URL(feedUrl), TransactionFeed.class); System.out.println("\nTransaction Feed\n================"); printBasicFeedDetails(transactionFeed); for (int i = 0; i < transactionFeed.getEntries().size(); i++) { TransactionEntry transactionEntry = transactionFeed.getEntries().get(i); printTransactionEntry(transactionEntry); } }
/** * Prints detailed contents for a position (i.e. a Position Feed entry) * * @param positionEntry The position entry of interest */ private static void printPositionEntry(PositionEntry positionEntry) { System.out.println("\nPosition Entry\n--------------"); printBasicEntryDetails(positionEntry); System.out.println("\tFeed Link: " + positionEntry.getFeedLink().getHref()); if (positionEntry.getFeedLink().getFeed() == null) { System.out.println("\tNo inlined feed."); } else { System.out.println("********** Beginning of inline feed ***************"); printBasicFeedDetails(positionEntry.getFeedLink().getFeed()); TransactionFeed inlinedFeed = positionEntry.getFeedLink().getFeed(); printBasicFeedDetails(inlinedFeed); for (int i = 0; i < inlinedFeed.getEntries().size(); i++) { TransactionEntry transactionEntry = inlinedFeed.getEntries().get(i); printTransactionEntry(transactionEntry); } System.out.println("************* End of inlined feed *****************"); } System.out.println("\tTicker:"); System.out.println("\t\tExchange: " + positionEntry.getSymbol().getExchange()); System.out.println("\t\tSymbol: " + positionEntry.getSymbol().getSymbol()); System.out.println("\t\tFull Name: " + positionEntry.getSymbol().getFullName()); PositionData positionData = positionEntry.getPositionData(); System.out.println("\tPosition Data:"); System.out.printf("\t\tShare count: %.2f\n", positionData.getShares()); System.out.printf("\t\tPercent Gain is %.2f%%\n", positionData.getGainPercentage() * 100.0); System.out.println("\t\tReturns:"); System.out.printf("\t\t\tOne week: %.2f%%\n", positionData.getReturn1w() * 100.0); System.out.printf("\t\t\tFour weeks: %.2f%%\n", positionData.getReturn4w() * 100.0); System.out.printf("\t\t\tThree months: %.2f%%\n", positionData.getReturn3m() * 100.0); System.out.printf("\t\t\tYear-to-date: %.2f%%\n", positionData.getReturnYTD() * 100.0); System.out.printf("\t\t\tOne year: %.2f%%\n", positionData.getReturn1y() * 100.0); System.out.printf("\t\t\tThree years: %.2f%%\n", positionData.getReturn3y() * 100.0); System.out.printf("\t\t\tFive years: %.2f%%\n", positionData.getReturn5y() * 100.0); System.out.printf("\t\t\tOverall: %.2f%%\n", positionData.getReturnOverall() * 100.0); if (positionData.getCostBasis() == null) { System.out.println("\t\tCost Basis not specified"); } else { for (int i = 0; i < positionData.getCostBasis().getMoney().size(); i++) { Money m = positionData.getCostBasis().getMoney().get(i); System.out.printf("\t\tThis position cost %.2f %s.\n", m.getAmount(), m.getCurrencyCode()); } } if (positionData.getDaysGain() == null) { System.out.println("\t\tDay's Gain not specified"); } else { for (int i = 0; i < positionData.getDaysGain().getMoney().size(); i++) { Money m = positionData.getDaysGain().getMoney().get(i); System.out.printf( "\t\tThis position made %.2f %s today.\n", m.getAmount(), m.getCurrencyCode()); } } if (positionData.getGain() == null) { System.out.println("\t\tTotal Gain not specified"); } else { for (int i = 0; i < positionData.getGain().getMoney().size(); i++) { Money m = positionData.getGain().getMoney().get(i); System.out.printf( "\t\tThis position has a total gain of %.2f %s.\n", m.getAmount(), m.getCurrencyCode()); } } if (positionData.getMarketValue() == null) { System.out.println("\t\tMarket Value not specified"); } else { for (int i = 0; i < positionData.getMarketValue().getMoney().size(); i++) { Money m = positionData.getMarketValue().getMoney().get(i); System.out.printf( "\t\tThis position is worth %.2f %s.\n", m.getAmount(), m.getCurrencyCode()); } } }