I am trying to color code a line based on its y value. I've done this already in pinescript, but am having trouble translating it to python.
as you can see the color changes based on level
this is the pinescript code I'm trying to recreate in python, the important section is inbetween the ****** lines
//@version=4
study("RSI against price", shorttitle= "RSI/P", resolution= "D")
rsi1= rsi(close, 98)
//ranges
topl= input(89)
lowl= input(38)
totalr= topl - lowl
lr= totalr/10
//strength level inputs
s9= lowl + lr*9
s8= lowl + lr*8
s7= lowl + lr*7
s6= lowl + lr*6
s5= lowl + lr*5
s4= lowl + lr*4
s3= lowl + lr*3
s2= lowl + lr*2
s1= lowl + lr*1
************************************
//level colors
l10= #0000ff
l9= rsi1 > s1 ? #00acff : l10
l8= rsi1 > s2 ? #00faff : l9
l7= rsi1 > s3 ? #00ff69 : l8
l6= rsi1 > s4 ? #00ff00 : l7
l5= rsi1 > s5 ? #ffff00 : l6
l4= rsi1 > s6 ? #ffcb00 : l5
l3= rsi1 > s7 ? #ff8a00 : l4
l2= rsi1 > s8 ? #ff4100 : l3
COLORv= rsi1 > s9 ? #ff0000 : l2
//plots
plot(rsi1, color=COLORv, linewidth=3)
*************************************
plot(topl)
plot(lowl)
plot(lowl + lr*9, color= #ffffff, transp = 75)
plot(lowl + lr*8, color= #ffffff, transp = 75)
plot(lowl + lr*7, color= #ffffff, transp = 75)
plot(lowl + lr*6, color= #ffffff, transp = 75)
plot(lowl + lr*5, color= #ffffff, transp = 50)
plot(lowl + lr*4, color= #ffffff, transp = 75)
plot(lowl + lr*3, color= #ffffff, transp = 75)
plot(lowl + lr*2, color= #ffffff, transp = 75)
plot(lowl + lr*1, color= #ffffff, transp = 75)
Im trying to recreate my COLORv variable, I have it as a variable so I can this same color sequence on different plots, like the closing price.
Here is the chart I've created in python and here is the code for it
import matplotlib.pyplot as plt
import pandas as pd
import pandas_ta as ta
import numpy as np
#dataframe
df = pd.read_csv('BTC1DRSI.csv')
#variables
x = df['time']
y = df['close']
# RSI
rsi = df['RSI']
# xx for equations;
xx = np.array(range(0,4086))
# TOP LINE; coordinates
p21 = [2, 86]
p22 = [4081, 77.5]
xl2 = [p21[0], p22[0]]
yl2 = [p21[1], p22[1]]
#SLOPE1
slope2 = (p22[1] - p21[1]) / (p22[0] - p21[0])
# TOP LINE
topl = slope2 * xx + p21[1]
# BOTTOM LINE; coordinates
p11 = [2, 44.4]
p12 = [4081, 36.6]
xl1 = [p11[0], p12[0]]
yl1 = [p11[1], p12[1]]
#SLOPE2
slope1 = (p12[1] - p11[1]) / (p12[0] - p11[0])
# LOW lINE
lowl = slope1 * xx + p11[1]
# FIX to 0 - 100
rang = topl - lowl
yyy = rsi - lowl
rsi2 = (yyy / rang) *100
# # RANGES
totalr= 100 - 0
lr= totalr/10
# STRENGTH LEVEL EQUATIONS
s10 = 0 + lr * 10
s9 = 0 + lr * 9
s8 = 0 + lr * 8
s7 = 0 + lr * 7
s6 = 0 + lr * 6
s5 = 0 + lr * 5
s4 = 0 + lr * 4
s3 = 0 + lr * 3
s2 = 0 + lr * 2
s1 = 0 + lr * 1
s0 = 0 + lr * 0
# PLOT LEVELS
plt.axhline(y = s10, color='silver', linestyle='-')
plt.axhline(y = s9, color='silver', linestyle='-')
plt.axhline(y = s8, color='silver', linestyle='-')
plt.axhline(y = s7, color='silver', linestyle='-')
plt.axhline(y = s6, color='silver', linestyle='-')
plt.axhline(y = s5, color='silver', linestyle='-')
plt.axhline(y = s4, color='silver', linestyle='-')
plt.axhline(y = s3, color='silver', linestyle='-')
plt.axhline(y = s2, color='silver', linestyle='-')
plt.axhline(y = s1, color='silver', linestyle='-')
plt.axhline(y = s0, color='silver', linestyle='-')
COLORv = red
# plots
plt.plot(x, rsi2, color = COLORv)
plt.tight_layout
plt.show()
Here's where the problem starts, these are the 2 things Ive tried
if rsi2 > s8:
COLORv = 'red'
else:
if rsi2 > s6:
COLORv = 'orange'
else:
if rsi2 > s4:
COLORv = 'yellow'
else:
if rsi2 > s2:
COLORv = 'green'
else:
COLORv = 'blue'
and
while rsi2 > 80:
COLORv = 'red'
while 80 > rsi2 > 60:
COLORv = 'orange'
while 60 > rsi2 > 40:
COLORv = 'yellow'
while 40 > rsi2 > 20:
COLORv = 'green'
while 20 > rsi2 > 0:
COLORv = 'blue'
BOTH times ive gotten in error saying : Exception has occurred: ValueError The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() line 72: while rsi2 > 80: