
    Cqic                         d Z ddlmZmZ dedefdZdededefdZdedefdZ	d	ed
edefdZ
d	ed
ededefdZdededefdZdededefdZdededefdZdededefdZ	 ddededededef
dZy)zl
Crypto price normalization utilities.
Handles decimal precision, pip calculations, and spread computation.
    )CRYPTO_PAIRSDEFAULT_PAIR_CONFIGsymbolreturnc                 R    t        j                  | j                         t              S )z
    Get configuration for a crypto pair.

    Args:
        symbol: Trading pair symbol (e.g., 'BTCUSDT')

    Returns:
        Dictionary with pair configuration
    )r   getupperr   )r   s    //var/www/html/crpytotradingbot/normalization.pyget_pair_configr   	   s     FLLN,?@@    pricec                 :    t        |      }|d   }t        | |      S )z
    Normalize price to proper decimal precision for the symbol.

    Args:
        price: Raw price value
        symbol: Trading pair symbol

    Returns:
        Price rounded to proper decimal places
    decimals)r   round)r   r   configr   s       r
   normalize_pricer      s%     V$Fj!H!!r   c                 "    t        |       }|d   S )z
    Get the pip size (minimum price movement) for a symbol.

    Args:
        symbol: Trading pair symbol

    Returns:
        Pip size value
    pip_size)r   )r   r   s     r
   get_pip_sizer   &   s     V$F*r   bidaskc                     || z
  S )z
    Calculate spread from bid and ask prices.

    Args:
        bid: Current bid price
        ask: Current ask price

    Returns:
        Spread value (ask - bid)
     )r   r   s     r
   
get_spreadr   4   s     9r   c                 H    t        | |      }t        |      }|dkD  r||z  S dS )z
    Calculate spread in pips for a symbol.

    Args:
        bid: Current bid price
        ask: Current ask price
        symbol: Trading pair symbol

    Returns:
        Spread measured in pips
    r   )r   r   )r   r   r   spreadr   s        r
   get_spread_in_pipsr   B   s0     S!FF#H (16H3!3r   price_distancec                 0    t        |      }|dkD  r| |z  S dS )z
    Convert a price distance to pips.

    Args:
        price_distance: Distance in price units
        symbol: Trading pair symbol

    Returns:
        Distance in pips
    r   r   )r   r   r   s      r
   price_to_pipsr!   S   s$     F#H(01>H$;!;r   pipsc                 "    t        |      }| |z  S )z
    Convert pips to price distance.

    Args:
        pips: Distance in pips
        symbol: Trading pair symbol

    Returns:
        Distance in price units
    r    )r"   r   r   s      r
   pips_to_pricer$   b   s     F#H(?r   lotsc                     t        |      }|d   }|d   }|d   }t        |t        | |            } |dkD  rt        | |z        |z  } t        ||       } | S )z
    Normalize lot size to comply with symbol constraints.

    Args:
        lots: Desired lot size
        symbol: Trading pair symbol

    Returns:
        Normalized lot size within min/max bounds and step size
    min_lotmax_lotlot_stepr   )r   maxminr   )r%   r   r   r'   r(   r)   s         r
   normalize_lot_sizer,   q   sp     V$FYGYGj!H wD'*+D !|TH_%0 wDKr   c                     | |z  S )z
    Calculate the notional value of a position.

    Args:
        lots: Position size in lots
        price: Current price

    Returns:
        Position value in quote currency
    r   )r%   r   s     r
   calculate_position_valuer.      s     %<r   entry_price
exit_priceis_buyc                 &    |r||| z
  z  S || |z
  z  S )a  
    Calculate profit/loss for a position.

    Args:
        entry_price: Position entry price
        exit_price: Position exit price
        lots: Position size
        is_buy: True for buy position, False for sell

    Returns:
        Profit/loss in quote currency
    r   )r/   r0   r%   r1   s       r
   calculate_profit_lossr3      s'     zK/00{Z/00r   N)T)__doc__r   r   r   strdictr   floatr   r   r   r   r!   r$   r,   r.   boolr3   r   r   r
   <module>r9      s%  
 5
AC 
AD 
A"5 "# "% "   E  % 4E 4 4s 4u 4"<% < < < s u U C E :5  5  7;1u 1% 1 %1/31?D1r   