Browse Source

同花顺高频数据、日期序列加参数

hsun 3 months ago
parent
commit
7899b6db59
1 changed files with 54 additions and 20 deletions
  1. 54 20
      ths_api.py

+ 54 - 20
ths_api.py

@@ -1,5 +1,5 @@
 import hug
-from bottle import route, run,NORUN
+from bottle import route, run, NORUN
 import iFinDPy as THS
 import json
 import time
@@ -21,11 +21,13 @@ file_handler.setFormatter(formatter)
 
 logger.addHandler(file_handler)
 
+
 @hug.get('/hz_server')
 def hello():
     return 1
     # return 'wind true'
 
+
 # 同花顺登录函数
 def thslogin():
     # 输入用户的帐号和密码
@@ -46,7 +48,6 @@ def GetEdbDataByThs(EdbCode, StartDate, EndDate):
     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")
@@ -56,21 +57,22 @@ def GetEdbDataByThs(EdbCode, StartDate, EndDate):
     result = json.loads(thsEDBDataQuery)
     print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ":end GetEdbDataByThs")
 
-    if result["errorcode"]==-1010:
+    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")
+            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
 
+
 @hug.get('/edbInfo/ths/future_good')
 def GetFutureGoodEdbDataByThs(EdbCode, StartDate, EndDate):
     print("THS_HistoryQuotes start")
@@ -89,20 +91,21 @@ def GetFutureGoodEdbDataByThs(EdbCode, StartDate, EndDate):
     logger.info(thsEDBDataQuery)
     thsEDBDataQuery = thsEDBDataQuery.decode("unicode_escape")
     result = json.loads(thsEDBDataQuery)
-    if result["errorcode"]==-1010:
+    if result["errorcode"] == -1010:
         thsLogin = THS.THS_iFinDLogin(username, password)
         print("登录一次")
         print(thsLogin)
         logger.info('THS_HistoryQuotes login')
         if (thsLogin == 0 or thsLogin == -201):
-           thsEDBDataQuery = THS.THS_HistoryQuotes(EdbCode, filed, params, StartDate, EndDate, True)
-           thsEDBDataQuery = thsEDBDataQuery.decode("unicode_escape")
-           result = json.loads(thsEDBDataQuery)
+            thsEDBDataQuery = THS.THS_HistoryQuotes(EdbCode, filed, params, StartDate, EndDate, True)
+            thsEDBDataQuery = thsEDBDataQuery.decode("unicode_escape")
+            result = json.loads(thsEDBDataQuery)
     else:
         print("已登录")
     print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ":end GetFutureGoodEdbDataByThs")
     return result
 
+
 def ths_data_to_dict(ths_instance):
     if ths_instance is not None and ths_instance.data is not None:
         return {
@@ -118,35 +121,66 @@ def ths_data_to_dict(ths_instance):
             'data': []
         }
 
+
 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)
+
+def process_single_edb_code(StockCode, SingleEdbCode, StartDate, EndDate, ExtraPars=''):
+    strExtra = ','.join(map(str, ExtraPars))
+    print(strExtra)
+    thsEDBDataQuery = THS.THS_DS(StockCode, SingleEdbCode, strExtra, '', 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)
+def GetEdbDataByThsDs(StockCode, EdbCode, StartDate, EndDate, ExtraPars=''):
+    print("GetEdbDataByThsDs-> StockCode: {}; EdbCode: {}; StartDate: {}; EndDate: {}; ExtraPars: {}".format(StockCode,
+                                                                                                             EdbCode,
+                                                                                                             StartDate,
+                                                                                                             EndDate,
+                                                                                                             ExtraPars))
     result_list = []
 
     if isinstance(EdbCode, list):
         for SingleEdbCode in EdbCode:
-            result_list.append(process_single_edb_code(StockCode, SingleEdbCode, StartDate, EndDate))
+            result_list.append(process_single_edb_code(StockCode, SingleEdbCode, StartDate, EndDate, ExtraPars))
             print("THS_DS end")
         print(result_list)
         return result_list
     else:
         # 如果不包含逗号,直接处理
-        result = process_single_edb_code(StockCode, EdbCode, StartDate, EndDate)
+        result = process_single_edb_code(StockCode, EdbCode, StartDate, EndDate, ExtraPars)
         print(result)
         return result
 
+
+@hug.get('/edbInfo/ths/hf')
+def GetEdbDataByThsHf(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_HF(codes, indicators, function_para, start_time, end_time)
+    print("data_query: {}".format(data_query))
+
+    if data_query.errorcode == -1010 or data_query.errorcode == -209:
+        ths_login = THS.THS_iFinDLogin(username, password)
+        print("登录一次")
+        print("ths_login: {}".format(ths_login))
+
+        if ths_login == 0 or ths_login == -201:
+            repeat_query = THS.THS_HF(codes, indicators, function_para, start_time, end_time)
+            print("repeat_query: {}".format(repeat_query))
+
+            return json.dumps(ths_data_to_dict(repeat_query), indent=2)
+    else:
+        return json.dumps(ths_data_to_dict(data_query), indent=2)
+
+
 if __name__ == "__main__":
     # ths登录函数
     thslogin()
     app = __hug__.http.server()
-    run(app=app, reloader=True,host='0.0.0.0', port=7000)
+    run(app=app, reloader=True, host='0.0.0.0', port=7000)