Simple quantification of short-term commodity sentiment

Sometimes, the sentiment of the market will be deduced to a more exaggerated position. In fact, very simple indicators can be quantified, such as the cumulative rise and fall of the past 20 trading days, and the quantile of the past rolling rise and fall can be roughly judged. market sentiment.

import datetime

from WindPy import w
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
# 启动Wind开放接口
w.start()

def s_plot(code, start_date):
    # 获取沪深300指数上市以来的行情数据
    wind_df = w.wsd(code, "close", start_date, datetime.date.today().strftime("%Y-%m-%d"))
    tpd_df = pd.DataFrame(wind_df.Data[0], columns=['close'])
    tpd_df.index = wind_df.Times
    tpd_df['shift_close'] = tpd_df['close'].shift(1)
    tpd_df['daily_return'] = tpd_df['close'] / tpd_df['shift_close'] - 1
    rolling_20day_return = tpd_df['daily_return'].rolling(20).apply(lambda x:(x+1).prod())
    analysis_df = pd.DataFrame(rolling_20day_return)
    analysis_df['0.025_bot'] = analysis_df['daily_return'].quantile(0.0255)
    analysis_df['0.05_bot'] = analysis_df['daily_return'].quantile(0.05)
    analysis_df['0.95_bot'] = analysis_df['daily_return'].quantile(0.95)
    analysis_df['0.975_bot'] = analysis_df['daily_return'].quantile(0.975)
    analysis_df['close'] = tpd_df['close']
    # plt.figure()
    # analysis_df['daily_return'].hist(bins=200, figsize=(19, 9))
    # plt.show()
    print('current code is : %s' % code)
    plt.figure()
    analysis_df[['daily_return', '0.025_bot', '0.05_bot', '0.95_bot', '0.975_bot']].plot(figsize=(19, 9))
    plt.show()

    plt.figure()
    analysis_df['close'].plot(figsize=(19, 9))
    plt.show()

base_code = ['UR', 'AU', 'IM', 'IC', 'IH', 'AG', 'AP', 'SM', 'JD', 'IF', 'PB', 'SA', 'TS', 'SI', 'CJ', 'TF', 'CY', 'RS', 'T', 'PK', 'CF', 'SF', 'C',
            'CS', 'CU', 'LH', 'SS', 'L', 'SN', 'PP', 'AL', 'MA', 'PG', 'NI', 'V', 'RU', 'SP', 'SR', 'A','M','B','ZN', 'Y', 'RB','EB','HC','PF',
            'FG', 'TA','BU','EG', 'OI','P','RM','JM', 'I', 'J', 'LU', 'SC','FU']

start_date = '2015-01-01'
for code in base_code:
    s_plot('%sFI.WI'% code, start_date)

For example, take gold as an example. In mid-April, due to the market’s expectation of the Fed’s interest rate hike, it was almost unanimous that May was the last time to raise interest rates, and then it would start to cut interest rates; at the same time, the US bank risk events also caused the market to buy gold. Hedging. We can see that the increase of gold in the past 20 trading days has reached the quantile above 0.95. Obviously, if chasing higher at this time, it is necessary to consider whether there will be short-term pullback pressure.

 

Guess you like

Origin blog.csdn.net/qtlyx/article/details/130536336