
    'i                         d dl mZmZmZmZ ddlmZmZ  G d de      Z G d de      Z	 G d d	e	      Z
 G d
 de	      Z G d de      Zy)    )absolute_importdivisionprint_functionunicode_literals   )	IndicatorAndc                   4    e Zd ZdZdZdZdZd Zd Zd Z	d Z
y	)
NonZeroDifferencez
    Keeps track of the difference between two data inputs skipping, memorizing
    the last non zero value if the current difference is zero

    Formula:
      - diff = data - data1
      - nzd = diff if diff else diff(-1)
       )NZD)nzdc                 p    | j                   d   | j                  d   z
  | j                  j                  d<   y )Nr   data0data1lr   )selfs    ]/var/www/app/trading-bot/venv/lib/python3.12/site-packages/backtrader/indicators/crossover.py	nextstartzNonZeroDifference.nextstart(   s(    

1

15

1    c                     | j                   d   | j                  d   z
  }|r|n| j                  j                  d   | j                  j                  d<   y )Nr   r   )r   ds     r   nextzNonZeroDifference.next+   s=    JJqMDJJqM)DFFJJrN

1r   c                     | j                   j                  |   | j                  j                  |   z
  | j                  j                  |<   y N)r   arrayr   line)r   startends      r   	oncestartzNonZeroDifference.oncestart/   s:    JJU#djj&6&6u&== 			r   c                     | j                   j                  }| j                  j                  }| j                  j                  }||dz
     }t	        ||      D ]  }||   ||   z
  }|r|n|x||<   } y )Nr   )r   r   r   r   range)	r   r    r!   d0arrayd1arraylarrayprevir   s	            r   oncezNonZeroDifference.once3   su    **""**""eai uc" 	0A
WQZ'A$%q4/F1I	0r   N)__name__
__module____qualname____doc__	_mindatasaliaslinesr   r   r"   r*    r   r   r   r      s,     IEE63?0r   r   c                   2    e Zd ZdZdZ edddg      Zd Zy)	
_CrossBaser   )cross皙?              ?plotymarginplotyhlinesc                 $   t        | j                  | j                        }| j                  r% |d      dk  }| j                  | j                  kD  }n$ |d      dkD  }| j                  | j                  k  }t	        ||      | j
                  _        y )Nr   r7   )r   r   r   _crossupr	   r1   r5   )r   r   beforeafters       r   __init__z_CrossBase.__init__E   sn    

DJJ7==Ws]FJJ+EWs]FJJ+Evu-

r   N)r+   r,   r-   r/   r1   dictplotinfor@   r2   r   r   r4   r4   >   s!    IE3*=H
.r   r4   c                       e Zd ZdZdZy)CrossUpaS  
    This indicator gives a signal if the 1st provided data crosses over the 2nd
    indicator upwards

    It does need to look into the current time index (0) and the previous time
    index (-1) of both the 1st and 2nd data

    Formula:
      - diff = data - data1
      - upcross =  last_non_zero_diff < 0 and data0(0) > data1(0)
    TNr+   r,   r-   r.   r=   r2   r   r   rD   rD   R   s    
 Hr   rD   c                       e Zd ZdZdZy)	CrossDownaT  
    This indicator gives a signal if the 1st provided data crosses over the 2nd
    indicator upwards

    It does need to look into the current time index (0) and the previous time
    index (-1) of both the 1st and 2nd data

    Formula:
      - diff = data - data1
      - downcross = last_non_zero_diff > 0 and data0(0) < data1(0)
    FNrE   r2   r   r   rG   rG   a   s    
 Hr   rG   c                   6    e Zd ZdZdZdZ edddg      Zd Zy	)
	CrossOvera  
    This indicator gives a signal if the provided datas (2) cross up or down.

      - 1.0 if the 1st data crosses the 2nd data upwards
      - -1.0 if the 1st data crosses the 2nd data downwards

    It does need to look into the current time index (0) and the previous time
    index (-1) of both the 1t and 2nd data

    Formula:
      - diff = data - data1
      - upcross =  last_non_zero_diff < 0 and data0(0) > data1(0)
      - downcross = last_non_zero_diff > 0 and data0(0) < data1(0)
      - crossover = upcross - downcross
    r   )	crossoverr6   g      r8   r9   c                     t        | j                  | j                        }t        | j                  | j                        }||z
  | j                  _        y r   )rD   datar   rG   r1   rJ   )r   upcross	downcrosss      r   r@   zCrossOver.__init__   s;    $))TZZ0dii4	&2

r   N)	r+   r,   r-   r.   r/   r1   rA   rB   r@   r2   r   r   rI   rI   p   s(     IE4+>H3r   rI   N)
__future__r   r   r   r    r   r	   r   r4   rD   rG   rI   r2   r   r   <module>rQ      sQ   ** *  0	  0F. .(j 
 3	 3r   