Skip to content

Instantly share code, notes, and snippets.

@DhavalDalal
Last active April 15, 2019 03:46
Show Gist options
  • Save DhavalDalal/a44db41fa60f21e718426d28cedaaa4f to your computer and use it in GitHub Desktop.
Save DhavalDalal/a44db41fa60f21e718426d28cedaaa4f to your computer and use it in GitHub Desktop.
Portfolio Tracker.

Portfolio Tracker (Java)

  • Track individual stocks worth and
  • Net worth of the portfolio.

Pre-Requisites

  • It requires java-json lib. You can download java-json from http://mvnrepository.com/artifact/org.json/json/20180130
  • If you are within a proxy
    • you can use eclipse or idea settings.
    • If the above does not work, you can do it programmatically like this:
     System.setProperty("http.proxyHost", "host.company.com"));
     System.setProperty("http.proxyPort", "number"));
    
import java.util.*;
public class Main {
public static void main(String[] args) {
Portfolio portfolio = new Portfolio();
List<Double> prices = portfolio.track("AAPL", "MSFT", "GOOG");
System.out.println("Got Prices => " + prices);
}
}
import java.io.*;
import java.net.*;
import java.security.*;
import javax.net.ssl.*;
import org.json.JSONObject;
public class NationalStockService {
private final String urlTemplate = "https://national-stock-service.herokuapp.com/stocks/%s";
public NationalStockService() {
}
public double getPrice(final String ticker) throws Exception {
setupSSLContext();
final String httpsUrl = String.format(urlTemplate, ticker);
final HttpsURLConnection connection = openSSLConnectionTo(httpsUrl);
final String data = getData(connection);
final JSONObject stockDetails = new JSONObject(data);
return stockDetails.getDouble("price");
}
private void setupSSLContext() throws NoSuchAlgorithmException, KeyManagementException {
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0], new TrustManager[] { new Empty() }, new SecureRandom());
SSLContext.setDefault(ctx);
}
private String getData(final HttpsURLConnection connection) throws IOException {
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr);
String line = null;
StringBuilder data = new StringBuilder();
while ((line = reader.readLine()) != null) {
data.append(line);
}
return data.toString();
}
private HttpsURLConnection openSSLConnectionTo(String url) throws MalformedURLException, IOException {
final URL https = new URL(url);
HttpsURLConnection connection = (HttpsURLConnection) https.openConnection();
connection.setHostnameVerifier(new VerifiedOK());
return connection;
}
private static class VerifiedOK implements HostnameVerifier {
@Override
public boolean verify(String arg0, SSLSession arg1) { return true; }
}
private static class Empty implements X509TrustManager {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
throws java.security.cert.CertificateException { }
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1)
throws java.security.cert.CertificateException { }
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; }
}
}
import java.util.ArrayList;
import java.util.List;
public class Portfolio {
private NationalStockService stockService;
public Portfolio() {
stockService = new NationalStockService();
}
public List<Double> track(String ...tickers) throws Exception {
List<Double> prices = new ArrayList<Double>();
for(String ticker : tickers) {
prices.add(stockService.getPrice(ticker));
}
return prices;
}
}
@DhavalDalal
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment