Skip to content

Commit

Permalink
Merge pull request #1 from StenAL/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
StenAL authored Mar 25, 2019
2 parents 0367cf6 + 91f76e2 commit 9e0c856
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 159 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ For functional usage navigate to "Help -> Getting Started" from the menu bar of

* [Java 8](https://www.oracle.com/technetwork/java/javase/overview/java8-2100321.html) - The programming language used
* [Maven](https://maven.apache.org/) - Dependency Management, project building and deployment
* [Alpha Vantage API](https://www.alphavantage.co/) - API for fetching stock data
* [Alpha Vantage API](https://www.alphavantage.co/) - API for fetching stock price data
* [Quotes API for Yahoo Finance] (https://financequotes-api.com/) - API for fetching additional stock data (e.g. currency it's traded in)
* [ECB SDMX 2.1 RESTful web service](https://sdw-wsrest.ecb.europa.eu/help/) - API for fetching currency data

## Authors
Expand Down
24 changes: 13 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>Laane.Sten</groupId>
<artifactId>StockTracker</artifactId>
<version>1.2.2</version>
<version>1.3.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Expand All @@ -31,7 +31,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.1</version>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -45,10 +45,14 @@
<version>5.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.2</version>
<scope>test</scope>
<groupId>com.yahoofinance-api</groupId>
<artifactId>YahooFinanceAPI</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.26</version>
</dependency>
</dependencies>
<scm>
Expand All @@ -61,7 +65,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
Expand All @@ -71,11 +75,11 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<!-- JUnit 5 requires Surefire version 2.22.0 or higher -->
<version>2.22.0</version>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
Expand Down Expand Up @@ -169,6 +173,4 @@
</plugin>
</plugins>
</build>


</project>
15 changes: 11 additions & 4 deletions src/main/java/stocktracker/CurrencyRateFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.Arrays;
import java.util.List;

//TODO: Pad rate to four places after decimal
class CurrencyRateFetcher {

private final XMLParser xmlParser;
Expand Down Expand Up @@ -73,6 +72,9 @@ private void downloadXMLFile(URL url) throws IOException {
connection.setRequestProperty("Accept", "text/xml");

System.out.println("Response code: " + connection.getResponseCode());
if (connection.getResponseCode() != 200) {
System.out.println("Error fetching '" +currencyCode + "'");
}
String readStream = readStream(connection.getInputStream());
try {
List<String> lines = Arrays.asList(readStream.split("\n"));
Expand Down Expand Up @@ -109,10 +111,15 @@ private List<String> parse() {

Element eElement = (Element) nNode;

Element date = (Element) eElement.getElementsByTagName("ObsDimension").item(0);
Element exchangeRate = (Element) eElement.getElementsByTagName("ObsValue").item(0);
Element dateElement = (Element) eElement.getElementsByTagName("ObsDimension").item(0);
Element exchangeRateElement = (Element) eElement.getElementsByTagName("ObsValue").item(0);

String line = date.getAttribute("value") + "," + exchangeRate.getAttribute("value");
// Padding with trailing zeroes:
String exchangeRate = exchangeRateElement.getAttribute("value");
while (exchangeRate.split("\\.")[1].length() < 4) {
exchangeRate = exchangeRate.concat("0");
}
String line = dateElement.getAttribute("value") + "," + exchangeRate;
dataList.add(line);
}
}
Expand Down
34 changes: 17 additions & 17 deletions src/main/java/stocktracker/DataAggregator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
import java.util.Collections;
import java.util.List;


//TODO: add unit tests
class DataAggregator {
// TODO: Padding money and stock decimal places with zeroes
public static void main(String[] args) throws IOException {
test();
}
Expand All @@ -21,8 +22,8 @@ private static void test() throws IOException {
calculateMoney(testList, testAmounts);
}

static void calculateMoney(List<String> ticker_currency, List<Number> stockAmounts) throws IOException {
aggregate(ticker_currency);
static void calculateMoney(List<String> tickers, List<Number> stockAmounts) throws IOException {
aggregate(tickers);
List<String> finalData = FileManager.readLines(StockTracker.PATH + "aggregated_temp.csv");
List<String> dateMoney = new ArrayList<>();
for (String line: finalData) {
Expand All @@ -32,33 +33,32 @@ static void calculateMoney(List<String> ticker_currency, List<Number> stockAmoun
double stockPrice = Double.parseDouble(components[i]);
double currencyRate = Double.parseDouble(components[i+1]);

//System.out.println(stockPrice + " " + currencyRate);
money += stockPrice/currencyRate * stockAmounts.get((i-1)/2).doubleValue();
}
money = Math.round(money * 100D) / 100D;
dateMoney.add(line + "," + money);
// Padding with trailing zeroes:
String paddedMoney = "" + money;
while (paddedMoney.split("\\.")[1].length() < 2) {
paddedMoney = paddedMoney.concat("0");
}
dateMoney.add(line + "," + paddedMoney);
}
FileManager.writeList(StockTracker.PATH + "aggregated_with_money_temp.csv", dateMoney);
}

private static void aggregate(List<String> ticker_currency) throws IOException {
String workingDir = StockTracker.PATH;
for (String combination: ticker_currency) {
aggregate(combination);
}
private static void aggregate(List<String> tickers) throws IOException {
List<String> data;
try {
String dest = workingDir + "aggregated_temp.csv";
data = FileManager.readLines(workingDir + "/" + ticker_currency.get(0) + "_temp.csv");
for (int i = 1; i < ticker_currency.size(); i++) {
List<String> fileLines = FileManager.readLines(workingDir + "\\" + ticker_currency.get(i) + "_temp.csv");
data = FileManager.readLines(StockTracker.PATH + tickers.get(0) + "_currency_temp.csv");
for (int i = 1; i < tickers.size(); i++) {
List<String> fileLines = FileManager.readLines(StockTracker.PATH + tickers.get(i) + "_currency_temp.csv");
for (int j = 0; j < fileLines.size(); j++) {
String stockPrice = fileLines.get(j).split(",")[1];
String currencyRate = fileLines.get(j).split(",")[2];
data.set(j, data.get(j) + "," + stockPrice + "," + currencyRate);
}
}
FileManager.writeList(dest, data);
FileManager.writeList(StockTracker.PATH + "aggregated_temp.csv", data);
}catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -70,7 +70,7 @@ private static void aggregate(List<String> ticker_currency) throws IOException {
* on dates with no values. If the first day of the whole file happens to be a market
* holiday then we use the next available day's close value instead.
*/
private static void aggregate(String ticker_currency) throws IOException {
static void aggregate(String ticker_currency) throws IOException {
String workingDir = StockTracker.PATH;
String ticker = ticker_currency.split("_")[0];
String currency = ticker_currency.split("_")[1];
Expand Down Expand Up @@ -121,7 +121,7 @@ private static void aggregate(String ticker_currency) throws IOException {
}


String dest = StockTracker.PATH + ticker + "_" + currency + "_temp.csv";
String dest = StockTracker.PATH + ticker + "_currency_temp.csv";
List<String> writeList = new ArrayList<>();
for (int i = 0; i < aggregateDates.size(); i++) {
writeList.add(aggregateDates.get(i) + "," + stockRates.get(i) + "," + currencyRates.get(i));
Expand Down
32 changes: 22 additions & 10 deletions src/main/java/stocktracker/StockInfoFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,32 @@ private static Map<String, String> fetchData(String ticker, LocalDate startDate,
if (stock.getSplitCoefficient() != 1) {
splitCoefficient *= stock.getSplitCoefficient();
}
double money = Math.round(stock.getClose()*splitCoefficient*100)/100.0;
dateCloses.put("" + entryDate, "" + money);

// Padding with trailing zeroes:
String actualPrice = "" + Math.round(stock.getClose()*splitCoefficient*100)/100.0;
while (actualPrice.split("\\.")[1].length() < 2) {
actualPrice = actualPrice.concat("0");
}

dateCloses.put("" + entryDate, "" + actualPrice);
}
}
List<String> oldConfig = FileManager.readLines(StockTracker.PATH + "save_config.csv");
List<String> newConfig = new ArrayList<>();
for (String line: oldConfig) {
if (line.startsWith(ticker) && splitCoefficient != 1) {
String[] splitLine = line.split(" ");
line = splitLine[0] + "," + splitLine[1] + "," + splitCoefficient;

// needed for first time startup when no config file exists yet
try {
List<String> oldConfig = FileManager.readLines(StockTracker.PATH + "save_config.csv");
List<String> newConfig = new ArrayList<>();
for (String line: oldConfig) {
if (line.startsWith(ticker) && splitCoefficient != 1) {
String[] splitLine = line.split(" ");
line = splitLine[0] + "," + splitLine[1] + "," + splitCoefficient;
}
newConfig.add(line);
}
newConfig.add(line);
FileManager.writeList(StockTracker.PATH + "save_config.csv", newConfig);
} catch (Exception e) {
//e.printStackTrace();
}
FileManager.writeList(StockTracker.PATH + "save_config.csv", newConfig);
return dateCloses;
}

Expand Down
Loading

0 comments on commit 9e0c856

Please sign in to comment.