Skip to content
This repository has been archived by the owner on Nov 14, 2017. It is now read-only.

Commit

Permalink
Cleanup formatting and remove unused imports
Browse files Browse the repository at this point in the history
  • Loading branch information
fearofcode committed Mar 26, 2013
1 parent 958d911 commit 9b3f536
Show file tree
Hide file tree
Showing 35 changed files with 1,195 additions and 1,187 deletions.
54 changes: 27 additions & 27 deletions src/main/java/org/wkh/bateman/fetch/GoogleQuoteFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,54 +14,54 @@
import org.joda.time.DateTime;

public class GoogleQuoteFetcher extends QuoteFetcher {

@Override
public String fetchQuotes(String symbol, int days, int interval)
public String fetchQuotes(String symbol, int days, int interval)
throws Exception {
String url = "http://www.google.com/finance/getprices?i=" + interval +
"&p=" + days + "d&f=d,o,h,l,c,v&df=cpct&q=" + symbol;

String url = "http://www.google.com/finance/getprices?i=" + interval
+ "&p=" + days + "d&f=d,o,h,l,c,v&df=cpct&q=" + symbol;

return fetchURLasString(url);
}

@Override
public List<Quote> parseQuotes(String quoteList, int interval) {
String[] lines = dropLines(quoteList, 6);

List<Quote> quotes = new ArrayList<Quote>();

for(String line: lines)
{
if(line.startsWith("TIMEZONE_OFFSET")) {

for (String line : lines) {
if (line.startsWith("TIMEZONE_OFFSET")) {
continue;
}

String[] parts = line.split(",");

String dateStr = parts[0];

DateTime date;
if(dateStr.startsWith("a")) {

if (dateStr.startsWith("a")) {
final String intPart = dateStr.substring(1);
final int timestamp = Integer.parseInt(intPart);
date = new DateTime((long)timestamp*1000L);
date = new DateTime((long) timestamp * 1000L);
} else {
DateTime previousDate = quotes.get(quotes.size()-1).getOpenDate();
DateTime previousDate = quotes.get(quotes.size() - 1).getOpenDate();
date = previousDate.plusSeconds(interval);
}
Quote quote = new Quote(date,
interval,
new BigDecimal(parts[4]),
new BigDecimal(parts[2]),
new BigDecimal(parts[3]),
new BigDecimal(parts[1]),

Quote quote = new Quote(date,
interval,
new BigDecimal(parts[4]),
new BigDecimal(parts[2]),
new BigDecimal(parts[3]),
new BigDecimal(parts[1]),
Integer.parseInt(parts[5]));

quotes.add(quote);
}

return quotes;
}
}
9 changes: 5 additions & 4 deletions src/main/java/org/wkh/bateman/fetch/Quote.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import org.joda.time.DateTime;

public class Quote {

private DateTime openDate;
private int interval;
private BigDecimal open;
private BigDecimal high;
private BigDecimal low;
private BigDecimal close;
private int volume;

public Quote(DateTime openDate, int interval, BigDecimal open, BigDecimal high, BigDecimal low, BigDecimal close, int volume) {
this.openDate = openDate;
this.interval = interval;
Expand All @@ -24,10 +25,10 @@ public Quote(DateTime openDate, int interval, BigDecimal open, BigDecimal high,

@Override
public String toString() {
return "Date = " + openDate + ", OHLC = " + open + "/" + high + "/" +
low + "/" + close + ", Volume = " + volume;
return "Date = " + openDate + ", OHLC = " + open + "/" + high + "/"
+ low + "/" + close + ", Volume = " + volume;
}

public DateTime getOpenDate() {
return openDate;
}
Expand Down
53 changes: 26 additions & 27 deletions src/main/java/org/wkh/bateman/fetch/QuoteCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,54 @@
import org.wkh.bateman.trade.TimeSeries;

public class QuoteCollection {
private static Logger logger = LoggerFactory.getLogger(QuoteCollection.class.getName());


private static Logger logger = LoggerFactory.getLogger(QuoteCollection.class.getName());
private Map<String, TimeSeries> quotes;

public void QuoteCollection() {}


public void QuoteCollection() {
}

public Map<String, TimeSeries> getQuotes() {
return quotes;
}

public TimeSeries convertQuoteToTimeSeries(List<Quote> quotes) throws Exception
{

public TimeSeries convertQuoteToTimeSeries(List<Quote> quotes) throws Exception {
TreeMap<DateTime, BigDecimal> prices = new TreeMap<DateTime, BigDecimal>();

TimeSeries series = new TimeSeries(prices);

for(Quote quote : quotes)
{

for (Quote quote : quotes) {
prices.put(quote.getOpenDate(), quote.getOpen());
}

return series;
}

public void fetchAllQuotes(QuoteFetcher fetcher) throws Exception {
logger.info("Starting quote fetching");

Properties properties = new Properties();
properties.load(ClassLoader.getSystemResourceAsStream("application.properties"));

String symbolPath = properties.getProperty("symbolPath");
int days = Integer.parseInt(properties.getProperty("days"));
int interval = Integer.parseInt(properties.getProperty("interval"));

Path paths = Paths.get(symbolPath);
List<String> symbols = Files.readAllLines(paths, StandardCharsets.UTF_8);
for(String symbol : symbols) {
symbol = symbol.replaceAll("\\s","");

for (String symbol : symbols) {
symbol = symbol.replaceAll("\\s", "");

logger.info("Fetching quotes for " + symbol);
List<Quote> quoteList = fetcher.parseQuotes(fetcher.fetchQuotes(symbol, days, interval), interval);
quotes.put(symbol, convertQuoteToTimeSeries(quoteList));
}

logger.info("Done fetching quotes");
}
public void fetchAllGoogleQuotes() throws Exception {
fetchAllQuotes(new GoogleQuoteFetcher());
}
}

public void fetchAllGoogleQuotes() throws Exception {
fetchAllQuotes(new GoogleQuoteFetcher());
}
}
9 changes: 5 additions & 4 deletions src/main/java/org/wkh/bateman/fetch/QuoteFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
import org.wkh.bateman.trade.TimeSeries;

public abstract class QuoteFetcher {

abstract String fetchQuotes(String symbol, int days, int interval) throws Exception;

abstract List<Quote> parseQuotes(String quoteList, int interval);
abstract List<Quote> parseQuotes(String quoteList, int interval);

public TimeSeries fetchAndParse(String symbol, int days, int interval) throws Exception {
String requestResult = fetchQuotes(symbol, days, interval);
List<Quote> parsed = parseQuotes(requestResult, interval);

QuoteCollection qc = new QuoteCollection();

return qc.convertQuoteToTimeSeries(parsed);
}

Expand Down
72 changes: 36 additions & 36 deletions src/main/java/org/wkh/bateman/fetch/YahooQuoteFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,86 +10,86 @@
public class YahooQuoteFetcher extends QuoteFetcher {

public TimeSeries fetchAndParseDaily(String symbol, int days) throws Exception {
return fetchAndParse(symbol, days, 60*60*24);
return fetchAndParse(symbol, days, 60 * 60 * 24);
}

public BigDecimal fetchBidAskSpread(String symbol) throws Exception {
String url = "http://download.finance.yahoo.com/d/quotes.csv?s=" + symbol + "&f=b2b3";

String result = fetchURLasString(url).replaceAll("\r\n", "").replaceAll("\n", "");

String[] parts = result.split(",");

return new BigDecimal(parts[0]).subtract(new BigDecimal(parts[1]));
}

@Override
String fetchQuotes(String symbol, int days, int interval) throws Exception {
String period;
switch(interval) {
case 60*60*24:

switch (interval) {
case 60 * 60 * 24:
period = "d";
break;
case 60*60*24*7:
case 60 * 60 * 24 * 7:
period = "w";
break;
default:
throw new Exception();
}

DateTime now = new DateTime();
DateTime startDate = now.minusDays(days);
int endMonth = now.getMonthOfYear()-1;

int endMonth = now.getMonthOfYear() - 1;
int endDay = now.getDayOfMonth();
int endYear = now.getYear();
int startMonth = startDate.getMonthOfYear()-1;

int startMonth = startDate.getMonthOfYear() - 1;
int startDay = startDate.getDayOfMonth();
int startYear = startDate.getYear();
String url = String.format("http://ichart.yahoo.com/table.csv?s=%s&a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&g=%s&ignore=.csv",

String url = String.format("http://ichart.yahoo.com/table.csv?s=%s&a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&g=%s&ignore=.csv",
symbol, startMonth, startDay, startYear, endMonth, endDay, endYear, period);

return fetchURLasString(url);
}

@Override
List<Quote> parseQuotes(String quoteList, int interval) {
List<Quote> quotes = new ArrayList<Quote>();

String[] lines = dropLines(quoteList, 1);
for(String line : lines) {

for (String line : lines) {
String[] parts = line.split(",");

// Date,Open,High,Low,Close,Volume,Adj Close

DateTime date = DateTime.parse(parts[0]);
Quote quote = new Quote(date,
interval,
new BigDecimal(parts[1]),
new BigDecimal(parts[2]),
new BigDecimal(parts[3]),
new BigDecimal(parts[6]),

Quote quote = new Quote(date,
interval,
new BigDecimal(parts[1]),
new BigDecimal(parts[2]),
new BigDecimal(parts[3]),
new BigDecimal(parts[6]),
Integer.parseInt(parts[5]));

quotes.add(quote);
}

return quotes;
}

public static void main(String[] args) throws Exception {
YahooQuoteFetcher fetcher = new YahooQuoteFetcher();
TimeSeries series = fetcher.fetchAndParseDaily("AAPL", 5);
for(Map.Entry<DateTime, BigDecimal> entry : series.getPrices().entrySet()) {

for (Map.Entry<DateTime, BigDecimal> entry : series.getPrices().entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}

System.out.println(fetcher.fetchBidAskSpread("AAPL"));
}
}
Loading

0 comments on commit 9b3f536

Please sign in to comment.