You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Pine Script Renko策略无交易记录显示问题求助

问题诊断与修复方案

核心问题分析

你的代码存在几个关键问题,导致策略无法生成交易列表和自动信号:

1. ATR值获取逻辑错误

你提到每日ATR值通过ATR指标传入,但当前代码中ei = input(close, "ATR")只是将收盘价作为输入,并未真正计算或引入ATR指标值。这会导致box_size计算完全偏离预期,进而破坏Renko K线生成和信号触发逻辑。

2. firstflag变量未激活

代码定义了var firstflag = 0但从未将其设为1,导致处理价格波动小于盒子大小的代码块永远不会执行,Renko K线连续性受影响,间接干扰信号判断。

3. 策略退出指令位置错误

strategy.exit放在条件判断外部,会在每根K线重复执行,可能引发提前平仓或逻辑混乱。

4. 信号触发的时序匹配问题

call和put的判断依赖c变量的前后值,但c基于真实K线的close-open计算,和Renko K线的生成时序不匹配,可能导致信号触发时机错误。


修复后的完整代码

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Sahil_Trader

//@version=5
strategy("Renko Strat", overlay=true, margin_long=100, margin_short=100, default_qty_type=strategy.cash, default_qty_value=10000)

var firstflag = 0
var r_open = 0.0
var r_close = 0.0
var r_low = 0.0
var r_high = 0.0
var c_color = color.white
var c = 0

// 时间窗口逻辑简化
timeexit = time("D", "1500-1534") != na
timeentry = time("D", "0900-0909") != na

// 正确计算每日ATR
atr_length = input.int(14, "ATR计算周期")
d_atr = ta.atr(atr_length)

// 计算Renko盒子大小
box_size = 0.07 * d_atr

// 初始化当日Renko基准价格
if timeentry
    r_open := open    
    r_close := close
    firstflag := 1  // 激活小波动处理逻辑

// Renko K线核心计算
size = math.abs(close - open)
c := size >= box_size and close > open ? 1 : size >= box_size and close < open ? -1 : 0

if c == 1
    r_open := r_close[1]
    r_low := r_open
    r_close := r_open + box_size
    r_high := r_close
    c_color := color.green
if c == -1
    r_open := r_close[1]
    r_high := r_open
    r_close := r_open - box_size
    r_low := r_close
    c_color := color.red

// 处理价格波动小于盒子大小的情况
if math.abs(close - open) <= box_size and firstflag == 1 and not timeexit
    r_open := r_open[1]
    r_close := r_close[1]
    r_high := r_high[1]
    r_low := r_low[1]
    c_color := c_color[1]

plotcandle(r_open, r_high, r_low, r_close, title='Renko K线', color = c_color)

if timeexit
    c_color := color.yellow

// 交易信号条件
call = c == 1 and c[1] == 0
put = c == -1 and c[1] == 0

// 多头交易逻辑
if call
    strategy.entry("Long", strategy.long)
    strategy.close("Short")
    strategy.exit("Long Exit", "Long", profit=4000, loss=1000)

plotshape(call, style=shape.triangleup, color=color.blue, location=location.bottom, size=size.small)

// 空头交易逻辑
if put
    strategy.entry("Short", strategy.short)
    strategy.close("Long")
    strategy.exit("Short Exit", "Short", profit=4000, loss=1000)

plotshape(put, style=shape.triangledown, color=color.red, location=location.top, size=size.small)

关键修复点说明

  1. 修正ATR获取逻辑:用ta.atr(atr_length)计算真实的ATR值,确保盒子大小基于波动率合理计算。
  2. 激活firstflag变量:在每日开盘时段将其设为1,让小波动处理逻辑生效,保证Renko K线连续性。
  3. 调整退出指令位置:将strategy.exit移入对应的开仓条件块内,避免无意义重复执行。
  4. 优化信号显示位置:将空头信号移至K线顶部,避免与多头信号重叠,提升可读性。

内容的提问来源于stack exchange,提问作者Sahil Nischal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 03:06:23