Skip to content

Commit

Permalink
Merge branch 'pr/6' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
sstrickx committed Feb 15, 2015
2 parents ecd6afd + c72cfc1 commit 1f8838d
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 194 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ Please check the javadoc (available in dist directory) to get a complete overvie
```java
Stock stock = YahooFinance.get("INTC");

double price = stock.getQuote().getPrice();
double change = stock.getQuote().getChangeInPercent();
double peg = stock.getStats().getPeg();
double dividend = stock.getDividend().getAnnualYieldPercent();
BigDecimal price = stock.getQuote().getPrice();
BigDecimal change = stock.getQuote().getChangeInPercent();
BigDecimal peg = stock.getStats().getPeg();
BigDecimal dividend = stock.getDividend().getAnnualYieldPercent();

stock.print();
```
Expand Down
125 changes: 85 additions & 40 deletions src/yahoofinance/Utils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package yahoofinance;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand All @@ -16,6 +17,11 @@
*/
public class Utils {

public static final BigDecimal HUNDRED = new BigDecimal(100);
public static final BigDecimal THOUSAND = new BigDecimal(1000);
public static final BigDecimal MILLION = new BigDecimal(1000000);
public static final BigDecimal BILLION = new BigDecimal(1000000000);

public static String join(String[] data, String d) {
if (data.length == 0) {
return "";
Expand All @@ -28,15 +34,45 @@ public static String join(String[] data, String d) {
}
return sb.append(data[i]).toString();
}

private static String cleanNumberString(String data) {
return Utils.join(data.trim().split(","), "");
}

private static boolean isParseable(String data) {
return !(data == null || data.equals("N/A") || data.equals("-") || data.equals(""));
}


public static BigDecimal getBigDecimal(String data) {
BigDecimal result = BigDecimal.ZERO;
if (!Utils.isParseable(data)) {
return result;
}
try {
data = Utils.cleanNumberString(data);
char lastChar = data.charAt(data.length() - 1);
BigDecimal multiplier = BigDecimal.ONE;
switch (lastChar) {
case 'B':
data = data.substring(0, data.length() - 1);
multiplier = BILLION;
break;
case 'M':
data = data.substring(0, data.length() - 1);
multiplier = MILLION;
break;
case 'K':
data = data.substring(0, data.length() - 1);
multiplier = THOUSAND;
break;
}
result = new BigDecimal(data).multiply(multiplier);
} catch (NumberFormatException e) {
YahooFinance.logger.log(Level.INFO, "Failed to parse: " + data, e);
}
return result;
}

public static double getDouble(String data) {
double result = 0.00;
if (!Utils.isParseable(data)) {
Expand Down Expand Up @@ -80,43 +116,52 @@ public static int getInt(String data) {
}
return result;
}

public static long getLong(String data) {
long result = 0;
if (!Utils.isParseable(data)) {
return result;
}
try {
data = Utils.cleanNumberString(data);
data = Utils.cleanNumberString(data);
result = Long.parseLong(data);
} catch (NumberFormatException e) {
YahooFinance.logger.log(Level.INFO, "Failed to parse: " + data, e);
}
return result;
}

public static BigDecimal getPercent(BigDecimal numerator, BigDecimal denominator) {
if (denominator.equals(BigDecimal.ZERO)) {
return BigDecimal.ZERO;
}
return numerator.divide(denominator, 4, BigDecimal.ROUND_HALF_EVEN)
.multiply(HUNDRED).setScale(2, BigDecimal.ROUND_HALF_EVEN);
}

public static double getPercent(double numerator, double denominator) {
if(denominator == 0) {
if (denominator == 0) {
return 0;
}
return numerator / denominator;
return (numerator / denominator) * 100;
}

private static String getDividendDateFormat(String date) {
if(date.matches("[0-9][0-9]-...-[0-9][0-9]")) {
if (date.matches("[0-9][0-9]-...-[0-9][0-9]")) {
return "dd-MMM-yy";
} else if(date.matches("[0-9]-...-[0-9][0-9]")) {
} else if (date.matches("[0-9]-...-[0-9][0-9]")) {
return "d-MMM-yy";
} else {
return "MMM d";
}
}

/**
* Used to parse the dividend dates.
* Returns null if the date cannot be parsed.
*
* @param date String received that represents the date
* @return Calendar object representing the parsed date
* Used to parse the dividend dates. Returns null if the date cannot be
* parsed.
*
* @param date String received that represents the date
* @return Calendar object representing the parsed date
*/
public static Calendar parseDividendDate(String date) {
if (!Utils.isParseable(date)) {
Expand All @@ -129,40 +174,40 @@ public static Calendar parseDividendDate(String date) {
Calendar today = Calendar.getInstance(TimeZone.getTimeZone(YahooFinance.TIMEZONE));
Calendar parsedDate = Calendar.getInstance(TimeZone.getTimeZone(YahooFinance.TIMEZONE));
parsedDate.setTime(format.parse(date));
if(parsedDate.get(Calendar.YEAR) == 1970) {

if (parsedDate.get(Calendar.YEAR) == 1970) {
// Not really clear which year the dividend date is... making a reasonable guess.
int monthDiff = parsedDate.get(Calendar.MONTH) - today.get(Calendar.MONTH);
int year = today.get(Calendar.YEAR);
if(monthDiff > 6) {
if (monthDiff > 6) {
year -= 1;
} else if(monthDiff < -6) {
} else if (monthDiff < -6) {
year += 1;
}
parsedDate.set(Calendar.YEAR, year);
}

return parsedDate;
} catch(ParseException ex) {
} catch (ParseException ex) {
YahooFinance.logger.log(Level.SEVERE, ex.getMessage(), ex);
return null;
}
}

/**
* Used to parse the last trade date / time.
* Returns null if the date / time cannot be parsed.
*
* @param date String received that represents the date
* @param time String received that represents the time
* @return Calendar object with the parsed datetime
* Used to parse the last trade date / time. Returns null if the date / time
* cannot be parsed.
*
* @param date String received that represents the date
* @param time String received that represents the time
* @return Calendar object with the parsed datetime
*/
public static Calendar parseDateTime(String date, String time) {
String datetime = date + " " + time;
SimpleDateFormat format = new SimpleDateFormat("M/d/yyyy h:mma");
format.setTimeZone(TimeZone.getTimeZone(YahooFinance.TIMEZONE));
try {
if(Utils.isParseable(date) && Utils.isParseable(time)) {
if (Utils.isParseable(date) && Utils.isParseable(time)) {
Calendar c = Calendar.getInstance();
c.setTime(format.parse(datetime));
return c;
Expand All @@ -172,12 +217,12 @@ public static Calendar parseDateTime(String date, String time) {
}
return null;
}

public static Calendar parseHistDate(String date) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
format.setTimeZone(TimeZone.getTimeZone(YahooFinance.TIMEZONE));
try {
if(Utils.isParseable(date)) {
if (Utils.isParseable(date)) {
Calendar c = Calendar.getInstance();
c.setTime(format.parse(date));
return c;
Expand All @@ -187,11 +232,11 @@ public static Calendar parseHistDate(String date) {
}
return null;
}

public static String getURLParameters(Map<String, String> params) {
StringBuilder sb = new StringBuilder();
for(Entry<String, String> entry : params.entrySet()) {

for (Entry<String, String> entry : params.entrySet()) {
if (sb.length() > 0) {
sb.append("&");
}
Expand All @@ -206,20 +251,20 @@ public static String getURLParameters(Map<String, String> params) {
}
sb.append(String.format("%s=%s", key, value));
}
return sb.toString();
return sb.toString();
}

/**
* Strips the unwanted chars from a line returned in the CSV
* Strips the unwanted chars from a line returned in the CSV
* Used for parsing the FX CSV lines
*
* @param line the original CSV line
* @return the stripped line
*
* @param line the original CSV line
* @return the stripped line
*/
public static String stripOverhead(String line) {
return line.replaceAll("\"", "");
}

public static String unescape(String data) {
StringBuilder buffer = new StringBuilder(data.length());
for (int i = 0; i < data.length(); i++) {
Expand Down
10 changes: 5 additions & 5 deletions src/yahoofinance/histquotes/HistQuotesRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ private HistoricalQuote parseCSVLine(String line) {
String[] data = line.split(YahooFinance.QUOTES_CSV_DELIMITER);
return new HistoricalQuote(this.symbol,
Utils.parseHistDate(data[0]),
Utils.getDouble(data[1]),
Utils.getDouble(data[3]),
Utils.getDouble(data[2]),
Utils.getDouble(data[4]),
Utils.getDouble(data[6]),
Utils.getBigDecimal(data[1]),
Utils.getBigDecimal(data[3]),
Utils.getBigDecimal(data[2]),
Utils.getBigDecimal(data[4]),
Utils.getBigDecimal(data[6]),
Utils.getLong(data[5])
);
}
Expand Down
33 changes: 17 additions & 16 deletions src/yahoofinance/histquotes/HistoricalQuote.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

package yahoofinance.histquotes;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;

Expand All @@ -14,18 +15,18 @@ public class HistoricalQuote {

private Calendar date;

private double open;
private double low;
private double high;
private double close;
private BigDecimal open;
private BigDecimal low;
private BigDecimal high;
private BigDecimal close;

private double adjClose;
private BigDecimal adjClose;

private long volume;

public HistoricalQuote() {}

public HistoricalQuote(String symbol, Calendar date, double open, double low, double high, double close, double adjClose, long volume) {
public HistoricalQuote(String symbol, Calendar date, BigDecimal open, BigDecimal low, BigDecimal high, BigDecimal close, BigDecimal adjClose, long volume) {
this.symbol = symbol;
this.date = date;
this.open = open;
Expand All @@ -52,43 +53,43 @@ public void setDate(Calendar date) {
this.date = date;
}

public double getOpen() {
public BigDecimal getOpen() {
return open;
}

public void setOpen(double open) {
public void setOpen(BigDecimal open) {
this.open = open;
}

/**
*
* @return the intra-day low
*/
public double getLow() {
public BigDecimal getLow() {
return low;
}

public void setLow(double low) {
public void setLow(BigDecimal low) {
this.low = low;
}

/**
*
* @return the intra-day high
*/
public double getHigh() {
public BigDecimal getHigh() {
return high;
}

public void setHigh(double high) {
public void setHigh(BigDecimal high) {
this.high = high;
}

public double getClose() {
public BigDecimal getClose() {
return close;
}

public void setClose(double close) {
public void setClose(BigDecimal close) {
this.close = close;
}

Expand All @@ -100,11 +101,11 @@ public void setClose(double close) {
* that an investor earned if shares were purchased on that date.
* @return the adjusted close price
*/
public double getAdjClose() {
public BigDecimal getAdjClose() {
return adjClose;
}

public void setAdjClose(double adjClose) {
public void setAdjClose(BigDecimal adjClose) {
this.adjClose = adjClose;
}

Expand Down
Loading

0 comments on commit 1f8838d

Please sign in to comment.