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

时间:2020-05-14 17:00:32

标签: plot pine-script

当价格突破上一季度高点和上个月高点,然后跌破前一周低点时,我希望在所有蜡烛位置获得支撑,收盘价成为支撑。 只要价格保持在前一周之下,每次收盘都将成为支撑。只要价格不跌破前一个季度低点 OR 或低于前一个月低点,该条件仍然存在,支撑也将停止。现在,它仅在交叉处绘图。

对于阻力而言则相反。 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 个答案:

答案 0 :(得分:1)

每当遇到问题时,我总是用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)

这是结果:

enter image description here

如果仔细观察,这里的问题是crossover()crossunder()函数仅在发生交叉/穿越时才返回BOOL值。因此,您在绘图函数中的条件仅对一格变为TRUE

您需要做的是弄清楚如何保持信号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 )

enter image description here

请注意,在上图中,以下指示符(调试一个)仍然具有您的代码。所以您可以进行比较。