in

Trying to write a pine script in python


I’m trying to convert that pine script

//@version=5
strategy("X test", overlay=true)
length = input.int(14, minval=1)
OverBought = input(80)
OverSold = input(20)
smoothK = 3
smoothD = 3
k = ta.sma(ta.stoch(close, high, low, length), smoothK)
d = ta.sma(k, smoothD)
co = ta.crossover(k,d)
cu = ta.crossunder(k,d)
if (not na(k) and not na(d))
    if (co and k < OverSold)
        strategy.entry("StochLE", strategy.long, comment="StochLE")
    if (cu and k > OverBought)
        strategy.entry("StochSE", strategy.short, comment="StochSE")

into python, i endup with that code

def crossOver(k, d):
    return k.values[-1] > d.values[-1] and k.values[-2] < d.values[-2]

def crossUnder(k, d):
    return k.values[-1] < d.values[-1] and k.values[-2] > d.values[-2]


def stoch( dataframe, low, high, close, length, smooth):
    df = dataframe.copy()
    overSold = 80
    overBought = 20
    low_min  = df[low].rolling( window = length ).min().values[-1]
    high_max = df[high].rolling( window = length ).max().values[-1]
    stoch = 100 * (df[close] - low_min)/(high_max - low_min)
    k = stoch.rolling(window = smooth).mean()
    d = k.rolling(window = smooth).mean()
    co = crossOver(k, d)
    cu = crossUnder(k, d)
    i = 0
    
    if (type(k.values[-1].item()) == int or type(k.values[-1].item()) == float) and (type(d.values[-1].item()) == int or type(d.values[-1].item()) == float):
        if co and k.values[-1].item() < overSold:
            i=1
        if cu and k.values[-1].item() > overBought:
            i=2
    return i

bars = exchange.fetch_ohlcv('BTC/USDT', timeframe="1m", limit=100)
df = pd.DataFrame(bars[-15:-1], columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit="ms")
stochs = stoch( df, 'low', 'high', 'close', 14, 3 )
if(stochs == 1):
    print(df.tail(5))
    print("BUY")
if(stochs == 2):
    print(df.tail(5))
    print("SELL")

But the result isn’t matching at all

is there a way to debug the pine code so i know what is the difference OR can someone catch what am i doing wrong? THANKS



Source: https://stackoverflow.com/questions/70603791/trying-to-write-a-pine-script-in-python

Copy of the official Flutter integration test plugin

Bharti Airtel Stock Jumps Almost 3% Post New JV, Sale of Tower Assets