如何在TradingView表格中实现数值评分系统并展示总分?
TradingView评分系统:数值转换、求和与表格展示实现
一、将布尔条件转换为数值评分
把AFour/AThree等布尔条件转换为0-4的数值评分,用Pine Script的三元运算符可简化代码,同时生成可直接计算的数值变量:
// 生成A的数值评分 AScore = 4 if AFour else 3 if AThree else 2 if ATwo else 1 if AOne else 0 // 生成B的数值评分 BScore = 4 if BFour else 3 if BThree else 2 if BTwo else 1 if BOne else 0
二、计算A与B的总分
直接对两个数值评分求和即可得到总分:
TotalScore = AScore + BScore
三、整合表格展示(单个评分+总分)
保留原有的表格样式逻辑,用数值变量简化单个评分的文本生成,同时新增总分的表格单元格,完整示例代码如下:
// 基础指标定义(替换为你的实际参数) EMA5 = ta.ema(close, 5) EMA13 = ta.ema(close, 13) A = close // 示例参数A B = open // 示例参数B // A评分条件 AFour = (A>EMA5 and EMA5>EMA13) AThree = (A>EMA5 and EMA5<EMA13) ATwo = (A<EMA5 and EMA5>EMA13) AOne = (A<EMA5 and EMA5<EMA13) // B评分条件 BFour = (B>EMA5 and EMA5>EMA13) BThree = (B>EMA5 and EMA5<EMA13) BTwo = (B<EMA5 and EMA5>EMA13) BOne = (B<EMA5 and EMA5<EMA13) // 转换为数值评分 AScore = 4 if AFour else 3 if AThree else 2 if ATwo else 1 if AOne else 0 BScore = 4 if BFour else 3 if BThree else 2 if BTwo else 1 if BOne else 0 // 计算总分 TotalScore = AScore + BScore // 创建表格 indicatorTable = table.new(position = position.bottom_right, columns = 4, rows = 2) // 表格表头(可选) table.cell(table_id = indicatorTable, column = 1, row = 0, text = "指标", bgcolor = color.black, text_color = color.white) table.cell(table_id = indicatorTable, column = 2, row = 0, text = "A评分", bgcolor = color.black, text_color = color.white) table.cell(table_id = indicatorTable, column = 3, row = 0, text = "B评分", bgcolor = color.black, text_color = color.white) table.cell(table_id = indicatorTable, column = 4, row = 0, text = "总分", bgcolor = color.black, text_color = color.white) // 展示A评分 table.cell(table_id = indicatorTable, column = 2, row = 1, text = str.tostring(AScore), bgcolor = AFour ? color.blue : AThree ? color.green : ATwo ? color.orange : AOne ? color.red : #F7E98E, text_color = AFour or AThree ? color.white : color.black) // 展示B评分 table.cell(table_id = indicatorTable, column = 3, row = 1, text = str.tostring(BScore), bgcolor = BFour ? color.blue : BThree ? color.green : BTwo ? color.orange : BOne ? color.red : #F7E98E, text_color = BFour or BThree ? color.white : color.black) // 展示总分 table.cell(table_id = indicatorTable, column = 4, row = 1, text = str.tostring(TotalScore), bgcolor = color.purple, text_color = color.white)
关键说明
- 用三元运算符替代嵌套
if,既简化了数值评分生成逻辑,也让表格背景色的判断更紧凑 - 数值评分变量
AScore/BScore可直接参与运算,解决了无法计算总分的问题 - 总分单元格的位置、样式可根据需求自行调整
内容的提问来源于stack exchange,提问作者finallynitin
相关产品推荐
相关产品推荐

