首页 > 解决方案 > Python、matplotlib、ValueError:一个序列的真值是模棱两可的。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()

问题描述

我正在尝试根据其 y 值对线条进行颜色编码。我已经在 pinescript 中完成了这项工作,但是在将其转换为 python 时遇到了麻烦。

如您所见,颜色会根据级别而变化

这是我试图在 python 中重新创建的 pinescript 代码,重要的部分在 ****** 行之间

//@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)

我试图重新创建我的 COLORv 变量,我把它作为一个变量,所以我可以在不同的地块上使用相同的颜色序列,比如收盘价。

这是我在 python 中创建的图表 ,这是它的代码

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()

这是问题开始的地方,这是我尝试过的两件事

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'

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'

两次我都出错说:发生异常:ValueError 系列的真值是模棱两可的。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all() 第 72 行:而 rsi2 > 80:

标签: pythonpandasdataframematplotlibpine-script

解决方案


您可以尝试使用 np.filter 来根据 rsi2 定义颜色(如果尚未完成,则需要在 df 中创建一个列)

col         = 'rsi2'
conditions  = [ df[col]>80, df[col]>60, df[col] >40,  df[col] >20, df[col] >0]
choices     = [ 'red','orange','yellow','green','blue'   ]

df["COLORv"] = np.select(conditions, choices, default=np.nan)

然后图形


推荐阅读