/ src / Symbol.java
Symbol.java
  1  import java.io.IOException;
  2  import java.math.BigDecimal;
  3  import java.math.RoundingMode;
  4  import java.util.ArrayList;
  5  import java.util.Calendar;
  6  import java.util.List;
  7  
  8  import yahoofinance.Stock;
  9  import yahoofinance.YahooFinance;
 10  import yahoofinance.histquotes.HistoricalQuote;
 11  import yahoofinance.histquotes.Interval;
 12  
 13  public class Symbol extends Stock {
 14  	
 15  	private final int DAYS_HISTORY = 2000;
 16  	
 17  	// Keep historical data that we have already pulled to speed up getHistory method
 18  	private List<HistoricalQuote> history = new ArrayList<>(0);
 19  	
 20  	public Symbol(String symbol) throws IOException {
 21  		super(symbol);
 22  		Stock stock = YahooFinance.get(symbol);
 23  		setQuote(stock.getQuote());
 24  		setStats(stock.getStats());
 25  		setDividend(stock.getDividend());
 26  		
 27  		// Set history
 28  		Calendar from = Calendar.getInstance();
 29  		from.add(Calendar.DAY_OF_MONTH, -DAYS_HISTORY);
 30  
 31  		// Update list of historical data with new historical data
 32  		history = getHistory(from, Interval.DAILY);
 33  	}
 34  	
 35  	/**
 36  	 * Returns list of historical quotes using days from most to least recent
 37  	 * @author Michael Bick
 38  	 * @param daysAgo days ago the first quote is from
 39  	 * @param days amount of days of historical quotes
 40  	 * @return list of historical quotes from time period
 41  	 * @throws IOException
 42  	 */
 43  	public List<HistoricalQuote> getHistory(int daysAgo, int days) throws IOException {		
 44  		// Create an integer to hold the farthest back day requested
 45  		int fromDay = daysAgo + days;
 46  
 47  		// Filter the list down to what we need
 48  		// Only subtract 1 from first parameter because it is inclusive
 49  		return history.subList(daysAgo, fromDay);
 50  	}
 51  
 52  	/**
 53  	 * Returns one day of historical information
 54  	 * @author Michael Bick
 55  	 * @param daysAgo amount of days ago to get information from
 56  	 * @return historical quote from a given amount of days ago
 57  	 * @throws IOException
 58  	 */
 59  	public HistoricalQuote getDay(int daysAgo) throws IOException {
 60  		return getHistory(daysAgo, 1).get(0);
 61  	}
 62  
 63  	/**
 64  	 * Returns a historical adjusted closing price of a stock
 65  	 * @author Michael Bick
 66  	 * @param daysAgo amount of days ago to get the closing price from
 67  	 * @return historical adjusted closing price
 68  	 * @throws IOException
 69  	 */
 70  	public BigDecimal getAdjClose(int daysAgo) throws IOException {
 71  		return getDay(daysAgo).getAdjClose();
 72  	}
 73  	
 74  	/**
 75  	 * Returns a historical volume of shares traded
 76  	 * @author Michael Bick
 77  	 * @param daysAgo amount of days ago to get the volume from
 78  	 * @return volume of shares traded
 79  	 * @throws IOException
 80  	 */
 81  	public long getVolume(int daysAgo) throws IOException {
 82  		return getDay(daysAgo).getVolume();
 83  	}
 84  	
 85  	/**
 86  	 *  Returns a moving average from a stock's history
 87  	 * @author Michael Bick
 88  	 * @param daysAgo amount of days ago the moving average is from
 89  	 * @param days amount of days to use in the moving average
 90  	 * @return moving average
 91  	 * @throws IOException
 92  	 */
 93  	public BigDecimal getMA(int daysAgo, int days) throws IOException {
 94  		// Gets historical quotes
 95  		List<HistoricalQuote> quotes = getHistory(daysAgo, days);
 96  
 97  		// Calculate the moving average
 98  		BigDecimal ma = new BigDecimal(0);
 99  		for (HistoricalQuote quote : quotes) {
100  			ma = ma.add(quote.getAdjClose());
101  			// System.out.println(quote.getAdjClose());
102  			// System.out.println(ma);
103  		}
104  		ma = ma.divide(new BigDecimal(quotes.size()), 2, RoundingMode.HALF_UP); // Rounds the "regular" way to 2 decimal places
105  		
106  		return ma;
107  	}
108  	
109  	public BigDecimal getPrice() {
110  		return getQuote().getPrice();
111  	}
112  	/*
113  	public BigDecimal getDayHigh() {
114  		return getQuote().getDayHigh();
115  	}
116  	
117  	public BigDecimal getDayLow() {
118  		return getQuote().getDayLow();
119  	}
120  	public BigDecimal getFtWkHigh(){
121  		return getQuote().getYearHigh();
122  	}
123  	public BigDecimal getFtWkLow(){
124  		return getQuote().getYearLow();
125  	}
126  	public BigDecimal getEPS() {
127  		return getStats().getEps();
128  	}
129  	public long getNumberOfShares() {
130  		return getStats().getSharesOutstanding();
131  	}
132  	public long getVolume() {
133  		return getQuote().getVolume();
134  	}
135  	*/
136  }