.AttributeError:“数据帧”对象没有属性“get_data_yahoo”。仅当我想通过循环传递多个符号时,我才会收到此错误

发布于 2025-01-11 12:44:49 字数 2523 浏览 1 评论 0原文

这是我的代码,没有错误。但是,当我按照下面的第三行进行更改时

symbol_list = ["INFY.NS", “TATAMOTORS.NS”] 

,我收到错误,

File "e:/PYTHON STOCK/test.py", line 19, in symbol_back_test
    pdr = pdr.get_data_yahoo(symbol, period="2y", interval="1d")
  File "C:\Users\harek\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\generic.py", line 5583, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'get_data_yahoo'

我无法理解发生了什么。为什么我不能在循环中传递多个符号。任何人都可以帮忙吗? 谢谢。

import copy

import pandas as pd
import talib
import yfinance as yf

pd.set_option("display.max_rows", None)
pd.set_option("display.max_columns", None)
pd.set_option("display.width", None)
from pandas_datareader import data as pdr

yf.pdr_override()


def symbol_back_test(symbol):
    global pdr
    pdr = pdr.get_data_yahoo(symbol, period="2y", interval="1d")

    pdr["MA_10"] = talib.MA(pdr["Close"], timeperiod=10)
    pdr["MA_50"] = talib.MA(pdr["Close"], timeperiod=50)
    pdr["RSI_14"] = talib.RSI(pdr["Close"], timeperiod=14)

    position = None
    symbol_trades = []
    trade = {"Symbol": None, "Buy/Sell": None, "Entry": None, "Entry Date": None, "Exit": None, "Exit Date": None}

    for i in pdr.index[49:]:

        if pdr["MA_10"][i] > pdr["MA_50"][i] and pdr["RSI_14"][i] > 50 and position != "Buy":
            if trade["Symbol"] is not None:
                trade["Exit"] = pdr["Close"][i]
                trade["Exit Date"] = i
                symbol_trades.append(copy.deepcopy(trade))
            if position is not None:
                trade["Symbol"] = symbol
                trade["Buy/Sell"] = "Buy"
                trade["Entry"] = pdr["Close"][i]
                trade["Entry Date"] = i

            position = "Buy"
        if pdr["MA_10"][i] < pdr["MA_50"][i] and pdr["RSI_14"][i] < 50 and position != "Sell":
            if trade["Symbol"] is not None:
                trade["Exit"] = pdr["Close"][i]
                trade["Exit Date"] = i
                symbol_trades.append(copy.deepcopy(trade))
            if position is not None:
                trade["Symbol"] = symbol
                trade["Buy/Sell"] = "Sell"
                trade["Entry"] = pdr["Close"][i]
                trade["Entry Date"] = i

            print("Sell")
            position = "Sell"
    return symbol_trades


symbol_list = ["INFY.NS"]
for symbol in symbol_list:
    print(symbol_back_test(symbol))

This is my code with no error. But when I make the change as

symbol_list = ["INFY.NS", “TATAMOTORS.NS”] 

in third line from bellow then I got the error

File "e:/PYTHON STOCK/test.py", line 19, in symbol_back_test
    pdr = pdr.get_data_yahoo(symbol, period="2y", interval="1d")
  File "C:\Users\harek\AppData\Local\Programs\Python\Python38\lib\site-packages\pandas\core\generic.py", line 5583, in __getattr__
    return object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'get_data_yahoo'

I cant understand what is going on. Why I cant pass more than one symbol through the loop. Can anybody please help.
Thanks.

import copy

import pandas as pd
import talib
import yfinance as yf

pd.set_option("display.max_rows", None)
pd.set_option("display.max_columns", None)
pd.set_option("display.width", None)
from pandas_datareader import data as pdr

yf.pdr_override()


def symbol_back_test(symbol):
    global pdr
    pdr = pdr.get_data_yahoo(symbol, period="2y", interval="1d")

    pdr["MA_10"] = talib.MA(pdr["Close"], timeperiod=10)
    pdr["MA_50"] = talib.MA(pdr["Close"], timeperiod=50)
    pdr["RSI_14"] = talib.RSI(pdr["Close"], timeperiod=14)

    position = None
    symbol_trades = []
    trade = {"Symbol": None, "Buy/Sell": None, "Entry": None, "Entry Date": None, "Exit": None, "Exit Date": None}

    for i in pdr.index[49:]:

        if pdr["MA_10"][i] > pdr["MA_50"][i] and pdr["RSI_14"][i] > 50 and position != "Buy":
            if trade["Symbol"] is not None:
                trade["Exit"] = pdr["Close"][i]
                trade["Exit Date"] = i
                symbol_trades.append(copy.deepcopy(trade))
            if position is not None:
                trade["Symbol"] = symbol
                trade["Buy/Sell"] = "Buy"
                trade["Entry"] = pdr["Close"][i]
                trade["Entry Date"] = i

            position = "Buy"
        if pdr["MA_10"][i] < pdr["MA_50"][i] and pdr["RSI_14"][i] < 50 and position != "Sell":
            if trade["Symbol"] is not None:
                trade["Exit"] = pdr["Close"][i]
                trade["Exit Date"] = i
                symbol_trades.append(copy.deepcopy(trade))
            if position is not None:
                trade["Symbol"] = symbol
                trade["Buy/Sell"] = "Sell"
                trade["Entry"] = pdr["Close"][i]
                trade["Entry Date"] = i

            print("Sell")
            position = "Sell"
    return symbol_trades


symbol_list = ["INFY.NS"]
for symbol in symbol_list:
    print(symbol_back_test(symbol))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你是孤单的心事 2025-01-18 12:44:49

问题出在这部分代码上。

def symbol_back_test(symbol):
    global pdr <-- Why this is needed ?
    pdr = pdr.get_data_yahoo(symbol, period="2y", interval="1d")

首先,您不需要将 pdr 声明为您正在使用的模块。现在,问题就变成这样了。您将 pdr 声明为全局。现在,当循环执行时,第一次一切正常。但由于您分配了 pdr = pdr.get_data_yahoo(..),pdr 变量现在引用了数据框,而不是 pandas_datareader。因此,在第二次循环执行时,pdr.get_data_yahoo 将引发错误,因为 DataFrame 没有这样的方法,

您必须删除全局声明并执行此操作 。不重新分配模块变量,例如这样做

from pandas_datareader import data as p_data_reader
def symbol_back_test(symbol):
   
    pdr = p_data_reader.get_data_yahoo(symbol, period="2y", interval="1d")
    ...

应该没问题。

The problem lies in this part of the code.

def symbol_back_test(symbol):
    global pdr <-- Why this is needed ?
    pdr = pdr.get_data_yahoo(symbol, period="2y", interval="1d")

Firstly, you don't need the global declaration for pdr as its a module you are using. Now, the issue becomes this. You declared pdr as global. Now when the loop executes, first time all goes ok. But since you assign pdr = pdr.get_data_yahoo(.., the pdr variable now references a data frame and not the pandas_datareader. So in the 2nd loop execution, pdr.get_data_yahoo will raise an error, as DataFrame does not have a method like that.

You have to remove the global declaration and do not re-assign the module variable. For eg do like this.

from pandas_datareader import data as p_data_reader
def symbol_back_test(symbol):
   
    pdr = p_data_reader.get_data_yahoo(symbol, period="2y", interval="1d")
    ...

All should be fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文