Pine Script需求:4小时级别MA50与MA100交叉点绘射线并自动删除
Pine Script 需求与问题解决
我是Pine Script新手,希望实现以下功能:
- 在4小时分辨率下,捕捉MA50(50周期简单移动平均线)与MA100(100周期简单移动平均线)的交叉点
- 在交叉点位置绘制水平射线(向右延伸)
- 当价格穿越射线后,自动删除所有已创建的射线
最初代码(无法实现需求)
//@version=5 indicator("CU",shorttitle = "CU", overlay=true) ma50 = ta.sma(close, 50) ma100 = ta.sma(close, 100) bcu = ma50 == ma100 bcu4h = input.timeframe("4H", "BCU4H") bcu_sec = request.security(syminfo.tickerid, bcu4h, close) line_bcu1d = line.new(na, na, na, na, style=line.style_solid,extend=extend.right, color=color.green) //PLOT plot(ma50, "MA50", color.rgb(255, 255, 255, 0), 2) plot(ma100, "MA100", color.rgb(255, 235, 59, 0), 2)
调整后代码(射线绘制成功,但无法自动删除)
//@version=5 indicator("CU2",shorttitle = "CU2", overlay=true) var float last_intersect_x = na var float last_intersect_y = na // Gets intersection point of two line segments [(x1, y1), (x2, y2)] and [(u1, v1), (u2, v2)]. f_get_intersect_coordinates(x1, y1, x2, y2, u1, v1, u2, v2) => x = -1 * ((x1 - x2) * (u1 * v2 - u2 * v1) - (u2 - u1) * (x2 * y1 - x1 * y2)) / ((v1 - v2) * (x1 - x2) - (u2 - u1) * (y2 - y1)) y = -1 * (u1 * v2 * y1 - u1 * v2 * y2 - u2 * v1 * y1 + u2 * v1 * y2 - v1 * x1 * y2 + v1 * x2 * y1 + v2 * x1 * y2 - v2 * x2 * y1) / (-1 * u1 * y1 + u1 * y2 + u2 * y1 - u2 * y2 + v1 * x1 - v1 * x2 - v2 * x1 + v2 * x2) [x,y] _ma50 = ta.sma(close, 50) _ma100 = ta.sma(close, 100) line line_bcu = na if ta.crossover(_ma50, _ma100) x1 = bar_index[1] y1 = _ma50[1] x2 = bar_index y2 = _ma50 u1 = bar_index[1] v1 = _ma100[1] u2 = bar_index v2 = _ma100 [x,y] = f_get_intersect_coordinates(x1, y1, x2, y2, u1, v1, u2, v2) last_intersect_x := x last_intersect_y := y line_bcu := line.new(bar_index-1, last_intersect_y, bar_index, last_intersect_y, extend=extend.right, color=color.rgb(255, 255, 255), style=line.style_solid, width = 2) //===================================================== //I tried with this loop, but it is not working, only some of the lines are deleted for i = 0 to 100 if line.get_y1(line_bcu[i]) > low line.delete(line_bcu[i]) //===================================================== plot(_ma50, "MA50", color.rgb(255, 255, 255, 0), 2) plot(_ma100, "MA100", color.rgb(255, 235, 59, 0), 2)
正确解决方案代码
//@version=5 indicator("MA Cross Ray with Auto-Delete", shorttitle="MA Ray", overlay=true) // 输入参数 ma_len1 = input.int(50, "MA50 周期") ma_len2 = input.int(100, "MA100 周期") target_tf = input.timeframe("4H", "目标周期") ray_color = input.color(color.new(color.white, 0), "射线颜色") ray_width = input.int(2, "射线宽度") // 用数组存储所有创建的射线对象 var array<line> ray_array = array.new_line() // 在目标周期(4H)获取MA交叉信号与交叉价格 [ma50_4h, ma100_4h, cross_over, cross_under] = request.security(syminfo.tickerid, target_tf, [ta.sma(close, ma_len1), ta.sma(close, ma_len2), ta.crossover(ta.sma(close, ma_len1), ta.sma(close, ma_len2)), ta.crossunder(ta.sma(close, ma_len1), ta.sma(close, ma_len2))]) // 当目标周期出现交叉时,绘制水平射线 if cross_over or cross_under // 获取交叉点的价格(取两个MA在交叉K线的平均值) cross_price = (ma50_4h + ma100_4h) / 2 // 创建射线,从当前K线左边缘延伸到右边缘并向右扩展 new_ray = line.new(bar_index - 1, cross_price, bar_index, cross_price, extend=extend.right, color=ray_color, width=ray_width) // 将射线对象存入数组 array.push(ray_array, new_ray) // 遍历数组,检查价格是否穿越射线,符合条件则删除 if barstate.islast // 倒序遍历避免数组索引混乱 for i = array.size(ray_array) - 1 downto 0 current_ray = array.get(ray_array, i) ray_price = line.get_y1(current_ray) // 判断价格是否穿越射线(当前K线的low低于射线价格 或 high高于射线价格) if low < ray_price or high > ray_price line.delete(current_ray) array.remove(ray_array, i) // 绘制当前周期的MA(可选,用于参考) plot(ta.sma(close, ma_len1), "MA50", color=color.white, linewidth=2) plot(ta.sma(close, ma_len2), "MA100", color=color.yellow, linewidth=2)
关键改进点说明
- 数组管理射线对象:用
array<line>类型数组保存所有射线,解决单个变量无法批量管理的问题,方便遍历和删除操作。 - 目标周期信号获取:通过
request.security()在4H周期内判断MA交叉,确保交叉信号来自指定的4小时分辨率,而非当前图表周期。 - 精准穿越判断:判断当前K线的
low是否低于射线价格,或high是否高于射线价格,满足任一条件即删除射线。 - 倒序遍历数组:删除数组元素时采用倒序遍历,避免因数组长度变化导致的索引错位问题。
内容的提问来源于stack exchange,提问作者xlameee
相关产品推荐
相关产品推荐

