Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Properties;
- public class Main {
- static HashMap<String, ArrayList<Stock>> stocks;
- public static void main(String args[]) {
- stocks = new HashMap<String, ArrayList<Stock>>();
- addStockList("MJNA");
- addStockList("CBIS");
- addStockList("HEMP");//adding some random stocks to fetch info about
- addStockList("ZNGA");
- addStockList("UA");
- addStockList("F");
- addStockList("GOOG");
- // Load properties file.
- File f = new File("C:\\Users\\adamc_000\\Desktop\\stocks.properties");
- Properties props = new Properties();
- if (f.exists()) {
- try {
- props.load(new FileInputStream(f.toString()));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- for (int x = 1; x <= 100; x++) { //fetches the stock info 100 times. every 20 seconds (end of loop)
- for (String key : stocks.keySet()) {
- stocks.get(key).add(Parser.getStock(key));
- Stock temp = stocks.get(key).get(stocks.get(key).size() - 1);
- // PRINT (temp.toString()); Print stock info each time
- double confidence = compare(stocks.get(key));
- if ((confidence > 0) && (props.getProperty(temp.ticker) == null)) {
- println(temp.toString());
- println("Recommended BUY on " + temp.ticker + " Confidence: " + confidence);
- props.setProperty(temp.ticker, temp.price + "");
- } else if ((confidence < 0) && (props.getProperty(temp.ticker) != null)) {
- println(temp.toString());
- println("Recommended SELL on " + temp.ticker + " Confidence: " + confidence);
- println("Outcome- Buy: " + Double.parseDouble(props.getProperty(temp.ticker)) + " Sell: "+ temp.price + "Change " + (Double.parseDouble(props.getProperty(temp.ticker)) - temp.price));
- props.remove(temp.ticker);
- }
- }
- try {
- Thread.sleep(20 * 1000);
- } catch (InterruptedException e) {
- }
- }
- // Store the properties file
- try {
- props.store(new FileOutputStream(new File(f.toString())),
- "Records Price At Purchase");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- static Double compare(ArrayList<Stock> arrayList) {
- //analyzes the ability to increase in future by comparing all stocks in arrayList
- double confidence = 0;
- int size = arrayList.size();
- if (size < 3) {
- return confidence;
- }
- Stock s1 = arrayList.get(size - 1);
- Stock s2 = arrayList.get(size - 2);
- Stock s3 = arrayList.get(size - 3);
- //TODO: 1. perfect this algorithm 2. ?? 3. Profit.
- if (s1.change > s2.change) {
- confidence += .25;
- if (s2.change > s3.change) {
- confidence += .75;
- }
- }
- if (s2.change < s1.change) {
- confidence -= .25;
- if (s3.change < s2.change) {
- confidence -= .75;
- }
- }
- return confidence;
- }
- static ArrayList<Stock> addStockList(String ticker) {
- ArrayList<Stock> stockList = new ArrayList<Stock>();
- stocks.put(ticker, stockList);
- return stockList;
- }
- static class Parser {
- public static String get(String xml, String DATA_TYPE) {
- // DATA_TYPE options
- // {pretty_symbol,company,exchange,last,high,low,volume,avg_volume,market_cap,open,...
- // y_close,change,perc_change,trade_timestamp,symbol_url,chart_url,isld_last,isld_trade_date_utc,isld_trade_time_utc}
- int x = xml.indexOf('"', xml.indexOf("<" + DATA_TYPE));
- return xml.substring(x + 1, xml.indexOf('"', x + 1));
- }
- static double d(String str) {
- if (str.startsWith("+"))
- str = str.substring(1);
- return Double.parseDouble(str);
- }
- public static Stock getStock(String ticker) {
- try {
- URL url = new URL("http://www.google.com/ig/api?stock="
- + ticker);
- String xml = create(url.openStream()).readLine();
- HashMap<String, Object> data = new HashMap<String, Object>();
- data.put("ticker", ticker);
- data.put("company", get(xml, "company"));
- data.put("price", d(get(xml, "last")));
- data.put("timestamp", get(xml, "trade_timestamp"));
- data.put("change", d(get(xml, "change")));
- data.put("percent", d(get(xml, "perc_change")));
- return new Stock(data);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- return null;
- }
- }
- static BufferedReader create(InputStream in) {
- return new BufferedReader(new InputStreamReader(in));
- }
- public static void print(Object o) {
- System.out.print(o);
- }
- public static void println(Object o) {
- System.out.println(o);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement