Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.sx.finance; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.table.DefaultTableModel; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; /** * * @author Administrator */ public class OldYahooFinanceJFrame extends javax.swing.JFrame { /** * */ private static final long serialVersionUID = 1L; /** * Creates new form NewJFrame */ public OldYahooFinanceJFrame() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jTextField1 = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); jScrollPane2 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); jTextField2 = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle(""); setName(""); // NOI18N jTextField1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField1ActionPerformed(evt); } }); jButton1.setText(""); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jTable1.setModel(new javax.swing.table.DefaultTableModel( new Object[][] { { null, null, null, null, null, null, null, null }, { null, null, null, null, null, null, null, null }, { null, null, null, null, null, null, null, null } }, this.title)); jScrollPane2.setViewportView(jTable1); jLabel1.setText(""); jLabel2.setText(""); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addGap(103, 103, 103) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel2).addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(78, 78, 78)) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(27, 27, 27).addComponent( jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 522, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup().addGap(251, 251, 251) .addComponent(jButton1))) .addContainerGap(26, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap(34, Short.MAX_VALUE).addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGap(35, 35, 35) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel2)) .addGap(44, 44, 44).addComponent(jButton1).addGap(60, 60, 60).addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(76, 76, 76))); pack(); }// </editor-fold>//GEN-END:initComponents private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed String gupiao = jTextField1.getText(); String sd = jTextField2.getText(); int dates = Integer.parseInt(sd); ArrayList<?> listDataFromWeb = null; try { listDataFromWeb = YahooStockData.getStockCsvData(gupiao, dates); } catch (Exception ex) { Logger.getLogger(OldYahooFinanceJFrame.class.getName()).log(Level.SEVERE, null, ex); return; } ArrayList<List<Object>> listDataRaw = convertFromWeb2Raw(listDataFromWeb); DefaultTableModel defaultTableModle = convertFromRaw2SwingTableModel(listDataRaw); jTable1.setModel(defaultTableModle); writeExcelFile(listDataRaw); }//GEN-LAST:event_jButton1ActionPerformed public void writeExcelFile(ArrayList<List<Object>> listDataRaw) { // webbookExcel HSSFWorkbook wb = new HSSFWorkbook(); // webbooksheet,Excelsheet HSSFSheet sheet = wb.createSheet("Data"); // sheet0,poiExcelshort HSSFRow row = sheet.createRow((int) 0); // HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // for (int i = 0; i < title.length; i++) { HSSFCell cell = row.createCell(i); cell.setCellValue(title[i]); cell.setCellStyle(style); } for (int i = 0; i < listDataRaw.size(); i++) { row = sheet.createRow(i + 1); for (int j = 0; j < listDataRaw.get(i).size(); j++) { Object cellData = listDataRaw.get(i).get(j); if (cellData instanceof Long) { row.createCell(j).setCellValue(((Long) cellData).longValue()); } else if (cellData instanceof Double) { row.createCell(j).setCellValue(((Double) cellData).doubleValue()); } else if (cellData instanceof Date) { row.createCell(j).setCellValue((Date) cellData); } else if (cellData instanceof Integer) { row.createCell(j).setCellValue(((Integer) cellData).intValue()); } else { row.createCell(j).setCellValue((String) cellData); } } } try { FileOutputStream fout = new FileOutputStream("D:/work/myprojects/yahooFinance/output/data.xls"); wb.write(fout); fout.close(); } catch (Exception e) { e.printStackTrace(); } } public DefaultTableModel convertFromRaw2SwingTableModel(ArrayList<List<Object>> listDataRaw) { DefaultTableModel defaultTableModle = new DefaultTableModel(); for (int i = 0; i < title.length; i++) { defaultTableModle.addColumn(title[i]); } for (int i = 0; i < listDataRaw.size(); i++) { List<?> rowData = listDataRaw.get(i); defaultTableModle.addRow(rowData.toArray()); } return defaultTableModle; } public ArrayList<List<Object>> convertFromWeb2Raw(ArrayList<?> listDataFromWeb) { YahooStockData csvData; YahooStockData csvDataYesterday; ArrayList<List<Object>> listDataRaw = new ArrayList<List<Object>>(); for (int i = 0; i < listDataFromWeb.size() - 1; i++) { csvData = (YahooStockData) listDataFromWeb.get(i); csvDataYesterday = (YahooStockData) listDataFromWeb.get(i + 1); //String datas[] = csvData.toString().split(","); List<Object> rowData = new ArrayList<>(); rowData.add(csvData.getTradeDate()); rowData.add(csvData.getOpen()); rowData.add(csvData.getDayHigh()); rowData.add(csvData.getDayLow()); rowData.add(csvData.getClose()); rowData.add(csvData.getVolume()); rowData.add(csvData.getAdjustClose()); double priceDeltaPercentage = 1.0 - (csvDataYesterday.getAdjustClose() / csvData.getAdjustClose()); rowData.add(priceDeltaPercentage); //Data mapping for neural network output //9%~10% -> 20 //8%~9% -> 19 //7%~8% -> 18 //6%~7% -> 17 //5%~6% -> 16 //4%~5% -> 15 //3%~4% -> 14 //2%~3% -> 13 //1%~2% -> 12 //0%~1% -> 11 //-1%~0% -> 10 //-2%~-1% -> 9 //-3%~-2% -> 8 //-4%~-3% -> 7 //-5%~-4% -> 6 //-6%~-5% -> 5 //-7%~-6% -> 4 //-8%~-7% -> 3 //-9%~-8% -> 2 //-10%~-9% -> 1 = -9 +1 int output = (int) Math.ceil(priceDeltaPercentage * 100) + 10; rowData.add(output); listDataRaw.add(rowData); } return listDataRaw; } public ArrayList<List<String>> convertFromWeb2String(ArrayList<?> listDataFromWeb) { YahooStockData csvData; YahooStockData csvDataYesterday; ArrayList<List<String>> listDataRaw = new ArrayList<List<String>>(); for (int i = 0; i < listDataFromWeb.size() - 1; i++) { csvData = (YahooStockData) listDataFromWeb.get(i); csvDataYesterday = (YahooStockData) listDataFromWeb.get(i + 1); String datas[] = csvData.toString().split(","); List<String> rowData = new ArrayList<String>(Arrays.asList(datas)); double priceDeltaPercentage = 1.0 - (csvDataYesterday.getAdjustClose() / csvData.getAdjustClose()); rowData.add(String.valueOf(priceDeltaPercentage)); //Data mapping for neural network output //9%~10% -> 20 //8%~9% -> 19 //7%~8% -> 18 //6%~7% -> 17 //5%~6% -> 16 //4%~5% -> 15 //3%~4% -> 14 //2%~3% -> 13 //1%~2% -> 12 //0%~1% -> 11 //-1%~0% -> 10 //-2%~-1% -> 9 //-3%~-2% -> 8 //-4%~-3% -> 7 //-5%~-4% -> 6 //-6%~-5% -> 5 //-7%~-6% -> 4 //-8%~-7% -> 3 //-9%~-8% -> 2 //-10%~-9% -> 1 = -9 +1 int output = (int) Math.ceil(priceDeltaPercentage * 100) + 10; rowData.add(String.valueOf(output)); listDataRaw.add(rowData); } return listDataRaw; } private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed // TODO add your handling code here: }//GEN-LAST:event_jTextField1ActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(OldYahooFinanceJFrame.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(OldYahooFinanceJFrame.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(OldYahooFinanceJFrame.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(OldYahooFinanceJFrame.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new OldYahooFinanceJFrame().setVisible(true); } }); } private String[] title = { "LastTradeDate", "Open", "DayHigh", "DayLow", "Close", "Volume", "Adjust Close", "Percentage", "Output" }; // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JTable jTable1; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; // End of variables declaration//GEN-END:variables }