问题
Overview
- I have created a script that identifies fractals
- If a fractal is identified a line is created and extended to the right
- If price passes above/below the y1 value of the line then the line gets deleted
Problem - inconsistent behaviour/errors
- Script works for FX:CADJPY on Daily timeframe: Picture of working example
- Tradingview says "Too many drawings, cannot delete the oldest" if I move to H4 timeframe on same instrument: Picture of error1
- Tradingview says "Internal server study error" if I open FX:EURAUD on any timeframe: Picture of error2
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])
回答1:
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, 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])
来源:https://stackoverflow.com/questions/63368668/pinescript-recursively-delete-lines-if-price-crosses-above-below-them