简体   繁体   中英

Real-time data from yfinance

I am trying to get real rime data from yfinance with following code. But I just get the same close/high/low/open results. I am starting to think that it is impossible with yfinance. Is there any free and european alternatives?

Thanks a lot in advance!!

#import libraries
import schedule
import time
import alpaca_trade_api as tradeapi
import yfinance as yf
import pandas as pd

# Ask what stocks you want to check
pd = pd.DataFrame()
n = int(input("Enter the size of the list "))
print("\n")
numList = list(num for num in input("Enter the list numbers separated by space ").strip().split())[:n]
print("User List: ", numList)

# Get info for every stock chosen. 
def get_data():
    for ticker in numList:
        ticker_yahoo = yf.Ticker(ticker)
        data = ticker_yahoo.history(period = "1d", interval="1m")
        data = (data.tail(1).iloc[0])
        pd[ticker] = data 
    
    print(pd)
    
get_data()

It is faster to do it with Pandas Datareader by using get_quote_yahoo() method

import pandas_datareader as web

tickers = ["MSFT", "XOM", "KKR"]

current_price = web.get_quote_yahoo(tickers)["regularMarketPrice"]

If you insist on using yfinance , you can use the following code, but beware that it will be MUCH SLOWER This is because 1) instantiating Ticker object and pulling the info property takes time, and 2) it is inconvenient to use Tickers class (as opposed to Ticker ), so you have to use a for a loop.

import yfinance as yf

tickers = ["MSFT", "XOM", "KKR"]
current_price = list()

for i in range(len(tickers)):
    company = yf.Ticker(ticker[i])
    current_price[i] = company.info["regularMarketPrice"]

Obviously, these solutions apply to other types of securities as well. Note though that using this (or any other free) data source for algorithmic trading with real money is impossible at best due to requests limit, high latency, stability issues etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM