Skip to content

Commit

Permalink
Made fa/pt more accurate (#4503)
Browse files Browse the repository at this point in the history
* Got things working

* Got things working

* Got things working
  • Loading branch information
colin99d authored Mar 17, 2023
1 parent 3aec94c commit 09f53dd
Show file tree
Hide file tree
Showing 4 changed files with 7,253 additions and 2,878 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import os
import textwrap
from datetime import datetime, timedelta
from functools import partial
from typing import Optional, Union

import pandas as pd
from pandas.core.frame import DataFrame

from openbb_terminal import OpenBBFigure, theme
Expand All @@ -17,7 +19,10 @@
print_rich_table,
)
from openbb_terminal.rich_config import console
from openbb_terminal.stocks.fundamental_analysis import business_insider_model
from openbb_terminal.stocks.fundamental_analysis import (
business_insider_model,
yahoo_finance_model,
)
from openbb_terminal.stocks.stocks_helper import load

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -61,6 +66,7 @@ def display_management(symbol: str, export: str = "", sheet_name: Optional[str]
logger.error("Data not available")


# pylint: disable=R0913
@log_start_end(log=logger)
def price_target_from_analysts(
symbol: str,
Expand All @@ -71,6 +77,7 @@ def price_target_from_analysts(
export: str = "",
sheet_name: Optional[str] = None,
external_axes: bool = False,
adjust_for_splits: bool = True,
) -> Union[OpenBBFigure, None]:
"""Display analysts' price targets for a given stock. [Source: Business Insider]
Expand All @@ -92,13 +99,22 @@ def price_target_from_analysts(
Export dataframe data to csv,json,xlsx file
external_axes: bool, optional
Whether to return the figure object or not, by default False
adjust_for_splits: bool
Whether to adjust analyst price targets for stock splits, by default True
Examples
--------
>>> from openbb_terminal.sdk import openbb
>>> openbb.stocks.fa.pt_chart(symbol="AAPL")
"""

def adjust_splits(row, splits: pd.DataFrame):
original_value = row["Price Target"]
for index, sub_row in splits.iterrows():
if row.name < index:
original_value = original_value / sub_row["Stock Splits"]
return round(original_value, 2)

if start_date is None:
start_date = (datetime.now() - timedelta(days=1100)).strftime("%Y-%m-%d")

Expand All @@ -108,6 +124,11 @@ def price_target_from_analysts(
df_analyst_data = business_insider_model.get_price_target_from_analysts(symbol)
if df_analyst_data.empty:
return console.print("[red]Could not get data for ticker.[/red]\n")
if adjust_for_splits:
df_splits = yahoo_finance_model.get_splits(symbol)
df_splits.index = df_splits.index.tz_convert(None)
adjusted_splits = partial(adjust_splits, splits=df_splits)
df_analyst_data["Price Target"] = df_analyst_data.apply(adjusted_splits, axis=1)

fig = OpenBBFigure(yaxis_title="Share Price").set_title(
f"{symbol} (Time Series) and Price Target"
Expand Down
Loading

0 comments on commit 09f53dd

Please sign in to comment.