def read_signals(cls, model_from=None, field=None, **kw): """ query signals from DB :param model_from: :param field:属性域 :return: """ try: sql = dict() if model_from is not None: sql = {"model_from": model_from} sql = dict(sql, **kw) cursor = MODEL_TABLE(cls.location, cls.dbname, 'signals').query(sql, field) if cursor.count(): data = pd.DataFrame(list(cursor)) # data['date'] = pd.to_datetime(data.date) # data = cls.merge_time(data) data = data.sort_values(['open_date'], ascending=False) data = data.reset_index(drop=True) # data.drop(['_id', 'classtype'], axis=1, inplace=True) # data = data.drop_duplicates(['stock_code', 'date', 'time']) # data.stock_code.astype('int') cursor.close() return data else: cursor.close() return pd.DataFrame([]) except Exception: raise MongoIOError('query signals from db raise a error')
def read_his_signals(cls, field=None, **kw): """ 读取持有过的股票数据,包括在回测中买入过的股票 :param kw: :return: """ try: sql = kw cursor = MODEL_TABLE(cls.location, cls.dbname, 'signals_his').query(sql, field) if cursor.count(): data = pd.DataFrame(list(cursor)) return data else: return pd.DataFrame([]) except Exception: raise MongoIOError('query positions from db raise a error') # @classmethod # def push_macd_signals(cls, model_name='traitor_a', date='2017-12-25'): # try: # data = md.get_signals(model_from=MODEL_WEB_NAME[model_name], date=date) # bins = [0, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 100000] # group_index = [1, 2, 3, 4, 5, 6, 7, 8, 0] # data['rmd'] = pd.cut(data.confidence, bins=bins, labels=group_index, right=False) # data['status'] = 1 # print(data.head()) # return data.loc[:, ['stock_code', 'date', 'buy_price', 'rmd', 'status']] # except Exception: # ExceptionInfo() # return pd.DataFrame()
def read_account_info(cls, **kw): """ 读取账户-订阅 :return: """ try: cursor = MODEL_TABLE(cls.location, cls.dbname, 'accounts').query(kw) if cursor.count(): data = pd.DataFrame(list(cursor)) return data else: return pd.DataFrame() except Exception as e: ExceptionInfo(e) return pd.DataFrame() pass
def read_his_signals(cls, field=None, **kw): """ 读取持有过的股票数据,包括在回测中买入过的股票 :param kw: :return: """ try: sql = kw cursor = MODEL_TABLE(cls.location, cls.dbname, 'signals_his').query(sql, field) if cursor.count(): data = pd.DataFrame(list(cursor)) return data else: return pd.DataFrame([]) except Exception: raise MongoIOError('query positions from db raise a error')
def read_finance_index(cls, model_from, field=None, **kwargs): """ 读取模型回测的财务指标数据 :return: """ try: sql = {'model_from': model_from} sql = dict(sql, **kwargs) cursor = MODEL_TABLE(cls.location, cls.dbname, 'financeindex').query(sql, field) if cursor.count(): fis = pd.DataFrame(list(cursor)) fis.drop(['_id', 'classtype'], axis=1, inplace=True) return fis return pd.DataFrame() except Exception: raise MongoIOError( 'query finance indicators from db raise a error')
def read_orders(cls, table_name='orders', field=None, **kw): """ 读取持仓信息,可以读取包括orders,orders_simulated,orders_his三个表的数据 :param table_name: :param field: :param kw: :return: """ try: cursor = MODEL_TABLE(cls.location, cls.dbname, table_name).query(kw, field) if cursor.count(): ods = pd.DataFrame(list(cursor)) return ods return pd.DataFrame() except Exception: raise MongoIOError('query orders data from db raise a error')
def read_his_orders(cls, model_from, start_date=None, end_date=None, field=None, **kw): """ 读取持仓的数据 :param end_date: :param start_date: :param model_from: :param kw: :return: """ try: sql = {'model_from': model_from} if start_date is None and end_date is None: pass else: if start_date is not None and end_date is None: date = {'close_date': {'$gte': start_date}} elif end_date is not None and start_date is None: date = {'close_date': {'$lte': end_date}} else: date = { 'close_date': { '$gte': start_date, '$lte': end_date } } sql = dict(sql, **date) sql = dict(sql, **kw) cursor = MODEL_TABLE(cls.location, cls.dbname, 'orders_his').query(sql, field) if cursor.count(): ods = pd.DataFrame(list(cursor)) # ods.drop(['_id', 'classtype'], axis=1, inplace=True) return ods return pd.DataFrame() except Exception: raise MongoIOError('query orders data from db raise a error')
def read_client_info(cls, model_from, field=None, **kw): """ :param model_from: :return: """ try: sql = {'model_from': model_from} sql = dict(sql, **kw) cursor = MODEL_TABLE(cls.location, cls.dbname, 'clients').query(sql, field) if cursor.count(): clis = pd.DataFrame(list(cursor)) cursor.close() return clis cursor.close() return pd.DataFrame() except Exception as e: ExceptionInfo(e) return pd.DataFrame()
def read_account_info(cls, model_from, field=None, **kw): """ 读取现有的账户信息 :return: """ try: sql = {'model_from': model_from} sql = dict(sql, **kw) cursor = MODEL_TABLE(cls.location, cls.dbname, 'asset').query(sql, field) if cursor.count(): ai = pd.DataFrame(list(cursor)) ai.drop(['classtype'], axis=1, inplace=True) ai = ai.sort_values(['date'], ascending=False) ai = ai.drop_duplicates(['client_no'], keep='first') cursor.close() return ai cursor.close() return pd.DataFrame() except Exception as e: ExceptionInfo(e) return pd.DataFrame()
def read_orders_(cls, model_from=None, **kw): """ 读取持仓的数据,一个特殊的实现 :param model_from: :param kw: :return: """ try: sql = dict() if model_from is not None: sql = {'model_from': model_from} sql = dict(sql, **kw) cursor = MODEL_TABLE(cls.location, cls.dbname, 'orders').query(sql) if cursor.count(): ods = pd.DataFrame(list(cursor)) ods = cls.merge_time(ods, 'date', 'time') ods = cls.merge_time(ods, 'max_pst_date', 'pst_time') ods.drop(['_id', 'classtype'], axis=1, inplace=True) return ods return pd.DataFrame() except Exception: raise MongoIOError('query orders data from db raise a error')