日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

zipline-benchmarks.py文件改写

發(fā)布時間:2023/12/20 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。