Juros
Juros

Reputation: 39

Some resistance is not plotted for some unknown reason

I completed the code for support & resistance. I added additional conditions for the previous quarter and month high and low. It plots pretty good now, but in some occasions it does not plot a resistance that should be there, and I cannot figure out why that is. Plotting support & resistance

The green arrows represent support, and red arrows are resistance. At the 2 peaks to ther right, resistance red arrows should be plotted, because the price crossed under the previous quarter and month low, and then crossedup above the previous week high, while staying under the previous quarter and month high, but nothing is plotted.

//@version=4
//By Juros

// Resistance is not plotted 100% accurate, some are missing.


//-------------------------------
// 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?")
prevMH = input(true, title= "Show previous month high")
prevML = input(true, title= "Show previous month low")
prevQH = input(true, title= "Show previous quarter high")
prevQL = input(true, title= "Show previous quarter low")

//previous week high and low
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.fuchsia,transp=20)
plot(prevwkL and prevWeekLow ? prevWeekLow : na, title="Prev Week Low", style=plot.style_stepline, linewidth=1, color=color.fuchsia,transp=20)

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

// Previous month high and low
prevMonthHigh = security(syminfo.tickerid, 'M', high[1], lookahead=true)
prevMonthLow = security(syminfo.tickerid, 'M', low[1], lookahead=true)

plot(prevMH and prevMonthHigh ? prevMonthHigh : na, title="Prev Month High",   style=plot.style_stepline, linewidth=1, color=color.orange, transp=40)
plot(prevML and prevMonthLow ? prevMonthLow : na, title="Prev Month Low", style=plot.style_stepline, linewidth=1, color=color.orange, transp=40)

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

//Previous quarter high and low
prevQuarterHigh = security(syminfo.tickerid, '3M', high[1], lookahead=true)
prevQuarterLow = security(syminfo.tickerid, '3M', low[1], lookahead=true)

plot(prevQH and prevQuarterHigh ? prevQuarterHigh : na, title="Prev Quarter High", style=plot.style_stepline, linewidth=1, color=color.aqua, transp=0)
plot(prevQL and prevQuarterLow ? prevQuarterLow : na, title="Prev Quarter Low", style=plot.style_stepline, linewidth=1, color=color.aqua, transp=0)

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

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


//-------------------------------------------------------------
// weekly support and resistance

var isSupport = false
var isResistance = false 

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

if (crossover(high, prevWeekHigh))
    isSupport := false
    isResistance := true
//-------------------------------------------------------------

// Monthly support
var isSupportM = false

if (crossunder(low, prevMonthLow))
    isSupportM := false

if (crossover(high, prevMonthHigh))
    isSupportM := true

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


// Quarterly support
var isSupportQ = false

if (crossunder(low, prevQuarterLow))
    isSupportQ := false

if (crossover(high, prevQuarterHigh))
    isSupportQ := true

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

// Monthly resistance

var isResistanceM = false 

if (crossunder(low, prevMonthLow))
    isResistanceM := true

if (crossover(high, prevMonthHigh))
    isResistanceM := false
//-------------------------------------------------------------


// Quarterly resistance
var isResistanceQ = false 

if (crossunder(low, prevQuarterLow))
    isResistanceQ := true

if (crossover(high, prevQuarterHigh))
    isResistanceQ := false

//plot support & resistance 
plotshape (upTrend and isSupport and isSupportM and isSupportQ ? close: na, style=shape.arrowup, location=location.belowbar, color=color.green, size=size.normal, transp=0 )
plotshape (not upTrend and isResistance and isResistanceM and isResistanceQ ? close: na, style=shape.arrowdown, location=location.abovebar, color=color.red, size= size.normal, transp=0 )

Upvotes: 1

Views: 94

Answers (1)

vitruvius
vitruvius

Reputation: 21199

Let's look at what's going on with plotting some important signals.

plotshape (not upTrend and isResistance and isResistanceM and isResistanceQ ? close: na, style=shape.arrowdown, location=location.abovebar, color=color.red, size= size.normal, transp=0 )

This is your original plot function to plot the resistance. So, your conditions are not only if the price crosses under the previous month and quarter low, but also the upTrend is a condition there.

I suggest we should have a look at not upTrend, isResistance, isResistanceM, isResistanceQ. Note the not upTrend, because your condition is exatly that and not upTrend itself.

Create a new indicator with overlay=false and copy&paste your original code there. Then remove all plot functions and add below code at the end.

plot(series=not upTrend ? 1:0, title="not upTrend", color=color.orange, linewidth=2)
plot(series=isResistance ? 1:0, title="isResistance", color=color.green, linewidth=2)
plot(series=isResistanceM ? 1:0, title="isResistanceM", color=color.red, linewidth=2)
plot(series=isResistanceQ ? 1:0, title="isResistanceQ", color=color.blue, linewidth=2)

All of those variables must be TRUE in order for your indicator to plot the resistance arrow for any given candle. Well, technically, upTrend must be FALSE because you are using not upTrend. What I mean is not upTrend must evaluate to TRUE.

enter image description here

If you look at the signals, you will see that the orange one (not upTrend) is 0 at those points. This is what is preventing your indicator from plotting the resistance.

So, you should have a look at upTrend.

We can only point what is causing the issue. The fix should come from you because its your algorithm, it's your way of detecting the up trend.

If, however, you think there is something wrong with the calculation of upTrend, that's another topic and requires another question here.

Upvotes: 1

Related Questions