需要绘制所有相关蜡烛的支撑和阻力,但它只在交叉点绘制

Need to plot the support and resistance @ all relevant candles, but it is only plotting at crossover

我想在价格上穿前一季度高点和前一个月高点,然后下穿上周低点时绘制所有蜡烛的支撑,收盘价变为一个支持。 只要价格保持在前一周 高位 以下,每次收盘都成为支撑。只要价格不低于上季度低点 上月低点,这种情况就会持续,支撑也会停止。它现在只在交叉处绘制。

抵抗则相反。 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 )

每当我遇到问题时,我总是使用 study(overlay=false) 克隆我的代码并绘制一些我怀疑导致问题的信号。

因此,在您的情况下,您可以将代码复制并粘贴到新指标并在末尾添加以下行。请删除所有其他 plot() 函数。否则,您将遇到缩放问题并且结果不会那么明显。

//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)

这是结果:

如果你仔细观察,这里的问题是 crossover()crossunder() 函数 return 一个 BOOL 值只有当 crossover/crossunder 发生时。因此,您在 plot 函数中的条件变为 TRUE 仅一根柱。

您需要做的是弄清楚如何为 crossover/crossunder 保持信号 TRUE,直到遇到相反的信号。

为此,我建议您开始使用 v4。然后你可以使用 var 关键字。使用 var 关键字创建的变量会保留它们的值,直到您覆盖它们。因此,您可以执行以下操作:

var isSupport = false
var isResistance = false 

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

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

这是使用 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 )

请注意,在上图中,下面的指示器(调试指示器)仍然有您的代码。所以你可以比较一下。