mirror of
https://gitee.com/fasiondog/hikyuu.git
synced 2024-11-30 02:48:57 +08:00
新浪实时数据获取;完善编码基础设施
This commit is contained in:
parent
1a14c57bc4
commit
80fc3fedfe
@ -1,7 +1,7 @@
|
||||
# 已废弃接口
|
||||
|
||||
from .extend import *
|
||||
from .util.mylog import escapetime
|
||||
from .util.mylog import spend_time
|
||||
|
||||
|
||||
def deprecated_func(new_func, old_func_name, new_func_name):
|
||||
|
@ -31,7 +31,7 @@
|
||||
"""
|
||||
绘制普通K线图 + 成交量(成交金额)
|
||||
"""
|
||||
from hikyuu.util.mylog import escapetime
|
||||
from hikyuu.util.mylog import spend_time
|
||||
from hikyuu import Query
|
||||
from hikyuu.indicator import Indicator, IF, MA, CLOSE, VOL, CVAL, PRICELIST, SG_Cross
|
||||
from .drawplot import (
|
||||
|
@ -5,10 +5,58 @@
|
||||
# Created on: 2020-11-29
|
||||
# Author: fasiondog
|
||||
|
||||
import requests
|
||||
import json
|
||||
import datetime
|
||||
from hikyuu.util import hku_logger, hku_catch, hku_check
|
||||
|
||||
def getProxy():
|
||||
zhimahttp_url = 'http://webapi.http.zhimacangku.com/getip?num=1&type=2&pro=330000&city=0&yys=0&port=1&time=1&ts=1&ys=1&cs=1&lb=1&sb=0&pb=45&mr=2®ions='
|
||||
|
||||
# 上次获取时间
|
||||
g_pre_fetch_time = datetime.datetime.now()
|
||||
|
||||
# 过期时间
|
||||
g_expire_time = None
|
||||
|
||||
# 当前代理ip
|
||||
g_host_ip = None
|
||||
|
||||
# 当前代理端口号
|
||||
g_host_port = None
|
||||
|
||||
# 最大重试次数
|
||||
g_max_retry_count = 10
|
||||
|
||||
|
||||
def request_proxy_from_zhima():
|
||||
"""返回格式如下:
|
||||
{'ip': '115.226.137.200',
|
||||
'port': 4223,
|
||||
'expire_time': '2020-12-01 00:12:33',
|
||||
'city': '浙江省丽水市',
|
||||
'isp': '电信'}
|
||||
"""
|
||||
ret = requests.get(zhimahttp_url).text
|
||||
x = json.loads(ret)
|
||||
hku_check(x['success'], "fetch failed!")
|
||||
return x['data'][0]
|
||||
|
||||
|
||||
def get_proxy_from_zhima():
|
||||
global g_max_retry_count
|
||||
for i in range(g_max_retry_count):
|
||||
try:
|
||||
data = request_proxy_from_zhima()
|
||||
return "{}:{}".format(data['ip'], data['port'])
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
def get_proxy():
|
||||
"""
|
||||
返回代理地址,格式 :代理ip地址:代理端口号,如:183.164.239.57:4264
|
||||
如无法获取时,返回 None
|
||||
"""
|
||||
return None
|
||||
#return None
|
||||
#ProxyError
|
||||
return "123.156.181.103:4281"
|
@ -7,39 +7,67 @@
|
||||
|
||||
import requests
|
||||
from hikyuu.util import *
|
||||
from hikyuu.fetcher.proxy import get_proxy
|
||||
|
||||
|
||||
@hku_catch()
|
||||
def parse_one_result(resultstr):
|
||||
result = {}
|
||||
try:
|
||||
HKU_CHECK(resultstr, "Invalid input param!")
|
||||
HKU_CHECK(len(resultstr) <= 3 and resultstr[:3] != 'var', "Invalid input param!")
|
||||
hku_check_ignore(resultstr, "Invalid input param: {}!".format(resultstr))
|
||||
hku_check_ignore(
|
||||
len(resultstr) > 3 and resultstr[:3] == 'var', "Invalid input param! {}".format(resultstr)
|
||||
)
|
||||
|
||||
a = resultstr.split(',')
|
||||
HKU_CHECK(len(a) < 9, "被解析的字符串长度小于9,无效")
|
||||
|
||||
tmp = a[0].split('"')
|
||||
result['name'] = tmp[1]
|
||||
except HKUCheckError as e:
|
||||
print(e)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
a = resultstr.split(',')
|
||||
result['market'] = a[0][11:13]
|
||||
result['code'] = a[0][13:19]
|
||||
result['name'] = a[0][21:]
|
||||
result['open'] = float(a[1]) # 今日开盘价
|
||||
result['yesterday close'] = float(a[2]) # 昨日收盘价
|
||||
result['last_price'] = float(a[3]) # 当前价格
|
||||
result['high'] = float(a[4]) # 今日最高价
|
||||
result['low'] = float(a[5]) # 今日最低价
|
||||
result['bid'] = float(a[6]) # 竞买价,即“买一”报价
|
||||
result['ask'] = float(a[7]) # 竞卖价,即“卖一”报价
|
||||
result['amount'] = float(a[8]) # 成交的股票数,由于股票交易以一百股为基本单位,所以在使用时,通常把该值除以一百
|
||||
result['volumn'] = float(a[9]) # 成交金额,单位为“元”,若要以“万元”为成交金额的单位,需要把该值除以一万
|
||||
result['bid1_amount'] = float(a[10]) # “买一”申请4695股,即47手
|
||||
result['bid1'] = float(a[11]) # “买一”报价
|
||||
result['bid2_amount'] = float(a[12])
|
||||
result['bid2'] = float(a[13])
|
||||
result['bid3_amount'] = float(a[14])
|
||||
result['bid3'] = float(a[15])
|
||||
result['bid4_amount'] = float(a[16])
|
||||
result['bid4'] = float(a[16])
|
||||
result['bid5_amount'] = float(a[18])
|
||||
result['bid5'] = float(a[19])
|
||||
result['ask1_amount'] = float(a[20]) # “卖一”申报3100股,即31手
|
||||
result['ask1'] = float(a[21]) #“卖一”报价
|
||||
result['ask2_amount'] = float(a[22])
|
||||
result['ask2'] = float(a[23])
|
||||
result['ask3_amount'] = float(a[24])
|
||||
result['ask3'] = float(a[25])
|
||||
result['ask4_amount'] = float(a[26])
|
||||
result['ask4'] = float(a[27])
|
||||
result['ask5_amount'] = float(a[28])
|
||||
result['ask5'] = float(a[29])
|
||||
result['date'] = a[30]
|
||||
result['time'] = a[31]
|
||||
return result
|
||||
|
||||
|
||||
def request_func(querystr):
|
||||
try:
|
||||
result = requests.get(querystr).text
|
||||
except Exception as e:
|
||||
print(result)
|
||||
print(e)
|
||||
return
|
||||
|
||||
result = result.split('\n')
|
||||
for tmpstr in result:
|
||||
parse_one_result(tmpstr)
|
||||
|
||||
return
|
||||
def request_data(querystr):
|
||||
proxy = get_proxy()
|
||||
proxies = {'http': 'http://{}'.format(proxy)} if proxy else None
|
||||
print(proxies)
|
||||
query = requests.get(querystr, proxies=proxies).text
|
||||
query = query.split('\n')
|
||||
result = []
|
||||
for tmpstr in query:
|
||||
one_record = parse_one_result(tmpstr)
|
||||
if one_record is not None:
|
||||
result.append(one_record)
|
||||
return result
|
||||
|
||||
|
||||
def get_spot_from_sina(stocklist):
|
||||
@ -47,16 +75,21 @@ def get_spot_from_sina(stocklist):
|
||||
max_size = 140
|
||||
count = 0
|
||||
tmpstr = queryStr
|
||||
result = []
|
||||
for stock in stocklist:
|
||||
tmpstr += ("%s,") % (stock)
|
||||
count = count + 1
|
||||
count += 1
|
||||
if count >= max_size:
|
||||
request_func(tmpstr)
|
||||
phase_result = request_data(tmpstr)
|
||||
if phase_result:
|
||||
result.extend(phase_result)
|
||||
count = 0
|
||||
tmpstr = queryStr
|
||||
|
||||
if tmpstr != queryStr:
|
||||
request_func(tmpstr)
|
||||
phase_result = request_data(tmpstr)
|
||||
if phase_result:
|
||||
result.extend(phase_result)
|
||||
return result
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
@ -13,7 +13,6 @@ from .mylog import *
|
||||
from .check import *
|
||||
|
||||
__all__ = [
|
||||
'escapetime', 'class_logger', 'add_class_logger_handler', 'HKU_TRACE', 'HKU_DEBUG', 'HKU_TRACE',
|
||||
'HKU_INFO', 'HKU_WARN', 'HKU_ERROR', 'HKU_FATAL', 'HKUCheckError', 'HKU_CHECK',
|
||||
'HKU_CHECK_THROW'
|
||||
'spend_time', 'hku_logger', 'class_logger', 'add_class_logger_handler', 'HKUCheckError',
|
||||
'hku_check', 'hku_check_throw', 'hku_check_ignore', 'hku_catch'
|
||||
]
|
||||
|
@ -7,7 +7,10 @@
|
||||
# 1. 20200821, Added by fasiondog
|
||||
#==========================================================
|
||||
|
||||
from .mylog import mylogger
|
||||
import sys
|
||||
import traceback
|
||||
from functools import wraps
|
||||
from .mylog import hku_logger
|
||||
|
||||
|
||||
class HKUCheckError(Exception):
|
||||
@ -16,7 +19,16 @@ class HKUCheckError(Exception):
|
||||
self.message = message
|
||||
|
||||
def __str__(self):
|
||||
return self.message
|
||||
return str(self.message)
|
||||
|
||||
|
||||
class HKUIngoreError(Exception):
|
||||
def __init__(self, expression, message=None):
|
||||
self.expression = expression
|
||||
self.message = message if message is not None else 'ignore'
|
||||
|
||||
def __str__(self):
|
||||
return str(self.message)
|
||||
|
||||
|
||||
def checkif(expression, message, excepion=None, **kwargs):
|
||||
@ -33,12 +45,12 @@ def checkif(expression, message, excepion=None, **kwargs):
|
||||
raise excepion(message, **kwargs)
|
||||
|
||||
|
||||
def HKU_CHECK(exp, msg):
|
||||
def hku_check(exp, msg):
|
||||
if not exp:
|
||||
raise HKUCheckError(exp, msg)
|
||||
|
||||
|
||||
def HKU_CHECK_THROW(expression, message, excepion=None, **kwargs):
|
||||
def hku_check_throw(expression, message, excepion=None, **kwargs):
|
||||
"""如果 expression 为 False,则抛出异常。
|
||||
|
||||
:param boolean expression: 判断条件
|
||||
@ -50,3 +62,46 @@ def HKU_CHECK_THROW(expression, message, excepion=None, **kwargs):
|
||||
raise HKUCheckError(expression, message)
|
||||
else:
|
||||
raise excepion(message, **kwargs)
|
||||
|
||||
|
||||
def hku_check_ignore(exp, msg=None):
|
||||
"""可忽略的检查"""
|
||||
if not exp:
|
||||
raise HKUIngoreError(exp, msg)
|
||||
|
||||
|
||||
def get_exception_info():
|
||||
info = sys.exc_info()
|
||||
return "{}: {}".format(info[0].__name__, info[1])
|
||||
|
||||
|
||||
def hku_catch(ret=None, trace=False):
|
||||
"""捕获发生的异常, 包装方式: @hku_catch()
|
||||
:param ret: 异常发生时返回值
|
||||
:param boolean trace: 打印异常堆栈信息
|
||||
"""
|
||||
def hku_catch_wrap(func):
|
||||
def wrappedFunc(*args, **kargs):
|
||||
try:
|
||||
return func(*args, **kargs)
|
||||
except HKUIngoreError:
|
||||
hku_logger.debug(
|
||||
"{} [{}.{}]".format(get_exception_info(), func.__module__, func.__name__)
|
||||
)
|
||||
except Exception:
|
||||
hku_logger.error(
|
||||
"{} [{}.{}]".format(get_exception_info(), func.__module__, func.__name__)
|
||||
)
|
||||
traceback.print_exc()
|
||||
except:
|
||||
hku_logger.error(
|
||||
"Unknown error! {} [{}.{}]".format(
|
||||
get_exception_info(), func.__module__, func.__name__
|
||||
)
|
||||
)
|
||||
traceback.print_exc()
|
||||
return ret
|
||||
|
||||
return wrappedFunc
|
||||
|
||||
return hku_catch_wrap
|
@ -8,7 +8,7 @@ import time
|
||||
from functools import wraps
|
||||
|
||||
|
||||
def escapetime(func):
|
||||
def spend_time(func):
|
||||
@wraps(func)
|
||||
def wrappedFunc(*args, **kargs):
|
||||
starttime = time.time()
|
||||
@ -17,16 +17,16 @@ def escapetime(func):
|
||||
return func(*args, **kargs)
|
||||
finally:
|
||||
endtime = time.time()
|
||||
print("Escaped time: %.4fs, %.2fm" % (endtime - starttime, (endtime - starttime) / 60))
|
||||
print("spend time: %.4fs, %.2fm" % (endtime - starttime, (endtime - starttime) / 60))
|
||||
|
||||
return wrappedFunc
|
||||
|
||||
|
||||
FORMAT = '%(asctime)-15s [%(levelname)s] : %(message)s [%(name)s::%(funcName)s]'
|
||||
FORMAT = '%(asctime)-15s [%(levelname)s] %(message)s [%(name)s::%(funcName)s]'
|
||||
logging.basicConfig(format=FORMAT)
|
||||
|
||||
mylogger_name = 'hikyuu'
|
||||
mylogger = logging.getLogger(mylogger_name)
|
||||
hku_logger_name = 'hikyuu'
|
||||
hku_logger = logging.getLogger(hku_logger_name)
|
||||
|
||||
|
||||
def class_logger(cls, enable=False):
|
||||
@ -53,27 +53,3 @@ def add_class_logger_handler(handler, class_list, level=logging.INFO):
|
||||
logger = logging.getLogger("{}".format(cls.__name__))
|
||||
logger.addHandler(handler)
|
||||
logger.setLevel(level)
|
||||
|
||||
|
||||
def HKU_TRACE(msg):
|
||||
mylogger.debug(msg)
|
||||
|
||||
|
||||
def HKU_DEBUG(msg):
|
||||
mylogger.debug(msg)
|
||||
|
||||
|
||||
def HKU_INFO(msg):
|
||||
mylogger.info(msg)
|
||||
|
||||
|
||||
def HKU_WARN(msg):
|
||||
mylogger.warning(msg)
|
||||
|
||||
|
||||
def HKU_ERROR(msg):
|
||||
mylogger.error(msg)
|
||||
|
||||
|
||||
def HKU_FATAL(msg):
|
||||
mylogger.critical(msg)
|
||||
|
Loading…
Reference in New Issue
Block a user