Advertisement
illpastethat

Main stock janca

Jan 24th, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.76 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.net.URL;
  10. import java.util.ArrayList;
  11. import java.util.HashMap;
  12. import java.util.Properties;
  13.  
  14. public class Main {
  15.     static HashMap<String, ArrayList<Stock>> stocks;
  16.  
  17.     public static void main(String args[]) {
  18.         stocks = new HashMap<String, ArrayList<Stock>>();
  19.         addStockList("MJNA");
  20.         addStockList("CBIS");
  21.         addStockList("HEMP");//adding some random stocks to fetch info about
  22.         addStockList("ZNGA");
  23.         addStockList("UA");
  24.         addStockList("F");
  25.         addStockList("GOOG");
  26.         // Load properties file.
  27.         File f = new File("C:\\Users\\adamc_000\\Desktop\\stocks.properties");
  28.         Properties props = new Properties();
  29.         if (f.exists()) {
  30.             try {
  31.                 props.load(new FileInputStream(f.toString()));
  32.             } catch (FileNotFoundException e) {
  33.                 e.printStackTrace();
  34.             } catch (IOException e) {
  35.                 e.printStackTrace();
  36.             }
  37.         }
  38.         for (int x = 1; x <= 100; x++) { //fetches the stock info 100 times.  every 20 seconds (end of loop)
  39.             for (String key : stocks.keySet()) {
  40.                 stocks.get(key).add(Parser.getStock(key));
  41.                 Stock temp = stocks.get(key).get(stocks.get(key).size() - 1);
  42.                 // PRINT (temp.toString()); Print stock info each time
  43.                 double confidence = compare(stocks.get(key));
  44.                 if ((confidence > 0) && (props.getProperty(temp.ticker) == null)) {
  45.                     println(temp.toString());
  46.                     println("Recommended BUY on " + temp.ticker + " Confidence: " + confidence);
  47.                     props.setProperty(temp.ticker, temp.price + "");
  48.                 } else if ((confidence < 0) && (props.getProperty(temp.ticker) != null)) {
  49.                     println(temp.toString());
  50.                     println("Recommended SELL on " + temp.ticker + " Confidence: " + confidence);
  51.                     println("Outcome- Buy: " + Double.parseDouble(props.getProperty(temp.ticker)) + " Sell: "+ temp.price + "Change " + (Double.parseDouble(props.getProperty(temp.ticker)) - temp.price));
  52.                     props.remove(temp.ticker);
  53.                 }
  54.             }
  55.             try {
  56.                 Thread.sleep(20 * 1000);
  57.             } catch (InterruptedException e) {
  58.             }
  59.         }
  60.         // Store the properties file
  61.         try {
  62.             props.store(new FileOutputStream(new File(f.toString())),
  63.                     "Records Price At Purchase");
  64.         } catch (FileNotFoundException e) {
  65.             e.printStackTrace();
  66.         } catch (IOException e) {
  67.             e.printStackTrace();
  68.         }
  69.     }
  70.  
  71.     static Double compare(ArrayList<Stock> arrayList) {
  72.         //analyzes the ability to increase in future by comparing all stocks in arrayList
  73.         double confidence = 0;
  74.         int size = arrayList.size();
  75.         if (size < 3) {
  76.             return confidence;
  77.         }
  78.         Stock s1 = arrayList.get(size - 1);
  79.         Stock s2 = arrayList.get(size - 2);
  80.         Stock s3 = arrayList.get(size - 3);
  81.         //TODO: 1. perfect this algorithm 2. ?? 3. Profit.
  82.         if (s1.change > s2.change) {
  83.             confidence += .25;
  84.             if (s2.change > s3.change) {
  85.                 confidence += .75;
  86.             }
  87.         }
  88.         if (s2.change < s1.change) {
  89.             confidence -= .25;
  90.             if (s3.change < s2.change) {
  91.                 confidence -= .75;
  92.             }
  93.         }
  94.         return confidence;
  95.     }
  96.  
  97.     static ArrayList<Stock> addStockList(String ticker) {
  98.         ArrayList<Stock> stockList = new ArrayList<Stock>();
  99.         stocks.put(ticker, stockList);
  100.         return stockList;
  101.     }
  102.  
  103.     static class Parser {
  104.         public static String get(String xml, String DATA_TYPE) {
  105.             // DATA_TYPE options
  106.             // {pretty_symbol,company,exchange,last,high,low,volume,avg_volume,market_cap,open,...
  107.             // y_close,change,perc_change,trade_timestamp,symbol_url,chart_url,isld_last,isld_trade_date_utc,isld_trade_time_utc}
  108.             int x = xml.indexOf('"', xml.indexOf("<" + DATA_TYPE));
  109.             return xml.substring(x + 1, xml.indexOf('"', x + 1));
  110.         }
  111.  
  112.         static double d(String str) {
  113.             if (str.startsWith("+"))
  114.                 str = str.substring(1);
  115.             return Double.parseDouble(str);
  116.         }
  117.  
  118.         public static Stock getStock(String ticker) {
  119.             try {
  120.                 URL url = new URL("http://www.google.com/ig/api?stock="
  121.                         + ticker);
  122.                 String xml = create(url.openStream()).readLine();
  123.                 HashMap<String, Object> data = new HashMap<String, Object>();
  124.                 data.put("ticker", ticker);
  125.                 data.put("company", get(xml, "company"));
  126.                 data.put("price", d(get(xml, "last")));
  127.                 data.put("timestamp", get(xml, "trade_timestamp"));
  128.                 data.put("change", d(get(xml, "change")));
  129.                 data.put("percent", d(get(xml, "perc_change")));
  130.                 return new Stock(data);
  131.             } catch (Exception ex) {
  132.                 ex.printStackTrace();
  133.             }
  134.             return null;
  135.         }
  136.     }
  137.  
  138.     static BufferedReader create(InputStream in) {
  139.         return new BufferedReader(new InputStreamReader(in));
  140.     }
  141.  
  142.     public static void print(Object o) {
  143.         System.out.print(o);
  144.     }
  145.  
  146.     public static void println(Object o) {
  147.         System.out.println(o);
  148.     }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement