股票价格分析
根据历史股价计算
今天突然有一个想法,根据股票历史的数据,判断这个股票当前价格是否合适购买。
目前就这点思路,欢迎各位有思路的留言,后期继续更新
废话不多少上代码~~~
抓取数据
package org.zy.modules;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.zy.utils.HttpClient;
import java.io.IOException;
public class GpFx {
public static void main(String[] args) {
//市场 0 深 1 沪A
String type = "0";
//股票编码
String code = "000725";
String url = "https://17.push2his.eastmoney.com/api/qt/stock/kline/get?secid=" + type + "." + code + "&ut=fa5fd1943c7b386f172d6893dbfba10b&fields1=f1,f3&fields2=f51,f53&klt=101&fqt=0&end=20500101&lmt=1000000";
try {
String ret = HttpClient.sendGetData(url);
JSONObject jsonObject = JSONObject.parseObject(ret);
System.out.println("股票名称:" + jsonObject.getJSONObject("data").getString("name"));
JSONArray array = jsonObject.getJSONObject("data").getJSONArray("klines");
double avg = 0.0;
double min = 30000;
String minString = "";
double max = 0;
String maxString = "";
for (int i = 0; i < array.size(); i++) {
String[] a = array.getString(i).split(",");
double price = Double.parseDouble(a[1]);
avg += price;
if (min > price) {
min = price;
minString = array.getString(i);
}
if (max < price) {
max = price;
maxString = array.getString(i);
}
}
avg = avg / array.size();
System.out.println("根据当前股票历史数据计算得出平均股价为:" + avg);
System.out.println("当前最新股价为:" + array.getString(array.size() - 1));
System.out.println("历史股价最低值:" + minString);
System.out.println("历史股价最高值:" + maxString);
} catch (IOException e) {
e.printStackTrace();
}
}
}
自己封装的http请求工具工具类
package org.zy.utils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
/**
* http客户端
*
* @author:zy
* @date:2017年12月20日 下午8:26:51
*/
public class HttpClient {
/**
* post请求传输map数据
*
* @param url
* @param map
* @param encoding
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String sendPostDataByMap(String url, Map<String, String> map, String encoding, String cookie) throws ClientProtocolException, IOException {
String result = "";
// 创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = null;
// 装填参数
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
if (map != null) {
for (Entry<String, String> entry : map.entrySet()) {
nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
// 设置参数到请求对象中
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, encoding));
if (StringUtils.isNotEmpty(cookie)) {
httpPost.addHeader("Cookie", cookie);
}
// 设置header信息
// 指定报文头【Content-type】、【User-Agent】
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0;Windows NT; DigExt)");
try {
// 执行请求操作,并拿到结果(同步阻塞)
response = httpClient.execute(httpPost);
// 获取结果实体
// 判断网络连接状态码是否正常(0--200都数正常)
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(response.getEntity(), "utf-8");
}
} finally {
if (response != null) {
response.close();
}
// 关闭浏览器
httpClient.close();
}
return result;
}
/**
* post请求传输map数据
*
* @param url
* @param map
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String sendPostDataByMap(String url, Map<String, String> map) throws ClientProtocolException, IOException {
return sendPostDataByMap(url, map, "utf-8", null);
}
/**
* post请求传输map数据
*
* @param url
* @param map
* @param encoding
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String sendPostDataByMap(String url, Map<String, String> map, String encoding) throws ClientProtocolException, IOException {
return sendPostDataByMap(url, map, encoding, null);
}
/**
* post请求传输json数据
*
* @param url
* @param json
* @param encoding
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String sendPostDataByJson(String url, String json, String encoding, String cookie) throws ClientProtocolException, IOException {
String result = "";
CloseableHttpResponse response = null;
// 创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
// 设置参数到请求对象中
StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
stringEntity.setContentEncoding("utf-8");
httpPost.setEntity(stringEntity);
if (StringUtils.isNotEmpty(cookie)) {
httpPost.addHeader("Cookie", cookie);
}
try {
// 执行请求操作,并拿到结果(同步阻塞)
response = httpClient.execute(httpPost);
// 获取结果实体
// 判断网络连接状态码是否正常(0--200都数正常)
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(response.getEntity(), "utf-8");
}
} finally {
if (response != null) {
response.close();
}
// 关闭浏览器
httpClient.close();
}
return result;
}
public static String sendPostDataByJson(String url, String json, String encoding) throws IOException {
return sendPostDataByJson(url, json, encoding, null);
}
/**
* get请求传输数据
*
* @param url
* @param encoding
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static String sendGetData(String url, String encoding, String cookie) throws ClientProtocolException, IOException {
String result = "";
// 创建httpclient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
// 创建get方式请求对象
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Content-type", "application/json");
if (StringUtils.isNotEmpty(cookie)) {
httpGet.addHeader("Cookie", cookie);
}
try {
// 执行请求操作,并拿到结果(同步阻塞)
response = httpClient.execute(httpGet);
// 获取结果实体
// 判断网络连接状态码是否正常(0--200都数正常)
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(response.getEntity(), "utf-8");
}
} finally {
if (response != null) {
response.close();
}
// 关闭浏览器
httpClient.close();
}
return result;
}
public static String sendGetData(String url) throws IOException {
return sendGetData(url, "UTF-8");
}
public static String sendGetData(String url, String encoding) throws IOException {
return sendGetData(url, encoding, null);
}
/**
* 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串 @param params
* 需要排序并参与字符拼接的参数组 @return 拼接后字符串 @throws UnsupportedEncodingException
*/
public static String createLinkStringByGet(Map<String, Object> params) throws UnsupportedEncodingException {
List<String> keys = new ArrayList<String>(params.keySet());
Collections.sort(keys);
StringBuilder prestr = new StringBuilder();
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
String value = String.valueOf(params.get(key) == null ? "" : params.get(key));
value = URLEncoder.encode(value, "UTF-8");
// 拼接时,不包括最后一个&字符
if (i == keys.size() - 1) {
prestr.append(prestr + key + "=" + value);
} else {
prestr.append(prestr + key + "=" + value + "&");
}
}
return prestr.toString();
}
public static void main(String[] args) throws UnsupportedEncodingException {
}
}
分析结果
股票名称:京东方A
根据当前股票历史数据计算得出平均股价为:5.7864073855823905
当前最新股价为:2022-07-21,3.89
历史股价最低值:2012-01-05,1.64
历史股价最高值:2001-06-25,23.25