def updateOne(filter, col: str, doc, upsert=False): client = pymongo.MongoClient(ct.conf('MONGODB')['uri']) db = client.stock updateResult = db[col].update_one(filter, {"$set": doc}, upsert) client.close() return ((0 if updateResult.upserted_id is None else 1) + updateResult.modified_count)
def get_hs_stock_list(param): """ 沪深股市股票列表 Parameters ------ Dict is_hs: 是否沪深港通标的,N否 H沪股通 S深股通 list_status: 上市状态: L上市 D退市 P暂停上市 exchange: 交易所 SSE上交所 SZSE深交所 HKEX港交所(未上线) fields: ts_code,symbol,name,area,industry,list_date Return ------- DataFrame 股票列表(DataFrame): ts_code ts股票代码 symbol 市场代码 name 名称 area 上市地区 industry 行业 list_date 上市日期 """ ts.set_token(ct.conf('TOKEN')) pro = ts.pro_api() df = pro.stock_basic( list_status=param['list_status'], exchange=param['exchange'], filelds=param['fields']) new = pd.DataFrame({'ts_code': ['399300.SZ'], 'symbol': ['399300']}) df = df.append(new) return df
def get_adj_price(param): """ 获取复权行情 Parameters ------ Dict ts_code: str ts股票代码 start_date: str 开始日期 (格式:YYYYMMDD) end_date: str 结束日期 (格式:YYYYMMDD) adj: 复权类型(只针对股票):None未复权 qfq前复权 hfq后复权 , 默认None freq: 数据频度 :1MIN表示1分钟(1/5/15/30/60分钟) D日线 ,默认D ma: list 均线,支持任意周期的均价和均量,输入任意合理int数值 Return ------- DataFrame 股票列表(DataFrame): ts_code ts股票代码 symbol 市场代码 name 名称 area 上市地区 industry 行业 list_date 上市日期 """ ts.set_token(ct.conf('TOKEN')) ts.pro_api() df = ts.pro_bar(ts_code=param['ts_code'], adj=param['adj'], start_date=param['start_date'], asset=param['asset'], freq=param['freq'], end_date=param['end_date']) return df
def redisHGet(key: str, field): connConfig = ct.conf('REDIS') conn = redis.Redis(host=connConfig['host'], port=connConfig['port'], password=connConfig['password']) val = conn.hget(connConfig['keyPrefix'] + key, field) return val
def redisSet(key: str, value) -> bool: connConfig = ct.conf('REDIS') conn = redis.Redis(host=connConfig['host'], port=connConfig['port'], password=connConfig['password']) val = conn.set(connConfig['keyPrefix'] + key, value) return val
def queryMany(_filter, _projection, _limit, _sort, col: str): client = pymongo.MongoClient(ct.conf('MONGODB')['uri']) db = client.stock result = db[col].find(filter=_filter, projection=_projection, limit=_limit, sort=_sort) client.close() return result
def create_index(index_name: str): ret_jsons = None try: html = requests.put(ct.conf('ES')['url'] + index_name) ret_jsons = json.loads(html.text) except Exception as err: logger.error(err) return None return ret_jsons
def delete_document(index_name: str, key: str): ret_jsons = None try: html = requests.delete( ct.conf('ES')['url'] + index_name + '/_doc/' + key) ret_jsons = json.loads(html.text) except Exception as err: logger.error(err) return None return ret_jsons
def insert_document(index_name: str, document: str, key: str): ret_jsons = None headers = {"Content-Type": "application/json"} try: html = requests.put(ct.conf('ES')['url'] + index_name + '/_doc/' + key, data=document, headers=headers) ret_jsons = json.loads(html.text) except Exception as err: logger.error(err) return None return ret_jsons
def create_index_setting(index_name: str, settings: str): ret_jsons = None try: headers = {"Content-Type": "application/json"} html = requests.put(ct.conf('ES')['url'] + index_name, data=settings, headers=headers) ret_jsons = json.loads(html.text) except Exception as err: logger.error(err) return None return ret_jsons
def create_mapping(index_name: str, mapping_obj: str): ret_jsons = None headers = {"Content-Type": "application/json"} try: html = requests.post(ct.conf('ES')['url'] + index_name + '/_doc/_mapping', data=mapping_obj, headers=headers) ret_jsons = json.loads(html.text) except Exception as err: logger.error(err) return None return ret_jsons
def his_fq_stock_price_task(): logger.info(f'*******his_fq_stock_price_task start*******') back_track_datys = ct.conf('BACK_TRACK_DAYS') exchange = '' if len(sys.argv) > 2 and sys.argv[2].isdigit(): back_track_datys = int(sys.argv[2]) # 获取交易市场 if len(sys.argv) > 3 and (sys.argv[3] == 'SSE' or sys.argv[3] == 'SZSE'): exchange = sys.argv[3] # 获取个股代码 if len(sys.argv) > 4: given_stock = sys.argv[4] task.run_his_given_stock_adj_price_task(given_stock, back_track_datys) logger.info( f'*******given stock****{given_stock}**{back_track_datys}*') return logger.info( f'fetch stock price data {back_track_datys} days ago market is \ {exchange} ') task.run_his_stock_adj_price_task(back_track_datys, exchange) logger.info(f'*******his_fq_stock_price_task end*******')
def queryMinLowPrice(col: str, code: str, date: int) -> []: client = pymongo.MongoClient(ct.conf('MONGODB')['uri']) db = client.stock query = [{ '$match': { 'code': code, 'date': { '$gt': date } } }, { '$group': { '_id': 'min', 'min_value': { '$min': '$low' } } }] result = None docs = db[col].aggregate(query) for doc in docs: result = doc client.close() return result
import sys from loguru import logger from config import cons as ct from elasticsearch import init_es from elasticsearch import es from stock import task if __name__ == "__main__": logger.add(sys.stderr, format='{time} {level} {message}', filter="es", level="ERROR") logger.add(sys.stdout, colorize=True, format="<green>{time}</green> <level>{message}</level>", level="INFO") logger.add("es_{time}.log", rotation="1 day", level="INFO") env = ct.conf('ENV') logger.info(f'env is {env}') es.delete_index('cbond') es.delete_index('stock') init_es.init_cbond_es() init_es.init_stock_es() task.run_stock_insert_es_task() task.run_his_cb_basic_ino_task()