|
@@ -0,0 +1,247 @@
|
|
|
+import hug
|
|
|
+from bottle import route, run, NORUN
|
|
|
+from WindPy import w
|
|
|
+import iFinDPy as THS
|
|
|
+import json
|
|
|
+import pandas as pd
|
|
|
+import time
|
|
|
+import os
|
|
|
+import logging
|
|
|
+import sys
|
|
|
+
|
|
|
+hug.API(__name__).http.output_format = hug.output_format.json
|
|
|
+
|
|
|
+username = ""
|
|
|
+password = ""
|
|
|
+
|
|
|
+
|
|
|
+@hug.get('/hz_server')
|
|
|
+def hello():
|
|
|
+ return 1
|
|
|
+ # return 'wind true'
|
|
|
+
|
|
|
+
|
|
|
+# 同花顺登录函数
|
|
|
+def thslogin():
|
|
|
+ # 输入用户的帐号和密码
|
|
|
+ thsLogin = THS.THS_iFinDLogin(username, password)
|
|
|
+ print(thsLogin)
|
|
|
+ if (thsLogin == 0 or thsLogin == -201):
|
|
|
+ print('ths 登录成功')
|
|
|
+ logger.info('ths login success')
|
|
|
+ else:
|
|
|
+ print('ths 登录失败')
|
|
|
+ logger.info('ths login fail')
|
|
|
+
|
|
|
+
|
|
|
+@hug.get('/edbInfo/ths')
|
|
|
+def GetEdbDataByThs(EdbCode, StartDate, EndDate):
|
|
|
+ print("GetEdbDataByThs")
|
|
|
+ logger.info('GetEdbDataByThs')
|
|
|
+ thsEDBDataQuery = THS.THS_EDBQuery(EdbCode, StartDate, EndDate, True)
|
|
|
+ print(thsEDBDataQuery)
|
|
|
+ logger.info(thsEDBDataQuery)
|
|
|
+
|
|
|
+ print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ":start GetEdbDataByThs")
|
|
|
+ print("thsEDBDataQuery")
|
|
|
+ print(thsEDBDataQuery)
|
|
|
+ thsEDBDataQuery = thsEDBDataQuery.decode("unicode_escape")
|
|
|
+
|
|
|
+ result = json.loads(thsEDBDataQuery)
|
|
|
+ print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ":end GetEdbDataByThs")
|
|
|
+
|
|
|
+ if result["errorcode"] == -1010:
|
|
|
+ thsLogin = THS.THS_iFinDLogin(username, password)
|
|
|
+ print("登录一次")
|
|
|
+ print(thsLogin)
|
|
|
+ logger.info("ths login")
|
|
|
+ if (thsLogin == 0 or thsLogin == -201):
|
|
|
+ thsEDBDataQuery = THS.THS_EDBQuery(EdbCode, StartDate, EndDate, True)
|
|
|
+ thsEDBDataQuery = thsEDBDataQuery.decode("unicode_escape")
|
|
|
+ result = json.loads(thsEDBDataQuery)
|
|
|
+ logger.info("login success")
|
|
|
+ else:
|
|
|
+ print("已登录")
|
|
|
+
|
|
|
+ return result
|
|
|
+
|
|
|
+
|
|
|
+def has_comma(input_string):
|
|
|
+ return isinstance(input_string, str) and ',' in input_string
|
|
|
+
|
|
|
+
|
|
|
+def process_single_edb_code(StockCode, SingleEdbCode, StartDate, EndDate):
|
|
|
+ thsEDBDataQuery = THS.THS_DS(StockCode, SingleEdbCode, '', '', StartDate, EndDate)
|
|
|
+ print(thsEDBDataQuery)
|
|
|
+ result = json.dumps(ths_data_to_dict(thsEDBDataQuery))
|
|
|
+ return result
|
|
|
+
|
|
|
+
|
|
|
+@hug.get('/edbInfo/ths/ds')
|
|
|
+def GetEdbDataByThsDs(StockCode, EdbCode, StartDate, EndDate):
|
|
|
+ print("THS_DataSequence start")
|
|
|
+ print(EdbCode)
|
|
|
+ result_list = []
|
|
|
+
|
|
|
+ if isinstance(EdbCode, list):
|
|
|
+ for SingleEdbCode in EdbCode:
|
|
|
+ result_list.append(process_single_edb_code(StockCode, SingleEdbCode, StartDate, EndDate))
|
|
|
+ print("THS_DS end")
|
|
|
+ print(result_list)
|
|
|
+ return result_list
|
|
|
+ else:
|
|
|
+ # 如果不包含逗号,直接处理
|
|
|
+ result = process_single_edb_code(StockCode, EdbCode, StartDate, EndDate)
|
|
|
+ print(result)
|
|
|
+ return result
|
|
|
+
|
|
|
+
|
|
|
+@hug.get('/edbInfo/ths/future_good')
|
|
|
+def GetFutureGoodEdbDataByThs(EdbCode, StartDate, EndDate):
|
|
|
+ print("THS_HistoryQuotes start")
|
|
|
+ thsLogin = THS.THS_iFinDLogin("hzmd150", "689719")
|
|
|
+
|
|
|
+ filed = 'lastclose,open,high,low,close,avgprice,change,changeper,volume,amount,hsl,lastsettlement,settlement,zdsettlement,zdfsettlement,ccl,ccbd,zf,zjlx,zjcd'
|
|
|
+ # filed = 'open,high,low,close,volume,amount,settlement,ccl,zjlx'
|
|
|
+ params = 'Interval:D,CPS:1,baseDate:1900-01-01,Currency:YSHB,fill:Previous'
|
|
|
+ # params = 'YSHB;Tradedays'
|
|
|
+ # print(params)
|
|
|
+ thsEDBDataQuery = THS.THS_HistoryQuotes(EdbCode, filed, params, StartDate, EndDate, True)
|
|
|
+ # thsEDBDataQuery = THS.THS_HQ(EdbCode, filed, params, StartDate, EndDate)
|
|
|
+ # thsEDBDataQuery = THS.THS_EDBQuery(EdbCode, StartDate, EndDate, True)
|
|
|
+ print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ":start GetFutureGoodEdbDataByThs")
|
|
|
+ print("THS_HistoryQuotes end")
|
|
|
+ print(thsEDBDataQuery)
|
|
|
+ thsEDBDataQuery = thsEDBDataQuery.decode("unicode_escape")
|
|
|
+ result = json.loads(thsEDBDataQuery)
|
|
|
+ print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ":end GetFutureGoodEdbDataByThs")
|
|
|
+ return result
|
|
|
+
|
|
|
+
|
|
|
+@hug.get('/edbInfo/ths/hf')
|
|
|
+def GetEdbDataByThsDs(codes, indicators, function_para, start_time, end_time):
|
|
|
+ print("codes: {}; indicators: {}; function_para: {}; start_time: {}; end_time: {}".format(codes, indicators,
|
|
|
+ function_para, start_time,
|
|
|
+ end_time))
|
|
|
+ data_query = THS.THS_DF(codes, indicators, function_para, start_time, end_time)
|
|
|
+ print(data_query)
|
|
|
+ result = json.dumps(ths_data_to_dict(data_query))
|
|
|
+ return result
|
|
|
+
|
|
|
+
|
|
|
+@hug.get('/edbInfo/wind')
|
|
|
+def GetEdbDataByWind(EdbCode, StartDate, EndDate):
|
|
|
+ print("GetEdbDataByWind:", EdbCode)
|
|
|
+ isConnected = w.isconnected()
|
|
|
+ print("isconnected")
|
|
|
+ print(isConnected)
|
|
|
+ if isConnected == False:
|
|
|
+ print("re isconnected")
|
|
|
+ w.start()
|
|
|
+ isConnected = w.isconnected()
|
|
|
+ if isConnected == False:
|
|
|
+ return "{'ErrMsg':'启动Wind接口失败'}"
|
|
|
+
|
|
|
+ print("getdata")
|
|
|
+ option = "Fill=Previous"
|
|
|
+ data = w.edb(EdbCode, StartDate, EndDate, option)
|
|
|
+ print("wind data")
|
|
|
+ df = pd.DataFrame()
|
|
|
+ if data.ErrorCode == -40521010: # Internet Timeout 超时退出
|
|
|
+ os._exit(0)
|
|
|
+ return "a"
|
|
|
+ df['DT'] = data.Times
|
|
|
+ df['CLOSE'] = data.Data[0]
|
|
|
+ df['ErrorCode'] = data.ErrorCode
|
|
|
+ df = df[['DT', 'CLOSE', 'ErrorCode']]
|
|
|
+ df = df.dropna()
|
|
|
+ json_data = df.to_json()
|
|
|
+ # w.stop()
|
|
|
+ print("wind data end")
|
|
|
+ result = json.loads(json_data)
|
|
|
+ return result
|
|
|
+
|
|
|
+
|
|
|
+@hug.get('/edbInfo/wind/future_good')
|
|
|
+def GetFutureGoodEdbDataByWind(FutureGoodEdbCode, StartDate, EndDate):
|
|
|
+ print("GetFutureGoodEdbDataByWind:", FutureGoodEdbCode)
|
|
|
+ isConnected = w.isconnected()
|
|
|
+ print("isconnected")
|
|
|
+ print(isConnected)
|
|
|
+ if isConnected == False:
|
|
|
+ print("re isconnected")
|
|
|
+ w.start()
|
|
|
+ isConnected = w.isconnected()
|
|
|
+ if isConnected == False:
|
|
|
+ return "{'ErrMsg':'启动Wind接口失败'}"
|
|
|
+
|
|
|
+ print("getdata")
|
|
|
+ option = "Fill=Previous"
|
|
|
+ data = w.edb(FutureGoodEdbCode, StartDate, EndDate, option)
|
|
|
+ data = w.wsd(FutureGoodEdbCode, "trade_code,open,high,low,close,volume,amt,oi,settle", StartDate, EndDate, option)
|
|
|
+ print("wind data")
|
|
|
+ print(data)
|
|
|
+ df = pd.DataFrame()
|
|
|
+
|
|
|
+ if data.ErrorCode == -40521010: # Internet Timeout 超时退出
|
|
|
+ os._exit(0)
|
|
|
+ return "a"
|
|
|
+
|
|
|
+ df['DT'] = data.Times
|
|
|
+ df['TRADE_CODE'] = data.Data[0]
|
|
|
+ df['OPEN'] = data.Data[1]
|
|
|
+ df['HIGH'] = data.Data[2]
|
|
|
+ df['LOW'] = data.Data[3]
|
|
|
+ df['CLOSE'] = data.Data[4]
|
|
|
+ df['VOLUME'] = data.Data[5]
|
|
|
+ df['AMT'] = data.Data[6]
|
|
|
+ df['OI'] = data.Data[7]
|
|
|
+ df['SETTLE'] = data.Data[8]
|
|
|
+ df['ErrorCode'] = data.ErrorCode
|
|
|
+ df = df[['DT', 'TRADE_CODE', 'OPEN', 'HIGH', 'LOW', 'CLOSE', 'VOLUME', 'AMT', 'OI', 'SETTLE', 'ErrorCode']]
|
|
|
+ df = df.dropna()
|
|
|
+ json_data = df.to_json()
|
|
|
+ # w.stop()
|
|
|
+ print("wind data end")
|
|
|
+ result = json.loads(json_data)
|
|
|
+ return result
|
|
|
+
|
|
|
+
|
|
|
+@hug.get('/edbInfo/wind/wsd')
|
|
|
+def GetEdbDataWindWsd(StockCode, EdbCode, StartDate, EndDate):
|
|
|
+ print("GetEdbDataByWind:", EdbCode)
|
|
|
+ isConnected = w.isconnected()
|
|
|
+ print("isconnected")
|
|
|
+ print(isConnected)
|
|
|
+ if isConnected == False:
|
|
|
+ print("re isconnected")
|
|
|
+ w.start()
|
|
|
+ isConnected = w.isconnected()
|
|
|
+ if isConnected == False:
|
|
|
+ return "{'ErrMsg':'启动Wind接口失败'}"
|
|
|
+
|
|
|
+ option = "Fill=Previous"
|
|
|
+ wsd_data = w.wsd(StockCode, EdbCode, StartDate, EndDate, option)
|
|
|
+
|
|
|
+ if wsd_data.ErrorCode == -40521010: # Internet Timeout 超时退出
|
|
|
+ os._exit(0)
|
|
|
+ return "a"
|
|
|
+
|
|
|
+ fm = pd.DataFrame(wsd_data.Data, index=wsd_data.Fields, columns=wsd_data.Times)
|
|
|
+ json_data = fm.to_json()
|
|
|
+ result = json.loads(json_data)
|
|
|
+ return result
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ # wind 登录
|
|
|
+ wStart = w.start()
|
|
|
+ if wStart.ErrorCode != 0:
|
|
|
+ print("启动万得API接口失败")
|
|
|
+ print(wStart)
|
|
|
+
|
|
|
+ # ths登录函数
|
|
|
+ thslogin()
|
|
|
+
|
|
|
+ app = __hug__.http.server()
|
|
|
+ run(app=app, reloader=True, port=7000)
|