In [1]:
# 啟動互動式繪圖環境
%pylab inline
In [6]:
import numpy as np
import pandas as pd
from numpy import random
import matplotlib.pyplot as plt
result = pd.read_csv('/Users/wy/Desktop/3008.txt')
In [7]:
result.head()
Out[7]:
In [7]:
result.to_csv('/Users/wy/Desktop/3008.csv')
In [7]:
result[result['Open'] ==1]
Out[7]:
In [9]:
import numpy as np
import pandas as pd
from numpy import random
import matplotlib.pyplot as plt
with open ('/Users/wy/Desktop/3008.txt','r') as f:
line = f.readlines()
line[0].rstrip()
ll=[]
for a in line[1:]:
l =[]
a = a.rstrip()
a = a.split(',')
l.append(a[0])
for a in a[1:]:
try:
l.append(float(a))
except:
l.append(0)
ll.append(l)
rowname = ['Date','Open','High','Low','Close','Volume','Adj Close']
stock_df = pd.DataFrame(ll, columns=rowname)
In [10]:
stock_df.head(584)
Out[10]:
In [11]:
stock_df.Open.describe()
Out[11]:
In [25]:
fig = plt.figure()
ax = fig.add_subplot(1, 1,1)
ax.plot(stock_df['Open'])
ax.plot(stock_df['High'])
ax.plot(stock_df['Low'])
ax.plot(stock_df['Close'])
ax.plot(stock_df['Adj Close'])
Out[25]:
In [26]:
stock_df[stock_df['Volume'] > 5*10**7]
Out[26]:
In [3]:
%reset
In [401]:
result['Date'].size
Out[401]:
In [561]:
# Rise Ratio 漲幅比
def RR(data):
# 由於 data 新到舊 0~xxx,遞增,因此需反轉陣列
dataList = range(data['Date'].size)
dataList.reverse()
tmpList = []
for item in dataList:
# 防止 第一筆data沒有更舊的
if item-1 >=0:
# (今日收盤價 - 昨日收盤價)/昨日收盤價
tmp = (data['Close'][item-1]-data['Close'][item])/data['Close'][item]*100
tmpList.append(tmp)
# 前day 沒data會出現NA
tmpList.reverse()
tmpSeries = pd.Series(tmpList)
# create RR 欄位
data['RR']=tmpSeries
In [439]:
# 威廉指標(WMS%R或%R)
def WMS(data,day):
# 由於 data 新到舊 0~xxx,遞增,因此需反轉陣列
dataList = range(data['Date'].size)
dataList.reverse()
tmpList = []
for item in dataList:
# 防止前day沒有data
if item-day+1 >= 0:
# 9日WMS%R =(9日內最高價-第9日收盤價) / (9日內最高價-9日內最低價)*100
# [item-day+1:item+1] 今日區間 [item-day+1] 第N日 583-9=574+1=575
tmp = (data['High'][item-day+1:item+1].max()-data['Close'][item-day+1])/(data['High'][item-day+1:item+1].max()-data['Low'][item-day+1:item+1].min())*100
tmpList.append(tmp)
# 前day 沒data會出現NA
tmpList.reverse()
tmpSeries = pd.Series(tmpList)
# create WMS 欄位
data['WMS']=tmpSeries
In [345]:
# 買賣意願指標 day 建議26
def BR(data,day):
# 由於 data 新到舊 0~xxx,遞增,因此需反轉陣列
dataList = range(data['Date'].size)
dataList.reverse()
tmpList = []
for item in dataList:
# 防止前day沒有data
if item-day >= 0:
# 26日BR = (今日最高價 - 昨日收盤價)26天累計總數 / (昨日收盤價 - 今日最低價)26天累計總數
# [(item-day+1)-1:(item+1)-1] 有-1 今日區間 [(item-day+1):(item+1)] 昨日區間
tmp = (data['High'][(item-day+1)-1:(item+1)-1].sum()-data['Close'][item-day+1:item+1].sum())/(data['Close'][item-day+1:item+1].sum()-data['Low'][(item-day+1)-1:(item+1)-1].sum())
tmpList.append(tmp)
# 前day 沒data會出現NA
tmpList.reverse()
tmpSeries = pd.Series(tmpList)
# create BR 欄位
data['BR']=tmpSeries
In [446]:
# 買賣氣勢指標 day建議26
def AR(data,day):
# 由於 data 新到舊 0~xxx,遞增,因此需反轉陣列
dataList = range(data['Date'].size)
dataList.reverse()
tmpList = []
for item in dataList:
# 防止前day沒有data
if item-day+1 >= 0:
# 26日AR = (最高價 - 開盤價)26天累計總數 / (開盤價 - 最低價)26天累計總數
# [item-day+1:item+1] 今日區間
tmp = (data['High'][item-day+1:item+1].sum()-data['Open'][item-day+1:item+1].sum())/(data['Open'][item-day+1:item+1].sum()-data['Low'][item-day+1:item+1].sum())
tmpList.append(tmp)
# 前day 沒data會出現NA
tmpList.reverse()
tmpSeries = pd.Series(tmpList)
# create AR 欄位
data['AR']=tmpSeries
In [454]:
# 平均成交量 mean volumn day建議12
def MV(data,day):
# 由於 data 新到舊 0~xxx,遞增,因此需反轉陣列
dataList = range(data['Date'].size)
dataList.reverse()
tmpList = []
for item in dataList:
# 防止前day沒有data
if item-day+1 >= 0:
# N日平均量 = N日內的成交量總和 / N
# [item-day+1:item+1] 今日區間
tmp = data['Volume'][item-day+1:item+1].mean()
tmpList.append(tmp)
# 前day 沒data會出現NA
tmpList.reverse()
tmpSeries = pd.Series(tmpList)
# create MV 欄位
data['MV']=tmpSeries
In [8]:
# 移動平均線(MA,Moving Average) 建議12
def MA(data,day):
# 由於 data 新到舊 0~xxx,遞增,因此需反轉陣列
dataList = range(data['Date'].size)
dataList.reverse()
tmpList = []
for item in dataList:
# 防止前day沒有data
if item-day+1 >= 0:
# 移動平均數 = 採樣天數的股價合計 / 採樣天數
# [item-day+1:item+1] 今日區間
tmp = data['Close'][item-day+1:item+1].mean()
tmpList.append(tmp)
# 前day 沒data會出現NA
tmpList.reverse()
tmpSeries = pd.Series(tmpList)
# create MA 欄位
data['MA'+str(day)]=tmpSeries
In [475]:
# 心理線(PSY) 建議13
def PSY(data,day):
# 由於 data 新到舊 0~xxx,遞增,因此需反轉陣列
dataList = range(data['Date'].size)
dataList.reverse()
tmpList = []
for item in dataList:
# 防止前day沒有data
if item-day >= 0:
# 13日PSY值 = ( 13日內之上漲天數 / 13 ) * 100
# [item-day+1-1:item+1-1] 跳一天 最早的天沒有RR值
count = 0
for a in data['RR'][item-day+1-1:item+1-1]:
if a > 0:
count+=1
tmp = float(count)/float(13)*100
tmpList.append(tmp)
# 前day 沒data會出現NA
tmpList.reverse()
tmpSeries = pd.Series(tmpList)
# create PSY 欄位
data['PSY']=tmpSeries
In [519]:
# 能量潮(OBV) 建議12
def OBV(data,day):
# 由於 data 新到舊 0~xxx,遞增,因此需反轉陣列
dataList = range(data['Date'].size)
dataList.reverse()
tmpList = []
for item in dataList:
# 防止前day沒有data
if item-day >= 0:
# 今日OBV值 = 最近12天股價上漲日成交量總和 - 最近12天股價下跌日成交量總和
# 先由 ['RR'] 求出boolean值 > 0 True 套入['Volume']符合True全加起來
bolRise = data['RR'][item-day+1-1:item+1-1]>0
sumVolRise = data['Volume'][item-day+1-1:item+1-1][bolRise].sum()
bolDesc = data['RR'][item-day+1-1:item+1-1]<0
sumVolDesc = data['Volume'][item-day+1-1:item+1-1][bolDesc].sum()
tmp = sumVolRise-sumVolDesc
# 可切換 OBV累積12日移動平均值 = (最近12天股價上漲日成交量總和 - 最近12天股價下跌日成交量總和) / 12
# tmp = (sumVolRise-sumVolDesc)/12
tmpList.append(tmp)
# 前day 沒data會出現NA
tmpList.reverse()
tmpSeries = pd.Series(tmpList)
# create OBV 欄位
data['OBV']=tmpSeries
In [545]:
# 數量指標(VR) 建議12
def VR(data,day):
# 由於 data 新到舊 0~xxx,遞增,因此需反轉陣列
dataList = range(data['Date'].size)
dataList.reverse()
tmpList = []
for item in dataList:
# 防止前day沒有data
if item-day >= 0:
# VR = ( N日內上漲日成交值總和 + 1/2*N日內平盤日成交值總和) / ( N日內下跌日成交值總和 + 1/2*N日內平盤日成交值總和)* 100%
# 先由 ['RR'] 求出boolean值 > 0 True 套入['Volume']符合True全加起來
bolRise = data['RR'][item-day+1-1:item+1-1]>0
sumVolRise = data['Volume'][item-day+1-1:item+1-1][bolRise].sum()
bolNorm = data['RR'][item-day+1-1:item+1-1] == 0
sumVolNorm = data['Volume'][item-day+1-1:item+1-1][bolNorm].sum()
bolDesc = data['RR'][item-day+1-1:item+1-1]<0
sumVolDesc = data['Volume'][item-day+1-1:item+1-1][bolDesc].sum()
tmp = (sumVolRise+0.5*sumVolNorm)/(sumVolDesc+0.5*sumVolNorm)*100
tmpList.append(tmp)
# 前day 沒data會出現NA
tmpList.reverse()
tmpSeries = pd.Series(tmpList)
# create VR 欄位
data['VR']=tmpSeries
In [569]:
# 相對強弱指標(RSI) 建議6
def RSI(data,day):
# 由於 data 新到舊 0~xxx,遞增,因此需反轉陣列
dataList = range(data['Date'].size)
dataList.reverse()
tmpList = []
for item in dataList:
# 防止前day沒有data
if item-day >= 0:
# 6日RSI=100*6日內收盤上漲總幅度平均值 / (6日內收盤上漲總幅度平均值 - 6日內收盤下跌總幅度平均值)
# 先由 ['RR'] 求出boolean值 > 0 True 套入['Volume']符合True全加起來
bolRise = data['RR'][item-day+1-1:item+1-1]>0
meanRise = data['RR'][item-day+1-1:item+1-1][bolRise].mean()
bolDesc = data['RR'][item-day+1-1:item+1-1]<0
meanDesc = data['RR'][item-day+1-1:item+1-1][bolDesc].mean()
tmp = 100*meanRise/(meanRise-meanDesc)
tmpList.append(tmp)
# 前day 沒data會出現NA
tmpList.reverse()
tmpSeries = pd.Series(tmpList)
# create RSI 欄位
data['RSI']=tmpSeries
In [583]:
# 乖離率(BIAS)
def BIAS(data,day):
# 由於 data 新到舊 0~xxx,遞增,因此需反轉陣列
dataList = range(data['Date'].size)
dataList.reverse()
tmpList = []
for item in dataList:
# 防止前day沒有data
if item-day+1 >= 0:
# N日乖離率 = (當日股價 - N日股價移動平均數) / N日平均股價
tmp = (data['Close'][item-day+1]-data['MA'][item-day+1])/data['MA'][item-day+1]*100
tmpList.append(tmp)
# 前day 沒data會出現NA
tmpList.reverse()
tmpSeries = pd.Series(tmpList)
# create BIAS 欄位
data['BIAS']=tmpSeries
In [584]:
BIAS(result,12)
In [573]:
MA(result,12)
In [570]:
RSI(result,6)
In [580]:
item = 583
data = result
day = 12
tmp = (data['Close'][item-day+1]-data['MA'][item-day+1])/data['MA'][item-day+1]*100
print(tmp)
In [585]:
result.tail(15)
Out[585]:
In [435]:
data = result
item = 583
day=9
print(data['High'][item-day+1:item+1])
print(data['Close'][item-day+1])
print(data['Low'][item-day+1:item+1])
s = (data['High'][item-day+1:item+1].max()-data['Close'][item-day+1])/(data['High'][item-day+1:item+1].max()-data['Low'][item-day+1:item+1].min())*100
print(s)
In [422]:
Out[422]:
In [441]:
result.tail(10)
Out[441]:
In [ ]:
In [ ]:
In [379]:
item = 583
day = 9
data = result
# (data['High'][item-day+1:item+1].max()-data['Close'][item])/(data['High'][item-day+1:item+1].max()-data['Low'][item-day+1:item+1].min())*100
data['High'][0:9]
Out[379]:
In [392]:
result.tail(15)
Out[392]:
In [104]:
In [99]:
# 平均成交量 mean volumn day建議12
def MV(data,day):
l=[]
for a in range(0,data['Volume'].size-day+1):
# N日平均量 = N日內的成交量總和 / N
b = data['Volume'][0+a:day+a].mean()
l.append(b)
# 前day補上0
for a in range(day-1):
l.insert(a,0)
sl = pd.Series(l)
# create MV 欄位
data['MV']=sl
In [86]:
# 移動平均線(MA,Moving Average) day建議短期6 中期24,72
def MA(data,day):
l=[]
for a in range(0,data['Volume'].size-day+1):
# 移動平均數 = 採樣天數的股價合計 / 採樣天數
b = data['Volume'][0+a:day+a].mean()
l.append(b)
# 前day補上0
for a in range(day-1):
l.insert(a,0)
sl = pd.Series(l)
# create MA 欄位
data['MA']=sl
In [94]:
MA(result,12)
In [100]:
VM(result,6)
In [105]:
AR(result,26)
In [108]:
BR(result,26)
In [115]:
WMS(result,9)
In [128]:
RR(result)
In [129]:
result
Out[129]:
In [85]:
result['Close'].size
Out[85]:
In [31]:
result['Close'][0]
Out[31]:
In [34]:
result['Close'][0:12].size
Out[34]:
In [38]:
range(0,2)
Out[38]:
In [ ]: