Retrieve historical stock valuation data with python - python

I am looking to retrieve historical data like P/E ratio, earnings, book value etc as well as stock price. I'd like to go through a whole bunch of stocks programmatically or to possibly select historical stocks based on valuation rules.
I found a few python packages to download historical stock prices from Yahoo like yahoo-finance and also this guide to downloading historical prices with the browser. But I could not find anything to download historical valuation data.
I am aware of Survivorship Bias problem with the stocks that affects these free services but I can't afford a paid one right now.

Related

Portfolio analytics in python based on transactions - NOT buy/hold backtesting

I'm struggling to find a python tool/library that could track performance of my portfolio based on some csv or list of transactions of buys and sells. It should be able to deal with situations where I buy 100 shares of some stock and sell only 14 shares later, so it should deal with impartial or incomplete transactions. It'd be great if I just could upload a csv with transaction dates and amounts and the library would figure out daily snapshots of performance analytics.
I see plenty of back-testing portfolio management tools that re-balance or project or optimize some ephemeral portfolio based on what would I buy and hold or buy and sell in the future. But it's not what I'm after. I have a list with transaction details and I want to see analytics, tracking, performance analysis on the back of it.
Thanks.

How to Write a Limit Order Strategy with Backtrader?

I am new to Backtrader and I can't figure out how to write the following strategy:
Every morning it places a limit buy order at 80% of Open price. If the order is executed during the day (i.e. Low price < the limit price for that day), then sell the stock at Close.
I am using Yahoo's OHLC daily data.
Can any one show me how to write the Strategy part of the code? I posted a similar question on BT's official forum but couldn't get an answer.
Thanks.

Is there a way to retrieve the hypothetical growth of a stock from Yahoo Finance API?

I'm interested pulling the hypothetical growth, including reinvested dividends, from the yahoo finance api. I wrote the following code that pulls the hypothetical growth in NOT including the dividends:
import yfinance as yf
data = yf.download("MSFT", '2015-01-01', '2021-09-10')['Adj Close']
ROI = (data[-1] - data[0]) / data[0]
print ("ROI:", ROI)
Is there data that factors in reinvested dividends available on their API somewhere, or do I need to go through the painstaking process of figuring out what the paid dividends were for each quarter, and factoring that in one quarter at a time inside a loop?
First, Your phrase "hypothetical growth" is confusing since your using "AdjClose" data. This is not hypothetical.
Second, You're mistaken. Your understanding of what "AdjClose" represents is wrong. This value exists so that it does include changes to the stock such as dividends and stock splits.
For details refer to yahoo's knowledge base
Yahoo's definition for Adj. Close
However, if you're interested in comparing the changes(GROWTH) between Closing prices and "AdjClose", there is an easy math solution.
simply divide all Closing Prices by the first Closing Price in the series. This will give you the relative change and the normalized growth rate.
perform the same method to the "AdjClose" series. Now you'll see that these ratios are larger, which of course include the changes due to dividends and stock splits. These two ratios can now be directly compared.
If you wish to see the total changes in value, these are also simple calculations. But there are several results to consider and compute: a. simple stock price changes, b. accumulated prices + dividends without reinvestment, c. reinvesting accumulated prices + dividends + stock splits.
However providing you with specific code would require that you narrow your question.

Python: How to get stock data for free (for many tickers, like S&P 500)

I want to get stock data in Python for some analysis.
And I want to do analysis on many stocks, not a single one like AAPL, but like S&P 500. Specifically, US stock end-of-day price and other info like adjusted price, sector etc.
How can I do it?
I'm sorry if this question is a bit too simple. I tried to find data but didn't have a good solution.
I tried quandl, but seems only support fetch data by ticker, and so is Yahoo finance (from https://www.quora.com/Using-Python-whats-the-best-way-to-get-stock-data). One possible source is Quantopian, but it only supports analysis in their online notebook. Now I'm trying Alph Advantage
When I see other's project, it's just about a csv. Did I miss something in quandl or Yahoo Finance? Or Do I need to manually fetch every ticker of SP 500 from them?
Thanks!
You can use yahoo finance for real time data like for nearly all quotes:
import pandas as pd
from pandas_datareader import data as wb
ticker='BTC-USD'
ticker=wb.DataReader(ticker,start='2015-1-1',data_source='yahoo')
print(ticker)
Yahoo Finance will give you hihg-low-adj_close etc.
Just look up for the quote name from yahoo finance page and you will get all the data

Stock price and volume derivative at time t

According to Forecasting of Jump Arrivals in Stock Prices: New Attention-based Network Architecture using Limit Order Book Data at page 9, I would be interested in derivating the the price and volume, but I am struggling how to do it. So far I tried nothing, because I just don't know where to start and how to do it.
How can I derive the price and the volume with python?

Categories

Resources