
    'i@                         d dl mZmZmZmZ d dlmZmZ d dlmZ d dl	m
Z
mZmZ d dlmZmZmZmZ d dlmZ d dlmZ  G d d	ej,                        Z G d
 d eee            Zy)    )absolute_importdivisionprint_functionunicode_literals)datetime	timedelta)DataBase)	TimeFramedate2numnum2date)integer_typesqueuestring_typeswith_metaclass)
MetaParams)
oandastorec                        e Zd Z fdZ xZS )MetaOandaDatac                 Z    t         t        |   |||       | t        j                  _        y)z+Class has already been created ... registerN)superr   __init__r   
OandaStoreDataCls)clsnamebasesdct	__class__s       T/var/www/app/trading-bot/venv/lib/python3.12/site-packages/backtrader/feeds/oanda.pyr   zMetaOandaData.__init__#   s(     	mS*4< ),
%    )__name__
__module____qualname__r   __classcell__r   s   @r   r   r   "   s    , ,r    r   c                        e Zd ZdZdZej                  Z ed      \  Z	Z
ZZZ e       Zd Zd Zd Z fdZ fdZdd	Z fd
Zd Zd Zd Zd Z xZS )	OandaDataa  Oanda Data Feed.

    Params:

      - ``qcheck`` (default: ``0.5``)

        Time in seconds to wake up if no data is received to give a chance to
        resample/replay packets properly and pass notifications up the chain

      - ``historical`` (default: ``False``)

        If set to ``True`` the data feed will stop after doing the first
        download of data.

        The standard data feed parameters ``fromdate`` and ``todate`` will be
        used as reference.

        The data feed will make multiple requests if the requested duration is
        larger than the one allowed by IB given the timeframe/compression
        chosen for the data.

      - ``backfill_start`` (default: ``True``)

        Perform backfilling at the start. The maximum possible historical data
        will be fetched in a single request.

      - ``backfill`` (default: ``True``)

        Perform backfilling after a disconnection/reconnection cycle. The gap
        duration will be used to download the smallest possible amount of data

      - ``backfill_from`` (default: ``None``)

        An additional data source can be passed to do an initial layer of
        backfilling. Once the data source is depleted and if requested,
        backfilling from IB will take place. This is ideally meant to backfill
        from already stored sources like a file on disk, but not limited to.

      - ``bidask`` (default: ``True``)

        If ``True``, then the historical/backfilling requests will request
        bid/ask prices from the server

        If ``False``, then *midpoint* will be requested

      - ``useask`` (default: ``False``)

        If ``True`` the *ask* part of the *bidask* prices will be used instead
        of the default use of *bid*

      - ``includeFirst`` (default: ``True``)

        Influence the delivery of the 1st bar of a historical/backfilling
        request by setting the parameter directly to the Oanda API calls

      - ``reconnect`` (default: ``True``)

        Reconnect when network connection is down

      - ``reconnections`` (default: ``-1``)

        Number of times to attempt reconnections: ``-1`` means forever

      - ``reconntimeout`` (default: ``5.0``)

        Time in seconds to wait in between reconnection attemps

    This data feed supports only this mapping of ``timeframe`` and
    ``compression``, which comply with the definitions in the OANDA API
    Developer's Guid::

        (TimeFrame.Seconds, 5): 'S5',
        (TimeFrame.Seconds, 10): 'S10',
        (TimeFrame.Seconds, 15): 'S15',
        (TimeFrame.Seconds, 30): 'S30',
        (TimeFrame.Minutes, 1): 'M1',
        (TimeFrame.Minutes, 2): 'M3',
        (TimeFrame.Minutes, 3): 'M3',
        (TimeFrame.Minutes, 4): 'M4',
        (TimeFrame.Minutes, 5): 'M5',
        (TimeFrame.Minutes, 10): 'M10',
        (TimeFrame.Minutes, 15): 'M15',
        (TimeFrame.Minutes, 30): 'M30',
        (TimeFrame.Minutes, 60): 'H1',
        (TimeFrame.Minutes, 120): 'H2',
        (TimeFrame.Minutes, 180): 'H3',
        (TimeFrame.Minutes, 240): 'H4',
        (TimeFrame.Minutes, 360): 'H6',
        (TimeFrame.Minutes, 480): 'H8',
        (TimeFrame.Days, 1): 'D',
        (TimeFrame.Weeks, 1): 'W',
        (TimeFrame.Months, 1): 'M',

    Any other combination will be rejected
    ))qcheckg      ?)
historicalF)backfill_startT)backfillT)backfill_fromN)bidaskT)useaskF)includeFirstT)	reconnectT)reconnections)reconntimeoutg      @   c                     | j                   S N)_TOFFSETselfs    r   _timeoffsetzOandaData._timeoffset   s    }}r    c                      y)z`Returns ``True`` to notify ``Cerebro`` that preloading and runonce
        should be deactivatedT r8   s    r   islivezOandaData.islive   s     r    c                 |     | j                   di || _        | j                  j                  rd| _        y d| _        y )Nr-   midpointr<   )_storeopr-   _candleFormat)r9   kwargss     r   r   zOandaData.__init__   s0    &v&)-XJr    c                 b    t         t        |   |       |j                  | j                         y)zWReceives an environment (cerebro) and passes it over to the store it
        belongs toN)r   r'   setenvironmentaddstorerA   )r9   envr   s     r   rF   zOandaData.setenvironment   s$     	i-c2TVVr    c                    t         t        |           d| _        t	               | _        t        j                         | _        | j                  | _
        | j                  j                  |        | j                  j                  | j                  | j                        }|-| j                  | j                          | j                  | _
        y| j                  j#                  | j$                  j&                        x| _        }|-| j                  | j*                         | j                  | _
        y| j$                  j,                  =| j.                  | _
        | j$                  j,                  j1                          d| _        y| j3                          | j4                  | _
        | j7                          d| _        y)z`Starts the Oanda connecction and gets the real contract and
        contractdetails if it existsF)dataNr   )r   r'   start_statelivereconndict
_storedmsgr   Queueqlive_ST_OVER_staterA   get_granularity
_timeframe_compressionput_notificationNOTSUPPORTED_TFget_instrumentrB   datanamecontractdetailsNOTSUBSCRIBEDr,   _ST_FROM_start_start_finish	_ST_START	_st_start_reconns)r9   otfcdr   s      r   rK   zOandaData.start   sI    	i$& !&&[[]
mm 	$ ff$$T__d6G6GH;!!$"6"67--DK$(FF$9$9$&&//$JJr:!!$"4"45--DK66+--DKFF  '') 	  ..DKNNr    c           	         | j                   j                  r| j                  | j                         d }| j                  t        d      k  rt        | j                        }d }| j                  t        d      kD  rt        | j                        }| j                  j                  | j                   j                  ||| j                  | j                  | j                  | j                   j                        | _        | j                   | _        y| j                  j%                  | j                   j                  |      | _        |r| j                   j(                  | _        n| j                   j,                  | _        | j*                  r| j                  | j                         | j.                  | _        |r| j                   j0                  | _        y)Ninf-infcandleFormatr/   T)tmout)rB   r)   rV   DELAYEDtodatefloatr   fromdaterA   candlesrY   rT   rU   rC   r/   qhist_ST_HISTORBACKrR   streaming_pricesrP   r*   rL   r+   _ST_LIVEr1   ra   )r9   instartri   dtenddtbegins        r   r`   zOandaData._st_start   sK   66!!$,,/E{{U5\) -G}}uV},"4==1%!2!2!//!VV00	 ( 2DJ --DKVV,,TVV__E,J
$(FF$9$9D!$(FFOOD!  !!$,,/mm FF00DMr    c                 ^    t         t        |           | j                  j                          y)z!Stops and tells the store to stopN)r   r'   stoprA   )r9   r   s    r   rw   zOandaData.stop   s    i#%r    c                 H    t        | j                  xs | j                        S r6   )boolrN   rP   r8   s    r   haslivedatazOandaData.haslivedata  s    DOO1tzz22r    c           	          | j                   | j                  k(  ry	 | j                   | j                  k(  r	 | j                  j	                  d d       xs& | j
                  j                  | j                        }|| j                  | j                         | j                  j                  r| j                  dk(  r-| j                  | j                         | j                  | _         y| xj                  dz  c_        | j!                  d| j                  j"                         d|v r| j                  | j                         |d   }|dvr-| j                  | j                         | j                  | _         y| j                  j                  r| j                  dk(  r-| j                  | j                         | j                  | _         y| xj                  dz  c_        | j!                  d| j                  j"                         | j                  j$                  | _        | j&                  sg| j(                  | j*                  k7  r8| j
                  j-                         dk  r| j                  | j*                         | j/                  |      }|ry|| j                  d <   | j(                  | j0                  k7  r| j                  | j0                         d }t3        |       dkD  r| j4                  j5                  d	      }n0| j6                  t9        d
      kD  rt;        | j6                        }nd }t5        j<                  t?        |d         dz        }| j@                  jC                  | j                  jD                  ||| jF                  | jH                  | jJ                  | j                  jL                        | _'        | jP                  | _         d| _        | j                   | jP                  k(  r| jN                  j                         }|-| j                  | j                         | j                  | _         yd|v rH| j                  | jR                         | j                  | j                         | j                  | _         y|r| jU                  |      ry| j                  jV                  r-| j                  | j                         | j                  | _         y| j                  | _         | j                   | jX                  k(  r| j                  jZ                  j]                         s| j^                  | _         5| j`                  jc                         D ]J  }te        | j                  jZ                  j`                  |      }te        | j`                  |      }|d   |d<   L y| j                   | j^                  k(  r$| j!                  d      s| j                  | _         y# t        j                  $ r Y y w xY w)NFT)timeoutr      )rs   ri   code)iW  iV  iT  r2   rf   time@B rg   )rs   )3rR   rQ   rr   rN   poprP   get_qcheckr   EmptyrV   
CONNBROKENrB   r0   ra   DISCONNECTEDr`   r3   r1   rL   _laststatusLIVEqsize
_load_tickrj   lenr   rm   rl   r   utcfromtimestampintrA   rn   rY   rT   rU   rC   r/   ro   rp   r[   _load_historyr)   r\   r,   nextr_   linesgetlinealiasesgetattr)	r9   msgr~   retrt   ru   aliaslsrcldsts	            r   _loadzOandaData._load  s   ;;$--'{{dmm+ ??..tT: @::>>$,,>? 
 ;))$//:66++t}}/A--d.?.?@&*mm$MMQ&MNN58L8LNMS=))$//:v;D?2--d.?.?@&*mm$66++t}}/A--d.?.?@&*mm$ MMQ&MNN58L8LNM $ 4 4 ,,''4994::++-2 11$))<//#.C#  ),% ##t||3))$,,7t9q="mm44R8G]]U6]2&t}}5G #G 11#c&k2BW2LM!VV^^FFOOWeOOT%6%6!%!3!3!%!4!4	 , 6
 #11(-% 3 33jjnn&;))$*;*;<"&--DK s]))$*<*<=))$*;*;<"&--DK ))#.# vv((--d.?.?@&*mm$ #mm-vv++002"&..DK "ZZ668 &E"466#7#7#=#=uED"4::u5D"1gDG	& .~~e~4"&--DK I 
 {{   s   AW7 7XXc                    t        j                  t        |d         dz        }t        |      }|| j                  j                   d   k  ry|| j                  j                   d<   d| j                  j
                  d<   d| j                  j                  d<   | j                  j                  rt        |d         nt        |d         }|| j                  j                  d<   || j                  j                  d<   || j                  j                  d<   || j                  j                  d<   d| j                  j
                  d<   d| j                  j                  d<   y	)
Nr   r   r2   Fr           askbidT)r   r   r   r   r   volumeopeninterestrB   r.   rl   openhighlowclose)r9   r   dtobjdtticks        r   r   zOandaData._load_tick  s   ))#c&k*:W*DEe_$$R(( "$

A"

!%(

" %)FFMMuSZ uSZ7H!

!

 

q"

"

!%(

"r    c                     t        j                  t        |d         dz        }t        |      }|| j                  j                   d   k  ry|| j                  j                   d<   t        |d         | j                  j                  d<   d| j                  j                  d<   | j                  j                  r@| j                  j                  st        |d         | j                  j                  d<   t        |d	         | j                  j                  d<   t        |d
         | j                  j                  d<   t        |d         | j                  j                  d<   yt        |d         | j                  j                  d<   t        |d         | j                  j                  d<   t        |d         | j                  j                  d<   t        |d         | j                  j                  d<   yt        |d         | j                  j                  d<   t        |d         | j                  j                  d<   t        |d         | j                  j                  d<   t        |d         | j                  j                  d<   y)Nr   r   r2   Fr   r   r   openBidhighBidlowBidcloseBidopenAskhighAsklowAskcloseAskopenMidhighMidlowMidcloseMidT)r   r   r   r   r   rl   r   r   rB   r-   r.   r   r   r   r   )r9   r   r   r   s       r   r   zOandaData._load_history  s   ))#c&k*:W*DEe_$$R(( "$

A$S]3

!%(

" 66==66==%*3y>%:

"%*3y>%:

"$)#h-$8

q!&+C
O&<

  #  &+3y>%:

"%*3y>%:

"$)#h-$8

q!&+C
O&<

  #  "'s9~!6DJJOOA!&s9~!6DJJOOA %c(m 4DJJNN1"'J"8DJJQr    )TN)r!   r"   r#   __doc__paramsr   r   r@   ranger\   r_   rr   rp   rQ   r   r7   r:   r=   r   rF   rK   r`   rw   rz   r   r   r   r$   r%   s   @r   r'   r'   ,   sx    ^~F ""F ?DAh;Hi>8{H
G#J!F
3H!T,r    r'   N)
__future__r   r   r   r   r   r   backtrader.feedr	   
backtraderr
   r   r   backtrader.utils.py3r   r   r   r   backtrader.metabaser   backtrader.storesr   r   r   r'   r<   r    r   <module>r      sT   ** * ) $ 4 42 2 * (,H&& ,U}h7 Ur    