Pinescript:如果价格超过/低于它们,则递归删除线 [英] Pinescript: Recursively delete lines if price crosses above/below them

查看:54
本文介绍了Pinescript:如果价格超过/低于它们,则递归删除线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概述

  • 我创建了一个识别分形的脚本
  • 如果确定了分形,则会创建一条线并向右延伸
  • 如果价格高于/低于线的 y1 值,则该线将被删除

问题 - 不一致的行为/错误

  • 脚本在每日时间范围内适用于 FX:CADJPY:工作示例图片
  • Tradingview 显示图纸太多,无法删除最旧的";如果我在同一工具上转到 H4 时间范围:错误图片1
  • Tradingview 显示内部服务器研究错误";如果我在任何时间范围内打开 FX:EURAUD:错误图片2

任何帮助将不胜感激.它让我的头融化了..谢谢.

Any help would be most appreciated. It has my head melted..thanks.

//@version=4
study("FRACTAL_LINES", shorttitle="FL", overlay=true, precision=0, max_bars_back=5000)
//////////////////////FRACTALS/////////////////////////////////
header_fractals         = input(false, title = "====== Fractal Settings ======")
display_fractals        = input(false, title="Display Fractal triangles")
fractal_join_line       = input(true, title='Display Fractal lines')
extend                  = input(true, title='Extend fractal lines to current bar if they remain uncrossed')

aggressive = false
price = hl2

// fractal calculation
n = 2
header_fractals4 = input(false, title = "========================")

// Identify FRACTAL TOPS
isBWFractalBullish(mode) => ret = mode == 1 ? ((high[n+2] < high[n]) and (high[n+1] < high[n]) and (high[n-1] < high[n]) 
 and (high[n-2] < high[n])) or ((high[n+3] < high[n]) and (high[n+2] < high[n]) 
 and (high[n+1] == high[n]) and (high[n-1] < high[n]) and (high[n-2] < high[n])) 
 or ((high[n+4] < high[n]) and (high[n+3] < high[n]) and (high[n+2] == high[n]) 
 and (high[n+1] <= high[n]) and (high[n-1] < high[n]) and (high[n-2] < high[n])) 
 or ((high[n+5] < high[n]) and (high[n+4] < high[n]) and (high[n+3] == high[n]) 
 and (high[n+2] == high[n]) and (high[n+1] <= high[n]) and (high[n-1] < high[n]) and (high[n-2] < high[n])) 
 or ((high[n+6] < high[n]) and (high[n+5] < high[n]) and (high[n+4] == high[n]) 
 and (high[n+3] <= high[n]) and (high[n+2] == high[n]) and (high[n+1] <= high[n]) 
 and (high[n-1] < high[n]) and (high[n-2] < high[n])) : false
 
// Identify FRACTAL BOTTOMS
isBWFractalBearish(mode) => ret = mode == -1 ? ((low[n+2] > low[n]) and (low[n+1] > low[n]) and (low[n-1] > low[n]) 
 and (low[n-2] > low[n]))or ((low[n+3] > low[n]) and (low[n+2] > low[n]) and (low[n+1] == low[n]) 
 and (low[n-1] > low[n]) and (low[n-2] > low[n])) or ((low[n+4] > low[n]) and (low[n+3] > low[n]) 
 and (low[n+2] == low[n]) and (low[n+1] >= low[n]) and (low[n-1] > low[n]) and (low[n-2] > low[n])) 
 or ((low[n+5] > low[n]) and (low[n+4] > low[n]) and (low[n+3] == low[n]) and (low[n+2] == low[n]) 
 and (low[n+1] >= low[n]) and (low[n-1] > low[n]) and (low[n-2] > low[n])) or ((low[n+6] > low[n]) 
 and (low[n+5] > low[n]) and (low[n+4] == low[n]) and (low[n+3] >= low[n]) and (low[n+2] == low[n]) 
 and (low[n+1] >= low[n]) and (low[n-1] > low[n]) and (low[n-2] > low[n])) : false
 
filteredtopf = isBWFractalBullish(1)
filteredbotf = isBWFractalBearish(-1)

plotshape(filteredtopf and display_fractals, title="Up-Fractal", style=shape.triangleup, location=location.abovebar, offset=-2, color=color.green, transp=0)
plotshape(filteredbotf and display_fractals, title="Down-Fractal", style=shape.triangledown, location=location.belowbar, offset=-2, color=color.red, transp=0)

//// FRACTAL TOPS AND BOTTOMS Plots //////
plot(fractal_join_line ? valuewhen(filteredtopf, high[2], 0) : na, title="Fractal Tops",style=plot.style_cross, linewidth=1, offset=-2, color=#b71c1c, transp=0) 
plot(fractal_join_line ? valuewhen(filteredbotf, low[2], 0) : na, title="Fractal Bottoms",style=plot.style_cross, linewidth=1, offset=-2, color=#1b5e20, transp=0) 

var line fractal_top_line = na
var line fractal_bottom_line = na

//// Draw fractal lines if extend option is TRUE
if extend

    if filteredtopf
        fractal_top_line := line.new(x1=bar_index[2], y1=valuewhen(filteredtopf, high[2], 0), x2=bar_index, y2=valuewhen(filteredtopf, high[2], 0), color=color.red)
        line.set_extend(fractal_top_line, extend.right)
       
    if filteredbotf
        fractal_bottom_line := line.new(x1=bar_index[2], y1=valuewhen(filteredbotf, low[2], 0), x2=bar_index, y2=valuewhen(filteredbotf, low[2], 0), color=color.green)
        line.set_extend(fractal_bottom_line, extend.right)
 
//////////////////////////////////////////////
//// **HERE'S WHERE I RUN INTO TROUBLE** ////
////////////////////////////////////////////

//// Delete fractal lines if price crosses them
    for i=0 to bar_index
        if (barssince(high > line.get_y1(fractal_top_line[i]))) < (bar_index - line.get_x1(fractal_top_line[i]))
            line.delete(fractal_top_line[i]) 
        
        if (barssince(low < line.get_y1(fractal_bottom_line[i]))) < (bar_index - line.get_x1(fractal_bottom_line[i]))
            line.delete(fractal_bottom_line[i])
       

推荐答案

这显示了理论上应该可以工作的代码,但这并不是因为如何处理过去历史中的绘图 id 检查问题.最高优先级任务列表中计划进行修复,但尚未确定预计到达时间.

This shows code that should theoretically work, but it doesn't because of a problem with how the inspection of drawing ids in past history is handled. A fix is planned in the highest-priority task list but there is no ETA yet.

Afaik,目前没有办法可靠地实现你想要的,除了创建单独的变量来包含你正在绘制的所有线条的 id,这会很痛苦.

Afaik, there is currently no way to achieve what you want reliably, other than creating separate variables to contain the ids of all the lines you are drawing, which would be a pain.

//@version=4
LOOKBACK = 1000
study("FRACTAL_LINES", shorttitle="FL2", overlay=true, precision=0, max_bars_back=LOOKBACK)
//////////////////////FRACTALS/////////////////////////////////
header_fractals         = input(false, title = "====== Fractal Settings ======")
display_fractals        = input(false, title="Display Fractal triangles")
fractal_join_line       = input(true, title='Display Fractal lines')
extend                  = input(true, title='Extend fractal lines to current bar if they remain uncrossed')

aggressive = false
price = hl2

// fractal calculation
n = 2
header_fractals4 = input(false, title = "========================")

// Identify FRACTAL TOPS
isBWFractalBullish(mode) => ret = mode == 1 ? ((high[n+2] < high[n]) and (high[n+1] < high[n]) and (high[n-1] < high[n]) 
 and (high[n-2] < high[n])) or ((high[n+3] < high[n]) and (high[n+2] < high[n]) 
 and (high[n+1] == high[n]) and (high[n-1] < high[n]) and (high[n-2] < high[n])) 
 or ((high[n+4] < high[n]) and (high[n+3] < high[n]) and (high[n+2] == high[n]) 
 and (high[n+1] <= high[n]) and (high[n-1] < high[n]) and (high[n-2] < high[n])) 
 or ((high[n+5] < high[n]) and (high[n+4] < high[n]) and (high[n+3] == high[n]) 
 and (high[n+2] == high[n]) and (high[n+1] <= high[n]) and (high[n-1] < high[n]) and (high[n-2] < high[n])) 
 or ((high[n+6] < high[n]) and (high[n+5] < high[n]) and (high[n+4] == high[n]) 
 and (high[n+3] <= high[n]) and (high[n+2] == high[n]) and (high[n+1] <= high[n]) 
 and (high[n-1] < high[n]) and (high[n-2] < high[n])) : false
 
// Identify FRACTAL BOTTOMS
isBWFractalBearish(mode) => ret = mode == -1 ? ((low[n+2] > low[n]) and (low[n+1] > low[n]) and (low[n-1] > low[n]) 
 and (low[n-2] > low[n]))or ((low[n+3] > low[n]) and (low[n+2] > low[n]) and (low[n+1] == low[n]) 
 and (low[n-1] > low[n]) and (low[n-2] > low[n])) or ((low[n+4] > low[n]) and (low[n+3] > low[n]) 
 and (low[n+2] == low[n]) and (low[n+1] >= low[n]) and (low[n-1] > low[n]) and (low[n-2] > low[n])) 
 or ((low[n+5] > low[n]) and (low[n+4] > low[n]) and (low[n+3] == low[n]) and (low[n+2] == low[n]) 
 and (low[n+1] >= low[n]) and (low[n-1] > low[n]) and (low[n-2] > low[n])) or ((low[n+6] > low[n]) 
 and (low[n+5] > low[n]) and (low[n+4] == low[n]) and (low[n+3] >= low[n]) and (low[n+2] == low[n]) 
 and (low[n+1] >= low[n]) and (low[n-1] > low[n]) and (low[n-2] > low[n])) : false
 
filteredtopf = isBWFractalBullish(1)
filteredbotf = isBWFractalBearish(-1)

plotshape(filteredtopf and display_fractals, title="Up-Fractal", style=shape.triangleup, location=location.abovebar, offset=-2, color=color.green, transp=0)
plotshape(filteredbotf and display_fractals, title="Down-Fractal", style=shape.triangledown, location=location.belowbar, offset=-2, color=color.red, transp=0)

//// FRACTAL TOPS AND BOTTOMS Plots //////
plot(fractal_join_line ? valuewhen(filteredtopf, high[2], 0) : na, title="Fractal Tops",style=plot.style_cross, linewidth=1, offset=-2, color=#b71c1c, transp=0) 
plot(fractal_join_line ? valuewhen(filteredbotf, low[2], 0) : na, title="Fractal Bottoms",style=plot.style_cross, linewidth=1, offset=-2, color=#1b5e20, transp=0) 


//// Draw fractal lines if extend option is TRUE
line fractal_top_line    = na
line fractal_bottom_line = na
int fractal_top_bar      = na
int fractal_bottom_bar   = na
yTop = valuewhen(filteredtopf, high[2], 0)
yBot = valuewhen(filteredbotf, low[2], 0)
if extend

    if filteredtopf
        fractal_top_line := line.new(x1=bar_index[2], y1=yTop, x2=bar_index, y2=yTop, color=color.red)
        line.set_extend(fractal_top_line, extend.right)
    if filteredbotf
        fractal_bottom_line := line.new(x1=bar_index[2], y1=yBot, x2=bar_index, y2=yBot, color=color.green)
        line.set_extend(fractal_bottom_line, extend.right)

    // Delete fractal lines if price crosses them
    // float linePrice = na
    // lineCrossed = false
    // for i=0 to LOOKBACK
    //     if not na(fractal_top_bar[i])
    //         linePrice := line.get_price(fractal_top_line[i], bar_index)
    //         lineCrossed := (close[i+1] < linePrice and close[i] > linePrice) or (close[i+1] > linePrice and close[i] < linePrice)
    //         if lineCrossed
    //             line.delete(fractal_top_line[i])
        
    //     if not na(fractal_bottom_bar[i])
    //         linePrice := line.get_price(fractal_bottom_line[i], bar_index)
    //         lineCrossed := (close[i+1] < linePrice and close[i] > linePrice) or (close[i+1] > linePrice and close[i] < linePrice)
    //         if lineCrossed
    //             line.delete(fractal_bottom_line[i]) 

这篇关于Pinescript:如果价格超过/低于它们,则递归删除线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆