基于绘图区间动态设置StopLoss与TakeProfit的技术问题求助
问题:基于5:00-8:55时段区间动态设置止损止盈
需求说明
- 以图表中5:00-8:55时段形成的区间(box)为基准,动态计算交易的止损(SL)和止盈(TP)
- 做多规则:止损设为区间最低点,止盈幅度为止损幅度的2倍
- 做空规则:止损设为区间最高点,止盈幅度为止损幅度的2倍
现有区间绘制代码
sessionHighPrice := high sessionLowPrice := low sessionOpenPrice := open // Else, during the session, track the highest high and lowest low else if inSession sessionHighPrice := math.max(sessionHighPrice, high) sessionLowPrice := math.min(sessionLowPrice, low) // STEP 4: // When a session begins, make a new box for that session if sessionStart sessionBox := box.new(left=bar_index, top=na, right=na, bottom=na, border_width=boxBorderSize) // STEP 5: // During the session, update that session's existing box if inSession box.set_top(sessionBox, sessionHighPrice) box.set_bottom(sessionBox, sessionLowPrice) box.set_right(sessionBox, bar_index + 1) // See if bar closed higher than session open. When it did, make // box green (and use red otherwise). if close > sessionOpenPrice box.set_bgcolor(sessionBox, upBoxColor) box.set_border_color(sessionBox, upBorderColor) else box.set_bgcolor(sessionBox, downBoxColor) box.set_border_color(sessionBox, downBorderColor) // Save Previous Max & Min Minpreday = ta.valuewhen(SessionEnd,sessionLowPrice,0) Maxpreday = ta.valuewhen(SessionEnd,sessionHighPrice,0) // Look if the close time of the current bar // falls inside the date range inDateRange = (time >= timestamp(syminfo.timezone, startYear, startMonth, startDate, 0, 0)) and (time < timestamp(syminfo.timezone, endYear, endMonth, endDate, 0, 0))
尝试的止损止盈代码(运行异常)
Value_entryshort = ta.valuewhen(EntryShort == 1, Minpreday,0) Value_sllong = ta.valuewhen(EntryLong == 1, Minpreday,0) Value_slshort = ta.valuewhen(EntryShort == 1, Maxpreday,0) SL_Long = (Value_entrylong-Value_sllong)*10000 SL_Short = (Value_entryshort-Value_slshort)*10000
问题排查与修复方案
原代码核心问题
- 变量未定义:
Value_entrylong未声明,直接用于计算SL_Long会触发报错 - 进场价记录错误:
Value_entryshort错误记录了区间低点,而非做空进场时的价格 - 逻辑缺失:未计算止盈价格,且止损的点值转换后未映射回实际价格
- 区间值获取时机问题:仅用
SessionEnd触发的历史区间值,若进场发生在区间结束前,会取到上一个区间的错误数据
修复后的完整实现代码
// -------------------------- // 1. 实时跟踪当前区间高低点(不管区间是否结束) // -------------------------- var float sessionRangeLow = na var float sessionRangeHigh = na if sessionStart sessionRangeLow := low sessionRangeHigh := high else if inSession sessionRangeLow := math.min(sessionRangeLow, low) sessionRangeHigh := math.max(sessionRangeHigh, high) // -------------------------- // 2. 记录进场价与对应区间值 // -------------------------- // 做多进场相关 Value_entrylong = ta.valuewhen(EntryLong == 1, close, 0) // 可根据实际进场逻辑替换为open/成交价格 SL_Long_Price = ta.valuewhen(EntryLong == 1, sessionRangeLow, 0) // 计算止损幅度(点值),EURUSD每点为0.0001 SL_Long_Pips = (Value_entrylong - SL_Long_Price) * 10000 // 止盈价格=进场价 + 2倍止损幅度 TP_Long_Price = Value_entrylong + (SL_Long_Pips * 0.0001 * 2) // 做空进场相关 Value_entryshort = ta.valuewhen(EntryShort == 1, close, 0) SL_Short_Price = ta.valuewhen(EntryShort == 1, sessionRangeHigh, 0) SL_Short_Pips = (SL_Short_Price - Value_entryshort) * 10000 // 止盈价格=进场价 - 2倍止损幅度 TP_Short_Price = Value_entryshort - (SL_Short_Pips * 0.0001 * 2) // -------------------------- // 3. 绘制止损止盈到图表 // -------------------------- // 做多标记与SL/TP线 plotshape(series=EntryLong, title="Long Entry", location=location.belowbar, color=color.new(color.green, 0), style=shape.triangleup, size=size.small) plot(ta.valuewhen(EntryLong == 1, SL_Long_Price, 0), title="Long SL", color=color.new(color.red, 0), style=style.line, linewidth=2, trackprice=true) plot(ta.valuewhen(EntryLong == 1, TP_Long_Price, 0), title="Long TP", color=color.new(color.green, 0), style=style.line, linewidth=2, trackprice=true) // 做空标记与SL/TP线 plotshape(series=EntryShort, title="Short Entry", location=location.abovebar, color=color.new(color.red, 0), style=shape.triangledown, size=size.small) plot(ta.valuewhen(EntryShort == 1, SL_Short_Price, 0), title="Short SL", color=color.new(color.red, 0), style=style.line, linewidth=2, trackprice=true) plot(ta.valuewhen(EntryShort == 1, TP_Short_Price, 0), title="Short TP", color=color.new(color.green, 0), style=style.line, linewidth=2, trackprice=true)
关键说明
- 实时跟踪区间高低点:避免进场在区间结束前时取到历史错误值
- 明确进场价记录:根据实际交易逻辑,可将
close替换为进场信号触发时的价格(如open或自定义成交价格) - 点值转换逻辑:EURUSD每点对应0.0001,因此用
*10000转换为整数点值,计算后再转换回价格 - 绘图添加
trackprice=true:确保SL/TP线随价格滚动显示
内容的提问来源于stack exchange,提问作者honeybadger21
相关产品推荐
相关产品推荐

