
    'is*                     *   d dl mZmZmZmZ d dlZd dlmZmZ d dlZd dl	Z	ddl
mZmZmZmZmZ d dlZddlmZ ddlmZ  G d d	ej,                        Z G d
 de      Z G d dej2                        Z G d de      Z G d dej2                        Zy)    )absolute_importdivisionprint_functionunicode_literalsN)datedatetime   )urlopenurlquoteProxyHandlerbuild_openerinstall_opener)feed)date2numc                   0     e Zd ZdZdZdZ fdZd Z xZS )YahooFinanceCSVDataa  
    Parses pre-downloaded Yahoo CSV Data Feeds (or locally generated if they
    comply to the Yahoo format)

    Specific parameters:

      - ``dataname``: The filename to parse or a file-like object

      - ``reverse`` (default: ``False``)

        It is assumed that locally stored files have already been reversed
        during the download process

      - ``adjclose`` (default: ``True``)

        Whether to use the dividend/split adjusted close and adjust all
        values according to it.

      - ``adjvolume`` (default: ``True``)

        Do also adjust ``volume`` if ``adjclose`` is also ``True``

      - ``round`` (default: ``True``)

        Whether to round the values to a specific number of decimals after
        having adjusted the close

      - ``roundvolume`` (default: ``0``)

        Round the resulting volume to the given number of decimals after having
        adjusted it

      - ``decimals`` (default: ``2``)

        Number of decimals to round to

      - ``swapcloses`` (default: ``False``)

        [2018-11-16] It would seem that the order of *close* and *adjusted
        close* is now fixed. The parameter is retained, in case the need to
        swap the columns again arose.

    )adjclose)reverseF)r   T)	adjvolumeT)roundT)decimalsr	   )roundvolumeF)
swapclosesFc                 v   t         t        |           | j                  j                  sy t        j                         }| j                  D ]  }|j                  |        t        j                  d       }|j                  |       |j                  d       | j                  j                          || _        y )Nnewliner   )superr   startparamsr   collectionsdequef
appendleftioStringIO
writelinesseekclose)selfdqliner#   	__class__s       T/var/www/app/trading-bot/venv/lib/python3.12/site-packages/backtrader/feeds/yahoo.pyr   zYahooFinanceCSVData.start]   s    !4.0{{""  FF 	 DMM$	  KK%	R	q	    c           	         	 d}|dd  D ]  }|dk(  s	d}| j                         }|s y n |sn-t        j                  d      }|t        |         }t	        t        |dd       t        |dd       t        |d	d
             }t        t        j                  || j                  j                              }|| j                  j                  d<   t        |t        |               }t        |t        |               }	t        |t        |               }
t        |t        |               }d| j                  j                  d<   t        |t        |               }	 t        |t        |               }| j                  j                  r||}}||z  }| j                  j                   r,||z  }|	|z  }	|
|z  }
|}| j                  j"                  r||z  }| j                  j$                  rF| j                  j&                  }t%        ||      }t%        |	|      }	t%        |
|      }
t%        ||      }t%        || j                  j(                        }|| j                  j*                  d<   |	| j                  j,                  d<   |
| j                  j.                  d<   || j                  j0                  d<   || j                  j2                  d<   || j                  j                   d<   y#  d}Y {xY w)NTF   nullr               
   g        )_getnextline	itertoolscountnextr   intr   r   combinep
sessionendlinesfloatopeninterestr   r    r   r   r   r   r   openhighlowr)   volume)r*   
linetokensnullseentokidttxtdtdtnumohlcadjustedclosev	adjfactorr   s                   r.   	_loadlinezYahooFinanceCSVData._loadlinen   s   H!!"~ &=#H!%!2!2!4J%$    OOA47##eAaj/3uQqz?Ca4DE))"dff.?.?@A!&

A*T!W%&*T!W%&*T!W%&*T!W%&%(

" ja12	ja)*A 66,a}A%	 ;;NANANAAvvY66<<vvHa"Aa"Aa"Aa"A!TVV''(





q

 

!!.

AE	As   K' 'K.)	__name__
__module____qualname____doc__r@   r    r   rU   __classcell__r-   s   @r.   r   r   %   s"    *V EF"Dr/   r   c                       e Zd ZdZdZy)YahooLegacyCSVz~
    This is intended to load files which were downloaded before Yahoo
    discontinued the original service in May-2017

    ))version N)rV   rW   rX   rY   r     r/   r.   r]   r]      s    
Fr/   r]   c                       e Zd ZeZy)YahooFinanceCSVN)rV   rW   rX   r   DataClsr`   r/   r.   rb   rb      s    !Gr/   rb   c                   <     e Zd ZdZdi fdddddfZd Z fd	Z xZS )
YahooFinanceDataa  
    Executes a direct download of data from Yahoo servers for the given time
    range.

    Specific parameters (or specific meaning):

      - ``dataname``

        The ticker to download ('YHOO' for Yahoo own stock quotes)

      - ``proxies``

        A dict indicating which proxy to go through for the download as in
        {'http': 'http://myproxy.com'} or {'http': 'http://127.0.0.1:8080'}

      - ``period``

        The timeframe to download data in. Pass 'w' for weekly and 'm' for
        monthly.

      - ``reverse``

        [2018-11-16] The latest incarnation of Yahoo online downloads returns
        the data in the proper order. The default value of ``reverse`` for the
        online download is therefore set to ``False``

      - ``adjclose``

        Whether to use the dividend/split adjusted close and adjust all values
        according to it.

      - ``urlhist``

        The url of the historical quotes in Yahoo Finance used to gather a
        ``crumb`` authorization cookie for the download

      - ``urldown``

        The url of the actual download server

      - ``retries``

        Number of times (each) to try to get a ``crumb`` cookie and download
        the data

      proxies)perioddr   )urlhistz*https://finance.yahoo.com/quote/{}/history)urldownz4https://query1.finance.yahoo.com/v7/finance/download)retries   c                 ^	   	 dd l }d | _        | j                  j
                  j                  | j                  j                        }t               }| j                  j                  r| j                  j                  |d<   d }|j                         }d|j                  d<   t        | j                  j                  dz         D ]  } |j                  |fi |}|j                  |j                   j"                  k7  r:|j$                  }	|	j'                  d      }|dk(  r]|	j'                  d	|      }|dk(  ru|	j'                  d
|t)        d	      z   dz         }
|
dk(  r|
dz  }
|	j'                  d
|
      }|dk(  r|	|
| }|j+                  d      j-                  d      } n |d| _        d | _        y t1        |      }dj                  | j                  j2                  | j                  j                        }g }t5        ddd      }| j                  j6                  ^| j                  j6                  j5                         |z
  j9                         }|j;                  dj                  t=        |                   | j                  j6                  ^| j                  j>                  j5                         |z
  j9                         }|j;                  dj                  t=        |                   t@        jB                  jD                  dt@        jB                  jF                  dt@        jB                  jH                  di}|j;                  dj                  || j                  jJ                                  |j;                  d       |j;                  dj                  |             dj                  |djM                  |            }d }t        | j                  j                  dz         D ]  } |j                  |fi |}|j                  |j                   j"                  k7  r:|j                  d   }|jO                  d      sd|z  | _        e	 tQ        jR                  |j$                  d       } || _        y  || _        y # t        $ r d}t        |      w xY w# t        $ r Y w xY w)Nr   zThe new Yahoo data feed requires to have the requests module installed. Please use pip install requests or the method of your choicerf   
backtraderz
User-Agentr1   
CrumbStorecrumb"asciizunicode-escapezCrumb not foundz{}/{}i  z
period2={}z
period1={}1d1wk1mozinterval={}zevents=historyzcrumb={}z{}?{}&zContent-Typeztext/zWrong content type: %sr   )*requestsImportError	Exceptionerrorr>   ri   formatdatanamedictrf   Sessionheadersrangerk   getstatus_codecodesoktextfindlenencodedecoder#   r   rj   r   todatetotal_secondsappendr<   fromdatebt	TimeFrameDaysWeeksMonths	timeframejoin
startswithr%   r&   )r*   rx   msgurl
sesskwargsrq   sessrJ   resptxtistartiendurldurlargsposixperiod2period1	intervalsr#   ctypes                       r.   start_v7zYahooFinanceData.start_v7   s   	! 
ffnn##DFFOO4V
66>>$(FFNNJy!!%1\"tvv~~)* 	A488C.:.D8>>#4#44))C&ABw!$ABwXXc1s7|#3a#78F|aKF88C(Drzt$ELL)001ABE-	0 =*DJDF
 ~~dffnndffoo>T1a 66==$vv}}))+e3BBDGNN<..s7|<=66==$vv++-5DDFGNN<..s7|<= LLtLLLL
	 	}++Idff6F6F,GHI'(z((/0~~dCHHW$56tvv~~)* 	A488D/J/D8>>#4#44LL0E##G,5=
KK		48 )	( }  	!/C C. 		!r  s   R !R R 	R,+R,c                 J    | j                          t        t        |           y )N)r   r   re   r   )r*   r-   s    r.   r   zYahooFinanceData.start`  s     	+-r/   )rV   rW   rX   rY   r    r   r   rZ   r[   s   @r.   re   re      s9    -
` 
BAKFaF. .r/   re   c                   D    e Zd ZeZej
                  j                         Zy)YahooFinanceN)rV   rW   rX   re   rc   r    	_gettupler`   r/   r.   r   r   g  s    G^^%%'Fr/   r   )
__future__r   r   r   r   r!   r   r   r%   r9   	utils.py3r
   r   r   r   r   rn   r   r_   r   utilsr   CSVDataBaser   r]   CSVFeedBaserb   re   r   r`   r/   r.   <module>r      s   ** *  # 	 ) )   M$** M`( "d&& "`.* `.F(4## (r/   