def add_margin_info(today=None): """ 插入融资融券信息 :param today: :return: """ if today is not None: """ 插入今日信息 """ pro = tushare_data.get_tushare_pro() trade_date = (datetime.datetime.now() - datetime.timedelta(days=1)).strftime('%Y%m%d') df = pro.margin(trade_date=trade_date) if df is not None and df.size != 0: try: tables.margin_info_add(trade_date, df['rzye'].sum(), df['rzmre'].sum(), df['rzche'].sum(), df['rqye'].sum(), df['rqmcl'].sum(), df['rzrqye'].sum()) print('{0}融资余额插入成功'.format(trade_date)) except Exception as err: print('{0}融资余额插入失败'.format(trade_date)) print(err) else: """ 插入历史信息 """ pro = tushare_data.get_tushare_pro() money_flow_info = tables.get_moneyflow_info() money_flow_info_date_sets = money_flow_info['date'].drop_duplicates() for trade_date in money_flow_info_date_sets: df = pro.margin(trade_date=trade_date) time.sleep(1) if df is not None and df.size != 0: try: tables.margin_info_add(trade_date, df['rzye'].sum(), df['rzmre'].sum(), df['rzche'].sum(), df['rqye'].sum(), df['rqmcl'].sum(), df['rzrqye'].sum()) print('{0}融资余额插入成功'.format(trade_date)) except Exception as err: print('{0}融资余额插入失败'.format(trade_date)) print(err)
def __init__(self) -> object: self.pro = tushare_data.get_tushare_pro() trade_date = datetime.datetime.now().strftime('%Y%m%d') df = self.pro.index_weight(index_code='399300.SZ') df = df.sort_values(by='trade_date', ascending=False) df = df.head(300) self.hs300_ts_code = df['con_code'] self.selected_ts = [] self.std = []
def __data_prepare(self): pro = tushare_data.get_tushare_pro() self.financing_date = pro.margin_detail(ts_code=self.ts_code) if not self.__data_check(): raise Exception("无融资信息") self.financing_date = self.financing_date.sort_values(by='trade_date', ascending=False) self.financing_date = pandas.DataFrame(self.financing_date.head(self.recent_days)[self.financing_type]) if not self.__data_check(): raise Exception("数据处理失败") print(self.financing_date.size, self.ts_code) self.financing_rolling()
def margin_info_figure(): pro = tushare_data.get_tushare_pro() df = pro.margin(start_date='20140922') rziy = df.groupby(['trade_date'])['rzye'].sum() print(rziy) draw_single(df['trade_date'].drop_duplicates(), rziy, '211', '融资余额') hs300_daily_info = tushare_data.get_index_daily('000300.SH') hs300_daily_info = hs300_daily_info.loc[hs300_daily_info['trade_date'] > '20140922'] hs300_close_rolling = date_rolling(hs300_daily_info['close']) draw_single(hs300_daily_info['trade_date'], hs300_close_rolling, '212', '沪深300指数') figure = plt.gcf() # 获取当前图片 figure.set_size_inches(21.6, 24) plt.savefig('./result/' + datetime.now().strftime('%Y%m%d') + 'yzye.png', dpi=100)
def hsgt_add(today=None): """ 插入沪港通资金流向信息 :return: """ pro = tushare_data.get_tushare_pro() if today is None: hs300_daily_info = tushare_data.get_index_daily('000300.SH') hs300_daily_info = hs300_daily_info.loc[ hs300_daily_info['trade_date'] > '20180630'] for date in hs300_daily_info['trade_date']: hsgt_info = pro.moneyflow_hsgt(trade_date=date) if hsgt_info is None or hsgt_info.size == 0: continue try: north_money = hsgt_info['north_money'][0] south_money = hsgt_info['south_money'][0] tables.money_flow_hsgt_add(trade_date=date, north_money=north_money, south_money=south_money) print(date, ':沪港通现金流信息插入成功') except Exception as err: print(date, ':沪港通现金流信息插入失败', err) print('沪港通现金流信息插入完成') else: date = datetime.datetime.now().strftime('%Y%m%d') hsgt_info = pro.moneyflow_hsgt(trade_date=date) if hsgt_info is None or hsgt_info.size == 0: return try: north_money = hsgt_info['north_money'][0] south_money = hsgt_info['south_money'][0] tables.money_flow_hsgt_add(trade_date=date, north_money=north_money, south_money=south_money) print(date, ':沪港通现金流信息插入成功') except Exception as err: print(date, ':沪港通现金信息插入失败', err)
def add_money_flow_today(): """ 沪深300当日生成小单统计数据 tips:成交明细列表中的买盘/卖盘:“买盘”表示以比市价高的价格进行委托买入,并已经“主动成交”,代表外盘; “卖盘”表示以比市价低的价格进行委托卖出,并已经“主动成交”,代表内盘 :return: """ print('插入当日现金流信息开始......') zz500 = tushare.get_zz500s() hs300_index = tushare.get_hs300s() zz800_ts_codes = hs300_index.append(zz500) ts_codes = zz800_ts_codes['code'].apply(util.stock_code_change) for ts_code in set(ts_codes): time.sleep(1) try: date = datetime.datetime.now().strftime('%Y%m%d') daily_info = tushare_data.get_tushare_pro().daily(ts_code=ts_code, trade_date=date) close = daily_info['close'] df = tushare.get_tick_data(str(ts_code[:6]), date=date, src='tt') if df is None: print("未获取到股票{0}-{1}的信息".format(ts_code, date)) continue total_amt = df['amount'].sum() total_vol = df['volume'].sum() sell_trade = df.loc[(df['type'] == '卖盘')] total_sell_vol = sell_trade['volume'].sum() total_sell_amt = sell_trade['amount'].sum() small_trade_amount = util.trade_scale(close) sell_sm_trade = df.loc[(df['type'] == '卖盘') & (df['amount'] < small_trade_amount)] sell_sm_vol = sell_sm_trade['volume'].sum() sell_sm_amt = sell_sm_trade['amount'].sum() buy_trade = df.loc[(df['type'] == '买盘')] total_buy_vol = buy_trade['volume'].sum() total_buy_amt = buy_trade['amount'].sum() buy_sm_trade = df.loc[(df['type'] == '买盘') & (df['amount'] < small_trade_amount)] buy_sm_vol = buy_sm_trade['volume'].sum() buy_sm_amt = buy_sm_trade['amount'].sum() total_sm_trade = df.loc[df['amount'] < small_trade_amount] total_sm_amt = total_sm_trade['amount'].sum() total_sm_vol = total_sm_trade['volume'].sum() id = ts_code[:6] + date tables.add_money_flow(id=id, code=str(ts_code[:6]), date=date, sell_sm_vol=sell_sm_vol, sell_sm_amt=sell_sm_amt, buy_sm_vol=buy_sm_vol, buy_sm_amt=buy_sm_amt, total_sell_vol=total_sell_vol, total_sell_amt=total_sell_amt, total_buy_vol=total_buy_vol, total_buy_amt=total_buy_amt, total_amt=total_amt, total_vol=total_vol, total_sm_amt=total_sm_amt, total_sm_vol=total_sm_vol) except Exception as err: print('插入当日现金流信息失败......') raise err print('插入当日现金流信息完成......')
def add_money_flow(): """ 沪深300生成小单统计数据 tips:成交明细列表中的买盘/卖盘:“买盘”表示以比市价高的价格进行委托买入,并已经“主动成交”,代表外盘; “卖盘”表示以比市价低的价格进行委托卖出,并已经“主动成交”,代表内盘 :return: """ print('插入历史现金流信息开始......') try: zz500 = tushare.get_zz500s() hs300_index = tushare.get_hs300s() zz800_ts_codes = hs300_index.append(zz500) ts_codes = zz800_ts_codes['code'].apply(util.stock_code_change) for ts_code in ts_codes: daily_info = tushare.pro_bar(ts_code, api=tushare_data.get_tushare_pro()) if daily_info is None: continue for index, row in daily_info.iterrows(): date = row['trade_date'] date = date[:4] + '-' + date[4:6] + '-' + date[6:] code = row['ts_code'][:6] if date > '2018-06-30': df = tushare.get_tick_data(code, date=date, src='tt') if df is None: continue total_amt = df['amount'].sum() total_vol = df['volume'].sum() sell_trade = df.loc[(df['type'] == '卖盘')] total_sell_vol = sell_trade['volume'].sum() total_sell_amt = sell_trade['amount'].sum() small_trade_amount = util.trade_scale(row['close']) sell_sm_trade = df.loc[(df['type'] == '卖盘') & (df['amount'] < small_trade_amount)] sell_sm_vol = sell_sm_trade['volume'].sum() sell_sm_amt = sell_sm_trade['amount'].sum() buy_trade = df.loc[(df['type'] == '买盘')] total_buy_vol = buy_trade['volume'].sum() total_buy_amt = buy_trade['amount'].sum() buy_sm_trade = df.loc[(df['type'] == '买盘') & (df['amount'] < small_trade_amount)] buy_sm_vol = buy_sm_trade['volume'].sum() buy_sm_amt = buy_sm_trade['amount'].sum() total_sm_trade = df.loc[df['amount'] < small_trade_amount] total_sm_amt = total_sm_trade['amount'].sum() total_sm_vol = total_sm_trade['volume'].sum() id = code + row['trade_date'] tables.add_money_flow(id=id, code=code, date=row['trade_date'], sell_sm_vol=sell_sm_vol, sell_sm_amt=sell_sm_amt, buy_sm_vol=buy_sm_vol, buy_sm_amt=buy_sm_amt, total_sell_vol=total_sell_vol, total_sell_amt=total_sell_amt, total_buy_vol=total_buy_vol, total_buy_amt=total_buy_amt, total_amt=total_amt, total_vol=total_vol, total_sm_amt=total_sm_amt, total_sm_vol=total_sm_vol) print('插入历史现金流信息完成......') except Exception as err: print('插入历史现金流信息失败......') raise err
test.var_clc() plt.plot(X, self.financing_date) plt.plot(X, self.__model.predict(X)) plt.title(self.ts_code) plt.show() self._financing_gradient = self.__model.coef_ @property def financing_gradient(self): return self._financing_gradient def var_clc(self): financing_date_predict = self.__model.predict(self.financing_date) rt = financing_date_predict - self.financing_date self.var = np.dot(rt.T, rt)/np.power(self.recent_days, 2) print('var:', self.var) pro = tushare_data.get_tushare_pro() df = pro.index_weight(index_code='399300.SZ') df = df.sort_values(by='trade_date', ascending=False) df = df.head(300) hs300_ts_code = df['con_code'] for ts_code in hs300_ts_code: test = Margin(ts_code=ts_code, financing_type='rzye') try: test.linear_model_learning() print(test.financing_gradient) except Exception as err: print(err)