Pine Script绘制仓位框偏离实际价格问题求助
Pine Script绘制仓位框偏离预期位置的问题
问题描述
在EURGBP的1H时间框架图表上使用Pine Script绘制入场价、止损线时,尽管输入了正确的价格数值(入场价0.84480,止损0.84081),但绘制的仓位框完全不在预期位置。为了避免脚本报错,将未设置的止盈值设为与入场价相同。
重现代码
//@version=5 indicator("Trade History: EURGBP! - Generated", overlay=true) // Colors for different types of trades longColor = color.new(color.green, 0) shortColor = color.new(color.red, 0) entryPrice136 = input(0.84480, title="Entry Price 136") takeProfitLevel136 = input(0.84480, title="Take Profit Level 136") stopLossLevel136 = input(0.84081, title="Stop Loss Level 136") startTime136 = input.time(timestamp("2025-01-17 10:58"), title="Start Time for Position 136 (UTC)") endTime136 = input.time(timestamp("2025-01-17 16:27"), title="End Time for Position 136 (UTC)") isInTimeRange136 = (time >= startTime136 and time <= endTime136) p_tpLvl136 = plot(isInTimeRange136 ? takeProfitLevel136 : na, title="TP 136", color=color.green, linewidth=2, style=plot.style_linebr) p_slLvl136 = plot(isInTimeRange136 ? stopLossLevel136 : na, title="SL 136", color=color.red, linewidth=2, style=plot.style_linebr) p_entryLvl136 = plot(isInTimeRange136 ? entryPrice136 : na, title="Entry 136", color=color.blue, linewidth=2, style=plot.style_linebr) fill(p_entryLvl136, p_tpLvl136, color=isInTimeRange136 ? color.new(color.green, 80) : na, title="TP Area 136") fill(p_entryLvl136, p_slLvl136, color=isInTimeRange136 ? color.new(color.red, 80) : na, title="SL Area 136")
问题原因及解决方案
核心问题
- 时间范围判断偏差:1H时间框架下,Pine Script的
time变量代表K线的开盘时间(UTC)。你设置的入场时间是10:58,落在10:00-11:00的K线内,但time >= startTime136会跳过该K线,从11:00的K线开始绘制,导致视觉上仓位框位置偏离。 - 时区不匹配:如果你的图表时区不是UTC,脚本中设置的UTC时间会与图表显示的时间存在差异,进一步导致时间范围判断错误。
修正后的代码
//@version=5 indicator("Trade History: EURGBP! - Generated", overlay=true) // Colors for different types of trades longColor = color.new(color.green, 0) shortColor = color.new(color.red, 0) // 明确指定输入类型为float,避免潜在类型问题 entryPrice136 = input.float(0.84480, title="Entry Price 136") takeProfitLevel136 = input.float(0.84480, title="Take Profit Level 136") stopLossLevel136 = input.float(0.84081, title="Stop Loss Level 136") // 启用confirm选项,确保时间与图表时区匹配 startTime136 = input.time(timestamp("2025-01-17 10:58"), title="Start Time for Position 136", confirm=true) endTime136 = input.time(timestamp("2025-01-17 16:27"), title="End Time for Position 136", confirm=true) // 调整时间范围判断:用time_close覆盖入场时间所在的K线 isInTimeRange136 = (time_close >= startTime136) and (time <= endTime136) // 使用style_line确保时间范围内线条连续,避免断开 p_tpLvl136 = plot(isInTimeRange136 ? takeProfitLevel136 : na, title="TP 136", color=color.green, linewidth=2, style=plot.style_line) p_slLvl136 = plot(isInTimeRange136 ? stopLossLevel136 : na, title="SL 136", color=color.red, linewidth=2, style=plot.style_line) p_entryLvl136 = plot(isInTimeRange136 ? entryPrice136 : na, title="Entry 136", color=color.blue, linewidth=2, style=plot.style_line) // 仅填充SL区域,TP与Entry重合时无需填充 fill(p_entryLvl136, p_slLvl136, color=isInTimeRange136 ? color.new(color.red, 80) : na, title="SL Area 136")
关键修正点
- 改用
input.float明确数值类型,避免隐式转换问题。 - 调整时间判断逻辑为
time_close >= startTime136,确保包含入场时间所在的K线(10:00-11:00的K线收盘时间为11:00,大于10:58)。 - 将plot样式从
style_linebr改为style_line,保证时间范围内线条连续显示。 - 移除TP与Entry重合时的无效填充操作。
- 启用
confirm=true,让TradingView自动转换时间为图表时区,避免时区差异。
额外检查项
- 确认图表时间框架确实为1H,避免K线时间范围不匹配。
- 验证输入的价格数值与EURGBP当前报价区间(0.8x-0.9x)一致。
内容的提问来源于stack exchange,提问作者Hassan Ashas
相关产品推荐
相关产品推荐

