# 用Python 淺析股票數據

## 數據導入

```import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

%matplotlib inline

%config InlineBackend.figure_format = 'retina'

%pylab inline

pylab.rcParams['figure.figsize'] = (10, 6) #設置繪圖尺寸

#讀取數據

stock = pd.read_table('stockData.txt', usecols=range(15), parse_dates=[0], index_col=0)

stock = stock[::-1] #逆序排列

```stock.info()

DatetimeIndex: 20 entries, 2015-01-05 to 2015-01-30

Data columns (total 14 columns):

open 20 non-null float64

high 20 non-null float64

close 20 non-null float64

low 20 non-null float64

volume 20 non-null float64

price_change 20 non-null float64

p_change 20 non-null float64

ma5 20 non-null float64

ma10 20 non-null float64

ma20 20 non-null float64

v_ma5 20 non-null float64

v_ma10 20 non-null float64

v_ma20 20 non-null float64

turnover 20 non-null float64

dtypes: float64(14)

memory usage: 2.3 KB```

```stock.columns

Index([' open', 'high', 'close', 'low', 'volume', 'price_change',

'p_change', 'ma5', 'ma10', 'ma20', 'v_ma5', 'v_ma10', 'v_ma20',

'turnover'],

dtype='object')```

`stock.rename(columns={' open':'open'}, inplace=True)`

`stock['close'].plot(grid=True)`

K線圖

Matplotlib.finance模塊提供了繪製K線圖的函數candlestick_ohlc()，但如果要繪製比較美觀的K線圖還是要下點功夫的。下面定義了pandas_candlestick_ohlc()函數來繪製適用於本文數據的K線圖，其中大部分代碼都是在設置坐標軸的格式。

```from matplotlib.finance import candlestick_ohlc

from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY

def pandas_candlestick_ohlc(stock_data, otherseries=None):

# 設置繪圖參數，主要是坐標軸

mondays = WeekdayLocator(MONDAY)

alldays = DayLocator()

dayFormatter = DateFormatter('%d')

fig, ax = plt.subplots()

if stock_data.index[-1] - stock_data.index[0] < pd.Timedelta('730 days'):

weekFormatter = DateFormatter('%b %d')

ax.xaxis.set_major_locator(mondays)

ax.xaxis.set_minor_locator(alldays)

else:

weekFormatter = DateFormatter('%b %d, %Y')

ax.xaxis.set_major_formatter(weekFormatter)

ax.grid(True)

# 創建K線圖

stock_array = np.array(stock_data.reset_index()[['date','open','high','low','close']])

stock_array[:,0] = date2num(stock_array[:,0])

candlestick_ohlc(ax, stock_array, colorup = "red", colordown="green", width=0.4)

# 可同時繪製其他折線圖

if otherseries is not None:

for each in otherseries:

plt.plot(stock_data[each], label=each)

plt.legend()

ax.xaxis_date()

ax.autoscale_view()

plt.setp(plt.gca().get_xticklabels(), rotation=45, horizo​​ntalalignment='right')

plt.show()

pandas_candlestick_ohlc(stock)```

```stock['return'] = stock['close'] / stock.close.iloc[0]

stock['return'].plot(grid=True)```

`stock['p_change'].plot(grid=True).axhline(y=0, color='black', lw=2)`

```close_price = stock['close']

log_change = np.log(close_price) - np.log(close_price.shift(1))

log_change.plot(grid=True).axhline(y=0, color='black', lw=2)```

```small = stock[['close', 'price_change', 'ma20','volume', 'v_ma20', 'turnover']]

_ = pd.scatter_matrix(small)```

```small = stock[['close', 'price_change', 'ma20','volume', 'v_ma20']]

cov = np.corrcoef(small.T)

cov

array([[ 1. , 0.30308764, 0.10785519, 0.91078009, -0.37602193],

[ 0.30308764, 1. , -0.45849273, 0.3721832 , -0.25950305],

[ 0.10785519, -0.45849273, 1. , -0.06002202, 0.51793654],

[ 0.91078009, 0.3721832 , -0.06002202, 1. , -0.37617624],

[-0.37602193, -0.25950305, 0.51793654, -0.37617624, 1. ]])```

```img = plt.matshow(cov,cmap=plt.cm.winter)

plt.colorbar(img, ticks=[-1,0,1])

plt.show()```

`stock[['close','volume']].plot(secondary_y='volume', grid=True)`

```import datetime

# 設置股票數據的時間跨度

start = datetime.datetime(2016,10,1)

end = datetime.date.today()

goog = web.DataReader("GOOG", "yahoo", start, end)

#修改索引和列的名稱，以適應本文的分析

goog.index.rename('date', inplace=True)

goog.rename(columns={'Open':'open', 'High':'high', 'Low':'low', 'Close':'close'}, inplace=True)

```goog["ma5"] = np.round(goog["close"].rolling(window = 5, center = False).mean(), 2)

goog["ma20"] = np.round(goog["close"].rolling(window = 20, center = False).mean(), 2)

goog = goog['2017-01-01':]

pandas_candlestick_ohlc(goog, ['ma5','ma20'])```

```goog['ma5-20'] = goog['ma5'] - goog['ma20']

goog['diff'] = np.sign(goog['ma5-20'])

goog['diff'].plot(ylim=(-2,2)).axhline(y=0, color='black', lw=2)```

```goog['signal'] = np.sign(goog['diff'] - goog['diff'].shift(1))

goog['signal'].plot(ylim=(-2,2))```

```trade = pd.concat([

pd.DataFrame({"price": goog.loc[goog["signal"] == 1, "close"],

pd.DataFrame({"price": goog.loc[goog["signal"] == -1, "close"],

"operation": "Sell"})

])

0