if all([ lagOpen[i], lagOpen[i] < lag2Close[i], lagClose[i] < Open[i], (lagClose[i] < lag2Close[i]) ]): Doji.append(i) else: Doji.append(0) ret = Close / Close.shift(1) - 1 lag1ret = ret.shift(1) lag2ret = ret.shift(2) Trend = [0, 0, 0] for i in range(3, len(ret)): if all([lag1ret[i] < lag2ret[i] < 0]): Trend.append(1) else: Trend.append(0) StarSig = [] for i in range(len(Trend)): if all([Shape[i] == 1, Doji[i] == 1, Trend[i] == 1]): StarSig.append(1) else: StarSig.append(0) for i in range(len(StarSig)): if StarSig[i] == 1: print(ssec2012.index[i]) ssec201209 = ssec2012['2012-08-21':'2012-09-30'] candle.candlePlot(ssec201209, title='上证综指2012年9月份的日K线图')
#2. shzheng = pd.read_csv('Data/Part5/001/problem27-2.csv', index_col='date') shzheng.index.name='Date' shzheng.index = pd.to_datetime(shzheng.index,format='%Y-%m-%d') shzheng131=shzheng['2013-01-01':'2013-06-30'] CL_OP = shzheng131.Close - shzheng131.Open CL_OP.describe() Doji = pd.Series(np.where(np.abs(CL_OP.values)<8,1,0), index=CL_OP.index) Doji[Doji==1].index cd.candlePlot(shzheng131, title='Candle Plot of Shanghai Composite Index') #3. shzheng = pd.read_csv('Data/Part5/001/problem27-3.csv', index_col='date') shzheng.index.name='Date' shzheng.index = pd.to_datetime(shzheng.index,format='%Y-%m-%d') CL_OP = shzheng.Close - shzheng.Open CL_OP.describe() dat = pd.concat([CL_OP,CL_OP.shift(1),CL_OP.shift(2)],1)
StarSig = [] for i in range(len(Trend)): if all([Shape[i] == 1, Doji[i] == 1, Trend[i] == 1]): StarSig.append(1) else: StarSig.append(0) for i in range(len(StarSig)): if StarSig[i] == 1: print(ssec2012.index[i]) ssec201209 = ssec2012['2012-08-21':'2012-09-30'] # Need to specify path before import import candle candle.candlePlot(ssec201209, title=' 上 证 综 指 2012 年9 月 份 的 日 K 线图 ') # Dark Cloud Cover # 提 取 读 入 上 证 综 指 年 的 日 交 易 数 据 import pandas as pd ssec2011 = pd.read_csv('ssec2011.csv') ssec2011.index = ssec2011.iloc[:, 1] ssec2011.index = pd.to_datetime(ssec2011.index, format='%Y-%m-%d') ssec2011 = ssec2011.iloc[:, 2:] # 提 取 价 格 数 据 Close11 = ssec2011.Close Open11 = ssec2011.Open # 刻 画 捕 捉 符 合 “ 乌 云 盖 顶 ” 形 态 的 连 续 两 个 蜡 烛 实 体 lagClose11 = Close11.shift(1)
plt.rcParams['font.sans-serif'] = ['SimHei'] plt.rcParams['axes.unicode_minus'] = False ax.set_title('加權股價指數2013年4月份K線圖') candlestick_ohlc(ax, taiex201304_listData, width=0.7, colorup='r', colordown='g') plt.setp(plt.gca().get_xticklabels(), rotation=50, horizontalalignment='center') plt.show() from candle import candlePlot candlePlot(taiex201304, title='加權股價指數2013年4月份K線圖') #morning star taiex2011 = pd.read_csv('taiex2011.csv', sep='\t') taiex2011.index = pd.to_datetime(taiex2011.Date, format='%Y-%m-%d') taiex2011.head(2) taiex2011.iloc[-2:, :] Close = taiex2011.Close Open = taiex2011.Open ClOp = Close - Open ClOp.head() ClOp.describe() Shape = [0, 0, 0]
RSV.name = 'RSV' periodHigh.head(3) periodLow.head(3) RSV.head() RSV.describe() close1 = close['2015'] RSV1 = RSV['2015'] Cl_RSV = pd.DataFrame([close1, RSV1]).transpose() Cl_RSV.plot(subplots=True, title='未成熟随机指标RSV') GSPC2015 = GSPC['2015'] import candle candle.candlePlot(GSPC2015,\ '标普500指数2015年日K线图') RSV1 = pd.Series([50, 50], index=date[6:8]).append(RSV) RSV1.name = 'RSV' RSV1.head() KValue = pd.Series(0.0, index=RSV1.index) KValue[0] = 50 for i in range(1, len(RSV1)): KValue[i] = 2 / 3 * KValue[i - 1] + RSV1[i] / 3 KValue.name = 'KValue' KValue.head() DValue = pd.Series(0.0, index=RSV1.index) DValue[0] = 50
RSI5 = rsi(jtyh.Close, 5) RSI6 = rsi(jtyh.Close, 6) RSI7 = rsi(jtyh.Close, 7) RSI8 = rsi(jtyh.Close, 8) RSI9 = rsi(jtyh.Close, 9) RSI10 = rsi(jtyh.Close, 10) RSI12 = rsi(jtyh.Close, 12) RSI_all = pd.concat([RSI5, RSI6, RSI7, RSI8, RSI9, RSI10, RSI12], 1) #2. jtyh = pd.read_csv('Data/Part5/003/problem29-2.csv', index_col='date') jtyh.index.name = 'Date' jtyh.index = pd.to_datetime(jtyh.index, format='%Y-%m-%d') import candle candle.candlePlot(jtyh, 'Candle Plot of Bank of Communications') RSI6 = rsi(jtyh.Close, 6) RSI30 = rsi(jtyh.Close, 30) RSI = pd.concat([RSI6, RSI30], 1) RSI.columns = ['RSI6', 'RSI30'] RSI.plot() signal = np.where(RSI6 > 90, -1, np.where(RSI6 < 10, 1, 0)) ret = (jtyh.Close - jtyh.Close.shift(1)) / jtyh.Close.shift(1) ret6 = ret[6:] signal = pd.Series(signal, index=ret6.index) RSI_ret = ret6[1:] * signal.shift(1)[1:] sum(RSI_ret > 0) / sum(signal != 0) RSI_all = pd.concat([RSI6, RSI30, RSI6.shift(1), RSI30.shift(1)], 1)
/(periodHigh-periodLow)) RSV=pd.Series(RSV,index=Close.index[8:]) RSV.name='RSV' RSV.head() RSV.describe() Close1=Close['2015'] RSV1=RSV['2015'] Cl_RSV=pd.DataFrame([Close1,RSV1]).transpose() Cl_RSV.plot(subplots=True, title='未成熟隨機指標RSV') GSPC2015=GSPC['2015'] import candle candle.candlePlot(GSPC2015,\ '2015年標普500指數日K線圖') K=[50] D=[50] for i in range(len(RSV)): KValue = (2/3)*K[-1] + (RSV[i]/3) DValue = (2/3)*D[-1] + KValue/3 K.append(KValue) D.append(DValue) K=pd.Series(K[1:],index=RSV.index) K.name='KValue' K.head()
# encoding: UTF-8 import pandas as pd import numpy as np import candle as cd import matplotlib.pyplot as plt # shzheng = pd.read_csv(r'data\problem27-1.csv', index_col='date') # shzheng.index.name = 'Date' # shzheng.index = pd.to_datetime(shzheng.index, format='%Y-%m-%d') # shzheng13 = shzheng['2013-03-01':'2013-05-31'].copy() # cd.candleVolume(shzheng13, candletitle='上证指数', bartitle='成交量') shzheng = pd.read_csv('data\problem27-2.csv', index_col='date') shzheng.index.name = 'Date' shzheng.index = pd.to_datetime(shzheng.index, format='%Y-%m-%d') shzheng131 = shzheng['2013-01-01':'2013-06-30'].copy() CL_OP = shzheng131.Close - shzheng131.Open print(CL_OP.describe()) Doji = pd.Series(np.where(np.abs(CL_OP.values) < 5, 1, 0), index=CL_OP.index) print(Doji[Doji == 1].index) cd.candlePlot(shzheng131, title='Candle Plot of Shanghai Composite Index')