1

这是我的第一篇文章,我希望它会做得很好。

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

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))

我受到https://www.quantinsti.com/blog/introduction-zipline-python/https://www.quantinsti.com/blog/importing-csv-data-zipline-backtesting/的启发。

我收到此错误:

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

我不明白:“ http://www.google.com/finance/historical?q=SPY&startdate=Dec+29%2C+1989&enddate=Dec+20%2C+2017&output=csv ”。我不要求在线数据请求……不是“SPY”股票,而是“APPL”……

这个错误对你意味着什么?

非常感谢你的帮助 !

C。

4

2 回答 2

1

我发现有关此问题的唯一参考和解决方法在这里

from pandas_datareader.google.daily import GoogleDailyReader

@property
def url(self):
    return 'http://finance.google.com/finance/historical'

GoogleDailyReader.url = url
于 2017-12-30T08:16:18.083 回答
0

做:

pip install fix_yahoo_finance

然后修改文件: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)

到:

data = pd_reader.get_data_yahoo(symbol,first_date, last_date)
于 2018-03-21T15:01:57.217 回答