pythonfunctionpython-imaging-librarypine-scripttimeserieschart

"cross", crossunder and crossover function of pinescript in python


I am trying to convert a pine script into python.

in pine I use function cross, crossunder and crossover for checking the if a particular series has crossed the other (say sma and ema crossing) over other to arrive at entry positions for stock.

I am not able to find equivalent function in python.

can anybody help me with that?


Solution

  • I use these ones. Works for me.

    import pandas as pd
    
    def crossover_series(x: pd.Series, y: pd.Series, cross_distance: int = None) -> pd.Series:
        shift_value = 1 if not cross_distance else cross_distance
            return (x > y) & (x.shift(shift_value) < y.shift(shift_value))
    
    def crossunder_series(x: pd.Series, y: pd.Series, cross_distance: int = None) -> pd.Series:
        shift_value = 1 if not cross_distance else cross_distance
        return (x < y) & (x.shift(shift_value) > y.shift(shift_value))