zipline-benchmarks.py文件改写
生活随笔
收集整理的這篇文章主要介紹了
zipline-benchmarks.py文件改写
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
改寫原因:在這個模塊中的?get_benchmark_returns() 方法回去谷歌財經(jīng)下載對應(yīng)SPY(類似于上證指數(shù))的數(shù)據(jù),但是Google上下載的數(shù)據(jù)在最后寫入Io操作的時候會報一個惡心的編碼的錯誤,很煩人,時好時壞的那種,就是圖下這種報錯。
改寫方式:
1.首先去雅虎財經(jīng)下載SPY.csv文件,然后把這個文件放到你對應(yīng)的目錄下
2.具體代碼如下
# # Copyright 2013 Quantopian, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np import pandas as pd import pytz from datetime import datetimeimport pandas_datareader.data as pd_readerdef get_benchmark_returns(symbol, first_date, last_date):"""Get a Series of benchmark returns from Google associated with `symbol`.Default is `SPY`.Parameters----------symbol : strBenchmark symbol for which we're getting the returns.first_date : pd.TimestampFirst date for which we want to get data.last_date : pd.TimestampLast date for which we want to get data.The furthest date that Google goes back to is 1993-02-01. It has missingdata for 2008-12-15, 2009-08-11, and 2012-02-02, so we add data for thedates for which Google is missing data.We're also limited to 4000 days worth of data per request. If we make arequest for data that extends past 4000 trading days, we'll still onlyreceive 4000 days of data.first_date is **not** included because we need the close from day N - 1 tocompute the returns for day N."""# 源碼# data = pd_reader.DataReader(# symbol,# 'google',# first_date,# last_date# )## data = data['Close']## data[pd.Timestamp('2008-12-15')] = np.nan# data[pd.Timestamp('2009-08-11')] = np.nan# data[pd.Timestamp('2012-02-02')] = np.nan## data = data.fillna(method='ffill')# return data.sort_index().tz_localize('UTC').pct_change(1).iloc[1:]# 自己寫的代碼# parse = lambda x: pytz.utc.localize(datetime.strptime(x, '%Y-%m-%d'))# data = pd.read_csv("SPY.csv", parse_dates=['Date'], index_col=0, date_parser=parse)# data = data['Close']# data = data.fillna(method='ffill')# return data.sort_index().pct_change(1).iloc[0:]總結(jié):
1.這次報錯后,我習(xí)慣性的找到最底層也就是最后兩行錯誤,但是只能知道是編碼的錯誤,但是解決不了。所以以后碰到類似的第三方包的錯誤,不要急著從最底層開始改,應(yīng)該適當(dāng)?shù)南胂?#xff0c;我在最開始出錯的地方可不可以成功的避免掉,這也是一種思路。
2.Google財經(jīng)的SPY數(shù)據(jù)不全,所以他在這個方法中定義了三列是因為這三天的數(shù)據(jù)他沒有。
?
轉(zhuǎn)載于:https://www.cnblogs.com/wuyongqiang/p/7943499.html
總結(jié)
以上是生活随笔為你收集整理的zipline-benchmarks.py文件改写的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript实现黏贴上传图片功能
- 下一篇: RocketMQ实战(一)