1.处理沪深300股票获得收益率
(数据内容来自dsai-book/tools at master · dsai-python/dsai-book · GitHub)
#1 load the api
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os
#2 read the data
index_path = r'D:\python_pycharm\dsai-book-master\data\SZ399300.TXT'
stock_path = r'D:/python_pycharm/dsai-book-master/data/hs300'
index0 = pd.read_table(index_path, encoding='cp936', header=None)
index0.columns = ['date', 'o', 'h', 'l', 'c', 'v', 'to'] # set the title of the columns
index1 = index0[:-1] # delete some chinese words in the last line
index1.index = index1['date'] # set the index of the data
names = os.listdir(stock_path) # read the 300 file names
close = [] # carry on the closing price column
#3 process the data
for name in names:
spach = stock_path + '/' + name # get every single data of the stocks
tmp = pd.read_table(spath, enconding='cp936', header=None)
df = tmp[:-1] # delete some chinese words in the last line
df.columns = ['date', 'o', 'h', 'l', 'c', 'v', 'to']
df.index = df['date']
df1 = df.reindex(index1.index, method='ffill')
df2 = df.fillna(method='bfill')
close.append(df2['c'].values)
close = np.asarry(close).T # from columns to arrays
retx = (close[1:,:] - clsoe[0:-1,:]) / close[:-1,:] # calculate the profit rate
2. 面向对象的编程思维:
# 1. import api
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os
# 2 import data
def read_data(path, name):
f = open(path + name, 'r')
close_price = []
for line in f:
s = line.split('\t')
if(len(s)>4): # ignore the last line
close_price.append(float(s.[4]))
f.close()
return close_price
# 3 deal with the data
def process_data(data):
p = np.asarray(data)
rate = (p[1:] - p[:-1])/p[:-1]
return rate
# 4 draw plot
def draw_plot(rate,k=30):
plt.figure(figsize=(20,8), dpi=100)
plt.plot(rate)