Hi Jurgen,
thanks for clarifying your setup. I made some modifications to your script, you can check it below.
Fyi: I think you received too many signals because of the conditions you plot your signals on. In your original script it were the Golong/Goshort variables that would plot a signal. If you incorporate my code snippet you would need to plot your signals on the conditions LongEntry/ShortEntry.
I coded the changes by the following logic.
If for example on a Friday the current high crosses the previous week high (or stayed there from the previous day) it checks if the day before (Thursday) a crossover occurred, or the day before that (Wednesday), or the day before that and so on (and we also check that there is no open position). All in all for 4 days out of a 5 day week, then the cycle begins again with a new weekly high/low.
I am sure there is a much more elegant way to program that, but I hope this helps never the less.
Cheers,
ps. I also made some changes to account for repainting issues with the weekly highs and lows and to the way trade exit signals are plotted.
Code: Select all
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=4
//By Juros
study(title="Universal weekly breakout + universal filter", shorttitle="weekly breakout+ filter", overlay=true, precision=8)
prevwkH = input(true, title="Show previous week high?")
prevwkL = input(true, title="show previous week low?")
//previous week high and low
prevWeekHigh = security(syminfo.tickerid, 'W', high[barstate.isrealtime ? 1:0])
prevWeekLow = security(syminfo.tickerid, 'W', low[barstate.isrealtime ? 1:0])
//previous Week high and low Plots
plot(prevwkH ? prevWeekHigh : na, title="Prev Week High", style=plot.style_stepline, linewidth=1, color=color.fuchsia, transp=20)
plot(prevwkL ? prevWeekLow : na, title="Prev Week Low", style=plot.style_stepline, linewidth=1, color=color.fuchsia, transp=20)
//------------------------------------------------------------------------------------------
// stop location for short- and long position (at crossover of previous week low or high)
Stopshort = crossover (high, prevWeekHigh)
Stoplong = crossunder (low, prevWeekLow)
Longposmemo = false
Longposmemo := Stopshort ? true : Stoplong ? false : Longposmemo[1]
Longcond = Stopshort and not Longposmemo[1]
color_1 = color.new(color.green, 70)
Shortposmemo = false
Shortposmemo := Stoplong ? true : Stopshort ? false : Shortposmemo[1]
Shortcond = Stoplong and not Shortposmemo[1]
color_2 = color.new(color.red, 70)
//bgcolor(Sellnow ? color_2 : na)
//------------------------------------------------------------------------------------------
// Entry & Exit Conditions
var Golong = false
var Goshort = false
LongEntry = high > prevWeekHigh and not Golong and (crossover (high[1], prevWeekHigh) or
crossover (high[2], prevWeekHigh) or
crossover (high[3], prevWeekHigh) or
crossover (high[4], prevWeekHigh))
if LongEntry
Golong := true
Goshort :=false
ShortEntry = low < prevWeekLow and not Goshort and (crossunder (low[1], prevWeekLow) or
crossunder (low[2], prevWeekLow) or
crossunder (low[3], prevWeekLow) or
crossunder (low[4], prevWeekLow))
if ShortEntry
Goshort := true
Golong := false
LSL = (Golong and Stoplong) ? 1 : 0
SSL = (Goshort and Stopshort) ? 1 : 0
//------------------------------------------------------------------------------------------
// plots : X where the position is stopped
plotchar (LSL, char='X', location=location.abovebar, color=color.yellow)
plotchar (SSL, char='X', location=location.belowbar, color=color.yellow)
plotshape (LongEntry ? high : na, style = shape.labelup, location = location.belowbar, color=color.lime, size=size.tiny, text = 'L', textcolor=color.black)
plotshape (ShortEntry ? low : na, style = shape.labeldown, location = location.abovebar, color=color.fuchsia, size=size.tiny, text ='S', textcolor=color.black)
// alerts
alertcondition(Golong, title="Weakly breakout go long", message="WBO go long")
alertcondition(Goshort, title="weakly breakout go short", message="WBO go short")
//------------------------------------------------------------------------------------------
// Filter to show only those signals that follow on the previous position (long or short) that was a loss.