Beispiel #1
0
 def get_all_stock_code_info(cls, date=None, time=None, kline='kline_min5'):
     sql = analyzer('date = {} and time = {}'.format(date, time))
     data = pd.DataFrame(list(BaseModel(kline).query(sql)))
     if len(data) == 0:
         return pd.DataFrame()
     data['pro'] = data.close / data.open - 1
     #以上是把数据从数据库中取出
     #并且求出各个时刻的收益
     #注意,如果要求收益的计算方式close/pre_close-1,则close需要通过shift()调整
     data2 = None
     if time == 1500:
         #如果time是1500,需要把明天的第一根取出来计算
         x = Calendar.calc(analyzer('date = {}'.format(date))['date'],
                           1)['date']
         sql = analyzer('date = {} and time = {}'.format(
             str(x.year * 10000 + x.month * 100 + x.day), 935))
         data2 = pd.DataFrame(list(BaseModel(kline).query(sql)))
         if len(data2) == 0:
             return pd.DataFrame()
         data = pd.merge(data, data2, on='stock_code')
         data['profit'] = (data.close_y / data.close_x - 1)
         return data.loc[:, [
             'stock_code', 'date_x', 'time_x', 'profit', 'pro'
         ]]
     else:
         time_index = KlineBase.get_time_index(time)
         sql = analyzer('date = {} and time = {}'.format(date, time_index))
         data2 = pd.DataFrame(list(BaseModel(kline).query(sql)))
         if len(data2) == 0:
             return pd.DataFrame()
         data = pd.merge(data, data2, on='stock_code')
         data['profit'] = (data.close_y / data.close_x - 1)
         return data.loc[:, [
             'stock_code', 'date_x', 'time_x', 'profit', 'pro'
         ]]
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_trade_menu(cls,
                     model_from,
                     start_date=None,
                     end_date=None,
                     version=None,
                     **kw):
     """
     读取模型收益数据,读取到的数据主要包括某种策略一个或一段时间所对应的收益
     :param model_from:
     :param start_date:
     :param end_date:
     :param version:
     :param kw:
     :return:
     """
     try:
         sql = {'model_from': model_from}
         if version is not None:
             sql['version'] = version
         if start_date is None and end_date is None:
             pass
         else:
             if start_date is not None and end_date is None:
                 date = analyzer("date >= {s}".format(s=start_date))
             elif end_date is not None and start_date is None:
                 date = analyzer("date <= {e}".format(e=end_date))
             else:
                 date = analyzer("date >= {s} and data <= {e}".format(
                     s=start_date, e=end_date))
             sql = dict(sql, **date)
         sql = dict(sql, **kw)
         cursor = MODEL_TABLE(cls.location, cls.dbname,
                              'trademenu').query(sql)
         menus = list(cursor)
         if len(menus):
             menus = pd.DataFrame(menus)
             menus = menus.sort_values(['date'], ascending=True)
             menus = menus.reset_index(drop=True)
             menus.drop(['_id', 'classtype'], axis=1, inplace=True)
             return menus
         return pd.DataFrame()
     except Exception:
         raise MongoIOError('query trade menu from db raise a error')
Beispiel #4
0
 def upsert_batch(self, condition, *args):
     """
     批量更新
     condition: {field1: sign1, field2: sign2...}
     例如:{'date': '>', 'stock_code': '=' ...}
     args: [{data1}, {data2}...]
     例如:[{'stock_code': 1, 'open': 12.50...}, ...]
     """
     bulk = self.mc.initialize_ordered_bulk_op()
     for item in args[0]:
         qs = ''
         for c, s in condition:
             qs += '{} {} {}'.format(c, s, item[c])
         sql = analyzer(qs)
         bulk.find(sql).upsert().update({'$set': item})
Beispiel #5
0
 def read_risk_pst(cls, stock_code=None, date=None, **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,
                              'risk_and_position').query(sql)
         rps = list(cursor)
         if len(rps):
             rps = pd.DataFrame(rps)
             rps.drop(['_id', 'classtype'], axis=1, inplace=True)
             return rps
         return pd.DataFrame()
     except Exception:
         raise MongoIOError('query risk and position from db raise a error')
Beispiel #6
0
 def read_risk(self, 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(self.location, self.dbname, 'risk').query(sql)
         rks = list(cursor)
         if len(rks):
             rks = pd.DataFrame(rks)
             rks.drop(['_id', 'classtype'], axis=1, inplace=True)
             return rks
         return pd.DataFrame()
     except Exception:
         raise MongoIOError('query risk from db raise a error')