mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-03 07:51:49 +00:00
* quick econ fix * black * keys and feature flags * terminal name 👀 * some more replacements * some more replacements * edit pyproject * gst -> openbb * add example portfolios back to git * Update api from gst * sorry. skipping some tests * another round of names * another round of test edits * Missed some .gst refs and update timezone * water mark stuff * Fixing Names in terminal.spec and name of GTFF_DEFAULTS to OBBFF_DEFAULTS * fix more GST to OpenBB Terminal * Logging : merge conflicts with main * Revert wrong files Co-authored-by: Andrew <andrew.kenreich@gmail.com> Co-authored-by: DidierRLopes <dro.lopes@campus.fct.unl.pt> Co-authored-by: Chavithra PARANA <chavithra@gmail.com>
116 lines
3.0 KiB
Python
116 lines
3.0 KiB
Python
"""Cramer Model"""
|
|
__docformat__ = "numpy"
|
|
|
|
import logging
|
|
import re
|
|
|
|
import pandas as pd
|
|
import requests
|
|
import numpy as np
|
|
import yfinance as yf
|
|
from bs4 import BeautifulSoup
|
|
|
|
from openbb_terminal.decorators import log_start_end
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@log_start_end(log=logger)
|
|
def get_cramer_daily(inverse: bool = True) -> pd.DataFrame:
|
|
"""Scrape the daily recommendations of Jim Cramer
|
|
|
|
Parameters
|
|
----------
|
|
inverse: bool
|
|
Whether to include inverse
|
|
|
|
Returns
|
|
-------
|
|
pd.DataFrame
|
|
Datafreme of daily Cramer recommendations
|
|
"""
|
|
|
|
link = (
|
|
"https://madmoney.thestreet.com/screener/index.cfm?showview=stocks&showrows=500"
|
|
)
|
|
r = requests.post(
|
|
link,
|
|
headers={
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
|
|
"AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.3 Safari/605.1.15",
|
|
},
|
|
)
|
|
if r.status_code != 200:
|
|
return pd.DataFrame()
|
|
soup = BeautifulSoup(r.text, "html.parser")
|
|
table = soup.find_all("table")[0]
|
|
trs = table.find_all("tr")
|
|
recs = {
|
|
"1": "Sell",
|
|
"2": "Sell",
|
|
"3": "Hold",
|
|
"4": "Buy",
|
|
"5": "Buy",
|
|
}
|
|
|
|
rec = []
|
|
|
|
for tr in trs[1:]:
|
|
rec.append(recs[tr.find_all("td")[3].find("img")["src"][-5]])
|
|
|
|
df = pd.read_html(r.text)[0]
|
|
df["Symbol"] = df.Company.apply(lambda x: re.findall(r"[\w]+", x)[-1])
|
|
last_price = []
|
|
for ticker in df.Symbol:
|
|
last_price.append(
|
|
round(
|
|
yf.download(ticker, period="1d", interval="1h", progress=False)[
|
|
"Close"
|
|
][-1],
|
|
2,
|
|
)
|
|
)
|
|
df["LastPrice"] = last_price
|
|
df["Price"] = df.Price.apply(lambda x: float(x.strip("$")))
|
|
df = df.drop(columns=["Segment", "Call", "Portfolio"])
|
|
df["Change (%)"] = 100 * np.round((df["LastPrice"] - df["Price"]) / df.LastPrice, 4)
|
|
df["Recommendation"] = rec
|
|
df["Company"] = df.apply(lambda x: x.Company.replace(f"({x.Symbol})", ""), axis=1)
|
|
cols = [
|
|
"Date",
|
|
"Company",
|
|
"Symbol",
|
|
"Price",
|
|
"LastPrice",
|
|
"Change (%)",
|
|
"Recommendation",
|
|
]
|
|
if inverse:
|
|
df["InverseCramer"] = df["Recommendation"].apply(
|
|
lambda x: ["Buy", "Sell"][x == "Buy"]
|
|
)
|
|
cols.append("InverseCramer")
|
|
return df[cols]
|
|
|
|
|
|
@log_start_end(log=logger)
|
|
def get_cramer_ticker(ticker: str) -> pd.DataFrame:
|
|
"""Get cramer recommendations from beginning of year for given ticker
|
|
|
|
Parameters
|
|
----------
|
|
ticker: str
|
|
Ticker to get recommendations for
|
|
|
|
Returns
|
|
-------
|
|
pd.DataFrame:
|
|
Dataframe with dates and recommendations
|
|
"""
|
|
|
|
link = "https://raw.githubusercontent.com/jmaslek/InverseCramer/main/AllRecommendations.csv"
|
|
df = pd.read_csv(link, index_col=0)
|
|
return df[df.Symbol == ticker].reset_index(drop=True)
|