Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@
</plugins>
</build>


<dependencies>

<dependency>
<groupId>com.yahoofinance-api</groupId>
<artifactId>YahooFinanceAPI</artifactId>
<version>3.15.0</version>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down
63 changes: 57 additions & 6 deletions src/main/java/stockanalyzer/ctrl/Controller.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,76 @@
package stockanalyzer.ctrl;

import stockanalyzer.downloader.Downloader;
import yahoofinance.YahooFinance;
import yahoofinance.histquotes.Interval;
import yahooApi.yahooFinanceIOException;
import yahoofinance.Stock;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;

public class Controller {

private Stock s;

public void process(String ticker) {
public void process(String ticker) throws yahooFinanceIOException {
System.out.println("Start process");

//TODO implement Error handling
try{

s = YahooFinance.get(ticker);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, -30);

var max = s.getHistory(cal, Interval.DAILY).stream()
.mapToDouble(i -> i.getClose().doubleValue())
.max()
.orElse(0.00d);

var avg = s.getHistory().stream()
.mapToDouble(i -> i.getClose().doubleValue())
.average()
.orElse(0.00d);


//TODO implement methods for
//1) Daten laden
//2) Daten Analyse
var amount = s.getHistory().stream()
.mapToDouble(i -> i.getClose().doubleValue())
.count();

//SIMPLE AUSGABE
System.out.println(ticker);
System.out.println("max: " + max);
System.out.println("avg: " + avg);
System.out.println("amount: " + amount);

} catch (IOException e){
e.printStackTrace();
}
}


public Object getData(String searchString) {


/*
YahooFinance yahooFinance = new YahooFinance();
List<String> tickers = Arrays.asList("searchString");
YahooResponse response = yahooFinance.getCurrentData(tickers);
QuoteResponse quotes = response.getQuoteResponse();
quotes.getResult().stream().forEach(quote -> System.out.println(quote.getAsk()));

yahooFinance.getCurrentData(Arrays.asList("OMV.VI",
"EBS.VI","DOC.VI","SBO.VI","RBI.VI","VIG.VI","TKA.VI","VOE.VI","FACC.VI","ANDR.VI","VER.VI",
"WIE.VI","CAI.VI","BG.VI","POST.VI","LNZ.VI","UQA.VI","SPI.VI","ATS.VI","IIA.VI"));
*/

return null;
}

public void downloadtickers(List <String> tickers, Downloader downloader) throws yahooFinanceIOException {
downloader.process(tickers);
}


public void closeConnection() {

Expand Down
41 changes: 41 additions & 0 deletions src/main/java/stockanalyzer/downloader/Downloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package stockanalyzer.downloader;

import yahooApi.YahooFinance;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public abstract class Downloader {

public static final String DIRECTORY_DOWNLOAD = "./download/";
private static final String JSON_EXTENSION = ".json";

public abstract int process(List<String> urls);

public String saveJson2File(String ticker) {
String fileName = "";
BufferedWriter writer= null;
try {
YahooFinance yahooFinance = new YahooFinance();
String json = yahooFinance.requestData(new ArrayList<>(Collections.singleton(ticker)));

fileName = DIRECTORY_DOWNLOAD +ticker + JSON_EXTENSION;

writer = new BufferedWriter(new FileWriter(fileName));
writer.write(json);
writer.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
Objects.requireNonNull(writer).close();
} catch (IOException e) {
e.printStackTrace();
}
}
return fileName;
}
}
44 changes: 44 additions & 0 deletions src/main/java/stockanalyzer/downloader/ParallelDownloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package stockanalyzer.downloader;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class ParallelDownloader extends Downloader{
@Override
public int process(List<String> tickers) {

ExecutorService executor = Executors.newCachedThreadPool();
List<Future> futures = new ArrayList<>();

for(String ticker : tickers){
futures.add(executor.submit(new Task(ticker)));
}

for(int i = 0; i < tickers.size(); i++) {
Future<String> future = futures.get(i);
try{
System.out.println("downloaded: " + tickers.get(i) + " to " + future.get());
} catch (InterruptedException e){
e.printStackTrace();
} catch (ExecutionException e){
e.printStackTrace();
}
}
return futures.size();
}

static class Task implements Callable<String>{
private String ticker;

public Task(String ticker){
this.ticker = ticker;
}

@Override
public String call() throws Exception {
SequentialDownloader sd = new SequentialDownloader();
return sd.saveJson2File(ticker);
}
}
}
17 changes: 17 additions & 0 deletions src/main/java/stockanalyzer/downloader/SequentialDownloader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package stockanalyzer.downloader;

import java.util.List;

public class SequentialDownloader extends Downloader {

@Override
public int process(List<String> tickers) {
int count = 0;
for (String ticker : tickers) {
String fileName = saveJson2File(ticker);
if(fileName != null)
count++;
}
return count;
}
}
79 changes: 71 additions & 8 deletions src/main/java/stockanalyzer/ui/UserInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,104 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import stockanalyzer.ctrl.Controller;
import stockanalyzer.downloader.Downloader;
import stockanalyzer.downloader.ParallelDownloader;
import stockanalyzer.downloader.SequentialDownloader;
import yahooApi.yahooFinanceIOException;

public class UserInterface
{

private Controller ctrl = new Controller();

public void getDataFromCtrl1(){
ctrl.process("ABC");
try {
ctrl.process("MSFT");
} catch (yahooFinanceIOException e) {
e.printStackTrace();
}
}

public void getDataFromCtrl2(){
try {
ctrl.process("GOOG");
} catch (yahooFinanceIOException e) {
e.printStackTrace();
}
}

public void getDataFromCtrl3(){

try {
ctrl.process("SONY");
} catch (yahooFinanceIOException e) {
e.printStackTrace();
}
}
public void getDataFromCtrl4(){

}

public void getDataForCustomInput() {

Scanner sc = new Scanner(System.in);
System.out.print("Geben Sie die gewünschte Aktie ein: ");
String userinputstock = sc.next();

try {
ctrl.process(userinputstock);
} catch (yahooFinanceIOException e) {
e.printStackTrace();
}

}

public void getDownloadedTickers(){
try {
Downloader downloader;
List<String> tickers = new ArrayList<>();

Scanner sc = new Scanner(System.in);
System.out.println("Geben Sie die gewünschten Aktie ein und tippen Sie nach jeder Angabe auf [ENTER]. Zum Bestätigen drücken Sie [.]: ");

while (true) {
String ticker = sc.next();
if (ticker.equals("."))
break;
else{
tickers.add(ticker);
}
}

System.out.print("Für sequenziellen Download: [S] ; Für parallelen Download: [P]: ");

String downloadselection = sc.next();
if (downloadselection.equals("S") || downloadselection.equals("s")){
downloader = new SequentialDownloader();
System.out.println("Sequential Download started...");
}
else {
downloader = new ParallelDownloader();
System.out.println("Parallel Download started...");
}

ctrl.downloadtickers(tickers, downloader);

} catch (yahooFinanceIOException e) {
e.printStackTrace();
}
}



public void start() {
Menu<Runnable> menu = new Menu<>("User Interfacx");
Menu<Runnable> menu = new Menu<>("User Interface");
menu.setTitel("Wählen Sie aus:");
menu.insert("a", "Choice 1", this::getDataFromCtrl1);
menu.insert("b", "Choice 2", this::getDataFromCtrl2);
menu.insert("c", "Choice 3", this::getDataFromCtrl3);
menu.insert("d", "Choice User Imput:",this::getDataForCustomInput);
menu.insert("z", "Choice User Imput:",this::getDataFromCtrl4);
menu.insert("e", "Download Tickers:",this::getDownloadedTickers);
menu.insert("q", "Quit", null);
Runnable choice;
while ((choice = menu.exec()) != null) {
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/yahooApi/YahooFinance.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected JsonObject convert(String jsonResponse) {
return jo;
}

public void fetchAssetName(Asset asset) {
public void fetchAssetName(Asset asset) throws yahooFinanceIOException {
YahooFinance yahoo = new YahooFinance();
List<String> symbols = new ArrayList<>();
symbols.add(asset.getSymbol());
Expand All @@ -73,7 +73,7 @@ private String extractName(JsonObject jo) {
return returnName;
}

public YahooResponse getCurrentData(List<String> tickers) {
public YahooResponse getCurrentData(List<String> tickers) throws yahooFinanceIOException {
String jsonResponse = requestData(tickers);
ObjectMapper objectMapper = new ObjectMapper();
YahooResponse result = null;
Expand All @@ -84,4 +84,6 @@ public YahooResponse getCurrentData(List<String> tickers) {
}
return result;
}
}

}

1 change: 1 addition & 0 deletions src/main/java/yahooApi/beans/Asset.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void setDate(Date date) {

public Quote getLastQuote() {
//TODO

return null;
}

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/yahooApi/yahooFinanceIOException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package yahooApi;

public class yahooFinanceIOException extends Exception {

public yahooFinanceIOException(String message)
{
super(message);
}
}
2 changes: 1 addition & 1 deletion src/test/java/yahooApi/YahooAPIExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class YahooAPIExample {

public static void main(String[] args){
public static void main(String[] args) throws yahooFinanceIOException{

YahooFinance yahooFinance = new YahooFinance();
List<String> tickers = Arrays.asList("AMZN", "TSLA", "GOOG");
Expand Down