
    Q-jXT                     0   d dl mZ d dlmZ d dlmZ d dlmZ d dl	m
Z d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$ d dlm%Z& d dlm'Z( d d	l)m*Z* d d
l+m,Z,m-Z- d dl.m/Z/ d dl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6 d dl7m8Z8 d dl9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZB g dZC	 	 dBde4de4deDdeDde4f
dZEde1de1fdZF	 	 dCde-de5de4de4de2de,fdZGde5de3fdZH e/d !      dDde4d"eDde1fd#       ZIde-de3fd$ZJde1d%e5de3fd&ZK	 	 dCde-d'e5de4de4de2de,fd(ZLde-de3fd)ZM	 dEde4d*eDd"eDde1fd+ZNde-d,eOde-fd-ZP	 	 	 dFde-d.e5d/e5d0e5d1e5de4de2de-fd2ZQde1d,e4de1fd3ZR	 dGd4e-d5e-d,e4d6eDde-f
d7ZS	 dHde4d"eDde6e5   fd8ZTd9e1defd:ZUde5de5fd;ZV	 	 dId<e,d=e,d>eDd?eDd@eWde,fdAZXy)J    )Callable)reduce)floor)mul)
float_info)allarraybroadcast_toconcatenatediffdotexpfabsfullisnanloglogical_andnannanmeannansumndarraynewaxisonespadsignsqrtsumtriuzeros)max)min)sliding_window_view)	DataFrameSeries)njit)ArrayDictLikeFloatIntIntFloatList)Imports)	v_arrayv_dataframev_floatv_intv_lowerboundv_offsetv_pos_defaultv_scalarv_series)combinationcubeconsecutive_streakdf_error_analysiserf	fibonaccigeometric_meanhpolyifisherlog_geometric_meanpascals_trianglepercent_rankremapstrided_windowsum_signed_rolling_deltassymmetric_triangleweightszeronr
repetitionmultichoosereturnc                    t        t        |             t        t        |            }} |s|r| |z   dz
  } t        | | |z
        }|dk(  ryt        t        t        | | |z
  d      d      }t        t        t        d|dz         d      }||z  S )a  Combination

    Combination computation.

    Sources:
        * [stackoverflow](https://stackoverflow.com/questions/4941753/is-there-a-math-ncr-function-in-python)

    Parameters:
        n (Int): ```n```
        r (Int): ```r```
        repetition (bool): Apply repetition.
        multichoose (bool): Apply multichoose.

    Returns:
        (Int): Combination value

    Note:
        ```n``` Choose ```r```: ```(n r)```
       r   )intr   r!   r   r   range)rH   rI   rJ   rK   	numeratordenominators         c/Users/jameslopez/projects/TradingBot25/.venv/lib/python3.12/site-packages/pandas_ta/utils/_math.pyr6   r6   ?   s    . tAw<T!WqA[EAI 	Aq1uAAvsE!QUB/3IeAq1uoq1K##    xc                 B    t        dgt        t        |             f      S )a  Consecutive Streak

    Computes the streak of consecutive value increases or decreases.

    Parameters:
        x (Array): Numpy array.

    Returns:
        (Array): Streak array of element changes.

    Note: Logic
        Yield an array where each value represents the streak value
        for that bar.

        1. Computes the difference between consecutive values.
        2. Assigns 1 for each positive change, -1 for each negative
           change -1 and 0 for no change.

    Note: Streaks
        * Positive: Consecutive bars of value increases
        * Negative: Consecutive bars of value decreases
        * Zero: When direction of the value change reverses

    Example:
        ```py
        x = np.array([100, 101, 102, 100, 100, 101, 102, 103])
        result = consecutive_streak(x)
        expected = np.array([0, 1, 1, -1, 0, 1, 1, 1])
        np.array_equal(result, expected)
        ```
    r   )r   r   r   rV   s    rT   r8   r8   f   s    @ T$q']+,,rU   Nsrcpwrsignal_offsetoffsetkwargsc                 
   t        |       } t        |ddd      }t        |dd      }t        |      }| |z  }t	        || j
                        }t	        || j
                        }|dk7  r"|j                  |      }|j                  |      }|dk7  r"|j                  |      }|j                  |      }t        t        |            rt        t        |            ryd|v r,|j                  |d   d	
       |j                  |d   d	
       d| d| }d| |_
        d| |_
        dx|_        |_        |j                  ||j                  |i}	t        |	| j
                        }
d| |
_
        |j                  |
_        |
S )a  Cube Transform

    This transform, by John Ehlers, is used to compress Svalues near zero for
    a normalized oscillator like the Inverse Fisher Transform.

    In other words, a Power Transform/Function: ```result = src ^ pwr```

    Sources:
        * [rengel8](https://github.com/rengel8) based on Markus K.
          (cryptocoinserver)'s source
        * "Cycle Analytics for Traders", 2014, by John Ehlers, page 200

    Parameters:
        src (Series): Source
        pwr (float): The transform power. Default: ```3```
        signal_offset (int): Signal offset. Default: ```-1```
        offset (int): Post shift. Default: ```0```

    Other Parameters:
        fillna (value): ```pd.DataFrame.fillna(value)```

    Returns:
        (DataFrame): 2 columns

    Note:
        * Values near ```-1``` and ```1``` are nearly unchanged, whereas
          values near zero are reduced.
        * Input effects of spectral dilation should have been removed
          (i.e. roofing filter).

    g      @F)strictrO   r   indexNfillnaTinplace_CUBECUBEs	transform)r5   r1   r0   r2   r$   ra   shiftr   r   rb   namecategoryr#   )rY   rZ   r[   r\   r]   resultct	ct_signal_propsdatadfs              rT   r7   r7      s|   H 3-C
sCU
3C-Q/MfF CZF	cii	(BvSYY/I {XXfOOF+	XXm$OOM2	
59~#eI./ 6
		&"D	1)48 Q}o&FVHoBGVH%IN'22BK)$GGR3D	4syy	)BVHoBG++BKIrU   c                     t        |       }t        |       } d}d}d}d}d}d}dd|| z  z   z  }d||z  |z   |z  |z   |z  |z   |z  |z   |z  t        |  | z        z  z
  }	||	z  S )a]  Error Function

    Computes the erf(x)

    Sources:
        * Handbook of Mathematical Functions, formula 7.1.26.
        * [stackoverflow](https://stackoverflow.com/questions/457408/is-there-an-easily-available-implementation-of-erf-for-python)

    Parameters:
        x (IntFloat): ```x``` value.

    Returns:
        (Float): Error value
    g~Z O?gi<15ҿgWU?g9LW@g-UB?g{=@?      ?)r   absr   )
rV   x_signa1a2a3a4a5ptys
             rT   r:   r:      s     !WFAA 
B	B	B	B	BA 	sQU{A26B;!#r)Q.3"A26{+ 	+AA:rU   T)cacheweightedc                     | dkD  r| nd} t        d      }dd|z   z  dd|z
  z  }}t        |       }t        d|       D ]"  }t        ||dz   z  ||dz   z  z
        |z  ||<   $ |r||j	                         z  S |S )a   Fibonacci

    Computes closed form Fibonacci values.

    Parameters:
        n (Int): Number of terms (n >= 2). Default: ```2```
        weighted (bool): Return weighted values. Default: ```False```

    Returns:
        (Array): Numpy array results
    rN      g      @      ?rs   r   )r   r   rQ   floatr   )rH   r   sqrt5phipsirl   is          rT   r;   r;      s     UAIEcEk"C3;$7C1XF1a[#!a%.31q5>9:UBq	  

$$MrU   c                     | j                   }|dk  r| j                  d   S d| j                         v }|r| j                  d      dz   } t	        | dkD        r| j                         d|z  z  }|s|S |dz
  S y)zGeometric Mean

    Computes the Geometric Mean of positive values.

    Parameters:
        x (Series): Values

    Returns:
        (Float): Geometric Mean
    rN   r   )sizeilocto_numpyrb   r   prod)rV   rH   	has_zerosmeans       rT   r<   r<     sy     	
A1uvvayQZZ\!IHHQK!O
1q5zvvxAE"$t2$(2rU   vc                     t        | t              st        |       } | j                  | d   }}t	        d|      D ]  }| |   ||z  z   } |S )a  Horner's Polynomial

    Evaluates a polynomial with an array of polynomial coefficients, ```x```,
    and a value, ```v```, using Horner's Calculation for Polynomial
    Evaluation.

    Parameters:
        x (Array): Polynomial coefficients as ```np.array```
        v (IntFloat): Value

    Returns:
        (Float): Polynomial value.

    Tip: Performance
        Use a ```np.array``` for best performance.

    Example:
        ```py
        coeffs_0 = [4, -3, 0, 1] # 4x^3 - 3x^2 + 0x + 1
        coeffs_1 = np.array(coeffs_0) # Faster
        coeffs_2 = pd.Series(coeffs_0).to_numpy()
        x = -6.5

        hpoly(coeffs_0, x) => -1224.25
        hpoly(coeffs_1, x) or hpoly(coeffs_2, x) => -1224.25 # Faster
        ```
    r   rN   )
isinstancer   r	   r   rQ   )rV   r   mr}   r   s        rT   r=   r=   -  sN    8 a!!H661Q4qA1a[aD1q5L HrU   ampc                    t        |       } t        |d      }t        |dd      }t        |      }| j	                         }t        |dk\  |dk        }t        |      s[t        |      t        |      }}t        | ||dd      }	|	"t        t        |	j	                                     ry|	j	                         }t        ||z        }
|
dz
  |
dz   z  }t        || j                        }t        || j                        }|dk7  r"|j                  |      }|j                  |      }|dk7  r"|j                  |      }|j                  |      }d|v r,|j                  |d   d	
       |j                  |d   d	
       d| }d| |_        d| |_        |j                   ||j                   |i}t#        || j                        }d| |_        |S )a  Inverse Fisher Transform

    This transform function, by John Ehlers, attempts to create clearer
    signals by changing the Probability Distribution Function (pdf) for the
    results of known oscillator-indicators.

    Sources:
        * [rengel8](https://github.com/rengel8) based on Markus K.
          (cryptocoinserver)'s source
        * "Cycle Analytics for Traders", 2014, by John Ehlers, page 198
        * [mesasoftware](https://www.mesasoftware.com/papers/TheInverseFisherTransform.pdf)

    Parameters:
        x (Series): Normalized to range ```[-1, 1]```
        amp (float): Amplifier. Default: ```1```
        signal_offset (int): Signal line offset. Default: ```-1```
        offset (int): Post shift. Default: ```0```

    Other Parameters:
        fillna (value): ```pd.DataFrame.fillna(value)```

    Returns:
        (DataFrame): 2 columns

    Note:
        * Normalized input, ```x```, with range ```[-1, 1]```
        * Data range of ```[-0.5, 0.5]``` would not have a significant impact

    Example: Preparation Examples
        Or use _ta.remap()_ function to prep

        (RSI - 50) * 0.1        RSI [0 to 100] -> -5 to 5

        (RSI - 50) * 0.02       RSI [0 to 100] -> -1 to 1 (use amp of 5 to match input of example above)
    rs   rO   r   rN   )from_minfrom_maxto_minto_maxNr`   rb   Trc   re   	INVFISHER
INVFISHERs)r5   r4   r0   r2   r   r   r   np_maxnp_minrB   r   r   r$   ra   ri   rb   rj   r#   )rV   r   r[   r\   r]   np_xis_remapped_np_max_np_minx_mapampedrl   
inv_fishersignalro   rp   rq   s                    rT   r>   r>   T  s   R 	A
3
C-Q/MfF ::<Ddbj$!)4K{!$<awa
 =Cenn&6 78~~d
OEaiEAI&Fagg.JF!''*F {%%f-
f%%%f-
f% 6&*D9fX&5 YF!&*JOvh'FKOOZf=D	4qww	'B&"BGIrU   c                     | j                   }|dkD  rJ| j                  d      dz   } t        | dkD        r(t        t	        |       j                         |z        dz
  S y)zLogarithmic Geometric Mean

    Computes the Logarithmic Geometric Mean of positive values.

    Parameters:
        x (Series): Values

    Returns:
        (Float): LogGeometric Mean or zero
    rN   r   )r   rb   r   r   r   r   )rV   rH   s     rT   r?   r?     sR     	
A1uHHQK!Oq1u:s1vzz|a'(1,,rU   inversec           	          | t        t        |             nd} t        t        d| dz         D cg c]  }t	        | |       c}      }t        |      }||z  }d|z
  }|r|r|S |r|S |ry|S c c}w )a  Pascal's Triangle

    The ```n```th row of Pascal's Triangle.

    Parameters:
        n (Int): ```n^th``` row of Pascal' Triange
        inverse (bool): Return Inverse weighted.
        weighted (bool): Return weighted.

    Returns:
        (Array): Classical, Weighted, or Inversely

    Example:
        ```py
        # Classical
        pt4 = pascals_triangle(4)
        # pt4 = [1, 4, 6, 4, 1]

        # Inverse
        invpt4 = pascals_triangle(4, inverse=True)
        # invpt4 = [0.9375, 0.75, 0.625, 0.75, 0.9375]

        # Weighted
        wpt4 = pascals_triangle(4, weighted=True)
        # wpt4 = [0.0625, 0.25, 0.375, 0.25, 0.0625]
        ```
    Nr   rN   )rH   rI   )rP   r   r	   rQ   r6   r   )rH   r   r   r   triangletriangle_sumtriangle_weightsinverse_weightss           rT   r@   r@     s    < DG1A U1a!e_E_kA+_EFHx=L,.**OGO Fs   A.lengthc                     | j                         j                         }t        ||dz   f      }|ddddf   |dddt        f   k  }dt	        |d      z  }t        t        |       t              }|||d |S )a'  Percent Rank

    Percent Rank of values over a specified length.

    Parameters:
        x (Series): ```x``` values
        length (int): The period.

    Returns:
        (Series): Percent Rank values.

    Note: Logic
        Yield a Series where the initial part (up to ```length - 1```) is
        padded with NaNs, and the rest contains the Percent Rank values.

        1. Computes the daily percentage returns.
        2. Creates a rolling window of these returns.
        3. Compares each value in the window to the current value (the
           last value in each window).
        4. Percent Rank is calculated as the percentage of values in each
           window that are less than the current value.

    Example:
        ```py
        x = Series([100, 80, 75, 123, 140, 80, 70, 40, 100, 120]).to_numpy()
        result = percent_rank(x, 3)
        expected_result = Series([np.nan, np.nan, np.nan, 66.666667, 66.666667, 0.0, 33.333333, 0.0, 100.0, 66.666667])
        np.allclose(result, expected_result, rtol=1e-6, equal_nan=True)
        ```
    rN   window_shapeNrO   d   axis)
pct_changer   r"   r   r   r   lenr   )rV   r   	np_pctchgrwscomparison_matrixprsrl   s          rT   rA   rA     s    > '')I
ivzm
DCAssFc!R.&99
)2
2C#a&#FF67O MrU   r   r   r   r   c                    t        |       } t        |dd      }t        |dd      }t        |dd      }t        |dd      }t        |      }||z
  ||z
  }}|dk  s|dk  ry|||z  | j                         |z
  z  z   }	t	        |	| j
                        }	|dk7  r|	j                  |      }	d|v r|	j                  |d   d	
       d| d| d| d| |	_        |	S )a  remap

    The standard method of transforming from a source range to a target range
    using Max-Min. Useful for bounded sources; not unbounded sources
    like _ohlcv_ data.

    Sources:
        * Linear (Max-Min) Normalization

    Parameters:
        x (Series): Series of 'x's
        from_min (IntFloat): Input minimum. Default: ```0.0```
        from_max (IntFloat): Input maximum. Default: ```100.0```
        to_min (IntFloat): Output minimum. Default: ```0.0```
        to_max (IntFloat): Output maximum. Default: ```100.0```
        offset (Int): Post shift. Default: ```0```

    Other Parameters:
        fillna (value): ```pd.DataFrame.fillna(value)```

    Returns:
        (Series): 1 column
    g        g      Y@g      rs   r   Nr`   rb   Trc   REMAP_re   )	r5   r/   r2   r   r$   ra   ri   rb   rj   )
rV   r   r   r   r   r\   r]   frangetrangerl   s
             rT   rB   rB   "  s    : 	Axc*Hx,HVT3'FVS#&FfF (&6/FF{fkv1::<(+BCCFF!''*F {f% 6fX&5 8*AhZq&BFK MrU   c                     ddl m} | j                  | j                  d   fz   }| j                  dd | j                  d   |z
  dz   |fz   } || ||d      S )a  Strided Window

    Creates a strided window view.

    Source:
        * [numpy](https://numpy.org/devdocs/reference/generated/numpy.lib.stride_tricks.as_strided.html)
        * [Issue #285](https://github.com/twopirllc/pandas-ta/issues/285)

    Parameters:
        x (Array): Source
        length (Int): Window period.

    Returns:
        (Array): Numpy Array of Strided Window Arrays

    Warning:
        Use if necessary, otherwise avoid when possible!
    r   )
as_stridedrO   NrN   F)shapestrides	writeable)numpy.lib.stride_tricksr   r   r   )rV   r   r   r   r   s        rT   rC   rC   ]  s\    & 3ii199R=**GGGCRLAGGBK&014f==EaugGGrU   open_close	exclusivec                 \   t        |d      }|s|dz  }t        | |      dd }t        ||d j                         ddt        f   |j
                        }t        ||z
        }t        |d      j                  t              }t        t        ||dfdt        	      |j                  
      S )a  Sum of Signed Rolling Series Deltas

    Calculates the sum of signed differences between the current closing bar
    and a rolling window of preceding opening bars. This sum is then padded
    to match the original series length.

    Parameters:
        open_ (Series): ```open``` Series
        close (Series): ```close``` Series
        length (Int): Window length. Default: ```4```
        exclusive (bool): Exclusive rolling window. Inclusive rolling window
            when ```False```.  Default: ```True```

    Returns:
        (Series): 1 column

    Notes: Mode
        **Exclusive**: Rolling window excludes the current bar in the
        lookback period.

        **Inclusive**: Rolling window includes the current bar in the
        lookback period.

    Example:
        ```py
        open_ = Series([95, 83,  71, 132, 129, 145, 133, 101, 68, 96])
        close = Series([100, 110, 140,  80,  90,  60,  50,  40, 90, 110])

        result = sum_signed_rolling_deltas(close, open_, 4, exclusive=True)
        expected_result = Series([np.nan, np.nan, np.nan, np.nan, 0.0, -4.0, -4.0, -4.0, -4.0, 0.0])
        np.allclose(result, expected_result, rtol=1e-6, equal_nan=True)

        result = sum_signed_rolling_deltas(close, open_, 4, exclusive=False)
        expected_result = Series([np.nan, np.nan, np.nan, -1.0, 1.0, -3.0, -3.0, -3.0, -3.0, 1.0])
        np.allclose(result, expected_result, rtol=1e-6, equal_nan=True)
        ```
       rN   r   NrO   r   r   constant)modeconstant_valuesr`   )r3   r"   r
   r   r   r   r   r   astyper   r$   r   r   ra   )r   r   r   r   rolling_openclose_broadcastedsigned_deltassum_signed_deltass           rT   rD   rD   w  s    P 61%F!&u6B3BGL$fg!!W*-|/A/A *\9:M}15<<UC{SQkk rU   c                    | t        t        |             nd} d}| dk(  rddg}| dkD  r| dz  dk(  r4t        dt        | dz              D cg c]  }|dz   	 }}||ddd   z   }nVt        dt        d| dz   z              D cg c]  }|dz   	 }}|j	                         }|j                          ||ddd   z  }|rt        |t              r|t        |      z  S |S c c}w c c}w )a  Symmetric Triangle

    Creates a Symmetric Triangle of given length.

    Parameters:
        n (Int): Array return size
        weighted (bool): Return weighted.

    Returns:
        (List[IntFloat]): List of Symmetric Triangle values.

    Example:
        ```py
        # Default
        symt4 = ta.symmetric_triangle(4)
        # symt4 = [1, 2, 2, 1]

        # Weighted
        wsymt4 = ta.symmetric_triangle(4, weighted=True)
        # wsymt4 = [0.16666667 0.33333333 0.33333333 0.16666667]
        ```
    Nr   rN   r   rO   r   )	rP   r   rQ   mfloorcopypopr   listr   )rH   r   r   r   fronts        rT   rE   rE     s    2 DG1AHAvq61uq5A:$)!VAE]$;<$;qQU$;E<uTrT{*H$)!VC1q5M-B$CD$CqQU$CEDzz|HIIKdd#HJx.#h-''O = Es   CC"wc                       fd}|S )zWeights

    Prepares weights for the dot product

    Parameters:
        w (Array): Input

    Returns:
        (Callable): Weights function for dot product.
    c                     t        |       S )N)r   )rV   r   s    rT   _dotzweights.<locals>._dot  s    1ayrU    )r   r   s   ` rT   rF   rF     s    KrU   c                 B    t        |       t        j                  k  rdS | S )zZero

    Zeros inputs near zero.

    Parameters:
        x (IntFloat): Value to attempt to zero

    Returns:
        (IntFloat): ```0``` or ```x```
    r   )rt   sfltepsilonrX   s    rT   rG   rG     s     A%1,1,rU   ABplot
triangularmethodc                 P   g d}||v r|n|d   }| |z
  }| j                  ||      }|r: |j                          ||dkD     j                         r |j                  d       |r@|j	                  t        t        |j                              j                  t                    S |S )aW  DataFrame Correlation Analysis

    Compares two DataFrames using both their difference and their correlations.

    Parameters:
        A (DataFrame): DataFrame A
        B (DataFrame): DataFrame B
        plot (bool): Create a KDE plot of differences. Default: ```False```
        triangular (bool): Return a Triangular Correlation DataFrame.
            Default: ```False```
        method (str): Correlation methods: ```"pearson"```, ```"kendall"```,
            or ```"spearman"```. Default: ```"pearson"```

    Returns:
        (DataFrame): Correlation DataFrame or a KDE Difference plot
    )pearsonkendallspearmanr   )r   kde)kind)
corrhistanyr   wherer   r   r   r   bool)	r   r   r   r   r   	_r_methodcorr_methodr   rl   s	            rT   r9   r9     s    * 3I"i/&Yq\K q5DVVAkV*F 		q>DII5!||Dfll!34;;DABBMrU   )rN   r   FF)NNN)r   F)NFF)NNNNN)T)NF)FFr   )Ycollections.abcr   	functoolsr   mathr   r   operatorr   sysr   r   numpyr   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r!   r   r   r"   pandasr#   r$   numbar%   pandas_ta._typingr&   r'   r(   r)   r*   r+   pandas_ta.mapsr,   pandas_ta.utils._validater-   r.   r/   r0   r1   r2   r3   r4   r5   __all__r   r6   r8   r7   r:   r;   r<   r=   r>   r?   r@   rP   rA   rB   rC   rD   rE   rF   rG   strr9   r   rU   rT   <module>r      sP   $     "      
    7 $   #
 
 
0 27#$
#$#$#$+/#$ 	#$N -%  -E  -J =AI	II69II"*I IZ8  D D D U  4f  2#U #x #E #R 04XX	X),X X #+X 	Xx& U * <A-
- -48-
-b)F )C )F )\ @D0477!74<77%-7 7 #+7 	7vHe HS HU H6 BF88 8*-8:>88x %*,
,!,	(^,`u  "-H - -* ,1%%%
%$(% % 	%rU   