def update_margin_buy(new_start_date, new_end_date, margin_df): future_trade_days = get_trade_days(start_date=margin_df.index[-1], end_date=new_end_date)[1:] # 第一天重复 old_trade_days = get_trade_days(start_date=new_start_date, end_date=margin_df.index[0])[:-1] # 最后一天重复 new_trade_days = list(future_trade_days) + list(old_trade_days) if len(new_trade_days) > 0: for date in new_trade_days: margin_df.loc[date] = np.nan margin_buy_value.index = pd.to_datetime(margin_buy_value.index) # 最后新加的行index不是datetime for stock in tqdm(margin_df.columns): if len(future_trade_days) > 0: for d in future_trade_days: margin_df.loc[d] = np.nan margin_buy_value.index = pd.to_datetime(margin_buy_value.index) # 最后新加的行index不是datetime df1 = get_mtss(stock, future_trade_days[0], future_trade_days[-1], fields=['date', 'sec_code', 'fin_buy_value']) df1.index = pd.to_datetime(df1['date']) margin_df[stock].loc[df1.index] = df1['fin_buy_value'].values if len(old_trade_days) > 0: for d in old_trade_days: margin_df.loc[d] = np.nan margin_buy_value.index = pd.to_datetime(margin_buy_value.index) # 最后新加的行index不是datetime df2 = get_mtss(stock, old_trade_days[0], old_trade_days[-1], fields=['date', 'sec_code', 'fin_buy_value']) df2.index = pd.to_datetime(df2['date']) margin_df[stock].loc[df2.index] = df2['fin_buy_value'].values margin_df.to_csv('/Users/caichaohong/Desktop/Zenki/融资融券/margin_buy_value.csv') else: print("No need to Update")
def jq_get_mtss(security_list, start_date=None, end_date=None, fields=None, count=None): logger.info( "HTTP GET: mtss, with security_list={}, start_date={}, end_date={}, \ fields={}, count={}".format(security_list, start_date, end_date, fields, count)) return get_mtss(security_list, start_date=start_date, end_date=end_date, fields=fields, count=count)
def get_securities_day_mtss(security_list, date): fields = [ "fin_value", "fin_buy_value", "fin_refund_value", "sec_value", "sec_sell_value", "sec_refund_value", "fin_sec_value" ] # start_date 能取到,end_date 取不到 data_df = jq.get_mtss(security_list, start_date=date, end_date=date) if data_df.empty: return pd.DataFrame( {field: [] for field in ["security"] + fields + ["date"]}) data_df = data_df.rename(columns={"sec_code": "security"}) data_df["date"] = date result_df = data_df[["security"] + fields + ["date"]] return result_df
def get_day_mtss(security, date): fields = [ "fin_value", "fin_buy_value", "fin_refund_value", "sec_value", "sec_sell_value", "sec_refund_value", "fin_sec_value" ] # start_date 能取到,end_date 取不到 data_df = jq.get_mtss(security, start_date=date, end_date=date, fields=fields) if data_df.empty: return pd.DataFrame( {field: [] for field in ["security"] + fields + ["date"]}) data_df["security"] = security data_df["date"] = date result_df = data_df[["security"] + fields + ["date"]] return result_df
def record(self, entity, start, end, size, timestamps): df = get_mtss(to_jq_entity_id(entity), start_date=start) if pd_is_not_null(df): df['entity_id'] = entity.id df['code'] = entity.code df.rename(columns={'date': 'timestamp'}, inplace=True) df['timestamp'] = pd.to_datetime(df['timestamp']) df['id'] = df[['entity_id', 'timestamp']].apply( lambda se: "{}_{}".format( se['entity_id'], to_time_str(se['timestamp'], fmt=TIME_FORMAT_DAY)), axis=1) print(df) df_to_db(df=df, data_schema=self.data_schema, provider=self.provider, force_update=self.force_update) return None
end = '2021-05-10' dateList = get_trade_days(start_date=start, end_date=end) margin_stock_list = [] for date in tqdm(dateList): temp = get_margincash_stocks(date=dateList[0]) margin_stock_list = list(set(margin_stock_list).union(set(temp))) margin_buy_value = pd.DataFrame(index=dateList, columns=margin_stock_list) margin_total_value = pd.DataFrame(index=dateList, columns=margin_stock_list) margin_sell_value = pd.DataFrame(index=dateList, columns=margin_stock_list) for stock in tqdm(margin_stock_list): temp_df = get_mtss(stock, start_date=start, end_date=end, fields=[ 'date', 'sec_code', 'fin_value', 'fin_buy_value', 'fin_refund_value', 'sec_value', 'sec_sell_value', 'sec_refund_value', 'fin_sec_value' ]) margin_buy_value[stock].loc[ temp_df['date']] = temp_df['fin_buy_value'].values margin_total_value[stock].loc[ temp_df['date']] = temp_df['fin_value'].values margin_sell_value[stock].loc[ temp_df['date']] = temp_df['sec_sell_value'].values margin_buy_value = margin_buy_value.sort_index(axis=1) margin_total_value = margin_total_value.sort_index(axis=1) margin_sell_value = margin_sell_value.sort_index(axis=1) margin_buy_value.to_csv( '/Users/caichaohong/Desktop/Zenki/融资融券/margin_buy_value.csv')
def getSingleMtss(code, date): return jq.get_mtss(code, end_date=today, count=1)