[Transfer] Stock K-line data resampling (resample) and data ex-weighting processing

3 Case: Stock K-line data resampling

DataFrame.resample(rule, how=None, axis=0, fill_method=None, closed=None,kind=None,)

For frequency conversion and time series resampling, the object must have a date and time index (DatetimeIndex, PeriodIndex or TimedeltaIndex)

Day K and Week K comparison:

So how do you switch between the daily line, weekly line, monthly line, etc.? ?

 

Note: Weekly K-line refers to a K-line chart drawn based on the opening price on Monday, the closing price on Friday, the highest price of the week and the lowest price of the week

 

Most weekly indicators are the value of this daily indicator on the last trading day of the week. For example, the "close" of the weekly line should be equal to the "close" of the daily data on the last day of the week, but some indicators are exceptions, for example, the "high" of the weekly line should be equal to the maximum value of all the daily lines of the week.

 

Next, we still use the market data of a stock in the previous stock_day

 

Convert the index to DatetimeIndex type

Resample different indicators

import pandas as pd
stock_day = pd.read_csv("./stock_day/stock_day.csv")
stock_day = stock_day.sort_index()
# 对每日交易数据进行重采样 (频率转换)
stock_day.index
 
 
# 1、必须将时间索引类型转换成Pandas默认的类型
stock_day.index = pd.to_datetime(stock_day.index)
 
# 2、进行频率转换日K---周K,首先让所有指标都为最后一天的价格
period_week_data = stock_day.resample('W').last()
 
# 分别对于开盘、收盘、最高价、最低价进行处理
period_week_data['open'] = stock_day['open'].resample('W').first()
# 处理最高价和最低价
period_week_data['high'] = stock_day['high'].resample('W').max()
# 最低价
period_week_data['low'] = stock_day['low'].resample('W').min()
# 成交量 这一周的每天成交量的和
period_week_data['volume'] = stock_day['volume'].resample('W').sum()

For missing values

period_week_data.dropna(inplace=True)

We can display the calculated week K and the original day K drawing

  • Draw a K-line graph display
Financial data drawn with the mpl_finance framework
need to specify the address of the installation:
pip install https://github.com/matplotlib/mpl_finance/archive/master.zip

Code:

from mpl_finance import candlestick_ochl
import matplotlib.pyplot as plt
from matplotlib.pylab import date2num
 
# 先画日K线
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(20, 8), dpi=80)
# 准备数据, array数组
stock_day['date'] = date2num(stock_day.index)
day_k = stock_day[['date', 'open', 'close', 'high', 'low']]
# 绘制k线图
candlestick_ochl(axes, day_k.values, width=0.2, colorup='r', colordown='g')
# x刻度设置为日期
axes.xaxis_date()
plt.show()
 
# 周K线图数据显示出来
fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(20, 8), dpi=80)
period_week_data['date'] = date2num(period_week_data.index)
week_k = period_week_data[['date', 'open', 'close', 'high', 'low']]
# 绘制k线图
candlestick_ochl(axes, week_k.values, width=0.2, colorup='r', colordown='g')
# x刻度设置为日期
axes.xaxis_date()
plt.show()

4 What is de-entitlement data and re-entitlement operations (understand)

Listed companies will have a series of equity changes such as cash dividends, bonus shares, etc. from time to time, which will cause abnormal changes in stock prices, which makes it impossible for us to directly calculate stock prices through stock prices. This kind of data is also called ex-rights data.

So we need to process this kind of data, which is also called re-rights data. How to restore rights?

简单的一种方式:
原始数据:
1号:100  2号:50 3号:53 4号:51
复权后:
100 / 50 = 2 比例
1号:100  2号:100 3号:106 4号:102

5 Fundamental data

The use of fundamental data

It is mainly used for fundamental analysis. It mainly focuses on the research and analysis of the company from the fundamental factors of the stock, such as the business ability, financial status, industry background, etc., in an attempt to find the "intrinsic value" of the stock from the company's perspective, so as to The stock market value is compared and the stocks with the most investment value are selected.

Guess you like

Origin blog.csdn.net/weixin_52071682/article/details/115218602