iT邦幫忙

2021 iThome 鐵人賽

DAY 28
0
永豐金融APIs

視覺化跨平台Client與永豐金融證券APIs系列 第 28

視覺化KBARS日K(1)-java controller

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的成果,
大家加油剩沒幾天了~


上一篇
視覺化KBARS(5)-1分k展示
下一篇
視覺化KBARS日K(2)-日K成果展示
系列文
視覺化跨平台Client與永豐金融證券APIs30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
juck30808
iT邦研究生 1 級 ‧ 2021-10-14 12:07:13

恭喜即將邁入完賽啦~

我要留言

立即登入留言