o
    sg                     @   st   d Z ddlZddlmZmZ ddlmZ ddlmZ dgZG dd dZ	e
d	kr8e	dd
Zeejddd dS dS )z&Get stock data from the OpenBB library    N)datetime	timedelta)obb)OpenBBErroryfinancec                   @   s>   e Zd ZdZdddZddefd	d
ZeddefddZdS )OpenBBStockDataz&Get Stock data from the OpenBB library1dNr   c                 C   sX   t | _|| jvrtd| dd| j || _|| _|s'| jdd| _dS || _dS )au  
        Initialize the class with the interval and start date
        interval: str: The interval of the stock data (default: 1d, options: 1m, 1h, 1d, 1W, 1M)
            minute, hour, day, week, month
        start_date: str: The start date of the stock data (default: 5 years from today)
        provider: str: The provider of the stock data (default: yfinance)
        zInvalid provider: z, options: z,    )durationN)	PROVIDERSprovider_list
ValueErrorjoinproviderinterval_get_start_date
start_date)selfr   r   r    r   A/var/www/html/XCapMarket/services/stock_info/openbb_stock_data.py__init__   s   	

zOpenBBStockData.__init__r	   r
   c           	   
   C   s  z7|st d| || _|s| j}tjjj|| j| j| jd	 }dd |j
D }|d  }||d}|W S  t y` } ztdt d|| j t d	| d
| j d| |d}~w ty } ztdt d|| j t d	| d
| j d| |d}~ww )a  
        Get stock data from the OpenBB library
        ticker: str: The ticker of the stock
        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)
        z&Ticker is required to fetch stock data)symbolr   r   r   c                 S   s   g | ]}| d qS )%Y-%m-%d)strftime).0dater   r   r   
<listcomp>>   s    z2OpenBBStockData.get_stock_data.<locals>.<listcomp>close)r   close_pricez8%s: Error fetching stock prices for %s from %s. 
Error: z%Y/%m/%d %H:%M:%SzError fetching z stock prices from z: N)r   r   r   r   equityprice
historicalr   r   to_dfindextolistloggingerrorr   nowr   r   )	r   tickerr   r
   
stock_datadatesclose_prices
stock_dicter   r   r   get_stock_data'   sZ   
zOpenBBStockData.get_stock_datac                 C   sR   t  }|td|  d d }| dv r"| d }|t|d8 }|d}|S )z
        Get the start date of the stock data
        duration: int: The duration of the stock data in years (default: 5 years)
        im     )days)r	         r   )r   r'   r   weekdayr   )r
   todayr   start_date_offsetr   r   r   r   [   s   
zOpenBBStockData._get_start_date)r   Nr   )NNr	   )r	   )	__name__
__module____qualname____doc__r   intr.   staticmethodr   r   r   r   r   r      s    
4r   __main__)r   r/   U)r
   r(   )r9   r%   r   r   openbbr   $openbb_core.app.model.abstract.errorr   r   r   r6   openbb_stockprintr.   r   r   r   r   <module>   s    a