TradingView指标转策略后无显示问题求助
问题描述
我尝试将一款TradingView指标转换为策略使用:该指标作为指标时可正常在图表显示,但将声明中的indicator改为strategy后,虽能无错误保存,却无法再显示;将指标代码加入默认策略模板(保留strategy声明、移除indicator)后,同样无错误保存但无任何显示。
原指标代码
//@version=5 indicator("Test10_v1",overlay=true,max_bars_back=1000,max_lines_count=500,max_labels_count=500) length = input.float(500,'Window Size',maxval=500,minval=0) h = input.float(8.,'Bandwidth') mult = input.float(3.) src = input.source(close,'Source') up_col = input.color(#39ff14,'Colors',inline='col') dn_col = input.color(#ff1100,'',inline='col') disclaimer = input(false, 'Hide Disclaimer') //---- n = bar_index var k = 2 var upper = array.new_line(0) var lower = array.new_line(0) lset(l,x1,y1,x2,y2,col)=> line.set_xy1(l,x1,y1) line.set_xy2(l,x2,y2) line.set_color(l,col) line.set_width(l,2) if barstate.isfirst for i = 0 to length/k-1 array.push(upper,line.new(na,na,na,na)) array.push(lower,line.new(na,na,na,na)) //---- line up = na line dn = na //---- cross_up = 0. cross_dn = 0. if barstate.islast y = array.new_float(0) sum_e = 0. for i = 0 to length-1 sum = 0. sumw = 0. for j = 0 to length-1 w = math.exp(-(math.pow(i-j,2)/(h*h*2))) sum += src[j]*w sumw += w y2 = sum/sumw sum_e += math.abs(src[i] - y2) array.push(y,y2) mae = sum_e/length*mult for i = 1 to length-1 y2 = array.get(y,i) y1 = array.get(y,i-1) up := array.get(upper,i/k) dn := array.get(lower,i/k) lset(up,n-i+1,y1 + mae,n-i,y2 + mae,up_col) lset(dn,n-i+1,y1 - mae,n-i,y2 - mae,dn_col) if src[i] > y1 + mae and src[i+1] < y1 + mae label.new(n-i,src[i],'▼',color=#00000000,style=label.style_label_down,textcolor=dn_col,textalign=text.align_center) if src[i] < y1 - mae and src[i+1] > y1 - mae label.new(n-i,src[i],'▲',color=#00000000,style=label.style_label_up,textcolor=up_col,textalign=text.align_center) cross_up := array.get(y,0) + mae cross_dn := array.get(y,0) - mae //---- var tb = table.new(position.top_right, 1, 1 , bgcolor = #35202b) if barstate.isfirst and not disclaimer table.cell(tb, 0, 0, 'Repaints' , text_size = size.small , text_color = #cc2f3c)
问题原因
- 策略默认不叠加主图:原指标用
overlay=true让绘图显示在主图,但strategy声明默认overlay=false,会把策略内容放到副图,导致主图看不到线条和标签。 - 绘图资源参数未迁移:原指标设置的
max_lines_count、max_labels_count等资源限制参数,需要同步到strategy声明中,否则策略无法创建足够的绘图元素。
修改后的策略代码
//@version=5 // 核心修改:替换为strategy声明,保留overlay=true并迁移资源限制参数 strategy("Test10_v1_Strategy", overlay=true, max_bars_back=1000, max_lines_count=500, max_labels_count=500) length = input.float(500,'Window Size',maxval=500,minval=0) h = input.float(8.,'Bandwidth') mult = input.float(3.) src = input.source(close,'Source') up_col = input.color(#39ff14,'Colors',inline='col') dn_col = input.color(#ff1100,'',inline='col') disclaimer = input(false, 'Hide Disclaimer') //---- n = bar_index var k = 2 var upper = array.new_line(0) var lower = array.new_line(0) lset(l,x1,y1,x2,y2,col)=> line.set_xy1(l,x1,y1) line.set_xy2(l,x2,y2) line.set_color(l,col) line.set_width(l,2) if barstate.isfirst for i = 0 to length/k-1 array.push(upper,line.new(na,na,na,na)) array.push(lower,line.new(na,na,na,na)) //---- line up = na line dn = na //---- cross_up = 0. cross_dn = 0. if barstate.islast y = array.new_float(0) sum_e = 0. for i = 0 to length-1 sum = 0. sumw = 0. for j = 0 to length-1 w = math.exp(-(math.pow(i-j,2)/(h*h*2))) sum += src[j]*w sumw += w y2 = sum/sumw sum_e += math.abs(src[i] - y2) array.push(y,y2) mae = sum_e/length*mult for i = 1 to length-1 y2 = array.get(y,i) y1 = array.get(y,i-1) up := array.get(upper,i/k) dn := array.get(lower,i/k) lset(up,n-i+1,y1 + mae,n-i,y2 + mae,up_col) lset(dn,n-i+1,y1 - mae,n-i,y2 - mae,dn_col) if src[i] > y1 + mae and src[i+1] < y1 + mae label.new(n-i,src[i],'▼',color=#00000000,style=label.style_label_down,textcolor=dn_col,textalign=text.align_center) if src[i] < y1 - mae and src[i+1] > y1 - mae label.new(n-i,src[i],'▲',color=#00000000,style=label.style_label_up,textcolor=up_col,textalign=text.align_center) cross_up := array.get(y,0) + mae cross_dn := array.get(y,0) - mae //---- var tb = table.new(position.top_right, 1, 1 , bgcolor = #35202b) if barstate.isfirst and not disclaimer table.cell(tb, 0, 0, 'Repaints' , text_size = size.small , text_color = #cc2f3c)
修改说明
- 将
indicator声明替换为strategy,并保留overlay=true确保绘图显示在主图。 - 把原指标的
max_bars_back、max_lines_count、max_labels_count参数迁移到strategy声明中,保证策略有足够资源创建绘图元素。 - 其余逻辑完全保留原指标的行为,确保绘图效果和原指标一致。
内容的提问来源于stack exchange,提问作者george10001000
相关产品推荐
相关产品推荐

