版权所有:光明与黑暗([email protected])

转载请注明出处: zhihu.com/people/guang-

未经测试不要用于实盘!

各位,好久无更新了.本篇不会有基于资金流的价差幅度预测,是不是很失望呢?无办法啦,因为虽然程序写好、数据备妥,但是模型的classification_report……出乎了我意料的好.所以我在考虑不公开那个系列.

闲话不多说,我们进入本次的正题: 基于资金流的市场情绪指数构建:

1、什么是市场情绪指数?市场情绪指数是反映市场上乐观或悲观程度的指标,是投资者心理的反应,也是投资者对市场表现的反应。

2、需要观察那些投资者的市场情绪指数?毫无疑问是两类人:大户和散户。

3、为什么要做市场情绪指数?因为大户和散户之间对市场的反映存在著一定时间差异,通常情况下是大户先于散户做出市场反应,因此观察他们的市场情绪有助于预先建仓或者提前离场。

4、用什么来衡量市场情绪指数?我的第一反应是用资金流的进出。基于以下原因,第一是媒体吹得市场环境再好,没有资金介入也是白搭;第二是在少数股票上倒手营造交投活跃气氛容易,但是在面对一个由大量股票组成的指数上要营造交投活跃气氛困难。

5、选用哪个指数利用它的权重来构建市场情绪指数?我选择000852中证1000的权重作为观察对象。因为它由1000只股票构成,涵盖了市场上约30%的股票范围;包括深市、沪市和创业板;避免了郭嘉队拉升上证50等指数导致的指数与市场不符。

开搞之前记住:本次需要tushare积分1500分,你可以通过tushare.pro/register? 注册,也可以参考tushare.pro/document/1? 提高积分.

1、获取指数权重:很简单,一句搞定:

df_index_weight=pro.index_weight(index_code=000852.SH) .head(1000)

介面:index_weight

描述:获取各类指数成分和权重,月度数据 .

来源:指数公司网站公开数据

积分:用户需要至少400 tushare积分才可以调取

更多介面信息请参阅:

tushare.pro/document/2?

2、计算每只股票的权重,tushare提供了现成的资金流介面来获取当天的资金流入/出金额和手数.我们只需要调用个股资金流向介面即可:

介面:moneyflow

描述:获取沪深A股票资金流向数据,分析大单小单成交情况,用于判别资金动向

限量:单次最大提取4000行记录,总量不限制

积分:用户需要至少1500积分才可以调取.

更多介面信息请参阅:

tushare.pro/document/2?

它根据成交额区分大中小单:小单:5万以下 中单:5万~20万 大单:20万~100万 特大单:成交额>=100万.因为我们观察的是大户和散户之间的行为,因此我们将小单认作是散户行为,中单以上认为是大户行为.为简化起见,我们只统计散户和大户的买入和卖出金额.代码没什么难懂的,我只是挑几句说明一下:

1)获取某只股票的资金流并按日期排序

df_moneyflow_temp=pro.moneyflow(ts_code=stock_code_temp,start_date=start_date,end_date=end_date).sort_values(by=trade_date,ascending=True)

2)逐只股票计算其散户资金流入/出以及大户资金流入/出

#散户资金流入等于小单买入金额之和乘以权重
df_moneyflow_temp[buy_small_amount_total]=df_moneyflow_temp[buy_sm_amount]*stock_weight_temp
#大户资金流入等于中单+大单+特大单买入金额之和乘以权重
df_moneyflow_temp[buy_big_amount_total]=(df_moneyflow_temp[buy_md_amount]+df_moneyflow_temp[buy_lg_amount]+df_moneyflow_temp[buy_elg_amount])*stock_weight_temp
#散户资金流出等于小单卖出金额之和乘以权重
df_moneyflow_temp[sell_small_amount_total]=df_moneyflow_temp[sell_sm_amount]*stock_weight_temp
#大户资金流出等于中单+大单+特大单卖出金额之和乘以权重
df_moneyflow_temp[sell_big_amount_total]=(df_moneyflow_temp[sell_md_amount]+df_moneyflow_temp[sell_lg_amount]+df_moneyflow_temp[sell_elg_amount])*stock_weight_temp

3)分类逐行求和

df_small_buy_mood_index[small_buy_sum] = df_small_buy_mood_index.apply(lambda x: x.sum(), axis=1)
df_big_buy_mood_index[big_buy_sum] = df_big_buy_mood_index.apply(lambda x: x.sum(), axis=1)
df_small_sell_mood_index[small_sell_sum] = df_small_sell_mood_index.apply(lambda x: x.sum(), axis=1)
df_big_sell_mood_index[big_sell_sum] = df_big_sell_mood_index.apply(lambda x: x.sum(), axis=1)

我们来观察一下这个指数的意义:

大户卖出情绪指数在5月9日达到了一个高峰,因此在5月10日中证1000指数基本呈下降趋势.

大户买入情绪指数在5月27日达到了一个高峰, 因此在5月28日中证1000指数基本呈上升趋势.

因此可以推断在一般情况下: 大户买入/卖出情绪指数具有一定的预警性.

文章最后是六一儿童节福利:送上代码.祝各位儿童节快乐!

# -*- coding: UTF-8 -*-
import tushare as ts
import pandas as pd
import datetime
import time
from datetime import datetime
from tqdm import *
#☆常量定义,改这句即可运行.请支持tushare和米哥.
ts_token=
#初始化TS_API介面
def get_ts_api():
ts.set_token(ts_token)
pro = ts.pro_api()
return pro
if __name__ == "__main__":
start_date=20190501
end_date=str(datetime.now().strftime(%Y%m%d))
pro=get_ts_api()
#获取中证1000的最新个股权重
df_index_weights=pro.index_weight(index_code=000852.SH).head(1000)
#获取起止日期间的交易日
df_trade_cal=pro.trade_cal(start_date=start_date,end_date=end_date,is_open=1)
#构建市场情绪指数DataFrame
df_small_buy_mood_index=pd.DataFrame(index=df_trade_cal[cal_date])
df_big_buy_mood_index=pd.DataFrame(index=df_trade_cal[cal_date])
df_small_sell_mood_index=pd.DataFrame(index=df_trade_cal[cal_date])
df_big_sell_mood_index=pd.DataFrame(index=df_trade_cal[cal_date])
df_mood_index=pd.DataFrame(index=df_trade_cal[cal_date])
for i in tqdm(range(0,len(df_index_weights))):
stock_code_temp=df_index_weights.iloc[i][con_code]
stock_weight_temp=df_index_weights.iloc[i][weight]
df_moneyflow_temp=pro.moneyflow(ts_code=stock_code_temp,start_date=start_date,end_date=end_date).sort_values(by=trade_date,ascending=True)
df_moneyflow_temp=df_moneyflow_temp.set_index(trade_date)
#散户资金流入等于小单买入金额之和乘以权重
df_moneyflow_temp[buy_small_amount_total]=df_moneyflow_temp[buy_sm_amount]*stock_weight_temp
#大户资金流入等于中单+大单+特大单买入金额之和乘以权重
df_moneyflow_temp[buy_big_amount_total]=(df_moneyflow_temp[buy_md_amount]+df_moneyflow_temp[buy_lg_amount]+df_moneyflow_temp[buy_elg_amount])*stock_weight_temp
#散户资金流出等于小单卖出金额之和乘以权重
df_moneyflow_temp[sell_small_amount_total]=df_moneyflow_temp[sell_sm_amount]*stock_weight_temp
#大户资金流出等于中单+大单+特大单卖出金额之和乘以权重
df_moneyflow_temp[sell_big_amount_total]=(df_moneyflow_temp[sell_md_amount]+df_moneyflow_temp[sell_lg_amount]+df_moneyflow_temp[sell_elg_amount])*stock_weight_temp
#分DataFrame统计散户和大户的资金流入和流出
df_small_buy_mood_index[stock+stock_code_temp+buy_small_amount_total]=df_moneyflow_temp[buy_small_amount_total]
df_big_buy_mood_index[stock+stock_code_temp+buy_big_amount_total]=df_moneyflow_temp[buy_big_amount_total]
df_small_sell_mood_index[stock+stock_code_temp+sell_small_amount_total]=df_moneyflow_temp[sell_small_amount_total]
df_big_sell_mood_index[stock+stock_code_temp+sell_big_amount_total]=df_moneyflow_temp[sell_big_amount_total]
#避免因调用频繁被伺服器拦截
time.sleep(1)
#统一求和
df_small_buy_mood_index[small_buy_sum] = df_small_buy_mood_index.apply(lambda x: x.sum(), axis=1)
df_big_buy_mood_index[big_buy_sum] = df_big_buy_mood_index.apply(lambda x: x.sum(), axis=1)
df_small_sell_mood_index[small_sell_sum] = df_small_sell_mood_index.apply(lambda x: x.sum(), axis=1)
df_big_sell_mood_index[big_sell_sum] = df_big_sell_mood_index.apply(lambda x: x.sum(), axis=1)
#分别赋值
df_mood_index[small_buy_sum_index]=df_small_buy_mood_index[small_buy_sum]
df_mood_index[big_buy_sum_index]=df_big_buy_mood_index[big_buy_sum]
df_mood_index[small_sell_sum_index]=df_small_sell_mood_index[small_sell_sum]
df_mood_index[big_sell_sum_index]=df_big_sell_mood_index[big_sell_sum]
#保存数据到本地
df_index=pro.index_daily(ts_code=000852.SH, start_date=start_date,end_date=end_date).sort_values(by=trade_date,ascending=True)
df_index=df_index.set_index(trade_date)
df_mood_index[real_index]=df_index[close]
df_big_buy_mood_index.to_csv(G:/data/df_big_buy_mood_index.csv,encoding=gbk,index=True)
df_small_buy_mood_index.to_csv(G:/data/df_small_buy_mood_index.csv,encoding=gbk,index=True)
df_small_sell_mood_index.to_csv(G:/data/df_small_sell_mood_index.csv,encoding=gbk,index=True)
df_big_sell_mood_index.to_csv(G:/data/df_big_sell_mood_index.csv,encoding=gbk,index=True)
df_mood_index.to_csv(G:/data/df_mood_index.csv,encoding=gbk,index=True)

文末发散:

1)小伙伴们可以以此类推,创造自己的上证深证创业板情绪指数哦

2)为什么有些地方明明前一天大户买入情绪指数是个高峰,为什么第二天中证指数反而降了(或者反过来)?你可以看一下大户买入情绪指数-大户卖出情绪指数是正还是负嘛.


推荐阅读:
相关文章