0

enter image description here

As shown in the image you can see 6 buy signals when the candle high crosses the green ( target line /entry).
I only need the one buy signal when the first candle crosses the entry line. but as you can see I am getting 6.

This is my code

//VWAP ploting
vwapSource = close
vwap = ta.vwap(vwapSource)
plot(vwap, color=color.blue, linewidth=2, title="VWAP")

//date and time input
inputTime =  input.time(timestamp("026 Jul 2023 09:45:00 GMT+5:30"), "Date")

//Searching Bar by time
searchBarTime = timestamp(year(time), month(time), dayofmonth(time), hour(inputTime), minute(inputTime))

// Check if the current bar matches the desired time
isTargetBar = time == searchBarTime

//ploting RSI and avg RSI
len = input(title="RSI Length", defval=14)
src = input(title="RSI Source", defval=close)
buffer = input(0.001, title="Buffer Length")
 
textColor = color.white
noneColor = color.new(color.white, 100)

ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(14, title="MA Length", group="MA Settings")
 
osc = ta.rsi(src, len)
rsiMA = ma(osc, maLengthInput, maTypeInput)

// plot(osc, title="RSI", linewidth=2, color=color.rgb(255, 255, 255, 20))
// plot(rsiMA, "RSI-based MA", color=color.yellow)

bufferHigh = high + (high * buffer)
bufferLow = low - (low * buffer)

bool isVwapAboveCandle = na
bool isVwapBelowCandle = na
bool isRsiAboveAvg = na
bool isRsipBelowAvg = na

if isTargetBar
    isVwapAboveCandle := vwap > close
    isVwapBelowCandle := vwap < close
    isRsiAboveAvg := osc > rsiMA
    isRsipBelowAvg := osc < rsiMA

buySignal = isVwapBelowCandle and isRsiAboveAvg
shortSignal = isVwapAboveCandle and isRsipBelowAvg


if buySignal
    buySignalTriangleUp = label.new(bar_index,(low-(0.004 * low)), style=label.style_triangleup, color=color.lime)

if shortSignal
    shortSignalTriangleDown = label.new(bar_index,(high+(0.004 * high)), style=label.style_triangledown, color=color.red)

// plotshape(buySignal, location = location.belowbar, style = shape.arrowup,  color = color.green, text ="BUY")
// plotshape(shortSignal, location = location.abovebar, style = shape.arrowdown,  color = color.red, text ="SHORT")

// //=====stoploss=====
var float sl = 0.0
for i = 1 to 5
    if buySignal or shortSignal //or shortcondition or pe_signalcondition1[i] or pe_signalcondition2[i])
        sl := bufferHigh - bufferLow
// //=========define target for long trades =======
tp1 = sl
tp2 = sl*2
tp3 = sl*3

if buySignal
    l1 = line.new ( bar_index, bufferHigh , bar_index + 7, bufferHigh, color = color.green, width = 2)
    l2 = line.new ( bar_index, bufferLow , bar_index + 7 , bufferLow , color = color.red , width = 2)
    l3 = line.new (bar_index, bufferHigh + tp1 , bar_index + 10 , bufferHigh+tp1, style=line.style_dotted, color=color.green, width = 1)
    l4 = line.new (bar_index, bufferHigh + tp2 , bar_index + 10 , bufferHigh+tp2, style=line.style_dotted, color=color.green, width = 1)
    l5 = line.new (bar_index, bufferHigh + tp3 , bar_index + 10 , bufferHigh+tp3, style=line.style_dotted, color=color.green, width = 1)
    
if shortSignal
    s1 = line.new ( bar_index, bufferLow , bar_index + 7, bufferLow, color = color.green, width = 2)
    s2 = line.new ( bar_index, bufferHigh , bar_index + 7, bufferHigh, color = color.red, width = 2)
    s3 = line.new ( bar_index, bufferLow - tp1, bar_index + 10, bufferLow - tp1, color = color.green,style=line.style_dotted, width = 1)
    s4 = line.new ( bar_index, bufferLow - tp2, bar_index + 10, bufferLow - tp2, color = color.green,style=line.style_dotted, width = 1)
    s5 = line.new ( bar_index, bufferLow - tp3, bar_index + 10, bufferLow - tp3, color = color.green,style=line.style_dotted, width = 1)

buyLine = buySignal ? bufferHigh : na
StopLossforBuy = buySignal ? bufferLow : na
shortLine = shortSignal ? bufferLow : na
StopLossforSell = shortSignal ? bufferHigh : na

longCondition = high >= buyLine[1] or high >= buyLine[2] or high >= buyLine[3] or high >= buyLine[4] or high >= buyLine[5] or high >= buyLine[6]
longPositionNegated = low <= StopLossforBuy[1] or low <= StopLossforBuy[2] or low <= StopLossforBuy[3] or low <= StopLossforBuy[4] or low <= StopLossforBuy[5] or  low <= StopLossforBuy[6] 
shortCondition = low <= shortLine[1] or low <= shortLine[2] or low <= shortLine[3] or low <= shortLine[4] or low <= shortLine[5] or  low <= shortLine[6] 
shortPositionNegated = high >= StopLossforSell[1] or high >= StopLossforSell[2] or high >= StopLossforSell[3] or high >= StopLossforSell[4] or high >= StopLossforSell[5] or  high >= StopLossforSell[6] 

if longPositionNegated
    pnL = label.new(bar_index,(low- (0.004 * low)), style=label.style_arrowup, color=color.white, text="pNeg", textcolor=color.white)
    label.delete(pnL)

else if shortPositionNegated
    pnS = label.new(bar_index,(high+ (0.002 * high)), style=label.style_arrowdown, color=color.white, text="pNeg", textcolor=color.white)
    
else if longCondition
    p1 = label.new(bar_index,(low- (0.004 * low)), style=label.style_arrowup, color=color.white, text="BUY", textcolor=color.white)

else if shortCondition
    p2 = label.new(bar_index,(high+ (0.002 * high)), style=label.style_arrowdown, color=color.white, text="SELL", textcolor=color.white)

I tried storing the bar index and all but I was not getting a proper way to do it. I am new to Pine script

1 Answers1

0

You have a condition like high >= buyLine[1]. This will be true as long as the high price is above your buyLine[1]. That's why you are getting multiple labels.

Instead of doing that, you can use the ta.crossover() function which will return true only on the bar that price crosses over your line. And for shorts, you can use ta.crossunder().

vitruvius
  • 15,740
  • 3
  • 16
  • 26
  • `longCondition = ta.crossover(high, buyLine[1] ) or ta.crossover(high, buyLine[2] ) or ta.crossover(high, buyLine[2]) or ta.crossover(high, buyLine[3]) or ta.crossover(high, buyLine[4]) or ta.crossover(high, buyLine[5]) or ta.crossover(high, buyLine[6])` i modified my code to this , but did not work – Parshuram Gore Jun 29 '23 at 05:09