以下程序为本人自己写,平常做运维工作的,不要和我说代码的规范性,能用就得。 平常我们都要工作,很难一直盯着盘,有时我们想知道股票突然上涨突然下跌情况,或是否达到当日最高价或最低价,这样我们好抄底或抛票。如果能自动弹出来桌面,那更好了。 正好各个网站都有股票的接口,我们可以调用查询实时的数据。 用JAVA开发的,支持多只股票,感兴趣的可以参考做修改 package yjz.nms.cla; import java.awt.Color; import java.awt.Font; import java.awt.TextArea; import java.awt.Toolkit; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import javax.swing.ImageIcon; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; import javax.swing.border.LineBorder; public class changing { public String getHisData(String code) throws IOException { String fileNameN = 'e:\\' + code + '.ini'; File f = new File(fileNameN); String data = null; if (f.exists()) { BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(fileNameN), 'UTF-8')); data = br.readLine(); br.close(); br=null; } else { f.createNewFile(); } if (data == null) { data = code + ',0,0,0,0,0,0,0,0,'; } return data; } public String getCurData(String code) { String urls = null; URL ur = null; if (code.startsWith('60')) { urls = 'http://hq./list=sh' + code; } else { urls = 'http://hq./list=sz' + code; } if (code.equals('000001')) { urls = 'http://hq./list=s_sh000001'; } if (code.equals('399001')) { urls = 'http://hq./list=s_sz399001'; } BufferedReader reader = null; String line = null; try { ur = new URL(urls); HttpURLConnection uc = (HttpURLConnection) ur.openConnection(); reader = new BufferedReader(new InputStreamReader(ur.openStream(), 'GBK')); line = reader.readLine(); reader.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return line; } public String dealData(String his, String cur, String code) throws IOException { String result = null; if (!code.equals('000001') && !code.equals('399001')) { boolean istop = false; boolean islow = false; String[] curDatas = cur.substring(cur.indexOf('\'') + 1, cur.lastIndexOf('\'')).split(','); String[] hisDatas = his.split(','); DecimalFormat df = new DecimalFormat('###.00'); // 前四次价格 float last1P = Float.parseFloat(hisDatas[1]); float last2P = Float.parseFloat(hisDatas[2]); float last3P = Float.parseFloat(hisDatas[3]); float last4P = Float.parseFloat(hisDatas[4]); // 前四次委比 float last1R = Math.round(Float.parseFloat(hisDatas[5])*1000)/1000; float last2R = Math.round(Float.parseFloat(hisDatas[6])*1000)/1000; float last3R = Math.round(Float.parseFloat(hisDatas[7])*1000)/1000; float last4R = Math.round(Float.parseFloat(hisDatas[8])*1000)/1000; System.out.println(his+':hisDatas'+hisDatas.length+':last:'+last1P+':'+last2P+':'+last3P+':'+last4P+':'); // 股票名称 String name = curDatas[0]; // 开盘 float baseP = Float.parseFloat(curDatas[1]); // 当前 float curP = Float.parseFloat(curDatas[3]); // 最高 float topP = Float.parseFloat(curDatas[4]); // 最低 float lowP = Float.parseFloat(curDatas[5]); // 买方数 float buy1N = Float.parseFloat(curDatas[10]); float buy2N = Float.parseFloat(curDatas[12]); float buy3N = Float.parseFloat(curDatas[14]); float buy4N = Float.parseFloat(curDatas[16]); float buy5N = Float.parseFloat(curDatas[18]); float buyAll = buy1N + buy2N + buy3N + buy4N + buy5N; // 卖方数 float sell1N = Float.parseFloat(curDatas[20]); float sell2N = Float.parseFloat(curDatas[22]); float sell3N = Float.parseFloat(curDatas[24]); float sell4N = Float.parseFloat(curDatas[26]); float sell5N = Float.parseFloat(curDatas[28]); float sellAll = sell1N + sell2N + sell3N + sell4N + sell5N; result = name; //当前买卖比 float curR = 1; if (sellAll < 1)=""> curR = 0; } else if (buyAll < 1)=""> curR = 10000; } else { curR = Math.round((sellAll/buyAll)*1000)/1000; } //当前涨幅 float curRatio = 1; if (curP < basep)=""> curRatio= Math.round(((baseP-curP)/curP)*1000)/1000; } else { curRatio=Math.round(((curP-baseP)/curP)*1000)/1000; } if (curP >= topP && !(last1P == last2P && last2P==last3P && last3P ==last4P )) { result += ',今日最高价'; if (last1P < last2p="" &&="" last2p="">< last3p="" &&="" last3p=""><> && last4P < curp)=""> result += ',且连续四次上涨,委比' + curR + ',前几次委比:' + last4R + ',' + last3R + ',' + last2R + ',' + last1R + ','; } else if (last2P < last3p="" &&="" last3p="">< last4p="" &&="" last4p="">< curp)=""> result += ',且连续三次上涨,委比' + curR + ',前几次委比:' + last4R + ',' + last3R + ',' + last2R + ',' + last1R + ','; } else if (last3P < last4p="" &&="" last4p="">< curp)=""> result += ',且连续二次上涨,委比' + curR + ',前几次委比:' + last4R + ',' + last3R + ',' + last2R + ',' + last1R + ','; } else if (last4P < curp)=""> result += ',委比' + curR + ',前几次委比:' + last4R + ',' + last3R + ',' + last2R + ',' + last1R + ','; } istop = true; } if (curP <= lowp="" &&="" !(last1p="=" last2p="" &&="" last2p="=last3P" &&="" last3p="=last4P" ))="">=> result += ',今日最低价'; if (last1P > last2P && last2P > last3P && last3P > last4P && last4P > curP) { result += ',且连续四次下跌,委比' + curR + ',前几次委比:' + last4R + ',' + last3R + ',' + last2R + ',' + last1R + ','; } else if (last2P > last3P && last3P > last4P && last4P > curP) { result += ',且连续三次下跌,委比' + curR + ',前几次委比:' + last4R + ',' + last3R + ',' + last2R + ',' + last1R + ','; } else if (last3P > last4P && last4P > curP) { result += ',且连续二次下跌,委比' + curR + ',前几次委比:' + last4R + ',' + last3R + ',' + last2R + ',' + last1R + ','; } else if (last4P > curP) { result += ',委比' + curR + ',前几次委比:' + last4R + ',' + last3R + ',' + last2R + ',' + last1R + ','; } islow = true; } if (last4P > curP && ((last4P - curP) * 100 / baseP) > 2) { result += ',2分钟内快速下跌2%以上,委比' + curR + ',前几次委比:' + last4R + ',' + last3R + ',' + last2R + ',' + last1R + ','; } if (last1P > curP && ((last1P - curP) * 100 / baseP) > 4) { result += ',8分钟内快速下跌4%以上,委比' + curR + ',前几次委比:' + last4R + ',' + last3R + ',' + last2R + ',' + last1R + ','; } if (last4P < curp="" &&="" ((curp="" -="" last4p)="" *="" 100="" basep)=""> 2) { result += ',2分钟内快速上涨2%以上,委比' + curR + ',前几次委比:' + last4R + ',' + last3R + ',' + last2R + ',' + last1R + ','; } if (last1P < curp="" &&="" ((curp-last1p)*100/basep)=""> 4) { result += ',8分钟内快速上涨4%以上,委比' + curR + ',前几次委比:' + last4R + ',' + last3R + ',' + last2R + ',' + last1R + ','; } String newData = name + ',' + last2P + ',' + last3P + ',' + last4P + ',' + curP + ',' + last2R + ',' + last3R + ',' + last4R + ',' + curR; System.out.println(newData); saveData(code, newData); } else { // 大盘指数,只判断涨跌率 String[] curDatas = cur.substring(cur.indexOf('\'') + 1, cur.lastIndexOf('\'')).split(','); String[] hisDatas = his.split(','); // 前四次指数 float last1C = Float.parseFloat(hisDatas[1]); float last2C = Float.parseFloat(hisDatas[2]); float last3C = Float.parseFloat(hisDatas[3]); float last4C = Float.parseFloat(hisDatas[4]); // 前四次涨跌率 float last1R = Float.parseFloat(hisDatas[5]); float last2R = Float.parseFloat(hisDatas[6]); float last3R = Float.parseFloat(hisDatas[7]); float last4R = Float.parseFloat(hisDatas[8]); // 股票名称 String name = curDatas[0]; result = name; // 当前指数 float curC = Float.parseFloat(curDatas[1]); // 当数涨 float curR = Float.parseFloat(curDatas[3]); if (curR > last4R && (curR - last4R) > 0.5) { result += ',在2分钟内上涨0.5%'; } if (curR > last3R && (curR - last3R) > 0.7) { result += ',在4分钟内上涨0.7%'; } if (curR > last2R && (curR - last2R) > 1) { result += ',在6分钟内上涨1%'; } if (curR > last1R && (curR - last1R) > 1.2) { result += ',在8分钟内上涨1.2%'; } if (curR < last4r="" &&="" (last4r="" -="" curr)=""> 0.5) { result += ',在2分钟内下跌0.5%'; } if (curR < last3r="" &&="" (last3r="" -="" curr)=""> 0.7) { result += ',在4分钟内下跌0.7%'; } if (curR < last2r="" &&="" (last2r="" -="" curr)=""> 1) { result += ',在6分钟内下跌1%'; } if (curR < last1r="" &&="" (last1r="" -="" curr)=""> 1.2) { result += ',在8分钟内下跌1.2%'; } String newData = name + ',' + last2C + ',' + last3C + ',' + last4C + ',' + curC + ',' + last2R + ',' + last3R + ',' + last4R + ',' + curR; System.out.println(newData); saveData(code, newData); } return result; } public void saveData(String code, String newStr) throws IOException { // 先读取原有文件内容,然后进行写入操作 String fileNameN = 'e:\\' + code + '.ini'; File f = new File(fileNameN); if (f.exists()) { f.delete(); } if (!f.exists()) { f.createNewFile(); } try { OutputStreamWriter write = new OutputStreamWriter( new FileOutputStream(f), 'UTF-8'); BufferedWriter writer = new BufferedWriter(write); writer.write(newStr); writer.close(); } catch (IOException e1) { // TODO 自动生成 catch 块 e1.printStackTrace(); } } public String doALL(String code) throws IOException { String hisData = getHisData(code); String getCurData = getCurData(code); return dealData(hisData, getCurData, code); } public String getCode() throws IOException { String fileNameN = 'e:\\allStockCode.ini'; File f = new File(fileNameN); String data = null; if (f.exists()) { BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(fileNameN), 'UTF-8')); data = br.readLine(); } else { System.out.println('请在E盘建文件allStockCode.ini,里面加上股票代码,用逗号分隔'); } return data; } public static void main(String args[]) throws IOException, InterruptedException { changing c = new changing(); SimpleDateFormat df = new SimpleDateFormat('HHmm');// 设置日期格式 // int hourNum=Integer.parseInt(df.format(new Date())); String hourNums = df.format(new Date()); while (hourNums.startsWith('092') ||hourNums.startsWith('093') || hourNums.startsWith('094') || hourNums.startsWith('095') || hourNums.startsWith('10') || hourNums.startsWith('110') || hourNums.startsWith('111') || hourNums.startsWith('112') || hourNums.startsWith('13') || hourNums.startsWith('14') || hourNums.startsWith('22')) { hourNums = df.format(new Date()); String codeSeq = c.getCode(); String result = ''; String[] codes = codeSeq.split(','); for (int i = 0; i < codes.length;="" i++)=""> String tmp = c.doALL(codes[i]); if (tmp.indexOf(',') > 0) { if (i == 0) { result = tmp; } else { result += '\n' + tmp; } } } if (result.indexOf(',') > 1) { JDialog myFrame = new JDialog(); myFrame.setTitle(hourNums); myFrame.setUndecorated(false);// 不显示窗口边框和标题 myFrame.setSize(300, 200); myFrame.setLayout(null); JLabel titleLabel = new JLabel( '股票信息 关闭 X');// 放在右上角做关闭按钮 titleLabel.setFont(new Font('宋体', 0, 14)); titleLabel.setForeground(Color.BLUE); myFrame.getContentPane() .setBackground(new Color(255, 255, 255)); JPanel p = ((JPanel) myFrame.getContentPane()); p.setBorder(new LineBorder(new java.awt.Color(10, 110, 10), 1, false)); titleLabel.setFont(new Font('宋体', 0, 12)); myFrame.setBounds( Toolkit.getDefaultToolkit().getScreenSize().width - 305, Toolkit.getDefaultToolkit().getScreenSize().height - 200, 300, 200); myFrame.getContentPane().add(titleLabel); titleLabel.setBounds(0, 0, 300, 20); // JLabel contentLabel = new JLabel(result); // contentLabel.setBounds(0, 20, 300, 180); // contentLabel.setHorizontalAlignment(SwingConstants.LEFT); // contentLabel.setVerticalAlignment(SwingConstants.TOP); // ImageIcon image =new ImageIcon('image.png'); // contentLabel.setIcon(image); // myFrame.getContentPane().add(contentLabel); TextArea ta = new TextArea(result); ta.setBounds(0, 20, 300, 180); ta.setFont(new Font('宋体', 0, 12)); ta.setEditable(false); myFrame.getContentPane().add(ta); myFrame.setVisible(true); myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); /*titleLabel.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { myFrame.dispose(); } public void mouseEntered(MouseEvent e) { super.mouseEntered(e); titleLabel.setForeground(Color.red); } public void mouseExited(MouseEvent e) { super.mouseExited(e); titleLabel.setForeground(Color.BLACK); } });*/ } Thread.sleep(120 * 1000); } } } |
|