Beispiel #1
0
 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')
Beispiel #2
0
 def read_pst(cls, stock_code=None, date=None, **kw):
     """
     读取仓位
     :param stock_code:
     :param date:
     :param kw:
     :return:
     """
     try:
         sql = dict()
         if stock_code is not None:
             sql['stock_code'] = stock_code
         if date is not None:
             sql = dict(sql, **analyzer("date = {d}".format(d=date)))
         sql = dict(sql, **kw)
         cursor = MODEL_TABLE(cls.location, cls.dbname,
                              'position').query(sql)
         pst = list(cursor)
         if len(pst):
             pst = pd.DataFrame(pst)
             pst.drop(['_id', 'classtype'], axis=1, inplace=True)
             return pst
         return pd.DataFrame()
     except Exception:
         raise MongoIOError('query position from db raise a error')
Beispiel #3
0
    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()
Beispiel #4
0
 def remove_data(cls, kline, **kw):
     """
     删除K线数据
     :param kline:
     :param kw:
     :return:
     """
     try:
         KLINE_MODEL_TABLE(cls.location, cls.dbname, kline).remove(kw)
     except Exception:
         raise MongoIOError('Failed with delete data by MongoDB')
Beispiel #5
0
 def remove_data(cls, table_name, **kw):
     """
     删除数据
     :param table_name:
     :param kw:
     :return:
     """
     try:
         BaseModel(table_name, cls.location, cls.dbname).remove(kw)
     except Exception:
         raise MongoIOError('Failed with delete data by MongoDB')
Beispiel #6
0
 def update_signals(cls, condition, **kw):
     """
     按condition条件更新K线数据
     :param condition: 形如{‘date':datetime.datetime(2018,1,1)}的一个字典
     :param kw:形如close=0这样的参数组
     :return:
     """
     try:
         MODEL_TABLE(cls.location, cls.dbname, 'signals').update_batch(condition, kw)
     except Exception:
         raise MongoIOError('Failed with update by MongoDB')
Beispiel #7
0
 def remove_orders(cls, table_name='orders', **kw):
     """
     移除订单
     :param table_name:
     :param kw:
     :return:
     """
     try:
         r = MODEL_TABLE(cls.location, cls.dbname, table_name).remove(kw)
         return r
     except Exception:
         raise MongoIOError('Failed with removing data ')
Beispiel #8
0
 def field(cls, table_name, field_name):
     """
     Query the value of a field in the database
     :param table_name: the database's table name
     :param field_name: the table's field name
     :return: all values in database
     """
     try:
         return BaseModel(table_name, cls.location,
                          cls.dbname).distinct(field_name)
     except Exception:
         raise MongoIOError('query the field raise a error')
Beispiel #9
0
 def remove_data(self, table_name, **kw):
     """
     删除数据
     :param table_name:
     :param kw:
     :return:
     """
     try:
         r = BaseModel(table_name, self.location, self.dbname).remove(kw)
         return r
     except Exception:
         raise MongoIOError('Failed with delete data by MongoDB')
Beispiel #10
0
 def update_orders(cls, condition, table_name='orders', **kw):
     """
     按condition条件更新K订单数据
     :param table_name:
     :param condition: 形如{‘date':datetime.datetime(2018,1,1)}的一个字典
     :param kw:形如close=0这样的参数组
     :return:
     """
     try:
         MODEL_TABLE(cls.location, cls.dbname,
                     table_name).update_batch(condition, kw)
     except Exception:
         raise MongoIOError('Failed with update by MongoDB')
Beispiel #11
0
 def field(cls, table_name, field_name, filter=None):
     """
     Query the value of a field in the database
     :param filter: dict
     :param table_name: the database's table name
     :param field_name: the table's field name
     :return: all values in database
     """
     try:
         return KLINE_MODEL_TABLE(cls.location, cls.dbname,
                                  table_name).distinct(field_name, filter)
     except Exception:
         raise MongoIOError('query the field raise a error')
Beispiel #12
0
 def insert_data(cls, table_name, data):
     """
     一个简易的数据插入接口
     :param table_name:
     :param data:
     :return:
     """
     try:
         if len(data):
             d = data.to_dict(orient='records')
             BaseModel(table_name, cls.location, cls.dbname).insert_batch(d)
     except Exception:
         raise MongoIOError('Failed with insert data by MongoDB')
Beispiel #13
0
 def update_date(cls, table_name, condition, **kw):
     """
     按condition条件更新table_name表数据
     :param table_name:
     :param condition: 形如{‘date':datetime.datetime(2018,1,1)}的一个字典
     :param kw:形如close=0这样的参数组
     :return:
     """
     try:
         BaseModel(table_name, cls.location,
                   cls.dbname).update_batch(condition, kw)
     except Exception:
         raise MongoIOError('Failed with update by MongoDB')
Beispiel #14
0
 def insert_finance_index(cls, fis):
     """
     保存模型回测过程中的一些财务指标
     :param fis:
     :return:
     """
     try:
         if len(fis):
             d = fis.to_dict(orient='records')
             MODEL_TABLE(cls.location, cls.dbname,
                         'financeindex').insert_batch(d)
     except Exception:
         raise MongoIOError('Failed with insert data by MongoDB')
Beispiel #15
0
 def insert_trade_menu(cls, menus):
     """
     记录日交易所获得的收益
     :param menus:
     :return:
     """
     try:
         if len(menus):
             d = menus.to_dict(orient='records')
             MODEL_TABLE(cls.location, cls.dbname,
                         'trademenu').insert_batch(d)
     except Exception:
         raise MongoIOError('Failed with insert data by MongoDB')
Beispiel #16
0
 def remove_signals(cls, model_from, **kw):
     """
     根据条件删除signals表当中的信号,其实只是将信号从signals转移到了信号历史
     表当中
     :param model_from:
     :param kw:
     :return:
     """
     try:
         sql = {'model_from': model_from}
         sql = dict(sql, **kw)
         MODEL_TABLE(cls.location, cls.dbname, 'signals').remove(sql)
     except Exception:
         raise MongoIOError('Failed with removing data ')
Beispiel #17
0
 def insert_one(self, table_name, data, add_id=False):
     """
     insert one record
     :param table_name:
     :param data: a dict
     :param add_id:
     :return:
     """
     try:
         if add_id:
             data['_id'] = ObjectId()
         BaseModel(table_name, self.location, self.dbname).insert(data)
     except Exception:
         raise MongoIOError('Failed with insert data by MongoDB')
Beispiel #18
0
 def update_data(cls, kline, condition, **kw):
     """
     按condition条件更新K线数据
     :param kline:
     :param condition: 形如{‘date':datetime.datetime(2018,1,1)}的一个字典
     :param kw:形如close=0这样的参数组
     :return:
     """
     try:
         r = KLINE_MODEL_TABLE(cls.location, cls.dbname,
                               kline).update_batch(condition, kw)
         return r
     except Exception:
         raise MongoIOError('Failed with update by MongoDB')
Beispiel #19
0
 def insert_data(cls, kline, data):
     """
     插入K线数据
     :param kline:
     :param data:
     :return:
     """
     try:
         if len(data):
             d = data.to_dict(orient='records')
             KLINE_MODEL_TABLE(cls.location, cls.dbname,
                               kline).insert_batch(d)
     except Exception:
         raise MongoIOError('Failed with insert data by MongoDB')
Beispiel #20
0
 def insert_risk(cls, risks):
     """
     插入风险参数
     :param risks:
     :return:
     """
     try:
         if len(risks):
             dit = []
             for i, row in risks.iterrows():
                 r = dict(row)
                 dit.append(r)
             MODEL_TABLE(cls.location, cls.dbname, 'risk').insert_batch(dit)
     except Exception:
         raise MongoIOError('Failed with insert data by MongoDB')
Beispiel #21
0
 def update_data(self, table_name, condition, **kw):
     """
     按condition条件更新table_name表数据
     :param table_name:
     :param condition: 形如{‘date':datetime.datetime(2018,1,1)}的一个字典
     :param kw:形如close=0这样的参数组
     :return:
     """
     try:
         r = BaseModel(table_name, self.location,
                       self.dbname).update_batch(condition, kw)
         return r
     except Exception as e:
         ExceptionInfo(e)
         raise MongoIOError('Failed with update by MongoDB')
Beispiel #22
0
 def insert_pst(cls, pst):
     """
     插入仓位
     :param pst:
     :return:
     """
     try:
         if len(pst):
             dit = []
             for i, row in pst.iterrows():
                 r = dict(row)
                 dit.append(r)
             MODEL_TABLE(cls.location, cls.dbname,
                         'position').insert_batch(dit)
     except Exception:
         raise MongoIOError('Failed with insert data by MongoDB')
Beispiel #23
0
 def insert_update_log(cls, kline, status, **kw):
     """
     插入基本K线数据更新的日志, 这个更新不同于修改,是指插入了某个周期的某个
     时点的Bar数据
     :param status:
     :param kline:
     :param kw:
     :return:
     """
     try:
         log = dict(kline=kline, status=status, date=dt.datetime.now())
         log = dict(log, **kw)
         KLINE_MODEL_TABLE(cls.location, cls.dbname,
                           'data_logs').insert(log)
     except Exception:
         raise MongoIOError('insert kline data update log to db raise a error')
Beispiel #24
0
def MODEL_TABLE(location=None, dbname=None, tablename=None):
    """
    数据源的映射,主要是指一些公共的数据源
    :param location:
    :param dbname:
    :param tablename:
    :return:
    """
    if tablename == 'signals_his':
        return SellList('signals_his', location, dbname)
    elif tablename == 'signals':
        return signal('signals', location, dbname)
    elif tablename == 'orders':
        return Orders('orders', location, dbname)
    elif tablename == 'orders_simulated':
        return BaseModel('orders_simulated', location, dbname)
    elif tablename == 'orders_his':
        return OrdersHis('orders_his', location, dbname)
    elif tablename == 'trademenu':
        return TradeMenu('trademenu', location, dbname)
    elif tablename == 'financeindex':
        return ModelFinanceIndex('financeindex', location, dbname)
    elif tablename == 'risk_and_position':
        return Risk_and_Position('risk_and_position', location, dbname)
    elif tablename == 'risk':
        return Risk('risk', location, dbname)
    elif tablename == 'position':
        return Position('position', location, dbname)
    elif tablename == 'naughtiers':
        return Naughtiers('naughtiers', location, dbname)
    elif tablename == 'inflexion':
        return Inflexion('inflexion', location, dbname)
    elif tablename == 'buy_point':
        return Buy_Point('buy_point', location, dbname)
    elif tablename == 'asset':
        return Asset('asset', location, dbname)
    elif tablename == 'rmds_his':
        return RmdsHis('rmds_his', location, dbname)
    elif tablename == 'RRADS':
        return rrads('announce', location, dbname)
    elif tablename == 'accounts':
        return BaseModel('accounts', location, dbname)
    elif tablename == 'clients':
        return BaseModel('clients', location, dbname)
    else:
        info = 'not find this "%s" in model list' % tablename
        raise MongoIOError(info)
Beispiel #25
0
 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')
Beispiel #26
0
 def insert_risk_pst(cls, rps):
     """
     插入风险仓位数据
     :param rps: a DataFrame
     :return:
     """
     try:
         if len(rps):
             dit = []
             for i, row in rps.iterrows():
                 r = dict(row)
                 dit.append(r)
             # print(dit[0])
             MODEL_TABLE(cls.location, cls.dbname,
                         'risk_and_position').insert_batch(dit)
     except Exception:
         raise MongoIOError('Failed with insert data by MongoDB')
Beispiel #27
0
 def insert_his_rmds(cls, rmds):
     """
     插入为账户推荐的历史
     :param rmds:
     :return:
     """
     try:
         if len(rmds):
             dit = []
             for i, row in rmds.iterrows():
                 r = dict(row)
                 dit.append(r)
             # print(dit[0])
             MODEL_TABLE(cls.location, cls.dbname,
                         'rmds_his').insert_batch(dit)
     except Exception:
         raise MongoIOError('Failed with insert data by MongoDB')
Beispiel #28
0
 def insert_data(self, table_name, data, add_id=False):
     """
     一个简易的数据插入接口
     :param table_name:
     :param data:
     :param add_id:
     :return:
     """
     try:
         if add_id:
             data['_id'] = data.index.map(lambda x: ObjectId())
         if len(data):
             d = data.to_dict(orient='records')
             BaseModel(table_name, self.location,
                       self.dbname).insert_batch(d)
     except Exception:
         raise MongoIOError('Failed with insert data by MongoDB')
Beispiel #29
0
 def insert_client_info(cls, client):
     """
     插入订阅账户信息
     :param client:
     :return:
     """
     try:
         if len(client):
             # 客户信息表必须包含的字段,缺省的填0
             col = ['client_no', 'model_from', 'update', 'status', 'Rr']
             if set(col) <= set(client.columns):
                 d = client.to_dict(orient='records')
                 MODEL_TABLE(cls.location, cls.dbname,
                             'clients').insert_batch(d)
             else:
                 raise Exception('lost must field')
     except Exception:
         raise MongoIOError('Failed with insert data by MongoDB')
Beispiel #30
0
 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')