
    'i2                     F    d dl mZmZmZmZ ddlmZmZmZ  G d de      Z	y)    )absolute_importdivisionprint_functionunicode_literals   )SumNMovingAverageBaseExponentialSmoothingDynamicc                   .     e Zd ZdZdZdZdZ fdZ xZS )AdaptiveMovingAveragea  
    Defined by Perry Kaufman in his book `"Smarter Trading"`.

    It is A Moving Average with a continuously scaled smoothing factor by
    taking into account market direction and volatility. The smoothing factor
    is calculated from 2 ExponetialMovingAverage smoothing factors, a fast one
    and slow one.

    If the market trends the value will tend to the fast ema smoothing
    period. If the market doesn't trend it will move towards the slow EMA
    smoothing period.

    It is a subclass of SmoothingMovingAverage, overriding once to account for
    the live nature of the smoothing factor

    Formula:
      - direction = close - close_period
      - volatility = sumN(abs(close - close_n), period)
      - effiency_ratio = abs(direction / volatility)
      - fast = 2 / (fast_period + 1)
      - slow = 2 / (slow_period + 1)

      - smfactor = squared(efficienty_ratio * (fast - slow) + slow)
      - smfactor1 = 1.0  - smfactor

      - The initial seed value is a SimpleMovingAverage

    See also:
      - http://fxcodebase.com/wiki/index.php/Kaufman's_Adaptive_Moving_Average_(KAMA)
      - http://www.metatrader5.com/en/terminal/help/analytics/indicators/trend_indicators/ama
      - http://help.cqg.com/cqgic/default.htm#!Documents/adaptivemovingaverag2.htm
    )KAMAMovingAverageAdaptive)kama))fast   )slow   c                 D   | j                   | j                  | j                  j                         z
  }t        t	        | j                   | j                  d      z
        | j                  j                        }t	        ||z        }d| j                  j
                  dz   z  }d| j                  j                  dz   z  }t        |||z
  z  |z   d      }t        | j                   | j                  j                  |      | j                  d<   t        t        | 3          y )N)periodg       @g      ?r   )r   alphar   )datapr   r   absr   r   powr
   linessuperr   __init__)self	direction
volatilityerr   r   sc	__class__s          X/var/www/app/trading-bot/venv/lib/python3.12/site-packages/backtrader/indicators/kama.pyr   zAdaptiveMovingAverage.__init__@   s     II		466==. 99	#dii$))B-78O
Z'(dffkkC'(dffkkC'("t$,a03DII;?66==:<>

1 	#T35    )	__name__
__module____qualname____doc__aliasr   paramsr   __classcell__)r$   s   @r%   r   r      s$    @ /EE(F6 6r&   r   N)

__future__r   r   r   r    r   r	   r
   r    r&   r%   <module>r1      s%   ** * E D66- 66r&   