嘗試繪制圖表時出現此錯誤。我正在嘗試使用提供的值繪制圖表。我正在使用 yahoo Finance 并想使用 python 繪制阻力線。
for index in range(len(pivots)):
print(str(pivots[index]) ": " str(dates[index]))
plt.plot_date([dates[index],dates[index] timeD,
pivots[index],pivots[index]], linestyle="-", linewidth=2, marker="none")
這是整個代碼
import pandas
import yfinance as yf
import datetime as dt
import pandas as pd
from pandas_datareader import data as pdr
import matplotlib.pyplot as plt
yf.pdr_override()
start= dt.datetime(2019,1,1)
now= dt.datetime.now()
stock = input("Enter the stock symbol: ")
while stock !="quit":
df=pdr.get_data_yahoo(stock, start, now)
df["Low"].plot(Label="low")
pivots=[]
dates=[]
counter=0
lastPivot=0
Range=[0,0,0,0,0,0,0,0,0,0]
dateRange=[0,0,0,0,0,0,0,0,0,0]
for i in df.index:
currentMin=min(Range, default=0)
value=round(df["Low"][i],2)
Range=Range[1:9]
Range.append(value)
dateRange=dateRange[1:9]
dateRange.append(i)
if currentMin==min(Range, default=0):
counter-=1
else:
counter=0
if counter==-5:
lastPivot=currentMin
dateloc=Range.index(lastPivot)
lastDate=dateRange[dateloc]
pivots.append(lastPivot)
dates.append(lastDate)
print()
# print(str(pivots))
# print(str(dates))
timeD=dt.timedelta(days=30)
for index in range(len(pivots)):
print(str(pivots[index]) ": " str(dates[index]))
plt.plot_date([dates[index],dates[index] timeD,
pivots[index],pivots[index]], linestyle="-", linewidth=2, marker="none")
plt.show()
stock= input ("Enter the stock symbol : ")
我收到的錯誤是
回溯(最近一次呼叫最后一次):檔案“Low.py”,第 57 行,在
plt.plot_date([dates[index],dates[index] timeD,
TypeError: unsupported operand type(s) for : 'int' and 'datetime.timedelta'
謝謝
uj5u.com熱心網友回復:
問題出在以下運算式中plt.plot_date:
dates[index] timeD
如果您列印日期,您將獲得以下輸出:
[0,
Timestamp('2019-02-08 00:00:00'),
Timestamp('2019-03-08 00:00:00'),
...
...
...
Timestamp('2021-09-20 00:00:00'),
Timestamp('2021-10-04 00:00:00')]
如您所見,該串列包含 Int 和 Timestamp 值。因此,當您添加timeD女巫是 timedelta 時,此操作是非法的。
我不明白您是如何構造的,dates但您需要查看該部分中的邏輯并檢查為什么會獲得 Int 和 Timestamp 值。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/371885.html
標籤:Python matplotlib 阴谋 金融 股票
