
    'ix                     V    d dl mZmZmZmZ d dlZd dlmZ ddl	m
Z
mZ  G d de
      Zy)    )absolute_importdivisionprint_functionunicode_literalsN)MAXINT   )MovingAverageBaseMovAvc                   V     e Zd ZdZdZdZddej                  ffZd Z	 fdZ
d Z xZS )	ZeroLagIndicatoraK  By John Ehlers and Ric Way

    The zero-lag indicator (ZLIndicator) is a variation of the EMA
    which modifies the EMA by trying to minimize the error (distance price -
    error correction) and thus reduce the lag

    Formula:
      - EMA(data, period)

      - For each iteration calculate a best-error-correction of the ema (see
        the paper and/or the code) iterating over ``-bestgain`` ->
        ``+bestgain`` for the error correction factor (both incl.)

      - The default moving average is EMA, but can be changed with the
        parameter ``_movav``

        .. note:: the passed moving average must calculate alpha (and 1 -
                  alpha) and make them available as attributes ``alpha`` and
                  ``alpha1`` in the instance

    See also:
      - http://www.mesasoftware.com/papers/ZeroLag.pdf

    )ZLIndicatorZLIndECErrorCorrecting)ec)	gainlimit2   _movavc                     | j                   j                  | j                   j                  g}|| j                   j                  g| j                   j	                  d      z  z  }|S )Nr   )pperiodr   r   
notdefault)selfplabelss     Y/var/www/app/trading-bot/venv/lib/python3.12/site-packages/backtrader/indicators/zlind.py
_plotlabelzZeroLagIndicator._plotlabel@   sK    66==$&&"2"23DFFMM?TVV%6%6x%@@@    c                     t        j                  | j                  j                        | _        | j                  j
                   | j                  j
                  dz   g| _        t        t        | '          y )N)r   r   )
r
   EMAr   r   emar   limitssuperr   __init__)r   	__class__s    r   r#   zZeroLagIndicator.__init__E   sS    99DFFMM2((($&&*:*:Q*>? 	.0r   c                    t         }| j                  d   x}}| j                  d   }| j                  j                  d   }| j                  j
                  | j                  j                  }}t        | j                   D ]3  }|dz  }	|||	||z
  z  z   z  ||z  z   }
t        ||
z
        }||k  s0|}|
}5 || j                  j                  d<   y )Nr   
   )
r   r    datalinesr   alphaalpha1ranger!   abs)r   
leasterrorbestecr    priceec1r*   r+   value1gainr   errors               r   nextzZeroLagIndicator.nextL   s    
xx{"		!jjmmBvT[[) 	FB;D# 445DB
OEz!"
	 "

ar   )__name__
__module____qualname____doc__aliasr)   r
   r   paramsr   r#   r5   __classcell__)r$   s   @r   r   r       s9    0 ?EE	599F

1"r   r   )
__future__r   r   r   r   
backtraderbtbacktrader.utils.py3r    r	   r
   r    r   r   <module>rC      s)   ** *  ' ';"( ;"r   