LFranco
LFranco

Reputation: 11

Fix Tradingview indicator - compare with current price

I'm trying to get alerts when 2 moments (conditions 1 and 2) are identified within the Tradingview indicator down below.

Condition 1- The period right after the red and green dots are displayed, especially when the dots are identified right before the current candle or [1]. I tried the following commands without success:HHcondition = plotH[1] LLcondition = plotL[1]

Condition 2- The current candle (current price) is greater or lower than the previous dots, green or red respectively. It should compare the " close"  value to the to the most recent dots. Green for high and red for low.I tried without success with the following command:HH= close > plotH LL= plotL>close

I'm using the same alert for both conditions.

Here is the indicator:

//@version=4
study("(Pivot Points High Low)", shorttitle="Pivots HL", overlay=true)
lenH = input(title="Length High", type=input.integer, defval=6, minval=1)
lenL = input(title="Length Low", type=input.integer, defval=6, minval=1)

fun(src, len, isHigh) =>
    p = nz(src[len])
    isFound = true
    for i = 0 to len - 1
        if isHigh and src[i] > p
            isFound := false
        if not isHigh and src[i] < p
            isFound := false
    for i = len + 1 to 2 * len
        if isHigh and src[i] >= p
            isFound := false
        if not isHigh and src[i] <= p
            isFound := false
    if isFound
        p

plotH = fun(high, lenH, true)
plotL = fun(low, lenL, false)

// Get highest body and lowest body for the current candle
highestBody = open > close ? open : close
lowestBody = open > close ? close : open

// Conditions
HHCondition1= plotH[1]
LLCondition1 = plotL[1]
HHCondition2= close > plotH
LLCondition2= plotL>close

BuyCondition=LLCondition1 or LLCondition2
SellCondition=HHCondition1 or HHCondition1

//plotshape(BuyCondition, title="buy", style=shape.triangleup,location=location.belowbar, color=color.orange, transp=0, size=size.small)
//plotshape(SellCondition, title="sell", style=shape.triangledown,location=location.abovebar, color=color.green, transp=0, size=size.small)

plot(plotH, color=color.lime, linewidth=3, style=plot.style_circles, offset=-lenH)
plot(plotL, color=color.red,  linewidth=3, style=plot.style_circles, offset=-lenL)

alertcondition(BuyCondition, title="buy",message="buy") 
alertcondition(SellCondition, title="sell",message="sell") 

Upvotes: 0

Views: 380

Answers (1)

rumpypumpydumpy
rumpypumpydumpy

Reputation: 3833

You'll need to return both when a high or low is found and the price value. Then you can store the price value using a var declared variable that is only modified when isFound occurs. You'll need this for your condition 2's to compare the current close to it.

fun(src, len, isHigh) =>
    p = nz(src[len])
    isFound = true
    for i = 0 to len - 1
        if isHigh and src[i] > p
            isFound := false
        if not isHigh and src[i] < p
            isFound := false
    for i = len + 1 to 2 * len
        if isHigh and src[i] >= p
            isFound := false
        if not isHigh and src[i] <= p
            isFound := false
    if isFound
        [p, isFound]

[plotH, foundH] = fun(high, lenH, true)
[plotL, foundL] = fun(low, lenL, false)

var float pivH = na
var float pivL = na

if foundH
    pivH := plotH
if foundL
    pivL := plotL
    
plot(pivH, color = color.green)
plot(pivL, color = color.red)

HHCondition1 = foundH
HHCondition2 = close > pivH

Upvotes: 1

Related Questions