ulle73
Pine Script Rookie
Pine Script Rookie
Posts: 1
Joined: August 30th, 2023

change indicator in script

Hi, im pretty new to pine. I found this screener but would like to change the indicator inside of the code, cant get the code to work on my own. Should be very simple to anyone with basic coding knowledge.This is the full code, i want to change RSI to Zscore,

original rsi code:

Code: Select all

//@version=5
indicator('Multi-Timeframe Indicator', overlay=true)

////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//                                   INPUTS                                   \\
////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

tf_multip1  = input.int( 1, minval= 1, maxval= 1440, 
             title = "TimeFrame", group = "TimeFrame", inline="1")
tf_period1  = input.string(title="", defval= "Chart TF", 
             options=["Chart TF", "Minute", "Hour", "Day", "Week", "Month"], 
             group = "TimeFrame", inline="1")

tf_multip2  = input.int( 30, minval= 1, maxval= 1440, 
             title = "TimeFrame", group = "TimeFrame", inline="2")
tf_period2  = input.string(title="", defval= "Minute", 
             options=["Chart TF", "Minute", "Hour", "Day", "Week", "Month"], 
             group = "TimeFrame", inline="2")

tf_multip3  = input.int( 1, minval= 1, maxval= 1440, 
             title = "TimeFrame", group = "TimeFrame", inline="3")
tf_period3  = input.string(title="", defval= "Day", 
             options=["Chart TF", "Minute", "Hour", "Day", "Week", "Month"], 
             group = "TimeFrame", inline="3")

tf_multip4  = input.int( 2, minval= 1, maxval= 1440, 
             title = "TimeFrame", group = "TimeFrame", inline="4")
tf_period4  = input.string(title="", defval= "Week", 
             options=["Chart TF", "Minute", "Hour", "Day", "Week", "Month"], 
             group = "TimeFrame", inline="4")


// Styling & Coloring
// Table Position
in_table_pos = input.string(title="Table Location  ", defval= "Top Right", 
     options =["Top Right",  "Middle Right",  "Bottom Right", 
               "Top Center", "Middle Center", "Bottom Center", 
               "Top Left",   "Middle Left",   "Bottom Left"], 
     group= "Table Styling", inline= "1")

// Table Size
in_table_size = input.string(title="Table Size    ", defval="Small", 
     options=["Auto",  "Huge",  "Large", "Normal", "Small", "Tiny"], 
     group= "Table Styling" , inline= "2")

// Color
column_title_bgcol  = input.color(color.gray  , title="Column Title Color", 
                      group = "Table Styling" , inline="3")
cell_bgcol          = input.color(#aaaaaa     , title="Cell Color    ", 
                      group = "Table Styling" , inline="4")
cell_txtcol         = input.color(color.white , title="Text Color       ", 
                      group = "Table Styling" , inline="5")


// Indicators
// RSI
rsi_len        = input.int(  14, minval=1, title = "RSI Length   ",   
                 group = "Indicators - RSI", inline= "1")
rsi_ob         = input.float(70, title = "RSI Overbought ", 
                 group = "Indicators - RSI", inline= "2")
rsi_ob_col     = input.color(color.red, title=" ", 
                 group = "Indicators - RSI", inline= "2")
rsi_os         = input.float(30, title = "RSI Oversold    ", 
                 group = "Indicators - RSI", inline= "3")
rsi_os_col     = input.color(color.green, title=" ", 
                 group = "Indicators - RSI", inline= "3")


////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//                                   SYMBOLS                                  \\
////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// Flag
u01 = input.bool(true,  title = "", group = 'Symbols', inline = 's01')
u02 = input.bool(true,  title = "", group = 'Symbols', inline = 's02')
u03 = input.bool(true,  title = "", group = 'Symbols', inline = 's03')
u04 = input.bool(true,  title = "", group = 'Symbols', inline = 's04')
u05 = input.bool(true,  title = "", group = 'Symbols', inline = 's05')
u06 = input.bool(true,  title = "", group = 'Symbols', inline = 's06')
u07 = input.bool(true,  title = "", group = 'Symbols', inline = 's07')
u08 = input.bool(true,  title = "", group = 'Symbols', inline = 's08')
u09 = input.bool(true,  title = "", group = 'Symbols', inline = 's09')
u10 = input.bool(true,  title = "", group = 'Symbols', inline = 's10')

// Symbols
s01 = input.symbol('XRPUSDT',  group = 'Symbols', inline = 's01')
s02 = input.symbol('BTCUSDT',  group = 'Symbols', inline = 's02')
s03 = input.symbol('DOGEUSDT', group = 'Symbols', inline = 's03')
s04 = input.symbol('BNBUSDT',  group = 'Symbols', inline = 's04')
s05 = input.symbol('ETHUSDT',  group = 'Symbols', inline = 's05')
s06 = input.symbol('ADAUSDT',  group = 'Symbols', inline = 's06')
s07 = input.symbol('XRPBTC',   group = 'Symbols', inline = 's07')
s08 = input.symbol('DOGEBTC',  group = 'Symbols', inline = 's08')
s09 = input.symbol('TRXUSDT',  group = 'Symbols', inline = 's09')
s10 = input.symbol('BTCBUSD',  group = 'Symbols', inline = 's10')


////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//                                CALCULATIONS                                \\
////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// Timeframe for security functions
TF(tf_period, tf_multip) => 
    switch tf_period 
        "Minute" => str.tostring(tf_multip)
        "Hour"   => str.tostring(tf_multip*60)
        "Day"    => str.tostring(tf_multip) + "D"
        "Week"   => str.tostring(tf_multip) + "W"
        "Month"  => str.tostring(tf_multip) + "M"
        => timeframe.period


// Timeframe shortcut form
tf_form( tf_period, tf_multip) =>
    if tf_period != "Chart TF"
        switch tf_period
            "Minute" => str.tostring(tf_multip)  + "min"
            "Hour"   => str.tostring(tf_multip)  + "H"
            "Day"    => str.tostring(tf_multip)  + "D"
            "Week"   => str.tostring(tf_multip)  + "W"
            "Month"  => str.tostring(tf_multip)  + "M"
    
    else 
        switch 
            timeframe.isminutes and  timeframe.multiplier % 60 != 0 
                                 => str.tostring(timeframe.multiplier)    + "min"
                                 
            timeframe.isminutes and  timeframe.multiplier % 60 == 0 
                                 => str.tostring(timeframe.multiplier/60) + "H"
                                 
            timeframe.isdaily    => str.tostring(timeframe.multiplier)    + "D"
            timeframe.isweekly   => str.tostring(timeframe.multiplier)    + "W"
            timeframe.ismonthly  => str.tostring(timeframe.multiplier)    + "M"


// TimeFrame OutPut
tf_form = array.from(tf_form(tf_period1, tf_multip1),  tf_form(tf_period2, tf_multip2),
                     tf_form(tf_period3, tf_multip3),  tf_form(tf_period4, tf_multip4))


// Get Table Position
table_pos(p) => 
    switch p
        "Top Right"     => position.top_right     
        "Middle Right"  => position.middle_right   
        "Bottom Right"  => position.bottom_right  
        "Top Center"    => position.top_center    
        "Middle Center" => position.middle_center
        "Bottom Center" => position.bottom_center
        "Top Left"      => position.top_left      
        "Middle Left"   => position.middle_left 
        => position.bottom_left


// Get Table Size
table_size(s) => 
    switch s
        "Auto"   => size.auto   
        "Huge"   => size.huge   
        "Large"  => size.large  
        "Normal" => size.normal 
        "Small"  => size.small
        => size.tiny


// Get only symbol
only_symbol(s) => 
    array.get(str.split(s, ":"), 1)


screener_func(flag) =>
    out_screener_func = array.new_string(2, string(na))
    
    if flag 
        // Price
        price = math.round_to_mintick(close)
        array.set(out_screener_func, 0, str.tostring(price))
        
        // RSI
        rsi = ta.rsi(close, rsi_len)
        array.set(out_screener_func, 1, str.tostring(rsi, "#.##"))
        
    out_screener_func


////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//                               Importing Data                               \\
////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
s01_TF1 = request.security(ticker.modify(s01, syminfo.session), TF(tf_period1, tf_multip1), screener_func(u01))
s02_TF1 = request.security(ticker.modify(s02, syminfo.session), TF(tf_period1, tf_multip1), screener_func(u02))
s03_TF1 = request.security(ticker.modify(s03, syminfo.session), TF(tf_period1, tf_multip1), screener_func(u03))
s04_TF1 = request.security(ticker.modify(s04, syminfo.session), TF(tf_period1, tf_multip1), screener_func(u04))
s05_TF1 = request.security(ticker.modify(s05, syminfo.session), TF(tf_period1, tf_multip1), screener_func(u05))
s06_TF1 = request.security(ticker.modify(s06, syminfo.session), TF(tf_period1, tf_multip1), screener_func(u06))
s07_TF1 = request.security(ticker.modify(s07, syminfo.session), TF(tf_period1, tf_multip1), screener_func(u07))
s08_TF1 = request.security(ticker.modify(s08, syminfo.session), TF(tf_period1, tf_multip1), screener_func(u08))
s09_TF1 = request.security(ticker.modify(s09, syminfo.session), TF(tf_period1, tf_multip1), screener_func(u09))
s10_TF1 = request.security(ticker.modify(s10, syminfo.session), TF(tf_period1, tf_multip1), screener_func(u10))

s01_TF2 = request.security(ticker.modify(s01, syminfo.session), TF(tf_period2, tf_multip2), screener_func(u01))
s02_TF2 = request.security(ticker.modify(s02, syminfo.session), TF(tf_period2, tf_multip2), screener_func(u02))
s03_TF2 = request.security(ticker.modify(s03, syminfo.session), TF(tf_period2, tf_multip2), screener_func(u03))
s04_TF2 = request.security(ticker.modify(s04, syminfo.session), TF(tf_period2, tf_multip2), screener_func(u04))
s05_TF2 = request.security(ticker.modify(s05, syminfo.session), TF(tf_period2, tf_multip2), screener_func(u05))
s06_TF2 = request.security(ticker.modify(s06, syminfo.session), TF(tf_period2, tf_multip2), screener_func(u06))
s07_TF2 = request.security(ticker.modify(s07, syminfo.session), TF(tf_period2, tf_multip2), screener_func(u07))
s08_TF2 = request.security(ticker.modify(s08, syminfo.session), TF(tf_period2, tf_multip2), screener_func(u08))
s09_TF2 = request.security(ticker.modify(s09, syminfo.session), TF(tf_period2, tf_multip2), screener_func(u09))
s10_TF2 = request.security(ticker.modify(s10, syminfo.session), TF(tf_period2, tf_multip2), screener_func(u10))

s01_TF3 = request.security(ticker.modify(s01, syminfo.session), TF(tf_period3, tf_multip3), screener_func(u01))
s02_TF3 = request.security(ticker.modify(s02, syminfo.session), TF(tf_period3, tf_multip3), screener_func(u02))
s03_TF3 = request.security(ticker.modify(s03, syminfo.session), TF(tf_period3, tf_multip3), screener_func(u03))
s04_TF3 = request.security(ticker.modify(s04, syminfo.session), TF(tf_period3, tf_multip3), screener_func(u04))
s05_TF3 = request.security(ticker.modify(s05, syminfo.session), TF(tf_period3, tf_multip3), screener_func(u05))
s06_TF3 = request.security(ticker.modify(s06, syminfo.session), TF(tf_period3, tf_multip3), screener_func(u06))
s07_TF3 = request.security(ticker.modify(s07, syminfo.session), TF(tf_period3, tf_multip3), screener_func(u07))
s08_TF3 = request.security(ticker.modify(s08, syminfo.session), TF(tf_period3, tf_multip3), screener_func(u08))
s09_TF3 = request.security(ticker.modify(s09, syminfo.session), TF(tf_period3, tf_multip3), screener_func(u09))
s10_TF3 = request.security(ticker.modify(s10, syminfo.session), TF(tf_period3, tf_multip3), screener_func(u10))

s01_TF4 = request.security(ticker.modify(s01, syminfo.session), TF(tf_period4, tf_multip4), screener_func(u01))
s02_TF4 = request.security(ticker.modify(s02, syminfo.session), TF(tf_period4, tf_multip4), screener_func(u02))
s03_TF4 = request.security(ticker.modify(s03, syminfo.session), TF(tf_period4, tf_multip4), screener_func(u03))
s04_TF4 = request.security(ticker.modify(s04, syminfo.session), TF(tf_period4, tf_multip4), screener_func(u04))
s05_TF4 = request.security(ticker.modify(s05, syminfo.session), TF(tf_period4, tf_multip4), screener_func(u05))
s06_TF4 = request.security(ticker.modify(s06, syminfo.session), TF(tf_period4, tf_multip4), screener_func(u06))
s07_TF4 = request.security(ticker.modify(s07, syminfo.session), TF(tf_period4, tf_multip4), screener_func(u07))
s08_TF4 = request.security(ticker.modify(s08, syminfo.session), TF(tf_period4, tf_multip4), screener_func(u08))
s09_TF4 = request.security(ticker.modify(s09, syminfo.session), TF(tf_period4, tf_multip4), screener_func(u09))
s10_TF4 = request.security(ticker.modify(s10, syminfo.session), TF(tf_period4, tf_multip4), screener_func(u10))


////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//                                   OutPut                                   \\
////////////////////////////// ~~~~~~~~~~~~~~~~~~ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// Set Table
var tbl = table.new(table_pos(in_table_pos),  7,  12, frame_color = #151715, 
         frame_width=1, border_width=1, border_color=color.new(color.white, 100))


// Set Up Screener Matrix
screener_mtx = matrix.new<string>(10,  6, na)


// Fill Up Matrix Cells 
mtx(mtxName, row, symbol, s_TF1, s_TF2, s_TF3, s_TF4)=>
    if not na(array.get(s_TF1, 0))
        matrix.set(mtxName, row,  0, symbol    ) //Symbol
        matrix.set(mtxName, row,  1, array.get(s_TF1, 0)) //Price
        
        matrix.set(mtxName, row,  2, array.get(s_TF1, 1)) //RSI
        matrix.set(mtxName, row,  3, array.get(s_TF2, 1))
        matrix.set(mtxName, row,  4, array.get(s_TF3, 1))
        matrix.set(mtxName, row,  5, array.get(s_TF4, 1))


if barstate.islast 
    mtx(screener_mtx, 0, only_symbol(s01), s01_TF1, s01_TF2, s01_TF3, s01_TF4)
    mtx(screener_mtx, 1, only_symbol(s02), s02_TF1, s02_TF2, s02_TF3, s02_TF4)
    mtx(screener_mtx, 2, only_symbol(s03), s03_TF1, s03_TF2, s03_TF3, s03_TF4)
    mtx(screener_mtx, 3, only_symbol(s04), s04_TF1, s04_TF2, s04_TF3, s04_TF4)
    mtx(screener_mtx, 4, only_symbol(s05), s05_TF1, s05_TF2, s05_TF3, s05_TF4)
    mtx(screener_mtx, 5, only_symbol(s06), s06_TF1, s06_TF2, s06_TF3, s06_TF4)
    mtx(screener_mtx, 6, only_symbol(s07), s07_TF1, s07_TF2, s07_TF3, s07_TF4)
    mtx(screener_mtx, 7, only_symbol(s08), s08_TF1, s08_TF2, s08_TF3, s08_TF4)
    mtx(screener_mtx, 8, only_symbol(s09), s09_TF1, s09_TF2, s09_TF3, s09_TF4)
    mtx(screener_mtx, 9, only_symbol(s10), s10_TF1, s10_TF2, s10_TF3, s10_TF4)
    
    //Remove NaN rows
    q= 0 
    while q <= matrix.rows(screener_mtx) - 1 and matrix.rows(screener_mtx) > 1
        if na(matrix.get(screener_mtx, q, 0))
            matrix.remove_row(screener_mtx, q)
            q := q
        else 
            q := q + 1

    // Fill up Table
    // Columns title 

    // Merge Columns title cells
  
    table.cell(tbl,  0, 0, 'Symbol',     bgcolor   = column_title_bgcol, 
                 text_color = cell_txtcol, text_size = table_size(in_table_size))
    table.cell(tbl,  0, 1, '', bgcolor   = column_title_bgcol)
    
    table.cell(tbl,  1, 0, 'Price',      bgcolor   = column_title_bgcol, 
                 text_color = cell_txtcol, text_size = table_size(in_table_size))
    table.cell(tbl,  1, 1, '', bgcolor   = column_title_bgcol)
    
    table.cell(tbl,  3, 0, 'RSI',        bgcolor   = column_title_bgcol, text_color = cell_txtcol, text_size = table_size(in_table_size))
    table.cell(tbl,  4, 0, '', bgcolor   = column_title_bgcol)
    table.cell(tbl,  5, 0, '', bgcolor   = column_title_bgcol)
    table.cell(tbl,  6, 0, '', bgcolor   = column_title_bgcol)

    table.merge_cells(tbl,  0, 0,  0,  1)
    table.merge_cells(tbl,  1, 0,  1,  1)
    table.merge_cells(tbl,  3, 0,  6,  0)
    

    

    // TimeFrame 
    for j = 0 to 3
        table.cell(tbl,  j + 3, 1, array.get(tf_form, j), 
             text_halign = text.align_center, 
             bgcolor     = column_title_bgcol, 
             text_color  = cell_txtcol, text_size = table_size(in_table_size))

    // Output Values
    max_row = 1
    for i = 0 to matrix.rows(screener_mtx) - 1
    
        if not na(matrix.get(screener_mtx, i, 1)) 
            // Symbol 
            table.cell(tbl, 0, i + 2, matrix.get(screener_mtx, i, 0), 
                 text_halign = text.align_center, bgcolor = column_title_bgcol, 
                 text_color = cell_txtcol, text_size = table_size(in_table_size))
        
            // Price
            table.cell(tbl, 1, i + 2, matrix.get(screener_mtx, i, 1), 
                 text_halign = text.align_center, bgcolor = cell_bgcol, 
                 text_color = cell_txtcol, text_size = table_size(in_table_size))
            
            for j = 0 to 3
                rsi_col = str.tonumber(matrix.get(screener_mtx, i, j + 2)) > rsi_ob ? rsi_ob_col : 
                          str.tonumber(matrix.get(screener_mtx, i, j + 2)) < rsi_os ? rsi_os_col : cell_bgcol
                
                table.cell(tbl, j + 3, i + 2, matrix.get(screener_mtx, i, j + 2), 
                         text_halign = text.align_center, bgcolor = rsi_col, 
                         text_color = cell_txtcol, text_size = table_size(in_table_size))
                         
            max_row := max_row + 1

    // Separator 
    for k = 0 to max_row
        table.cell(tbl, 2, k, "",  bgcolor = column_title_bgcol, text_size = size.tiny)

    table.merge_cells(tbl,  2, 0,  2,  max_row)
    



Here is Zscore indicator code:

Code: Select all

//@version=5
indicator("Z-Score Indicator", overlay=true)

// Inputs
price = close
length = input(20, title="Length")
ZavgLength = input(20, title="Z-Avg Length")

// Initialize values
oneSD = ta.stdev(price, length)
avgClose = ta.sma(price, length)
ofoneSD = oneSD * price[1]
Zscorevalue = ((price - avgClose) / oneSD)
avgZv = ta.sma(Zscorevalue, 20)

// Compute and plot Z-Score
avgZscore = ta.sma(Zscorevalue, ZavgLength)
Zscore = ((price - avgClose) / oneSD)

// Buy Conditions, currently set at 0.75
conditionbuy = ta.crossover(Zscore, avgZscore) and avgZscore < -1.5
conditionsell = ta.crossunder(Zscore, avgZscore) and avgZscore > 1.5

// Plot Buy and Sell Arrows
plotshape(conditionbuy, style=shape.triangleup, color=color.rgb(0, 167, 75), location=location.belowbar, size=size.large)
plotshape(conditionsell, style=shape.triangledown, color=color.rgb(172, 34, 0), location=location.abovebar, size=size.large)

Return to “Request Scripts”