1 Star 0 Fork 0

jx0913/stock

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
get_break_high_low.py 3.47 KB
一键复制 编辑 原始数据 按行查看 历史
Rocky Chen 提交于 2019-06-17 11:19 . 只关注持仓
# -*-coding=utf-8-*-
import time
__author__ = 'rocky'
# 获取破指定天数内的新高 比如破60日新高
import tushare as ts
import datetime
import pandas as pd
from setting import get_engine, get_mysql_conn
import pymongo
from config import token
from filter_stock import Filter_Stock
MONGO_HOST = '10.18.6.46'
MONGO_PORT = 27001
class BreakPoint(object):
def __init__(self):
self.engine = get_engine('db_stock', local=True)
self.conn = get_mysql_conn('db_stock', local='local')
self.info = pd.read_sql('tb_basic_info', con=self.engine, index_col='code')
self.db = pymongo.MongoClient(MONGO_HOST, MONGO_PORT)
self.doc = self.db['db_stock']['break_low_high']
ts.set_token(token)
self.pro = ts.pro_api()
self.count = 0
# 获取新高,新低
def loop_stocks(self, day):
total = 200 + 10
each_loop = 60 / total
for idx, row in self.info.iterrows():
stock_code = idx
print('Checking {}'.format(stock_code))
self.is_break(stock_code, day, stock_type='stock')
time.sleep(each_loop)
def code_convert(self, code):
if code[0] == '6':
return code + '.SH'
else:
return code + '.SZ'
def is_break(self, stockID, day, stock_type):
end_day = datetime.datetime.now()
days = day * 7 / 5
# 考虑到周六日非交易
start_day = end_day - datetime.timedelta(days)
start_day = start_day.strftime("%Y%m%d")
end_day = end_day.strftime("%Y%m%d")
name = self.info.ix[stockID]['name']
# get_h_data 有问题。
try:
# df = ts.get_k_data(stockID, start=start_day, end=end_day)
ts_code = self.code_convert(stockID)
df = ts.pro_bar(ts_code=ts_code, adj='qfq', start_date=start_day, end_date=end_day)
# df 第0个数据是最新的。
except Exception as e:
print(e)
print('{} {}获取行情识别'.format(stockID, name))
time.sleep(30)
return False
if df is None or df.empty:
print('{} {} df is None or empty'.format(stockID, name))
return False
if len(df) < 5:
print('上市时间太短'.format(stockID, name))
return False
period_high = df['close'][1:].max()
today_high = df.iloc[0]['high']
if today_high >= period_high:
stock_h = []
stock_h.append(stockID)
stock_h.append(name)
insert_dict = {'类型': '新高', '范围': days, '名称': name, '代码': stockID, 'run_time': datetime.datetime.now(),
'品种': stock_type, '开始日期': start_day, '结束日期': end_day}
self.doc.insert_one(insert_dict)
period_low = df['close'][1:].min()
today_low = df.iloc[0]['low']
if today_low <= period_low:
stock_l = []
name = self.info.ix[stockID]['name']
stock_l.append(stockID)
stock_l.append(name)
print('新低', stock_l)
insert_dict = {'类型': '新低', '范围': days, '名称': name, '代码': stockID, 'run_time': datetime.datetime.now(),
'品种': stock_type, '开始日期': start_day, '结束日期': end_day}
self.doc.insert_one(insert_dict)
if __name__ == '__main__':
obj = BreakPoint()
cal_day = 90
obj.loop_stocks(cal_day)
print("Done")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jx0913/stock.git
git@gitee.com:jx0913/stock.git
jx0913
stock
stock
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385