1
votes

I want to plot a support at all candles when the price crosses above the previous quarter high and previous month high, and then crosses down the previous week low, the close becomes a support. As long as the price stays then below the previous week high, each close becomes a support. The condition remains as long as the prices do not cross below previous quarter low OR previous month low, then support also stops. It now only plots at the crossover.

For resistance it is the opposite. image is here

//@version=3 
//By Juros
//-------------------------------
// support
// If the price crosses above the previous quarter high and previous month high, and then crosses down the previous week low, the close becomes a support. 
//As long as the price stays then below the previous week high, each close becomes a support. The    condition remains as long as the prices do not cross 
//below previous quarter low OR previous month low, then support also stops.

//-------------------------------
// resistance
// If the price crosses below the previous quarter low and previous month low, and then crosses up the previous week high, the close becomes a resistance.
// As long as the price stays then above the previous week low, each close becomes a support.  The condition remains as long as the price does not cross
// above previous quarter high OR previous month high, then resistance also stops.


study(title="Universal support and resistance", shorttitle="Univ sup & res", overlay=true, precision=8)
prevwkH = input(true, title="Show previous week high?")
prevwkL = input(true, title="show previous week low?")

//previous week
prevWeekHigh = security(tickerid, 'W', high[1], lookahead=true)
prevWeekLow = security(tickerid, 'W', low[1], lookahead=true)

//previous Week Plots
plot(prevwkH and prevWeekHigh ? prevWeekHigh : na, title="Prev Week High", style=stepline,   linewidth=1, color=green,transp=20)
plot(prevwkL and prevWeekLow ? prevWeekLow : na, title="Prev Week Low", style=stepline, linewidth=1, color=green,transp=20)

// alerts
Buy = close > prevWeekHigh
Sell = close < prevWeekLow

Buyposmemo = false
Buyposmemo := Buy ? true : Sell ? false : Buyposmemo[1]
Buynow = Buy and not (Buyposmemo[1])
bgcolor(Buynow ?  color(green, 90) :na)

Sellposmemo = false
Sellposmemo := Sell ? true : Buy ? false : Sellposmemo[1]
Sellnow = Sell and not (Sellposmemo[1])
bgcolor(Sellnow ?  color(red, 90) :na)

alertcondition(Buy, title = "Buy now", message="buy now")
alertcondition(Sell, title = "Sell now", message="Sell now")

//-------------------------------------------------------------

//universal trend line
prevQuarterHigh = security(tickerid, '3M', high[1], lookahead=true)
prevQuarterLow = security(tickerid, '3M', low[1], lookahead=true)

upTrend = false
upTrend := (not upTrend[1] and crossover(close, prevQuarterHigh)) or (upTrend[1] and not    crossunder(close, prevQuarterLow))

//-------------------------------------------------------------

//plot support & resistance 
plot (upTrend and crossunder(close, prevWeekLow) ? low-(high-low+1) : na, style=circles,    color=green, transp=0, linewidth=4 )
plot (not upTrend and crossover(close, prevWeekHigh) ? high+(high-low+1) : na, style=circles,   color=red, transp=0, linewidth=4 )
1
Can you add an image that shows the problem and what you want to achieve? - Baris Yakut
Hello Baris, just added an image. Tnx. - Juros

1 Answers

1
votes

Whenever I have problems, I always clone my code with study(overlay=false) and plot some of the signals that I suspect causing the problems.

So, in your case, you can copy&paste your code to a new indicator and add the following lines at the end. Please delete all other plot() functions. Otherwise, you will have scaling issues and the result won't be that visible.

//plot support & resistance 
plot(series=upTrend ? 1:0, color=color.orange)
plot(series=crossunder(close, prevWeekLow) ? 1:0, color=color.green)
plot(series=crossover(close, prevWeekHigh) ? 1:0, color=color.red)

This is the result:

enter image description here

If you look closely, the problem here is that crossover() and crossunder() functions return a BOOL value only when the crossover/crossunder happens. Therefore, your condition in the plot function becomes TRUE for one bar only.

What you need to do is figure out how you can keep a signal TRUE for crossover/crossunder until the opposite signal is encountered.

To do this, I suggest you start using v4. Then you can use the var keyword. Variables created with the var keyword hold their values until you overwrite them. So, you can do the following:

var isSupport = false
var isResistance = false 

if (crossunder(close, prevWeekLow))
    isSupport := true
    isResistance := false

if (crossover(close, prevWeekHigh))
    isSupport := false
    isResistance := true

Here is the full code using v4:

//@version=4
//By Juros
//-------------------------------
// support
// If the price crosses above the previous quarter high and previous month high, and then crosses down the previous week low, the close becomes a support. 
//As long as the price stays then below the previous week high, each close becomes a support. The    condition remains as long as the prices do not cross 
//below previous quarter low OR previous month low, then support also stops.

//-------------------------------
// resistance
// If the price crosses below the previous quarter low and previous month low, and then crosses up the previous week high, the close becomes a resistance.
// As long as the price stays then above the previous week low, each close becomes a support.  The condition remains as long as the price does not cross
// above previous quarter high OR previous month high, then resistance also stops.


study(title="Universal support and resistance", shorttitle="Univ sup & res", overlay=true, precision=8)
prevwkH = input(true, title="Show previous week high?")
prevwkL = input(true, title="show previous week low?")

//previous week
prevWeekHigh = security(syminfo.tickerid, 'W', high[1], lookahead=true)
prevWeekLow = security(syminfo.tickerid, 'W', low[1], lookahead=true)

//previous Week Plots
plot(prevwkH and prevWeekHigh ? prevWeekHigh : na, title="Prev Week High", style=plot.style_stepline,   linewidth=1, color=color.green,transp=20)
plot(prevwkL and prevWeekLow ? prevWeekLow : na, title="Prev Week Low", style=plot.style_stepline, linewidth=1, color=color.green,transp=20)

// alerts
Buy = close > prevWeekHigh
Sell = close < prevWeekLow

Buyposmemo = false
Buyposmemo := Buy ? true : Sell ? false : Buyposmemo[1]
Buynow = Buy and not (Buyposmemo[1])
bgcolor(Buynow ?  color(color.green) :na)

Sellposmemo = false
Sellposmemo := Sell ? true : Buy ? false : Sellposmemo[1]
Sellnow = Sell and not (Sellposmemo[1])
bgcolor(Sellnow ?  color(color.red) :na)

alertcondition(Buy, title = "Buy now", message="buy now")
alertcondition(Sell, title = "Sell now", message="Sell now")

//-------------------------------------------------------------

//universal trend line
prevQuarterHigh = security(syminfo.tickerid, '3M', high[1], lookahead=true)
prevQuarterLow = security(syminfo.tickerid, '3M', low[1], lookahead=true)

upTrend = false
upTrend := (not upTrend[1] and crossover(close, prevQuarterHigh)) or (upTrend[1] and not    crossunder(close, prevQuarterLow))

//-------------------------------------------------------------
var isSupport = false
var isResistance = false 

if (crossunder(close, prevWeekLow))
    isSupport := true
    isResistance := false

if (crossover(close, prevWeekHigh))
    isSupport := false
    isResistance := true

//plot support & resistance 
plot (upTrend and isSupport ? low-(high-low+1) : na, style=plot.style_circles, color=color.green, transp=0, linewidth=4 )
plot (not upTrend and isResistance ? high+(high-low+1) : na, style=plot.style_circles, color=color.red, transp=0, linewidth=4 )

enter image description here

Note that in the image above, the below indicator (debug one) still has your code. So you can kinda compare.