Search code examples
pine-scriptpine-script-v5

how to comparing rising and falling values


I've had similar problems before and I don't know if there would be a better way to write this code or not. This is the code:

//@version=5
btc = request.security('binance:btcusdt', timeframe.period, close)
btct = ta.rsi(btc, 14)
//plot(btct, color=color.new(#ffc252, 0), title='btc')   
best1 = btct
worst1 = btct

eth = request.security('binance:ethusdt', timeframe.period, close)
etht = ta.rsi(eth, 14)
//plot(etht, color=color.new(#4821f3, 0), title='eth')
if etht > best1
    best1 := etht
if etht < worst1
    worst1 := etht

avg = (btct + etht)/2

rsiavg = ta.rma(avg,14)
coloravg = color.blue
if rsiavg > rsiavg[1]
    coloravg := color.green
if rsiavg < rsiavg[1]
    coloravg := color.red
plot(2.2, color = coloravg, linewidth = 2)

rsi1 = ta.rsi(close,14)
rsiMA = ta.sma(rsi1, 14)

colorrsima = color.blue
if rsiMA > rsiMA[1]
    colorrsima := color.rgb(0, 255, 8)
if rsiMA < rsiMA[1] 
    colorrsima := color.rgb(255, 0, 0)

plotshape(rsiMA, title = "the 4 lookback for ticker rsi",style = shape.circle, location 
= location.bottom, color = colorrsima)

rsicolor = color.blue
if (rsiavg > rsiavg[1]) and (rsiavg > rsiavg[2]) and (rsiMA > rsiMA[1]) 
    rsicolor := color.green
if (rsiavg < rsiavg[1]) and (rsiavg < rsiavg[2]) and (rsiMA < rsiMA[1]) 
    rsicolor := color.red

plot(1, color = rsicolor, linewidth = 2)

enter image description here

The picture uploaded is showing the problem I am having. The output seems to work only some times. The circled areas show that the middle line, which should be filtered by the arguments of previous values to display colors red and green only when previous 2 were increasing or decreasing and previous 1 increasing or decreasing and alternately plotting the color blue. The issue is that they are clearly not doing what is intended at times. Can I please get help in determining my error or if there would be a better way to write this script. I write a lot of novice scripts in Pine that are like this and have encountered this dilemma before. I'd like to filter a leading indicator with a lagging one, but the output often does not produce my intended results.

edit: this addition of code will allow it to compile. As per the second picture, the program is still not doing what I intend. What I want to do is to have the top line be red or green for the two previous bars and have the bottom line be the same for at least one of the previous bars to meet the conditions to change the middle bar from blue to the alternative red or green color. It functions right seemingly most of the time, but there are instances as I highlighted where there may be only one bar the correct color on the top line rather than the intended two before the middle line is changed.

edit 2: So I cleaned up the request security code to what was stated. This did not help the problem I am trying to figure out. the image I am uploading now may show more clearly that what I am intending to code is not processing the way I wish. What I want here is happening the first time (the top bar is on its second green and bottom is at least green, so the middle bar displays green). The second time it is not working the way I intend (The top bar is only red for one bar and the bottom is only red for one bar so it should be blue but instead it is red). Sorry if this isn't clear or is too repetitive, but I appreciate any help i can get. I'm not sure at this point if this is a lack in understanding how to code properly or a possible bug in the processing of the data.

Thanks in advance for any help

edit 3:

I have cleaned up the code as it has been suggested. I will reupload the picture which still displays the problem I am having. The rewritten code is here:

//@version=5 indicator("My script 7")

close_btc = request.security(ticker.standard("binance:btcusdt"), 
timeframe.period, close)   // this is an example with ticker.*()
close_eth = request.security("BINANCE:ETHUSDT", timeframe.period, close)    // this is an example with correct spelling extracted of syminfo.tickerid

rsi_btc   = ta.rsi(close_btc, 14)
rsi_eth   = ta.rsi(close_eth, 14)

// this will find the highest and lowest value of both
best1  = math.max(rsi_btc, rsi_eth)
worst1 = math.min(rsi_btc, rsi_eth)
// sure you could use if <> with it too

rsiAVG = (rsi_btc + rsi_eth) / 2

rmaAVG         = ta.rma(rsiAVG,14)
rmaAVG_rising  = ta.rising(rmaAVG, 1)   // you don't need this but it makes it later pretty easy
rmaAVG_falling = ta.falling(rmaAVG, 1)

// next compare with plain RSI of the symbol
RSI = ta.rsi(close, 14)

rsiSymbol_rma       = ta.rma(RSI, 14)
rsiSymbol_rising    = ta.rising(rsiSymbol_rma, 1)
rsiSymbol_falling   = ta.falling(rsiSymbol_rma, 1)

// plot the rsi for the chosen ticker on top
rsiSymbol_color = rsiSymbol_rising[1] ? color.green : rsiSymbol_falling [1] ? color.red : color.blue
plot(2, "RMA_Symbol_RSI", rsiSymbol_color, 2)

// plot the averaged rsi value as it is on the bottom
rmaAVG_color = rmaAVG_rising[1] ? color.green : rmaAVG_falling[1] ? color.red : color.blue
plot(0, "RMA_Average_RSI", rmaAVG_color, 2)

// compares the normal moveing average of the rsi to the averaged 
rsi ma of multiple symbols displayed in the middle
rmaFiltered_color   = rmaAVG_rising[1] and rmaAVG_rising[2] and 
rsiSymbol_rising[1] ? color.green : rmaAVG_falling[1] and 
rmaAVG_falling[2] and rsiSymbol_falling[1] ?color.red : color.blue   
plot(1, "RMA_AVG_RSI_COMPARE", rmaFiltered_color, 2)   // 1 is for the middle line

so these are examples of when the bottom line is a color for only 1 bar rather than the intended 2, yet the color of the middle bar still changes color. it changes the color too early. The dotted line in the middle shows when the color changes compared with the candles. the color change happens just to the right of the candle. So this shows that it has only been red for one bar not two yet the middle line changes to red.

Thank you for the help making the code more legible and simpler by the way. I'm trying to still determine if this is my lack of understanding of how Pine script works or possibly a bug in the language programing. Of coarse I lean heavily towards my not understanding the way the candles are being read. If anyone has an explanation it is appreciated.

Second image of the problem with the scrypt


Solution

  • I will give you an example for your code and some reference, so you can learn fishing by your self xD

    I have four things to address...

    1. when creating a Ticker without knowing the exact Ticker ID then you should use ticker.*(). to get the ticker you can use syminfo.tickerid and put its output into a label.new() or table.cell_set_text(). In your case you wrote the correct ticker, but it should all be capital letters.
    2. some of your if statements could be shorter via ?: read the howto here. And read how the if statements work here.
    3. I think you should also read the guide for [] in here.
    4. when you combine those if statements with ta.rising(), ta.falling(), and [], it will make your code mutch more easy to handle.

    Here is an example...

    close_btc = request.security(ticker.standard("binance:btcusdt"), timeframe.period, close)   // this is an example with ticker.*()
    close_eth = request.security("BINANCE:ETHUSDT", timeframe.period, close)    // this is an example with correct spelling extracted of syminfo.tickerid
    rsi_btc   = ta.rsi(close_btc, 14)
    rsi_eth   = ta.rsi(close_eth, 14)
    
    // this will find the highest and lowest value of both
    best1  = math.max(rsi_btc, rsi_eth)
    worst1 = math.min(rsi_btc, rsi_eth)
    // sure you could use if <> with it too
    
    rsiAVG = (rsi_btc + rsi_eth) / 2
    
    rmaAVG         = ta.rma(rsiAVG,14)
    rmaAVG_rising  = ta.rising(rmaAVG, 1)   // you don't need this but it makes it later pretty easy
    rmaAVG_falling = ta.falling(rmaAVG, 1)
    
    // your comment: I want previous value to rise or fall for both the last two bars
    rmaAVG_color   = rmaAVG_rising[1] and rmaAVG_rising[2] ? color.green : rmaAVG_falling[1] and rmaAVG_falling[2] ?color.red : color.blue   // this one-liner isn't needed either, but it makes the code mutch better readable
    plot(2.2, "RMA_AVG", rmaAVG_color, 2)   // 2.2 is the top line
    

    the second to last line does this:
    if previous rmaAVG was rising and the bar before that was rising too then it is green. if instead the previous rmaAVG was falling and the bar before that was falling too then it is red. if both if statment failed then it will be blue.

    Here is the same line but mutch more complicated, so you can compare both.

    color rmaAVG_color = if rmaAVG[1] > rmaAVG[2] and rmaAVG[2] > rmaAVG[3]
        color.green
    else if rmaAVG[1] < rmaAVG[2] and rmaAVG[2] < rmaAVG[3]
        color.red
    else
        color.blue