def get_multiple_k_data(self, code_list=None, start=None, end=None): if start is None: start = get_next_date(-720) if end is None: end = get_current_date() sql = None if code_list is None: sql = ('''select * from k_data where time_key BETWEEN %(start)s and %(end)s order by time_key asc ''') codes_list = None else: sql = ('''select * from k_data where code in %(code_list)s and time_key BETWEEN %(start)s and %(end)s order by time_key asc ''') codes_list = [fill_market(code) for code in code_list] data = pd.read_sql(sql=sql, params={"code_list": codes_list, "start": start, "end": end} , con=dataSource.mysql_quant_conn) return data
def collect_single_daily(code, table_name='k_data_tech_feature'): start = get_next_date(-200) end = get_next_date(1) data = k_data_dao.get_k_data(code, start=start, end=end, cal_next_direction=False) data, features = collect_features(data) features.append('code') features.append('date') data = data[features].tail(1) data.to_sql(table_name, dataSource.mysql_quant_engine, if_exists='append', index=False)
def test_get_last_macd_cross_point(self): df = k_data_dao.get_k_data(code="601668", start=get_next_date(-120), end=get_current_date()) df = df.join(cal_macd(df)) item = k_data_dao.get_last_macd_cross_point(df, window_size=8) print(item)
def get_k_data(self, code, start=None, end=None): if start is None: start = get_next_date(-720) if end is None: end = get_current_date() sql = ('''select time_key, code, open, close as close, high, low, change_rate, last_close, turnover, turnover_rate, volume, pe_ratio from k_data where code=%(code)s and time_key BETWEEN %(start)s and %(end)s order by time_key asc ''') data = pd.read_sql(sql=sql, params={"code": fill_market(code), "start": start, "end": end} , con=dataSource.mysql_quant_conn) return data
def init_pool(self): self.truncate() stocks = stock_dao.query_all() k_data_list = k_data_dao.get_multiple_k_data(start=get_next_date(-720), end=get_current_date()) df = pd.DataFrame(columns=['code', 'name']) for stock in stocks: try: k_data = k_data_list.loc[k_data_list['code'] == fill_market(stock.code)] k_data = k_data.join(cal_macd(k_data)) k_data['turnover7'] = cal_mavol7(k_data, column='turnover') k_turnover7 = k_data['turnover7'].values[-1] if len(k_data['code'].values) == 0: continue stock_basic = stock_basic_dao.get_by_code(stock.code) eps_value = stock_basic['eps'].values[0] profits_yoy_value = stock_basic['profits_yoy'].values[0] if eps_value < 0: continue if profits_yoy_value < 0: continue if k_turnover7 < 65000000: continue dict = {"code": stock.code, "name": stock.name} df = df.append(dict, ignore_index=True) logger.debug("append code:%s" % stock.code) except Exception as e: logger.debug("code:%s, error:%s" % (stock.code, traceback.format_exc())) df.to_sql('stock_pool', dataSource.mysql_quant_engine, if_exists='append', index=False) '''
def cal_bk_vol(): bk_vol_frame = pd.DataFrame(columns=['bkcode', 'bk_name', 'total_mavol_3']) filter_list = [ 'BK0743', 'BK0804', 'BK0568', 'BK0707', 'BK0701', 'BK0611', 'BK0705', 'BK0612', 'BK0500' ] org_bk_code_list = list(stock_industry_dao.get_bkcode_list().values) bk_code_list = [c for c in org_bk_code_list if c not in filter_list] for bk in bk_code_list: bk_stocks = stock_industry_dao.get_by_bkcode(bk[0]) bk_vol3 = 0 try: bk_name = bk_stocks['bk_name'][0] except: bk_name = 'N/A' for code in bk_stocks['code'].values: stock_df = k_data_dao.get_k_data(code=code, start=get_next_date(-30), end=get_current_date()) if len(stock_df) == 0: continue stock_df['mavol3'] = cal_mavol3(stock_df) try: bk_vol3 += stock_df['mavol3'].values[-1:][0] except Exception as e: logger.debug("code:%s, error:%s" % (code, repr(e))) bk_vol_frame.loc[bk_vol_frame.shape[0] + 1] = { 'bkcode': bk, 'bk_name': bk_name, 'total_mavol_3': bk_vol3 } bk_vol_frame = bk_vol_frame.sort_values('total_mavol_3', ascending=False) bk_vol_frame.to_csv('bk_csv.csv', encoding='utf_8_sig')
def cal_stock_pool(): df_poll = stock_pool_dao.get_list() code_list = list(df_poll['code'].values) w_data_list = k_data_weekly_dao.get_multiple_k_data(code_list, start='2013-01-01', end=get_current_date()) k_data_list = k_data_dao.get_multiple_k_data(code_list=code_list, start=get_next_date(-720), end=get_current_date()) # k_data_list = k_data_list.set_index('code', inplace=True) # k_data_list = k_data_dao.get_market_snapshot(code_list=code_list, futu_quote_ctx=futu_quote_ctx) matched = [] for code in code_list: rs = cal_single_stock(code, k_data_list, w_data_list) if rs is True: matched.append(code) print("matched:%s" % matched)
def handle_data(self): temp_target_list = pd.DataFrame(columns=['code', 'pe', 'pb', 'eps', 'm_cap']) sh_index = k_data_dao.get_k_data('SH.000001', start=get_next_date(-30), end=get_next_date(-1)) # if sh_index['change_rate'].rolling(window=3).sum().values[-1] >= -0.039: # for position in context.portfolio.positions[:]: # code = position.code # shares = position.shares # price = position.price # self.sell_value(code=code, shares=shares, price=price) for code in self.context.pool: stock_basic_info = stock_basic_dao.get_by_code(code=code) if len(stock_basic_info) == 0: continue try: pe_value = stock_basic_info['pe'].values[0] pb_value = stock_basic_info['pb'].values[0] eps_value = stock_basic_info['eps'].values[0] m_cap = stock_basic_info['total_market'].values[0] except: pass if pe_value < 20 and pb_value < 1.8 and eps_value > 0: target_stock = {'code': code, 'pe': pe_value, 'pb': pb_value, 'eps': eps_value, 'm_cap': m_cap} temp_target_list.loc[temp_target_list.shape[0] + 1] = target_stock temp_target_stock = temp_target_list.sort_values('m_cap', ascending=False)[:5] target_code_list = temp_target_stock['code'].values current_stock_code = [] for positions in self.context.portfolio.positions: current_stock_code.append(positions.code) print(current_stock_code) stock_to_be_added = [i for i in target_code_list if i not in current_stock_code] stock_to_be_removed = [j for j in current_stock_code if j not in target_code_list] for code in stock_to_be_removed: # self.sell_value(code=code, price=price, shares=-1) logger.info('direction = sell:' + code) for code in stock_to_be_added: #k_data = k_data_dao.get_k_data(code=code, start=self.context.current_date, end=self.context.current_date) #if len(k_data) == 0: #continue #price = k_data['close'].values[-1] #if len(self.context.portfolio.positions) >= 5: #break # self.buy_in_percent(code=code, price=price, percent=0.2) k_data = k_data_dao.get_k_data(code=code, start=get_next_date(-2), end=self.context.current_date) price = k_data['close'].values[-1] shares = int(4000 / price / 100) * 100 print('direction = buy:%s, shares:%s'%(code, shares))
def handle_data(self): target_frame = pd.DataFrame(columns=[ 'code', 'close', 'k_value', 'd_value', 'pre_k', 'pre_d', 'macd', 'pre_macd', 'diff', 'pre_diff', 'dea', 'pre_dea', 'current_vol_weekly', 'pre_vol_weekly' ]) for code in self.context.pool: try: daily_stock_data = k_data_dao.get_k_data( code=code, start=get_next_date(days=-100, args=context.current_date), end=self.context.current_date, futu_quote_ctx=self.futu_quote_ctx) weekly_stock_data = k_data_weekly_dao.get_k_data( code=code, start='2013-01-01', end=get_next_date(-5), futu_quote_ctx=self.futu_quote_ctx) pre_vol_weely = weekly_stock_data['volume'].iloc[-2:].values[0] current_vol_weekly = weekly_stock_data['volume'].iloc[ -1:].values[0] daily_stock_data = daily_stock_data.join( acc_kdj(daily_stock_data)) daily_stock_data_withf = daily_stock_data.join( cal_macd(daily_stock_data)) daily_stock_data_withf['ma5'] = cal_ma20( daily_stock_data_withf) k_value = daily_stock_data_withf['k_value'].iloc[-1:].values[0] d_value = daily_stock_data_withf['d_value'].iloc[-1:].values[0] pre_k = daily_stock_data_withf['k_value'].iloc[-2:].values[0] pre_d = daily_stock_data_withf['d_value'].iloc[-2:].values[0] pre_macd_value = daily_stock_data_withf['macd'].iloc[ -2:].values[0] current_macd_value = daily_stock_data_withf['macd'].iloc[ -1:].values[0] pre_macd_diff = daily_stock_data_withf['diff'].iloc[ -2:].values[0] macd_diff = daily_stock_data_withf['diff'].iloc[-1:].values[0] pre_macd_dea = daily_stock_data_withf['dea'].iloc[-2:].values[ 0] macd_dea = daily_stock_data_withf['dea'].iloc[-1:].values[0] last_close = daily_stock_data_withf['close'].iloc[-1:].values[ 0] # 金叉 # and current_macd_value >= current_macd_signal and pre_macd_value < current_macd_value and current_vol_weekly / pre_vol_weely >= 1.3 # (k_value >= d_value or 10 >= abs(pre_k - pre_d) >= abs(k_value - d_value)) # k_value >= d_value and abs(k_value - d_value) <= 5 if (k_value >= d_value or abs(k_value - d_value) <= 10) and pre_k <= pre_d \ and ((macd_diff > macd_dea and pre_macd_diff < pre_macd_dea) or (macd_diff-macd_dea) < (pre_macd_diff-macd_dea) and 0.05 >= current_macd_value >= 0) \ and current_vol_weekly / pre_vol_weely >= 1.5: target_stock = { 'code': self.fill_zero(code), 'close': last_close, 'k_value': k_value, 'd_value': d_value, 'pre_k': pre_k, 'pre_d': pre_d, 'macd': current_macd_value, 'pre_macd': pre_macd_value, 'diff': macd_diff, 'pre_diff': pre_macd_diff, 'dea': macd_dea, 'pre_dea': pre_macd_dea, 'current_vol_weekly': current_vol_weekly, 'pre_vol_weekly': pre_vol_weely, } target_frame.loc[target_frame.shape[0] + 1] = target_stock print(target_stock) # self.buy_in_percent(code=code, price=last_close, percent=0.1) except: continue target_frame.to_csv('kdj_result.csv')
def handle_data(self): target_frame = pd.DataFrame(columns=[ 'code', 'close', 'k_value', 'd_value', 'pre_k', 'pre_d', 'ma20', 'profits_yoy', 'bm', 'mavol5', 'mavol20' ]) for code in self.context.pool: daily_stock_data = self.get_k_data( code=code, start=get_next_date(days=-30, args=self.context.current_date), end=self.context.current_date) daily_stock_data = daily_stock_data.join(acc_kdj(daily_stock_data)) k_value = daily_stock_data['k_value'].iloc[-1:].values[0] d_value = daily_stock_data['d_value'].iloc[-1:].values[0] pre_k = daily_stock_data['k_value'].iloc[-2:].values[0] pre_d = daily_stock_data['d_value'].iloc[-2:].values[0] # 金叉 if (k_value >= d_value or abs(k_value - d_value) <= 10) and pre_k <= pre_d: feature_frame = pd.concat([ acc_kdj(daily_stock_data), cal_ma5(daily_stock_data), cal_ma10(daily_stock_data), cal_ma20(daily_stock_data), cal_ma60(daily_stock_data), cal_mavol5(daily_stock_data), cal_mavol20(daily_stock_data) ], axis=1) feature_frame.columns = [ 'k_value', 'd_value', 'j_value', 'ma5', 'ma10', 'ma20', 'ma60', 'mavol5', 'mavol20' ] # weekly_stock_date = k_data_weekly_dao.get_k_data(code=code,start=get_next_date(days=-300, # args=context.current_date),end=get_current_date(context.current_date)) weekly_stock_date = # weekly_stock_date.join(acc_kdj(weekly_stock_date)) basic_data = self.get_stock_basic(code=code) last_close = daily_stock_data['close'].iloc[-1:].values[0] ma5_close = feature_frame['ma5'].iloc[-1:].values[0] ma10_close = feature_frame['ma10'].iloc[-1:].values[0] ma20_close = feature_frame['ma20'].iloc[-1:].values[0] profits_yoy = basic_data['profits_yoy'].iloc[0] mavol5 = feature_frame['mavol5'].iloc[-1:].values[0] mavol20 = feature_frame['mavol20'].iloc[-1:].values[0] # w_k_value = weekly_stock_date['k_value'] = weekly_stock_date['k_value'].iloc[-1:].values[0] # w_d_value = weekly_stock_date['d_value'] = weekly_stock_date['d_value'].iloc[-1:].values[0] # pre_w_k_value = weekly_stock_date['k_value'] = weekly_stock_date['k_value'].iloc[-2:].values[0] # pre_w_d_value = weekly_stock_date['d_value'] = weekly_stock_date['d_value'].iloc[-2:].values[0] # and mavol5 > mavol20 if last_close > ma5_close and profits_yoy > 30: target_stock = { 'code': self.fill_zero(code), 'close': last_close, 'k_value': k_value, 'd_value': d_value, 'pre_k': pre_k, 'pre_d': pre_d, 'ma20': ma20_close, 'profits_yoy': profits_yoy, 'bm': 1 / basic_data['pb'].loc[-1:].values[0], 'mavol5': mavol5, 'mavol20': mavol20 } target_frame.loc[target_frame.shape[0] + 1] = target_stock self.buy_in_percent(code=code, price=last_close, percent=0.2) # target_frame.to_csv('kdj_result.csv') # 死叉 if pre_k > pre_d and (k_value <= d_value): position = self.context.portfolio.get_position(code) # 清仓 if position is not None and position.shares > 0: self.sell_value(code, position.shares)