一.TuShare簡介和環境安裝python
TuShare是一個著名的免費、開源的python財經數據接口包。其官網主頁爲:TuShare -財經數據接口包。該接口包現在提供了大量的金融數據,涵蓋了股票、基本面、宏觀、新聞的等諸多類別數據(具體請自行查看官網),並還在不斷更新中。TuShare能夠基本知足量化初學者的回測需求 環境安裝:pip install tushare。若是是老版本升級,能夠用升級命令pip install tushare --upgrade3,在python中導入包:import tushare as ts
二.Tushare的應用網絡
咱們主要仍是應該掌握如何用tushare獲取股票行情數據,使用的是ts.get_hist_data()函數或者ts.get_k_data()函數。函數
輸入參數爲: code:股票代碼,即6位數字代碼,或者指數代碼(sh=上證指數 sz=深圳成指 hs300=滬深300指數 sz50=上證50 zxb=中小板 cyb=創業板) start:開始日期,格式YYYY-MM-DD end:結束日期,格式YYYY-MM-DD ktype:數據類型,D=日k線 W=周 M=月 5=5分鐘 15=15分鐘 30=30分鐘 60=60分鐘,默認爲D retry_count:當網絡異常後重試次數,默認爲3 pause:重試時停頓秒數,默認爲0 返回值說明: date:日期 open:開盤價 high:最高價 close:收盤價 low:最低價 volume:成交量 price_change:價格變更 p_change:漲跌幅 ma5:5日均價 ma10:10日均價 ma20:20日均價 v_ma5:5日均量 v_ma10:10日均量 v_ma20:20日均量 turnover:換手率[注:指數無此項]
import tushare as ts # 使用tushare包獲取某股票的歷史行情數據。 df = ts.get_k_data(code='600519',start='2000-01-01') # 將從Tushare中獲取的數據存儲至本地 df.to_csv("600519.csv") # 將原數據中的時間做爲行索引,並將字符串類型的時間序列化成時間對象類型 # 將date這一列做爲源數據的行索引且將數據類型轉成時間類型 df = pd.read_csv('./600519.csv',index_col='date',parse_dates=['date']) df.drop(labels='Unnamed: 0',axis=1,inplace=True) # 多出來一行 Unnamed: 0 ,須要去掉它 # inplace默認值爲false 將刪除的操做映射到原數據
#指定條件 #輸出該股票全部收盤比開盤上漲3%以上的日期。 #(收盤-開盤)/開盤 >= 0.03 df['close'] - df['open'] / df['open'] >= 0.03 # 打印結果: date 2001-08-27 True 2001-08-28 True 2001-08-29 True 2001-08-30 True 2001-10-12 True ... 2019-08-02 True 2019-08-05 True 2019-08-06 True 2019-08-07 True 2019-08-08 True 2019-08-09 True
#將上述表達式返回的布爾值做爲df的行索引:取出了全部符合需求的行數據 df.loc[(df['close']-df['open']) / df['open'] >= 0.03] # 打印結果: open close high low volume code date 2001-08-27 5.392 5.554 5.902 5.132 406318.00 600519 2001-08-28 5.467 5.759 5.781 5.407 129647.79 600519 2001-09-10 5.531 5.734 5.757 5.470 18878.89 600519 ... ... ... ... ... ... ... 2004-11-25 9.251 9.561 9.676 9.251 5924.14 600519 ... ... ... ... ... ... ... 2017-11-16 676.406 709.043 709.881 676.406 60716.00 600519 ... ... ... ... ... ... ... 2019-04-10 903.000 947.990 951.900 900.000 67814.00 600519 2019-04-16 904.900 939.900 939.900 901.220 46423.00 600519 2019-05-10 875.660 907.120 910.780 868.190 79907.00 600519 2019-05-15 890.240 927.000 933.000 890.240 63124.00 600519 2019-06-11 876.000 910.890 915.610 875.000 80106.00 600519 2019-06-20 932.500 975.000 975.500 932.200 67271.00 600519
df.loc[(df['close'] - df['open']) / df['open'] >= 0.03].index # index 取到行索引 df.loc[(df['close'] - df['open']) / df['open'] >= 0.03].index # 打印結果: DatetimeIndex(['2001-08-27', '2001-08-28', '2001-09-10', '2001-12-21', '2002-01-18', '2002-01-31', '2003-01-14', '2003-10-29', '2004-01-05', '2004-01-14', ... '2019-01-15', '2019-02-11', '2019-03-01', '2019-03-18', '2019-04-10', '2019-04-16', '2019-05-10', '2019-05-15', '2019-06-11', '2019-06-20'], dtype='datetime64[ns]', name='date', length=301, freq=None)
#輸出該股票全部開盤比前日收盤跌幅超過2%的日期。 #(開盤 - 前日收盤) / 前日收盤 < -0.02 # df['close'].shift(1)) 收盤數據往下移一位 (df['open'] - df['close'].shift(1)) / df['close'].shift(1) < -0.02 # 打印結果 DatetimeIndex(['2001-09-12', '2002-06-26', '2002-12-13', '2004-07-01', '2004-10-29', '2006-08-21', '2006-08-23', '2007-01-25', '2007-02-01', '2007-02-06', '2007-03-19', '2007-05-21', '2007-05-30', '2007-06-05', '2007-07-27', '2007-09-05', '2007-09-10', '2008-03-13', '2008-03-17', '2008-03-25', '2008-03-27', '2008-04-22', '2008-04-23', '2008-04-29', '2008-05-13', '2008-06-10', '2008-06-13', '2008-06-24', '2008-06-27', '2008-08-11', '2008-08-19', '2008-09-23', '2008-10-10', '2008-10-15', '2008-10-16', '2008-10-20', '2008-10-23', '2008-10-27', '2008-11-06', '2008-11-12', '2008-11-20', '2008-11-21', '2008-12-02', '2009-02-27', '2009-03-25', '2009-08-13', '2010-04-26', '2010-04-30', '2011-08-05', '2012-03-27', '2012-08-10', '2012-11-22', '2012-12-04', '2012-12-24', '2013-01-16', '2013-01-25', '2013-09-02', '2014-04-25', '2015-01-19', '2015-05-25', '2015-07-03', '2015-07-08', '2015-07-13', '2015-08-24', '2015-09-02', '2015-09-15', '2017-11-17', '2018-02-06', '2018-02-09', '2018-03-23', '2018-03-28', '2018-07-11', '2018-10-11', '2018-10-24', '2018-10-25', '2018-10-29', '2018-10-30', '2019-05-06', '2019-05-08'], dtype='datetime64[ns]', name='date', freq=None)
# 取出符合要求的行數據 df.loc[(df['open'] - df['close'].shift(1)) / df['close'].shift(1) < -0.02] df.loc[(df['open'] - df['close'].shift(1)) / df['close'].shift(1) < -0.02].index # 執行結果爲: DatetimeIndex(['2001-09-12', '2002-06-26', '2002-12-13', '2004-07-01', '2004-10-29', '2006-08-21', '2006-08-23', '2007-01-25', '2007-02-01', '2007-02-06', '2007-03-19', '2007-05-21', '2007-05-30', '2007-06-05', '2007-07-27', '2007-09-05', '2007-09-10', '2008-03-13', '2008-03-17', '2008-03-25', '2008-03-27', '2008-04-22', '2008-04-23', '2008-04-29', '2008-05-13', '2008-06-10', '2008-06-13', '2008-06-24', '2008-06-27', '2008-08-11', '2008-08-19', '2008-09-23', '2008-10-10', '2008-10-15', '2008-10-16', '2008-10-20', '2008-10-23', '2008-10-27', '2008-11-06', '2008-11-12', '2008-11-20', '2008-11-21', '2008-12-02', '2009-02-27', '2009-03-25', '2009-08-13', '2010-04-26', '2010-04-30', '2011-08-05', '2012-03-27', '2012-08-10', '2012-11-22', '2012-12-04', '2012-12-24', '2013-01-16', '2013-01-25', '2013-09-02', '2014-04-25', '2015-01-19', '2015-05-25', '2015-07-03', '2015-07-08', '2015-07-13', '2015-08-24', '2015-09-02', '2015-09-15', '2017-11-17', '2018-02-06', '2018-02-09', '2018-03-23', '2018-03-28', '2018-07-11', '2018-10-11', '2018-10-24', '2018-10-25', '2018-10-29', '2018-10-30', '2019-05-06', '2019-05-08'], dtype='datetime64[ns]', name='date', freq=None)
price_last = df['open'][-1] df = df['2010-01':'2019-01'] #剔除首尾無用的數據 #Pandas提供了resample函數用便捷的方式對時間序列進行重採樣,根據時間粒度的變大或者變小分爲降採樣和升採樣: df_monthly = df.resample("M").first() df_yearly = df.resample("A").last()[:-1] #去除最後一年 # [:-1] 把19年去掉,還沒到19年末,19年只買了,還沒賣 ost_money cost_money = df_monthly['open'].sum()*100 # cost_money 3339687.1 df_yearly['open'].sum()*1200 # 12個月 一個月買100支 2948584.7999999993 recv_monry = df['open'][-1] * 800 + df_yearly['open'].sum()*1200 # df['open'][-1] * 800 爲19年還剩的錢,今天是8月份 800支 recv_monry - cost_money # 391697.69999999925
循環的方式實現code
price_last = df['open'][-1] df = df['2010-01':'2019-01'] #剔除首尾無用的數據 #Pandas提供了resample函數用便捷的方式對時間序列進行重採樣,根據時間粒度的變大或者變小分爲降採樣和升採樣: df_monthly = df.resample("M").first() df_yearly = df.resample("A").last()[:-1] #去除最後一年 # [:-1] 把19年去掉,還沒到19年末,19年只買了,還沒賣 cost_money = 0 hold = 0 #每一年持有的股票 for year in range(2010, 2019): cost_money -= df_monthly.loc[str(year)]['open'].sum()*100 hold += len(df_monthly[str(year)]['open']) * 100 if year != 2019: cost_money += df_yearly[str(year)]['open'][0] * hold hold = 0 #每一年持有的股票 cost_money += hold * price_last print(cost_money)