
    6i                         d dl Z d dlmZ d dlZd dlmZmZmZ d dlZd dl	Z	d dl
m
Z
mZ d dlmZ dedefdZd	edee   fd
Zdeeef   deeef   defdZd Zd Zd Zy)    N)Decimal)UnionOptionalDict)datetimetimezone)UnknownDateFormatdate_strreturnc                 b   t        j                  dt        j                        }t	        j
                  | ddi      }|st        |       |j                  |j                  j                  |       |j                  t        j                        }t        ||z
  j                         dz        S )a4  Convert UTC date to milliseconds

    If using offset strings add "UTC" to date string e.g. "now UTC", "11 hours ago UTC"

    See dateparse docs for formats http://dateparser.readthedocs.io/en/latest/

    :param date_str: date in readable format, i.e. "January 01, 2018", "11 hours ago UTC", "now UTC"
    r   TIMEZONEUTC)settings)tzinfog     @@)r   fromtimestampr   utc
dateparserparser	   r   	utcoffsetreplacepytzinttotal_seconds)r
   epochds      M/var/www/app/trading-bot/venv/lib/python3.12/site-packages/binance/helpers.pydate_to_millisecondsr      s     ,,Qx||<E&,,XU@STA)) 	xx188--a08IITXXI& E	((*V344    intervalc                 r    dddddd}	 t        | dd       || d      z  d	z  S # t        t        f$ r Y yw xY w)
aW  Convert a Binance interval string to milliseconds

    :param interval: Binance interval string, e.g.: 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w

    :return:
         int value of interval in milliseconds
         None if interval prefix is not a decimal integer
         None if interval suffix is not one of m, h, d, w

       <   i  iQ i:	 )smhr   wNi  )r   
ValueErrorKeyError)r   seconds_per_units     r   interval_to_millisecondsr+   &   s\     (8CR=!$4Xb\$BBTII! s   $ 66quantity	step_sizec           	      p    t        t        |             } t        | | t        t        |            z  z
        S )zRounds a given quantity to a specific step size

    :param quantity: required
    :param step_size: required

    :return: decimal
    )r   strfloat)r,   r-   s     r   round_step_sizer1   >   s0     s8}%HHws9~'>>>??r   c                 D    | | S t        | t              r| S t        |       S )N)
isinstancer   r   )ts_strs    r   convert_ts_strr5   L   s&    ~&#''r   c                 X    | | S t        j                  |       }|j                  dd      S )N  )jsondumpsr   )lress     r   convert_list_to_json_arrayr=   T   s*    y
**Q-C;;sBr   c                      	 t        j                         } | S # t        $ rP}t        |      j	                  d      r0t        j
                         } t        j                  |        | cY d}~S  d}~ww xY w)zcheck if there is an event loop in the current thread, if not create one
    inspired by https://stackoverflow.com/questions/46727787/runtimeerror-there-is-no-current-event-loop-in-thread-in-async-apscheduler
    z(There is no current event loop in threadN)asyncioget_event_loopRuntimeErrorr/   
startswithnew_event_loopset_event_loop)loopes     r   get_looprG   [   sa    	%%' q6GH))+D""4(Ks!    	A1AA,%A1+A,,A1)r?   decimalr   r9   typingr   r   r   r   r   r   r   binance.exceptionsr	   r/   r   r   r+   r0   r1   r5   r=   rG    r   r   <module>rL      s       ( (   ' 053 53 50s x} 0@E7N#@05eWn0E@
@( r   