版權所有:光明與黑暗([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)為什麼有些地方明明前一天大戶買入情緒指數是個高峯,為什麼第二天中證指數反而降了(或者反過來)?你可以看一下大戶買入情緒指數-大戶賣出情緒指數是正還是負嘛.


推薦閱讀:
相關文章