.AttributeError:“数据帧”对象没有属性“get_data_yahoo”。仅当我想通过循环传递多个符号时,我才会收到此错误
这是我的代码,没有错误。但是,当我按照下面的第三行进行更改时
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在这部分代码上。
首先,您不需要将
pdr
声明为您正在使用的模块。现在,问题就变成这样了。您将pdr
声明为全局。现在,当循环执行时,第一次一切正常。但由于您分配了pdr = pdr.get_data_yahoo(..
),pdr
变量现在引用了数据框,而不是pandas_datareader
。因此,在第二次循环执行时,pdr.get_data_yahoo
将引发错误,因为DataFrame
没有这样的方法,您必须删除全局声明并执行此操作 。不重新分配模块变量,例如这样做
应该没问题。
The problem lies in this part of the code.
Firstly, you don't need the
global
declaration forpdr
as its a module you are using. Now, the issue becomes this. You declaredpdr
as global. Now when the loop executes, first time all goes ok. But since you assignpdr = pdr.get_data_yahoo(..
, thepdr
variable now references a data frame and not thepandas_datareader
. So in the 2nd loop execution,pdr.get_data_yahoo
will raise an error, asDataFrame
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.
All should be fine.