Python 2.7 PythonZipline:“;pandas“datareader.”“utils.RemoteDataError”&;本地数据

Python 2.7 PythonZipline:“;pandas“datareader.”“utils.RemoteDataError”&;本地数据,python-2.7,csv,windows-7-x64,zipline,Python 2.7,Csv,Windows 7 X64,Zipline,这是我的第一篇文章,我希望它能做好 我正在尝试使用本地AAPL数据运行以下ZipLine Algo: import pandas as pd from collections import OrderedDict import pytz from zipline.api import order, symbol, record, order_target from zipline.algorithm import TradingAlgorithm data = OrderedDict() da

这是我的第一篇文章,我希望它能做好

我正在尝试使用本地AAPL数据运行以下ZipLine Algo:

import pandas as pd
from collections import OrderedDict
import pytz
from zipline.api import order, symbol, record, order_target
from zipline.algorithm import TradingAlgorithm

data = OrderedDict()
data['AAPL'] = pd.read_csv('AAPL.csv', index_col=0, parse_dates=['Date'])

panel = pd.Panel(data)
panel.minor_axis = ['Open', 'High', 'Low', 'Close', 'Volume', 'Price']
panel.major_axis = panel.major_axis.tz_localize(pytz.utc)

print panel["AAPL"]

def initialize(context):
    context.security = symbol('AAPL')

def handle_data(context, data):
    MA1 = data[context.security].mavg(50)
    MA2 = data[context.security].mavg(100)
    date = str(data[context.security].datetime)[:10]
    current_price = data[context.security].price
    current_positions = context.portfolio.positions[symbol('AAPL')].amount
    cash = context.portfolio.cash
    value = context.portfolio.portfolio_value
    current_pnl = context.portfolio.pnl

# code (this will come under handle_data function only)
    if (MA1 > MA2) and current_positions == 0:
         number_of_shares = int(cash / current_price)
         order(context.security, number_of_shares)
         record(date=date, MA1=MA1, MA2=MA2, Price=
         current_price, status="buy", shares=number_of_shares, PnL=current_pnl, cash=cash, value=value)

    elif (MA1 < MA2) and current_positions != 0:
        order_target(context.security, 0)
        record(date=date, MA1=MA1, MA2=MA2, Price=current_price, status="sell", shares="--", PnL=current_pnl, cash=cash,
           value=value)

    else:
        record(date=date, MA1=MA1, MA2=MA2, Price=current_price, status="--", shares="--", PnL=current_pnl, cash=cash,
           value=value)


#initializing trading enviroment
algo_obj = TradingAlgorithm(initialize=initialize, handle_data=handle_data)
#run algo
perf_manual = algo_obj.run(panel)


#code
#calculation
print "total pnl : " + str(float(perf_manual[["PnL"]].iloc[-1]))
buy_trade = perf_manual[["status"]].loc[perf_manual["status"] == "buy"].count()
sell_trade = perf_manual[["status"]].loc[perf_manual["status"] == "sell"].count()
total_trade = buy_trade + sell_trade
print "buy trade : " + str(int(buy_trade)) + " sell trade : " + str(int(sell_trade)) + " total trade : " + str(int(total_trade))
将熊猫作为pd导入
从集合导入订单
进口皮茨
从zipline.api导入订单、符号、记录、订单和目标
从zipline.algorithm导入交易算法
数据=OrderedDict()
数据['AAPL']=pd.read\u csv('AAPL.csv',index\u col=0,parse\u dates=['Date']))
面板=局部放电面板(数据)
panel.minor_轴=[‘打开’、‘高’、‘低’、‘关闭’、‘音量’、‘价格’]
panel.major_axis=panel.major_axis.tz_本地化(pytz.utc)
打印面板[“AAPL”]
def初始化(上下文):
context.security=symbol('AAPL')
def句柄_数据(上下文、数据):
MA1=data[context.security].mavg(50)
MA2=数据[context.security].mavg(100)
date=str(数据[context.security].datetime)[:10]
当前价格=数据[context.security].price
当前头寸=context.portfolio.positions[符号('AAPL')].金额
cash=context.portfolio.cash
value=context.portfolio.portfolio\u value
当前_pnl=context.portfolio.pnl
#代码(仅在handle_数据函数下)
如果(MA1>MA2)和当前_位置==0:
股票数量=整数(现金/当前价格)
订单(context.security、股票数量)
记录(日期=日期,MA1=MA1,MA2=MA2,价格=
当前价格,状态=“买入”,股票=股票数量,PnL=当前价格,现金=现金,价值=价值)
elif(MA1
我受到了和的启发

我得到这个错误:

Traceback (most recent call last):
File "C:/Users/main/Desktop/docs/ALGO_TRADING/_DATAS/_zipline_data_bundle    /temp.py", line 51, in <module>
algo_obj = TradingAlgorithm(initialize=initialize, handle_data=handle_data)
File "C:\Python27-32\lib\site-packages\zipline\algorithm.py", line 273,  in __init__
self.trading_environment = TradingEnvironment()
File "C:\Python27-32\lib\site-packages\zipline\finance\trading.py", line 99, in __init__
self.bm_symbol,
File "C:\Python27-32\lib\site-packages\zipline\data\loader.py", line 166, in load_market_data
environ,
File "C:\Python27-32\lib\site-packages\zipline\data\loader.py", line 230, in ensure_benchmark_data
last_date,
File "C:\Python27-32\lib\site-packages\zipline\data\benchmarks.py", line 50, in get_benchmark_returns
last_date
File "C:\Python27-32\lib\site-packages\pandas_datareader\data.py", line 137, in DataReader
session=session).read()
File "C:\Python27-32\lib\site-packages\pandas_datareader\base.py", line 181, in read
params=self._get_params(self.symbols))
File "C:\Python27-32\lib\site-packages\pandas_datareader\base.py", line 79, in _read_one_data
out = self._read_url_as_StringIO(url, params=params)
File "C:\Python27-32\lib\site-packages\pandas_datareader\base.py", line 90, in _read_url_as_StringIO
response = self._get_response(url, params=params)
File "C:\Python27-32\lib\site-packages\pandas_datareader\base.py", line 139, in _get_response
raise RemoteDataError('Unable to read URL: {0}'.format(url))
pandas_datareader._utils.RemoteDataError: Unable to read URL: http://www.google.com/finance/historical?q=SPY&startdate=Dec+29%2C+1989&enddate=Dec+20%2C+2017&output=csv
回溯(最近一次呼叫最后一次):
文件“C:/Users/main/Desktop/docs/ALGO_TRADING/_DATAS/_zipline_data_bundle/temp.py”,第51行,在
算法对象=交易算法(初始化=初始化,句柄数据=句柄数据)
文件“C:\Python27-32\lib\site packages\zipline\algorithm.py”,第273行,在\uuu init中__
self.trading\u environment=交易环境()
文件“C:\Python27-32\lib\site packages\zipline\finance\trading.py”,第99行,在\uu init中__
self.bm_符号,
文件“C:\Python27-32\lib\site packages\zipline\data\loader.py”,第166行,在load\u market\u数据中
包围
文件“C:\Python27-32\lib\site packages\zipline\data\loader.py”,第230行,在确保基准测试数据中
最后一天,
文件“C:\Python27-32\lib\site packages\zipline\data\benchmarks.py”,第50行,在get\u benchmark\u返回中
最后日期
datareader中第137行的文件“C:\Python27-32\lib\site packages\pandas\u datareader\data.py”
会话=会话)。读取()
文件“C:\Python27-32\lib\site packages\pandas\u datareader\base.py”,第181行,已读
参数=self.\u获取参数(self.symbols))
文件“C:\Python27-32\lib\site packages\pandas\u datareader\base.py”,第79行,在\u read\u one\u数据中
out=self.\u读取\u url\u作为\u StringIO(url,params=params)
文件“C:\Python27-32\lib\site packages\pandas\u datareader\base.py”,第90行,在\u read\u url\u as\u StringIO中
response=self.\u get\u response(url,params=params)
文件“C:\Python27-32\lib\site packages\pandas\u datareader\base.py”,第139行,在\u get\u响应中
引发RemoteDataError('无法读取URL:{0}'。格式(URL))
pandas\u datareader.\u utils.RemoteDataError:无法读取URL:http://www.google.com/finance/historical?q=SPY&startdate=Dec+29%2C+1989年&截止日期=12月+20%2C+2017年&产出=csv
我不明白。 我不要求在线数据请求。。。而不是“间谍”股票,而是“应用”

这个错误对你意味着什么

非常感谢你的帮助


C.

关于这个问题,我找到的唯一参考和解决方法是:

来自pandas\u datareader.google.daily import GoogleDailyReader
@财产
def url(自我):
返回'http://finance.google.com/finance/historical'
GoogleDailyReader.url=url
执行以下操作:

然后修改文件:zipline/lib/pythonx.x/site-packages/zipline/data/benchmarks.py

将以下两条语句添加到文件中:

import fix_yahoo_finance as yf
yf.pdr_override ()
然后更改以下说明:

data = pd_reader.DataReader (symbol, 'Google' first_date, last_date)
致:


谢谢@serge bouschet!我相信这是默认的url。。。我将尝试捆绑数据。我将努力解决这个问题。C
data = pd_reader.DataReader (symbol, 'Google' first_date, last_date)
data = pd_reader.get_data_yahoo(symbol,first_date, last_date)