Пример #1
0
 def compute_index_zz500_specifications(self, df):
     # 注意:所有的数据库数据和列表数据都按照日期的正序排序(从小到大)
     # 计算zz500指数的技术指标并入库
     """
     @ 入参:指数k线信息
     """
     am = ArrayManager(size=600)   
     for ix, row in df.iterrows():
         d = row.to_dict()
         LOG.info(d['trade_date'])   
         d['ts_code'] = d['ts_code'].replace('.', '_')
         bar = BarData(
                 gateway_name='ctp', symbol=d['ts_code'],
                 exchange=Exchange.SSE,
                 datetime=string_to_datetime(d['trade_date']))
         bar.symbol = d['ts_code']
         bar.open_price = d['open']
         bar.high_price = d['high']
         bar.low_price = d['low']
         bar.close_price = d['close']
         am.update_bar(bar)      
         rsi_20 = am.rsi(20)
         d['rsi_20'] = rsi_20
         try:
             d['ma_5'] = am.sma(5)
         except:
             traceback.print_exc()                    
             LOG.error('************************')
             LOG.error(d['ts_code'])
             LOG.error(d['trade_date'])
             LOG.error(bar)
         d['ma_10'] = am.sma(10)
         d['ma_20'] = am.sma(20)
         d['ma_30'] = am.sma(30)
         d['ma_60'] = am.sma(60)
         d['ma_120'] = am.sma(120)
         d['ma_250'] = am.sma(250)
         d['ma_500'] = am.sma(500)
         flt = {'trade_date': d['trade_date']}
         cl_index_zz500 = self.db[CL_INDEX_ZZ500]
         # cl_index_zz500.replace_one(flt, d, upsert=False)
         cl_index_zz500.update_one(flt, {'$setOnInsert': d}, upsert=True)   # 插入数据时,flt不存在则插入d,存在则不执行
Пример #2
0
 def _update_k_data(self, code, k_data):
     # 注意:所有的数据库数据和列表数据都按照日期的正序排序(从小到大)
     """
     更新股票,股指每日数据(行情,K线,市值等0)
     @:param code  股票(指数)代码                
     @:param k_data  ts中获取的最新df数据
     """        
     if len(k_data) != 0:
         k_data = k_data.sort_values(by='trade_date')            
         cl_stock_code = self.db[code]
         cl_stock_code.create_index([('trade_date', ASCENDING)], unique=True)
         # 更新k线数据
         # 1、新增日K线入库
         # 2、遍历数据库找出最近的500+22(必须保证更新数据操作在22天以内进行)条数据并更新最后的22条的ma和最高 最低价
         for ix, row in k_data.iterrows():
             d = row.to_dict()
             d['ts_code'] = d['ts_code'].replace('.', '_')
             if self._is_in_vnpy_db(d['ts_code'], update=True):
                 # 更新vnpy数据库数据                    
                 self._build_db_vnpy(d)
             flt = {'trade_date': d['trade_date']}
             cl_stock_code.replace_one(flt, d, upsert=True)
         rec = list(cl_stock_code.find({}).sort("trade_date", DESCENDING).limit(522))
         rec.reverse()
         am = ArrayManager(size=600)
         last_5_vol = deque([0.0] * 5)
         last_5_amount = deque([0.0] * 5)
         for d in rec:                
             if 0.0 not in last_5_vol:
                 vol_rate = d['vol'] / (sum(last_5_vol) / 5.0)               
                 amount_rate = d['amount'] / (sum(last_5_amount) / 5.0)   
                 d['vol_rate'] = vol_rate
                 d['amount_rate'] = amount_rate 
             else:
                 d['vol_rate'] = 0.0
                 d['amount_rate'] = 0.0
             last_5_vol.popleft()
             last_5_amount.popleft()
             last_5_vol.append(d['vol'])
             last_5_amount.append(d['amount'])                                       
             if d['ts_code'][-3:] == '_SH':
                 exchange = Exchange.SSE
                 d['exchange'] = 'SSE'
             else:
                 exchange = Exchange.SZSE
                 d['exchange'] = 'SZSE'                
             bar = BarData(
                 gateway_name='ctp', symbol=d['ts_code'],
                 exchange=exchange,
                 datetime=string_to_datetime(d['trade_date']))
             bar.symbol = d['ts_code']
             bar.volume = d['vol']
             bar.open_price = d['open']
             bar.high_price = d['high']
             bar.low_price = d['low']
             bar.close_price = d['close']
             am.update_bar(bar)
             if d['trade_date'] >= self.db_date:
                 d['ma_5'] = am.sma(5)
                 d['ma_10'] = am.sma(10)
                 d['ma_20'] = am.sma(20)
                 d['ma_30'] = am.sma(30)
                 d['ma_60'] = am.sma(60)
                 d['ma_120'] = am.sma(120)
                 d['ma_250'] = am.sma(250)
                 d['ma_500'] = am.sma(500)
                 d['high_5'] = np.max(am.high[-5:])
                 d['high_10'] = np.max(am.high[-10:])
                 d['high_20'] = np.max(am.high[-20:])
                 d['high_30'] = np.max(am.high[-30:])
                 d['high_60'] = np.max(am.high[-60:])
                 d['high_120'] = np.max(am.high[-120:])
                 d['high_250'] = np.max(am.high[-250:])
                 d['high_500'] = np.max(am.high[-500:])
                 d['low_5'] = np.min(am.low[-5:])
                 d['low_10'] = np.min(am.low[-10:])
                 d['low_20'] = np.min(am.low[-20:])
                 d['low_30'] = np.min(am.low[-30:])
                 d['low_60'] = np.min(am.low[-60:])
                 d['low_120'] = np.min(am.low[-120:])
                 d['low_250'] = np.min(am.low[-250:])
                 d['low_500'] = np.min(am.low[-500:])
                 flt = {'trade_date': d['trade_date']}
                 cl_stock_code.replace_one(flt, d, upsert=True)
Пример #3
0
 def _init_k_data(self, code, k_data):
     # 注意:所有的数据库数据和列表数据都按照日期的正序排序(从小到大)
     """
     初始化股票数据库数据
     @:param code  股票(指数)代码                
     """     
     if len(k_data) != 0:
         last_5_vol = deque([0.0] * 5)
         last_5_amount = deque([0.0] * 5)
         k_data = k_data.sort_values(by='trade_date')            
         cl_stock_code = self.db[code]
         cl_stock_code.create_index([('trade_date', ASCENDING)], unique=True)
         am = ArrayManager(size=600)
         for ix, row in k_data.iterrows():
             d = row.to_dict()              
             d['ts_code'] = d['ts_code'].replace('.', '_')
             if 0.0 not in last_5_vol:
                 vol_rate = d['vol'] / (sum(last_5_vol) / 5.0)                
                 amount_rate = d['amount'] / (sum(last_5_amount) / 5.0)   
                 d['vol_rate'] = vol_rate
                 d['amount_rate'] = amount_rate 
             else:
                 d['vol_rate'] = 0.0
                 d['amount_rate'] = 0.0
             last_5_vol.popleft()
             last_5_amount.popleft()
             last_5_vol.append(d['vol'])
             last_5_amount.append(d['amount'])                   
             if self._is_in_vnpy_db(d['ts_code'], update=False):
                 # 构建vnpy股票数据库数据
                 self._build_db_vnpy(d)                               
             if d['ts_code'][-3:] == '_SH':
                 exchange = Exchange.SSE
                 d['exchange'] = 'SSE'
             else:
                 exchange = Exchange.SZSE                
                 d['exchange'] = 'SZSE'
             bar = BarData(
                 gateway_name='ctp', symbol=d['ts_code'],
                 exchange=exchange,
                 datetime=string_to_datetime(d['trade_date']))
             bar.symbol = d['ts_code']
             bar.volume = d['vol']
             bar.open_price = d['open']
             bar.high_price = d['high']
             bar.low_price = d['low']
             bar.close_price = d['close']
             am.update_bar(bar)
             try:
                 d['ma_5'] = am.sma(5)
             except:
                 traceback.print_exc()                    
                 LOG.error('************************')
                 LOG.error(d['ts_code'])
                 LOG.error(d['trade_date'])
                 LOG.error(bar)
             d['ma_10'] = am.sma(10)
             d['ma_20'] = am.sma(20)
             d['ma_30'] = am.sma(30)
             d['ma_60'] = am.sma(60)
             d['ma_120'] = am.sma(120)
             d['ma_250'] = am.sma(250)
             d['ma_500'] = am.sma(500)
             d['high_5'] = np.max(am.high[-5:])
             d['high_10'] = np.max(am.high[-10:])
             d['high_20'] = np.max(am.high[-20:])
             d['high_30'] = np.max(am.high[-30:])
             d['high_60'] = np.max(am.high[-60:])
             d['high_120'] = np.max(am.high[-120:])
             d['high_250'] = np.max(am.high[-250:])
             d['high_500'] = np.max(am.high[-500:])
             d['low_5'] = np.min(am.low[-5:])
             d['low_10'] = np.min(am.low[-10:])
             d['low_20'] = np.min(am.low[-20:])
             d['low_30'] = np.min(am.low[-30:])
             d['low_60'] = np.min(am.low[-60:])
             d['low_120'] = np.min(am.low[-120:])
             d['low_250'] = np.min(am.low[-250:])
             d['low_500'] = np.min(am.low[-500:])
             flt = {'trade_date': d['trade_date']}
             cl_stock_code.replace_one(flt, d, upsert=True)