Skip to content

Instantly share code, notes, and snippets.

@shashankvemuri
Last active September 7, 2022 15:19
Show Gist options
  • Save shashankvemuri/66de000153d5a9d3dea15aef7c86043e to your computer and use it in GitHub Desktop.
Save shashankvemuri/66de000153d5a9d3dea15aef7c86043e to your computer and use it in GitHub Desktop.
The beginning of the screener
# Index Returns
index_df = pdr.get_data_yahoo(index_name, start_date, end_date)
index_df['Percent Change'] = index_df['Adj Close'].pct_change()
index_return = (index_df['Percent Change'] + 1).cumprod()[-1]
# Find top 30% performing stocks (relative to the S&P 500)
for ticker in tickers:
# Download historical data as CSV for each stock (makes the process faster)
df = pdr.get_data_yahoo(ticker, start_date, end_date)
df.to_csv(f'{ticker}.csv')
# Calculating returns relative to the market (returns multiple)
df['Percent Change'] = df['Adj Close'].pct_change()
stock_return = (df['Percent Change'] + 1).cumprod()[-1]
returns_multiple = round((stock_return / index_return), 2)
returns_multiples.extend([returns_multiple])
print (f'Ticker: {ticker}; Returns Multiple against S&P 500: {returns_multiple}\n')
time.sleep(1)
# Creating dataframe of only top 30%
rs_df = pd.DataFrame(list(zip(tickers, returns_multiples)), columns=['Ticker', 'Returns_multiple'])
rs_df['RS_Rating'] = rs_df.Returns_multiple.rank(pct=True) * 100
rs_df = rs_df[rs_df.RS_Rating >= rs_df.RS_Rating.quantile(.70)]
@liav93eliyahu
Copy link

latest pip version breaks the code.
Solution is found here:
ranaroussi/yfinance#937

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment