convert support and resistance code from python to pinescript

Viewed 28

i have python code that finds support and resistance

def is_support(df,i):  
  cond1 = df['Low'][i] < df['Low'][i-1]   
  cond2 = df['Low'][i] < df['Low'][i+1]   
  cond3 = df['Low'][i+1] < df['Low'][i+2]   
  cond4 = df['Low'][i-1] < df['Low'][i-2]  
  return (cond1 and cond2 and cond3 and cond4)

def is_resistance(df,i):  
  cond1 = df['High'][i] > df['High'][i-1]   
  cond2 = df['High'][i] > df['High'][i+1]   
  cond3 = df['High'][i+1] > df['High'][i+2]   
  cond4 = df['High'][i-1] > df['High'][i-2]  
  return (cond1 and cond2 and cond3 and cond4)

def is_far_from_level(value, levels, df):    
  ave =  np.mean(df['High'] - df['Low'])    
  return np.sum([abs(value-level)<ave for _,level in levels])==0

rs=[]
levels=[]
rshl=round(columnobj[["Date","Open","Close","High","Low","Volume"]][::-1],2)
          for i in range(2, rshl.shape[0] - 2):  
            if is_support(rshl, i):    
              low = rshl['Low'][i]
              vol= rshl["Volume"][i]
              #dat=rshl["Date"][i]   
              if is_far_from_level(low, levels, rshl):      
                levels.append((i, low))
                rs.append((i, low,vol,"S"))
                
            elif is_resistance(rshl, i):    
              high = rshl['High'][i]
              vol= rshl["Volume"][i]
              #dat=rshl["Date"][i]       
              if is_far_from_level(high, levels, rshl):      
                levels.append((i, high))
                rs.append((i, high,vol,"R"))

and it produces this dataframe which i then use to plot the supports and resistance manually on tradingview.

    Level Index  Level Price  Level Volume Type
0             6        56.74          9678    R
1            58        56.48          3907    S
2            69        56.89          4741    R
3            82        54.69         13928    S
4            84        55.09         21207    R
5            93        54.49          7475    S
6            95        54.83          5067    R
7           121        54.09          4765    S
8           154        53.50         12702    S
9           188        54.26          5361    R
10          200        53.83          5541    S
11          234        52.72         13529    S
12          237        53.28         28787    R
13          252        52.94         16370    S

I want to convert the code into pinescript so that it automatically adds these levels on the charts on tradingview. Im not familiar with pinescripts as with python so i dont know where to start.

0 Answers
Related