不同时间周期下Heikin Ashi蜡烛色点显示异常求助
多时间周期Heikin Ashi蜡烛颜色显示异常问题
我编写了以下Pine Script代码,意图通过表格展示不同时间周期下的Heikin Ashi蜡烛颜色,但只有当前图表时间周期显示正常,其他周期的颜色存在错误:
//@version=5 indicator(title="Relative Strength Index", shorttitle="RSI", format=format.price) // User inputs for timeframes tf1 = input.string("1", title="Timeframe 1", options=["1", "3", "5", "15", "30", "45", "60", "120", "180", "240", "D", "W", "M", "3M", "6M", "12M"]) tf2 = input.string("3", title="Timeframe 2", options=["1", "3", "5", "15", "30", "45", "60", "120", "180", "240", "D", "W", "M", "3M", "6M", "12M"]) tf3 = input.string("5", title="Timeframe 3", options=["1", "3", "5", "15", "30", "45", "60", "120", "180", "240", "D", "W", "M", "3M", "6M", "12M"]) tf4 = input.string("15", title="Timeframe 4", options=["1", "3", "5", "15", "30", "45", "60", "120", "180", "240", "D", "W", "M", "3M", "6M", "12M"]) tf5 = input.string("30", title="Timeframe 5", options=["1", "3", "5", "15", "30", "45", "60", "120", "180", "240", "D", "W", "M", "3M", "6M", "12M"]) tf6 = input.string("60", title="Timeframe 5", options=["1", "3", "5", "15", "30", "45", "60", "120", "180", "240", "D", "W", "M", "3M", "6M", "12M"]) tf7 = input.string("D", title="Timeframe 5", options=["1", "3", "5", "15", "30", "45", "60", "120", "180", "240", "D", "W", "M", "3M", "6M", "12M"]) ha_open = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, open) ha_high = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, high) ha_low = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, low) ha_close = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close) cal_ha(ha_open,ha_high,ha_low,ha_close) => ha0 = ha_open < ha_close ? "🟢" : "🔴" ha1 = ha_open[1] < ha_close[1] ? "🟢" : "🔴" ha2 = ha_open[2] < ha_close[2] ? "🟢" : "🔴" ha3 = ha_open[3] < ha_close[3] ? "🟢" : "🔴" ha4 = ha_open[4] < ha_close[4] ? "🟢" : "🔴" [ha0, ha1, ha2, ha3, ha4] [ha10, ha11, ha12, ha13, ha14] = request.security(syminfo.tickerid, tf1, cal_ha(ha_open,ha_high,ha_low,ha_close)) [ha20, ha21, ha22, ha23, ha24] = request.security(syminfo.tickerid, tf2, cal_ha(ha_open,ha_high,ha_low,ha_close)) [ha30, ha31, ha32, ha33, ha34] = request.security(syminfo.tickerid, tf3, cal_ha(ha_open,ha_high,ha_low,ha_close)) [ha40, ha41, ha42, ha43, ha44] = request.security(syminfo.tickerid, tf4, cal_ha(ha_open,ha_high,ha_low,ha_close)) [ha50, ha51, ha52, ha53, ha54] = request.security(syminfo.tickerid, tf5, cal_ha(ha_open,ha_high,ha_low,ha_close)) [ha60, ha61, ha62, ha63, ha64] = request.security(syminfo.tickerid, tf6, cal_ha(ha_open,ha_high,ha_low,ha_close)) [ha70, ha71, ha72, ha73, ha74] = request.security(syminfo.tickerid, tf7, cal_ha(ha_open,ha_high,ha_low,ha_close)) // Add the results to the table table.cell(macdTable, 1, 0, text=tf1 + "m", text_color=color.white, text_size = textSize) table.cell(macdTable, 1, 1, text=ha14+ha13+ha12+ha11+ha10, text_color=color.black, text_size = textSize) table.cell(macdTable, 2, 0, text=tf2 + "m", text_color=color.white, text_size = textSize) table.cell(macdTable, 2, 1, text=ha24+ha23+ha22+ha21+ha20, text_color=color.black, text_size = textSize) table.cell(macdTable, 3, 0, text=tf3 + "m", text_color=color.white, text_size = textSize) table.cell(macdTable, 3, 1, text=ha34+ha33+ha32+ha31+ha30, text_color=color.black, text_size = textSize) table.cell(macdTable, 4, 0, text=tf4 + "m", text_color=color.white, text_size = textSize) table.cell(macdTable, 4, 1, text=ha44+ha43+ha42+ha41+ha40, text_color=color.black, text_size = textSize) table.cell(macdTable, 5, 0, text=tf5 + "m", text_color=color.white, text_size = textSize) table.cell(macdTable, 5, 1, text=ha54+ha53+ha52+ha51+ha50, text_color=color.black, text_size = textSize) table.cell(macdTable, 6, 0, text=tf6 + "m", text_color=color.white, text_size = textSize) table.cell(macdTable, 6, 1, text=ha64+ha63+ha62+ha61+ha60, text_color=color.black, text_size = textSize) table.cell(macdTable, 7, 0, text=tf7 + "m", text_color=color.white, text_size = textSize) table.cell(macdTable, 7, 1, text=ha74+ha73+ha72+ha71+ha70, text_color=color.black, text_size = textSize)
问题根源
- 跨周期数据传递错误:调用
request.security获取其他周期数据时,传递的ha_open/ha_high等是当前图表周期的Heikin Ashi数据,而非目标周期的,导致颜色判断基于错误的数据源。 - 历史引用逻辑错位:
cal_ha函数中的[1]/[2]等历史引用,在跨周期调用时应指向目标周期的历史K线,但原代码使用的是当前周期的历史数据,逻辑完全错误。 - 未初始化表格对象:原代码中
macdTable和textSize变量未定义,运行时会直接报错。 - 输入框标题重复:多个时间周期输入框标题均为
Timeframe 5,导致配置混乱。
修复后的完整代码
//@version=5 indicator(title="Multi-Timeframe Heikin Ashi Color Table", shorttitle="HA Color Table", overlay=false) // 表格样式配置 textSize = input.text_size(title="Text Size", defval=text_size.small) tableBgColor = input.color(title="Table Background", defval=color.new(color.blue, 0)) textColor = input.color(title="Text Color", defval=color.white) // 时间周期输入(修正重复标题) tf_list = ["1", "3", "5", "15", "30", "45", "60", "120", "180", "240", "D", "W", "M", "3M", "6M", "12M"] tf1 = input.string("1", title="Timeframe 1", options=tf_list) tf2 = input.string("3", title="Timeframe 2", options=tf_list) tf3 = input.string("5", title="Timeframe 3", options=tf_list) tf4 = input.string("15", title="Timeframe 4", options=tf_list) tf5 = input.string("30", title="Timeframe 5", options=tf_list) tf6 = input.string("60", title="Timeframe 6", options=tf_list) tf7 = input.string("D", title="Timeframe 7", options=tf_list) // 定义Heikin Ashi颜色计算函数(在目标周期内执行) get_ha_colors() => // 获取当前周期的Heikin Ashi数据 ha_ticker = ticker.heikinashi(syminfo.tickerid) ha_open = request.security(ha_ticker, timeframe.period, open) ha_close = request.security(ha_ticker, timeframe.period, close) // 计算最近5根K线的颜色 col0 = ha_open < ha_close ? "🟢" : "🔴" col1 = ha_open[1] < ha_close[1] ? "🟢" : "🔴" col2 = ha_open[2] < ha_close[2] ? "🟢" : "🔴" col3 = ha_open[3] < ha_close[3] ? "🟢" : "🔴" col4 = ha_open[4] < ha_close[4] ? "🟢" : "🔴" // 返回从旧到新的颜色序列(符合表格展示逻辑) [col4, col3, col2, col1, col0] // 获取各时间周期的HA颜色序列 colors_tf1 = request.security(syminfo.tickerid, tf1, get_ha_colors()) colors_tf2 = request.security(syminfo.tickerid, tf2, get_ha_colors()) colors_tf3 = request.security(syminfo.tickerid, tf3, get_ha_colors()) colors_tf4 = request.security(syminfo.tickerid, tf4, get_ha_colors()) colors_tf5 = request.security(syminfo.tickerid, tf5, get_ha_colors()) colors_tf6 = request.security(syminfo.tickerid, tf6, get_ha_colors()) colors_tf7 = request.security(syminfo.tickerid, tf7, get_ha_colors()) // 初始化表格 var macdTable = table.new(position.top_right, 2, 8, bgcolor=tableBgColor) // 填充表格内容 table.cell(macdTable, 0, 0, text="Timeframe", text_color=textColor, text_size=textSize) table.cell(macdTable, 1, 0, text="HA Colors", text_color=textColor, text_size=textSize) table.cell(macdTable, 0, 1, text=tf1 + (tf1 != "D" && tf1 != "W" && tf1 != "M" && tf1 != "3M" && tf1 != "6M" && tf1 != "12M" ? "m" : ""), text_color=textColor, text_size=textSize) table.cell(macdTable, 1, 1, text=str.join(colors_tf1, ""), text_color=color.black, text_size=textSize) table.cell(macdTable, 0, 2, text=tf2 + (tf2 != "D" && tf2 != "W" && tf2 != "M" && tf2 != "3M" && tf2 != "6M" && tf2 != "12M" ? "m" : ""), text_color=textColor, text_size=textSize) table.cell(macdTable, 1, 2, text=str.join(colors_tf2, ""), text_color=color.black, text_size=textSize) table.cell(macdTable, 0, 3, text=tf3 + (tf3 != "D" && tf3 != "W" && tf3 != "M" && tf3 != "3M" && tf3 != "6M" && tf3 != "12M" ? "m" : ""), text_color=textColor, text_size=textSize) table.cell(macdTable, 1, 3, text=str.join(colors_tf3, ""), text_color=color.black, text_size=textSize) table.cell(macdTable, 0, 4, text=tf4 + (tf4 != "D" && tf4 != "W" && tf4 != "M" && tf4 != "3M" && tf4 != "6M" && tf4 != "12M" ? "m" : ""), text_color=textColor, text_size=textSize) table.cell(macdTable, 1, 4, text=str.join(colors_tf4, ""), text_color=color.black, text_size=textSize) table.cell(macdTable, 0, 5, text=tf5 + (tf5 != "D" && tf5 != "W" && tf5 != "M" && tf5 != "3M" && tf5 != "6M" && tf5 != "12M" ? "m" : ""), text_color=textColor, text_size=textSize) table.cell(macdTable, 1, 5, text=str.join(colors_tf5, ""), text_color=color.black, text_size=textSize) table.cell(macdTable, 0, 6, text=tf6 + (tf6 != "D" && tf6 != "W" && tf6 != "M" && tf6 != "3M" && tf6 != "6M" && tf6 != "12M" ? "m" : ""), text_color=textColor, text_size=textSize) table.cell(macdTable, 1, 6, text=str.join(colors_tf6, ""), text_color=color.black, text_size=textSize) table.cell(macdTable, 0, 7, text=tf7, text_color=textColor, text_size=textSize) table.cell(macdTable, 1, 7, text=str.join(colors_tf7, ""), text_color=color.black, text_size=textSize)
关键修改说明
- 跨周期数据获取逻辑修正:将Heikin Ashi数据的获取放到
get_ha_colors函数内部,确保每个时间周期都获取自身的HA数据,而非传递当前周期的数据。 - 表格初始化补充:添加
var macdTable = table.new(...)代码,创建表格对象,避免运行报错。 - 输入框标题修正:将重复的
Timeframe 5改为Timeframe 6和Timeframe 7,便于区分配置。 - 时间单位优化:自动判断时间周期类型,仅对分钟级周期添加
m后缀,避免日/周/月周期显示错误格式。 - 颜色序列逻辑调整:返回从旧到新的颜色序列,表格展示时符合K线时间顺序(最左侧为最早K线,最右侧为最新K线)。
内容的提问来源于stack exchange,提问作者SMaccnt
相关产品推荐
相关产品推荐

