//@version=5 //indicator("040 K線假突破與真突破標示", overlay=true) // Input for customization…
- User
//@version=5 //indicator("040 K線假突破與真突破標示", overlay=true)
// Input for customization boxColorFalse = color.rgb(76, 175, 79, 40) // 假突破的框線顏色 boxColorTrue = color.rgb(255, 82, 82, 40) // 真突破的框線顏色 lineWidth = 10 // 框線的寬度
// Variables var float lastFalseHigh = na // 用於記錄假突破的最高點
// 判斷假突破條件:K線最高點未突破前高 isFalseBreak = high > low and close < open and close < (high - (high - low) * 0.3) if isFalseBreak lastFalseHigh := high // 畫出假突破的框 line.new(x1=bar_index - 1, y1=high, x2=bar_index, y2=high, color=boxColorFalse, width=lineWidth) label.new(bar_index, high, "假",textcolor=color.rgb(255, 255, 255), color=color.rgb(95, 146, 121, 40), style=label.style_label_down)
// 判斷真突破條件:K線突破前高 isTrueBreak = high > lastFalseHigh and not na(lastFalseHigh) if isTrueBreak // 畫出真突破的框 line.new(x1=bar_index - 1, y1=lastFalseHigh, x2=bar_index, y2=lastFalseHigh, color=boxColorTrue, width=lineWidth) lastFalseHigh := na // 真突破後重置記錄 label.new(bar_index, lastFalseHigh, "真", color=boxColorTrue, textcolor=color.rgb(23, 21, 21), style=label.style_none)
優化代碼
- Assistant
以下是优化后的代码,解决了原有代码中的错误并提升了可读性与效率:
//@version=5 indicator("040 K線假突破與真突破標示", overlay=true) // 输入参数 boxColorFalse = color.rgb(76, 175, 79, 40) // 假突破框线颜色 boxColorTrue = color.rgb(255, 82, 82, 40) // 真突破框线颜色 lineWidth = 10 // 框线宽度 // 变量声明 var float lastFalseHigh = na // 记录最近一次假突破的高点 // 判断假突破条件:阴线且收盘价低于K线实体下30%位置 isFalseBreak = close < open and close < (high - (high - low) * 0.3) if isFalseBreak // 更新假突破高点 lastFalseHigh := high // 绘制假突破线(从上一根K线到当前K线) line.new( x1 = bar_index - 1, y1 = high, x2 = bar_index, y2 = high, color = boxColorFalse, width = lineWidth ) // 添加假突破标签 label.new( bar_index, high, "假", textcolor = color.white, color = color.rgb(95, 146, 121, 40), style = label.style_label_down ) // 判断真突破条件:当前K线突破记录的假突破高点 isTrueBreak = high > lastFalseHigh and not na(lastFalseHigh) if isTrueBreak // 保存当前假突破高点用于标签绘制 prevLastFalseHigh = lastFalseHigh // 绘制真突破线(从上一根K线到当前K线) line.new( x1 = bar_index - 1, y1 = prevLastFalseHigh, x2 = bar_index, y2 = prevLastFalseHigh, color = boxColorTrue, width = lineWidth ) // 重置假突破记录 lastFalseHigh := na // 添加真突破标签 label.new( bar_index, prevLastFalseHigh, "真", color = boxColorTrue, textcolor = color.rgb(23, 21, 21), style = label.style_none )主要优化点说明:
-
修复真突破标签错误
- 原代码在真突破时使用
lastFalseHigh(已设为na)作为标签位置导致错误,现通过prevLastFalseHigh保存突破前的高点值,确保标签位置正确。
- 原代码在真突破时使用
-
简化条件判断
- 移除了冗余的
high > low条件(K线的high始终大于low)。 - 保留核心逻辑:阴线且收盘价低于K线实体下30%位置作为假突破判断。
- 移除了冗余的
-
提升代码可读性
- 增加注释说明关键逻辑,明确变量用途。
- 使用更直观的变量命名(如
prevLastFalseHigh)。
-
优化性能(潜在改进)
- 若需避免重复创建线和标签,可引入
var变量管理对象(如var line falseBreakLine = na),但当前逻辑已满足基本需求。
- 若需避免重复创建线和标签,可引入
-
增强可维护性
- 分离线和标签的绘制逻辑,便于后续扩展或修改样式。
注意事项:
- 颜色格式:
color.rgb()参数需为 0-255 的整数范围,确保颜色正确显示。 - 标签位置:真突破标签位于
prevLastFalseHigh的水平线上,与假突破标签对齐。 - 条件逻辑:真突破仅在
lastFalseHigh存在时触发,避免初始状态错误判断。
此版本代码在功能上与原始逻辑一致,但修复了关键错误并提升了代码质量。
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.