o
    sgW                     @   s.  d Z ddlZddlZejd ddlZddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZ G d	d
 d
Zedkre ZddgZe ZeD ]CZz(ejeddddZeedZede ZejrleZnejeddZW qJ ey Z ze de  W Y dZ[qJdZ[ww e!d dS dS )z6Class to store all stock performance related functions    N.)datetime)CompanyInfo)OpenBBStockData)DynamoDB)
parsed_urlc                   @   sX   e Zd ZdZ						ddededed	ed
ef
ddZedddZde	fddZ
dS )StockPerformancez4Class to fetch the stock price over some time periodNyfinance   Ftickercompany_urlprovider
start_datedurationc              
   C   s   t |d}|s|std|s|r| j|d}z
|j|||d}W n ty< }	 ztd|	  td|	 |	d}	~	ww |du rH| |}
|
S |S )a  
        Get stock price information of a company
        ticker: str: The ticker of the stock
        company_url: str: The company url
        provider: str: The provider of the stock data (default: yfinance)
        start_date: str: The start date of the stock data (default: 5 years from today)
        duration: int: The duration of the stock data (default: 5 years)
        rebase: bool: Rebase the stock prices (default: False)
        )r   z5Ticker or company url is required to fetch stock data)r   )r   r   r   Error fetching stock prices: NT)r   
ValueErrorget_ticker_from_company_urlget_stock_dataprintrebase_stock_prices)selfr   r   r   r   r   rebaseopenbb_stock_finder
stock_dataerebased_stock_data r   A/var/www/html/XCapMarket/services/stock_info/stock_performance.pyget_stock_info   s$   


zStockPerformance.get_stock_infoc                 C   s   t | }|j} | r9t }||j| }|durtdi |}nt|  d tdt	
 d|  t|  d|jr?|jS t|  d tdt	
 d|  t|  d)z3Get the stock ticker from the company url in our DBNz not found in the database.z!%s: %s not found in the database.z%Y/%m/%d %H:%M:%Sz- doesn't have a stock ticker in the database.z3%s: %s doesn't have a stock ticker in the database.r   )r   urlr   get_itemcompany_info_tabler   r   loggingerrorr   nowstrftimer   stock_ticker)r   
url_parseddbcompany_info_datacompany_infor   r   r   r   =   s2   z,StockPerformance.get_ticker_from_company_urlr   c                    sF   |du r
t d dS |d d   fdd|d D }|d|iB }|S )z;Rebase the stock prices to the first date in the stock dataNzNo stock data to rebaseclose_pricer   c                    s   g | ]}|  qS r   r   ).0pricerebase_stock_pricer   r   
<listcomp>h   s    z8StockPerformance.rebase_stock_prices.<locals>.<listcomp>rebased_close_price)r   )r   r   rebased_stock_pricesr   r.   r   r   a   s   
z$StockPerformance.rebase_stock_prices)NNr	   Nr
   F)N)__name__
__module____qualname____doc__strintr   staticmethodr   dictr   r   r   r   r   r      s,    
*#r   __main__zBTC-USDSPYTr	   r
   )r   r   r   r   date_outer)howr   zVC:\Users\sagar\X Cap Market\X Cap Market - Customers\Formidium\excel\stock_prices.xlsx)"r6   syspandaspdpathappendr"   r   2services.company_profile.data_classes.company_infor   %services.stock_info.openbb_stock_datar   utils.dynamo_dbr   utils.url_parserr   r   r3   stock_price_fetchertickers	DataFramestock_dfr   r   r   	set_indexstock_data_df
add_suffixemptyjoinr   err   to_excelr   r   r   r   <module>   sD    `	