You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

复制《Learn Algorithmic Trading》代码遇DataReader属性错误求助

AttributeError When Running Code from Learn Algorithmic Trading (Donadio & Ghosh)

Let's break down the errors you're seeing and fix them step by step:

First, the Root Cause of the AttributeError

Your code has a variable name collision that's breaking the DataReader call:

  • You start by importing data from pandas_datareader (this is a module).
  • Inside the try block, you assign data = pd.read_pickle(...) — this overwrites the data module with a pandas DataFrame object.
  • When the FileNotFoundError triggers and you try to call data.DataReader(), you're actually trying to access a method on a DataFrame, which doesn't exist. That's why you get AttributeError: 'DataFrame' object has no attribute 'DataReader'.

Additional Issues in the Original Code

  1. You're using pd.read_pickle() but never imported pandas with import pandas as pd — this would throw an error once the variable collision is fixed.
  2. There's a typo in your SYMBOLS list: JPNUSD=X should be JPYUSD=X (the correct Yahoo Finance ticker for Japanese Yen vs USD).
  3. Minor typo: TRADING_INSTRAMENT should be TRADING_INSTRUMENT.

Fixed Code

Here's the corrected version that addresses all these issues:

import pandas as pd
from pandas_datareader import data

# Fetch daily data for 4 years, for 7 major currency pairs
TRADING_INSTRUMENT = 'CADUSD=X'
SYMBOLS = ['AUDUSD=X', 'GBPUSD=X', 'CADUSD=X', 'CHFUSD=X', 'EURUSD=X', 'JPYUSD=X', 'NZDUSD=X']
START_DATE = '2014-01-01'
END_DATE = '2018-01-01'

# DataSeries for each currency
symbols_data = {}
for symbol in SYMBOLS:
    SRC_DATA_FILENAME = symbol + '_data.pkl'
    try:
        # Use a different variable name (df) to avoid overwriting the data module
        df = pd.read_pickle(SRC_DATA_FILENAME)
    except FileNotFoundError:
        # Now we're correctly calling DataReader from the pandas_datareader data module
        df = data.DataReader(symbol, 'yahoo', START_DATE, END_DATE)
        df.to_pickle(SRC_DATA_FILENAME)
    symbols_data[symbol] = df

If You Still Have Issues with Yahoo Finance Data

Over time, Yahoo has changed its API, so pandas_datareader's Yahoo backend might not work reliably. If you hit more errors fetching data, switch to the yfinance library (a popular, maintained replacement):

  1. Install it first:
pip install yfinance
  1. Modify the data-fetching part of the code:
import pandas as pd
import yfinance as yf

# ... keep the rest of your variables the same ...

for symbol in SYMBOLS:
    SRC_DATA_FILENAME = symbol + '_data.pkl'
    try:
        df = pd.read_pickle(SRC_DATA_FILENAME)
    except FileNotFoundError:
        # Use yfinance's download method instead
        df = yf.download(symbol, start=START_DATE, end=END_DATE)
        df.to_pickle(SRC_DATA_FILENAME)
    symbols_data[symbol] = df

This should resolve both the AttributeError and any potential data-fetching issues with Yahoo Finance.

内容的提问来源于stack exchange,提问作者Cinji18

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:46:52