import pandas as pd import numpy as np import matplotlib.pyplot as plt import movingAverage as ma TSMC = pd.read_csv('2330.csv', sep='\t') TSMC.index = pd.to_datetime(TSMC.Date) Close = TSMC.Close Close.describe() Close15 = Close['2015'] sma10 = ma.smaCal(Close15, 10) sma10.tail(n=3) weight = np.array(range(1, 11)) / sum(range(1, 11)) wma10 = ma.wmaCal(Close15, weight) wma10.tail(n=3) expo = 2 / (len(Close15) + 1) ewma10 = ma.ewmaCal(Close15, 10, expo) ewma10.tail(n=3) plt.plot(Close15[10:], label="Close", color='k') plt.plot(sma10[10:], label="sma10", color='r', linestyle='dashed') plt.plot(wma10[10:], label="wma10", color='b', linestyle=':') plt.plot(ewma10[10:], label="ewma10", color='G', linestyle='-.') plt.title('台積電股價和三種均線圖') plt.legend() sma5 = ma.smaCal(Close15, 5) sma30 = ma.smaCal(Close15, 30)
# -*- coding: utf-8 -*- import numpy as np import pandas as pd import movingAverage as ma #1. dow = pd.read_csv('Data/Part5/004/problem30-1.csv', index_col='date') dow.index.name = 'Date' dow.index = pd.to_datetime(dow.index, format='%Y-%m-%d') sma = ma.smaCal(dow.Close, 30) wma = ma.wmaCal(dow.Close, [(i + 1) / 465 for i in range(30)]) ema = ma.ewmaCal(dow.Close, 30, 0.8) maValues = pd.concat([sma, wma, ema], 1).replace(0, np.nan).dropna() maValues.columns = ['sma', 'wma', 'ema'] maValues.plot() #2. zgyh = pd.read_csv('Data/Part5/004/problem30-2.csv', index_col='date') zgyh.index.name = 'Date' zgyh.index = pd.to_datetime(zgyh.index, format='%Y-%m-%d') import matplotlib.pyplot as plt mom14 = (zgyh.Close - zgyh.Close.shift(14)) / zgyh.Close.shift(14) ma14 = ma.smaCal(zgyh.Close, 14)