股票查询1.2版本

无注释。

两个方法:

executeQuery();查询集合中所有的股票信息

executeQueryWithFilter(Float indexRate,Float normalRate);查询超过一定涨幅的股票,将低于涨幅的过滤掉。indexRate:指数涨幅点过滤;normalRate目标涨幅过滤点。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Query {
	private String requestUrl = "http://qt.gtimg.cn/r=0.7938921226847172q=";  
    private long sleepSecond = 10;  
  
    private List<String> stockCodeList;  
    private SimpleDateFormat dateFormate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    
    private Set<String> indexCodeSet = new HashSet<String>(2);;
    
    public Query(List<String> stockCodeList,long sleepSecond)  
    {  
        this.stockCodeList = stockCodeList;  
        this.sleepSecond = sleepSecond; 
        
        indexCodeSet.add("000001");
        indexCodeSet.add("399006");
    }  
      
    public void executeQuery() throws IOException, InterruptedException  
    {  
    	query(null, null);
    }  
    
    public void executeQueryWithFilter(Float indexRate,Float normalRate) throws IOException, InterruptedException  
    {  
    	query(indexRate, normalRate);
          
    }  
    
    private void query(Float indexRate,Float normalRate)throws IOException, InterruptedException{
    	String request = getRequestData();  
        while (true)  
        {  
        	
        	Calendar ca  = Calendar.getInstance();
        	ca.setTime(new Date());
            System.out.println(dateFormate.format(ca.getTime()));  
            System.out.println("***********************************************************************");  
             
           
            sendRequest(request,indexRate,normalRate);  
           
            if(!inTradeTime()){
            	break;
            }
            
            Thread.sleep(sleepSecond * 1000);  
        }  
    }
    
    private String getRequestData()  
    {  
        StringBuilder requestUrlWithPara = new StringBuilder(requestUrl);   
        for(String code : stockCodeList)  
        {  
            requestUrlWithPara.append(code+",");  
        }  
          
        if(stockCodeList.size() > 0)  
        {  
            requestUrlWithPara.deleteCharAt(requestUrlWithPara.length()-1);  
        }  
          
        return requestUrlWithPara.toString();  
    } 
    
    private boolean inTradeTime(){
 	   Calendar ca  = Calendar.getInstance();
 	   ca.setTime(new Date());
 	   int hour = ca.get(Calendar.HOUR_OF_DAY);
 	   if(hour>=9 && hour<=15){
 		   return true;
 	   }
 	   return false;
    }
    
      
    private void sendRequest(String request,Float indexRate,Float normalRate) throws IOException  
    {  
        URL url = new URL(request);  
        URLConnection conn = url.openConnection(); 
        
        InputStream in = conn.getInputStream();  
        BufferedReader br = new BufferedReader(new InputStreamReader(in,"GBK"));  
        String tmp = null; 
        List<String> stockList = new ArrayList<String>();
        while (null != (tmp = br.readLine()))  
        {  
        	stockList.add(tmp);
        }  
        br.close();  
        in.close();  
        
        for(String stock : stockList){
        	displayMessage(stock,indexRate,normalRate);
        }
    }  
    
    private void displayMessage(String stock,Float indexRange,Float normalRange){
    	 
    	 String stockInfo = stock.replace('~', '\t');  
         int index = stockInfo.indexOf('=');  
               
         stockInfo = stockInfo.substring(index + 2);  
         int headerIndex = stockInfo.indexOf("\t");  
         stockInfo = stockInfo.substring(headerIndex+1);  
         int last = stockInfo.lastIndexOf(";");  
         stockInfo = stockInfo.substring(0, last-1);  
         
         stockInfo = replaceInMap(stockInfo);
         
         String[] arr = stockInfo.split("\t");//SZZS, 000001, 2979.43, 18.46, 0.62, 175074344, 20081844
         StringBuilder sbuilder = new StringBuilder();
         
         if(indexCodeSet.contains(arr[1])){//上证指数,中小板指数
        	 if(indexRange== null 
        			 || (indexRange != null && outOfRange(indexRange, arr[4]))){
        		 for(int i = 0 ; i < 5 ; i++){
        			 sbuilder.append(arr[i]+"\t");	
        		 }
        	 }
         }else{
        	 
        	 if(normalRange == null || (normalRange != null && outOfRange(normalRange, arr[4])) ){
        		 for(int i = 0 ; i < 5 ; i++){
        			 sbuilder.append(arr[i]+"\t");	
        		 }
        	 }
        	 
         }
         
         if(sbuilder.length() > 0){
        	 System.out.println(sbuilder.toString()); 
         }
    }
    
    private boolean outOfRange(Float range,String actRateStr ){
    	Float actRate = Float.valueOf(actRateStr);
    	if(actRate >= 0){//
    		return actRate.floatValue() >= range.floatValue() ? true : false;
    	}else{
    		return actRate.floatValue() <= range.floatValue()*-1 ? true : false;
    	}
    }
    
    private String replaceInMap(String content){
    	
    	Map<String,String> map = new HashMap<String, String>();
    	map.put("粤 传 媒", "YCM");
    	map.put("晋西车轴", "JXCZ");
    	map.put("太安堂", "TAT*");
    	map.put("福耀玻璃", "FYBL*");
    	map.put("中联重科", "ZLZK*");
    	map.put("上证指数", "SZZS");
    	map.put("九洲电气", "JZDQ");
    	map.put("创业板指", "CYBZ");
    	map.put("云南铜业", "YNTY*");
    	map.put("慈星股份", "CXGF*");
    	map.put("招商证券", "ZSZQ*");
    	map.put("南都电源", "NDDY");
    	map.put("歌华有线", "GHYX*");
    	String newContent = new String(content);
    	for(Entry<String, String> entry : map.entrySet()){
    		newContent = newContent.replace(entry.getKey(), entry.getValue());
    	}
    	
    	
    	return newContent;
    }
      
    
    
    
    public static void main(String[] args)   
    {  
        //股票代号列表  
        List<String> codeList = new ArrayList<String>();  
        codeList.add("s_sh000001"); 
        codeList.add("s_sz399006");

        codeList.add("s_sz000878"); 
        codeList.add("s_sh600999");
        codeList.add("s_sz300307"); 
        codeList.add("s_sz002433");
        codeList.add("s_sz000157"); 
        codeList.add("s_sh600660"); 
        codeList.add("s_sh600037"); 
        
        codeList.add("s_sz002181"); 
        codeList.add("s_sz300040");
        codeList.add("s_sz300068");
       
        Query stockQuery = new Query(codeList, 20);  
        try  
        {  
            stockQuery.executeQuery(); //普通查询
//            stockQuery.executeQueryWithFilter(1.00F,2.00f); //过滤查询
        }  
        catch (IOException e)  
        {  
            e.printStackTrace();  
        }  
        catch (InterruptedException e)  
        {  
            e.printStackTrace();  
        }  
  
    }  
      
}

 

猜你喜欢

转载自commissioner.iteye.com/blog/2286292