1分K視覺化的功能昨天已經完成,
今天要嘗試將1分K組成日K做展示,
我會示範一個土法煉鋼的方式,
之後還會說更好的方法是什麼,
現在開始土法煉鋼吧!
首先我們的思路是先算出我們總共要查詢的KBARS資料是幾天,
算出相差天數後,
用迴圈一天一天加日期,
然後每個日期都呼叫我們的kbars python api,
再將取到的值做組裝(包含迴圈計算最高值和最低值),
最後回傳到前台。
@RequestMapping(value = "/showDayKbar" , method = {RequestMethod.GET})
String start_date = pRequest.getParameter("start_date") == null ? "" : pRequest.getParameter("start_date");
String end_date = pRequest.getParameter("end_date") == null ? "" : pRequest.getParameter("end_date");
String stock_code = pRequest.getParameter("stock_code") == null ? "" : pRequest.getParameter("stock_code");
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm");
SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");
Calendar ca = Calendar.getInstance();
ca.set(Calendar.YEAR, Integer.valueOf(start_date.substring(0, 4)));
ca.set(Calendar.MONTH, Integer.valueOf(start_date.substring(5, 7))-1);
ca.set(Calendar.DATE, Integer.valueOf(start_date.substring(8, 10)));
//計算相差天數
Date fromDate1 = sdf.parse(start_date.substring(0, 10)+" 12:00");
Date toDate1 = sdf.parse(end_date.substring(0, 10)+" 12:00");
long from1 = fromDate1.getTime();
long to1 = toDate1.getTime();
int days = (int) ((to1 - from1) / (1000 * 60 * 60 * 24));
JSONObject all_reesult = new JSONObject();
JSONArray all_reesult1 = new JSONArray();
Date date = ca.getTime();
String dateStringParse = sdf2.format(date);
for(int i = 0;i < (days+1);i++) {
if(i != 0) {
ca.add(Calendar.DATE, 1);
date = ca.getTime();
dateStringParse = sdf2.format(date);
}
String kbar_data = shioajiService.oneMinKbar(dateStringParse, dateStringParse, stock_code);
JSONObject jSONObject = new JSONObject(kbar_data);
if(jSONObject.getJSONArray("ts").length() > 0) {
double temp_low = 0.0;
double temp_high = 0.0;
JSONArray data = new JSONArray();
data.put(dateStringParse);
for(int k = 0; k < jSONObject.getJSONArray("ts").length();k++) {
if(k==0) {
data.put(jSONObject.getJSONArray("Open").getDouble(k));
temp_low = jSONObject.getJSONArray("Low").getDouble(k);
temp_high = jSONObject.getJSONArray("High").getDouble(k);
}else {
temp_low = temp_low > jSONObject.getJSONArray("Low").getDouble(k) ? jSONObject.getJSONArray("Low").getDouble(k) : temp_low;
temp_high = temp_high < jSONObject.getJSONArray("High").getDouble(k) ? jSONObject.getJSONArray("High").getDouble(k) : temp_high;
}
if((k+1) == jSONObject.getJSONArray("ts").length()) {
data.put(jSONObject.getJSONArray("Close").getDouble(k));
data.put(temp_low);
data.put(temp_high);
}
}
all_reesult1.put(data);
}
}
Gson gson = new Gson();
model.addAttribute("kbar_data", all_reesult1);
return "showKbarDayMA";
}
明天就會展示日K的成果,
大家加油剩沒幾天了~