簡體   English   中英

13號api之后yfinance不會給出任何收益數據

[英]after 13th api call yfinance will not give any earnings data

我有以下腳本來使用雅虎財經信息填充我的數據庫:

from multiprocessing import Pool

import json, time, yfinance

import django
django.setup()

from dividends_info.functions.stock_info import save_stock_info_data
from dividends_info.models import StockInfo

with open('tickers/nyse_tickers.json') as tickers_file:
    TICKERS = json.load(tickers_file)

TICKERS = TICKERS[0:100]

# https://www.digitalocean.com/community/tutorials/python-multiprocessing-example

def update_a_stock(ticker):
    stock, created = StockInfo.objects.get_or_create(ticker=ticker)
    yahoo_stock_obj = yfinance.Ticker(ticker.upper())
    earnings_history = yahoo_stock_obj.earnings_history
    save_stock_info_data(yahoo_stock_obj, ticker, stock, earnings_history)
    if not stock.earnings:
        print(f"No earnings for {ticker}")



def pool_handler():
    start = time.time()
    p = Pool(2)
    p.map(update_a_stock, TICKERS)

    with open("time_taken_to_populate.txt", "w") as time_file:
        time_taken = round((time.time() - start), 2)
        time_file.write(f"Time taken = {time_taken:.10f}")


if __name__ == '__main__':
    pool_handler()

這里重要的是

yahoo_stock_obj = yfinance.Ticker(ticker.upper())
earnings_history = yahoo_stock_obj.earnings_history

而我的save_stock_info_data打印出收益歷史的類型:

ticker in save stock info func: ABEV
<class 'pandas.core.frame.DataFrame'>
ticker in save stock info func: A
<class 'pandas.core.frame.DataFrame'>
Saved new information for stock ABEV
Saved new information for stock A
ticker in save stock info func: ABG
<class 'pandas.core.frame.DataFrame'>
ticker in save stock info func: AA
<class 'pandas.core.frame.DataFrame'>
Saved new information for stock ABG
Saved new information for stock AA
ticker in save stock info func: ABM
<class 'pandas.core.frame.DataFrame'>

如您所見,腳本開始時運行良好,對於前 13 次調用,它將保存收入數據:

python3 count_db_items.py 
21 many stocks
name: 18, summary: 18, dividends: 16, earnings: 13

保存前 13 個收入后,api 不再提供 earnings_history:

ticker in save stock info func: AB
<class 'NoneType'>
Could not find data for ACCO.
ticker in save stock info func: ACCO
<class 'NoneType'>
Saved new information for stock AB
No earnings for AB
Saved new information for stock ACCO
No earnings for ACCO
Could not find data for ABB.
ticker in save stock info func: ABB
<class 'NoneType'>

從 yfinance 的 yahoo_stock_object 收集的每個 earnings_history object 都是“無”。 每次在保存前 13 個腳本后運行腳本時都會發生這種情況。 包括股息在內的所有其他數據均可用。 我認為股息是作為數組發送的,而 earnings_history 是作為 pandas dataframe 發送的。

當我為所有代碼運行腳本時,我有 1800 只股票、1700 股股息和只有 25 個收益結果。

同步運行腳本無濟於事,實際上更糟糕的是根本沒有節省任何收入:

import json, os, sys, time, yfinance

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dividends_project.settings")

sys.path.append('../..')

import django
django.setup()

from dividends_info.functions.stock_info import save_stock_info_data
from dividends_info.models import StockInfo

# https://stackoverflow.com/questions/59159991/modulenotfounderror-no-module-named-foo-how-can-i-import-a-model-into-a-djang

# https://pythonspeed.com/articles/python-multiprocessing/

# https://github.com/pytorch/pytorch/issues/3492

f = open('tickers/nyse_tickers.json')
TICKERS = json.load(f)
f.close()

print(len(TICKERS))

TICKERS = TICKERS[:50]

def update_a_stock(ticker):
    stock, created = StockInfo.objects.get_or_create(ticker=ticker)
    yahoo_stock_obj = yfinance.Ticker(ticker.upper())
    earnings_history = yahoo_stock_obj.earnings_history
    # stock, yahoo_obj = save_stock_info_data(ticker, stock)
    save_stock_info_data(yahoo_stock_obj, ticker, stock, earnings_history)
    if not stock.earnings:
        print("earnings didn't save for this stock...the earnings are:")
        print(earnings_history)


def run():
    start = time.time()
    for ticker in TICKERS:
        update_a_stock(ticker)

    with open("time_taken_to_populate.txt", "w") as time_file:
        time_taken = round((time.time() - start), 2)
        time_file.write(f"Time taken = {time_taken:.10f}")


if __name__ == "__main__":
    run()

沒有收入數據,我的網站幾乎毫無用處。 這似乎不是速率限制問題,因為運行腳本的同步版本也不起作用。

如果我訪問一只股票,它有收益:

import sys

ticker = sys.argv[1]

def update_a_stock(ticker):
    stock, created = StockInfo.objects.get_or_create(ticker=ticker)
    yahoo_stock_obj = yfinance.Ticker(ticker.upper())
    earnings_history = yahoo_stock_obj.earnings_history
    earnings = gather_earnings_object

在 ipdb 中:

ipdb> earnings
[{'date': datetime.date(2022, 7, 29), 'expected': 3.31, 'actual': 3.37, 'surprise': '+1.69'}, {'date': datetime.date(2022, 4, 29), 'expected': 3.14, 'actual': 3.16, 'surprise': '+0.57'}, {'date': datetime.date(2022, 2, 2), 'expected': 3.29, 'actual': 3.31, 'surprise': '+0.73'}, {'date': datetime.date(2021, 10, 29), 'expected': 3.22, 'actual': 3.33, 'surprise': '+3.32'}, ....

我將不得不為單個 api 調用手動運行我的腳本 10000 次,以使用所有美國股票填充數據。

如何運行調用 yfinance 的異步腳本來填充我的數據庫?

給你 go。

import pandas_datareader as web
import pandas as pd
 
df = web.DataReader('AAPL', data_source='yahoo', start='2011-01-01', end='2021-01-12')
df.head()

import yfinance as yf
aapl = yf.Ticker("AAPL")
aapl
 


# show earnings
aapl.earnings
aapl.quarterly_earnings
 

結果:

              Revenue     Earnings
Quarter                           
4Q2021   123945000000  34630000000
1Q2022    97278000000  25010000000
2Q2022    82959000000  19442000000
3Q2022    90146000000  20721000000

pandas-datareader 庫中提供了各種可用的東西。

https://pandas-datareader.readthedocs.io/en/latest/readers/yahoo.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM