Vinayak Verma
Vinayak Verma

Reputation: 11

Pinescript giving unexpected alerts

i am new to this automated trading, but recently i developed a system using supertrend and breakouts. but i am facing trouble in setting alerts, 90% time they are working fine but sometimes i am getting alerts without even signal is generated.

problems faced till now:

  1. TP alert before it actually gets trigger, 1 or 2 candle before the signal is generated.
  2. long signal is generated, quantity is bought but after 2 candle again got the message of long without any relevant signal generated on trading view, causing binance to buy double the quantity.

////////////////////script////////////////////

`//@version=5
strategy("MATIC algo supertrend", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, process_orders_on_close=true)
import PineCoders/VisibleChart/4

atrPeriod = input(30, "ATR Length")
factor = input.float(3.02, "Factor", step = 0.01)

[supertrend, direction] = ta.supertrend(factor, atrPeriod)

bodyMiddle = plot((open + close) / 2, display=display.none)
upTrend = plot(direction < 0 ? supertrend : na, "Up Trend", color = color.green, style=plot.style_linebr)
downTrend = plot(direction < 0? na : supertrend, "Down Trend", color = color.red, style=plot.style_linebr)

fill(bodyMiddle, upTrend, color.new(color.green, 90), fillgaps=false)
fill(bodyMiddle, downTrend, color.new(color.red, 90), fillgaps=false)

//@version=5

length = input.int(12, 'Swing Detection Lookback')
mult = input.float(0., 'Slope', minval = 0, step = .1)
calcMethod = input.string('Atr', 'Slope Calculation Method', options = ['Atr','Stdev','Linreg'])
backpaint = input(false, tooltip = 'Backpainting offset displayed elements in the past. Disable backpainting to see real time information returned by the indicator.')

//Style
upCss = input.color(color.teal, 'Up Trendline Color', group = 'Style')
dnCss = input.color(color.red, 'Down Trendline Color', group = 'Style')
showExt = input(true, 'Show Extended Lines')

//-----------------------------------------------------------------------------}
//Calculations
//-----------------------------------------------------------------------------{
var upper = 0.
var lower = 0.
var slope_ph = 0.
var slope_pl = 0.

var offset = backpaint ? length : 0

n = bar_index
src = close

ph = ta.pivothigh(length, length)
pl = ta.pivotlow(length, length)

//Slope Calculation Method
slope = switch calcMethod
    'Atr'    => ta.atr(length) / length * mult
    'Stdev'  => ta.stdev(src,length) / length * mult
    'Linreg' => math.abs(ta.sma(src * n, length) - ta.sma(src, length) * ta.sma(n, length)) / ta.variance(n, length) / 2 * mult

//Get slopes and calculate trendlines
slope_ph := ph ? slope : slope_ph
slope_pl := pl ? slope : slope_pl

upper := ph ? ph : upper - slope_ph
lower := pl ? pl : lower + slope_pl

var upos = 0
var dnos = 0
upos := ph ? 0 : close > upper - slope_ph * length ? 1 : upos
dnos := pl ? 0 : close < lower + slope_pl * length ? 1 : dnos

//-----------------------------------------------------------------------------}
//Extended Lines
//-----------------------------------------------------------------------------{
var uptl  = line.new(na,na,na,na, color = upCss, style = line.style_dashed, extend = extend.right)
var dntl  = line.new(na,na,na,na, color = dnCss, style = line.style_dashed, extend = extend.right)

if ph and showExt
    uptl.set_xy1(n-offset, backpaint ? ph : upper - slope_ph * length)
    uptl.set_xy2(n-offset+1, backpaint ? ph - slope : upper - slope_ph * (length+1))

if pl and showExt
    dntl.set_xy1(n-offset, backpaint ? pl : lower + slope_pl * length)
    dntl.set_xy2(n-offset+1, backpaint ? pl + slope : lower + slope_pl * (length+1))

//-----------------------------------------------------------------------------}
//Plots
//-----------------------------------------------------------------------------{
plot(backpaint ? upper : upper - slope_ph * length, 'Upper', color = ph ? na : upCss, offset = -offset)
plot(backpaint ? lower : lower + slope_pl * length, 'Lower', color = pl ? na : dnCss, offset = -offset)

//Breakouts
plotshape(upos > upos[1] ? low : na, "Upper Break"
  , shape.labelup
  , location.absolute
  , upCss
  , text = "B"
  , textcolor = color.white
  , size = size.tiny)

plotshape(dnos > dnos[1] ? high : na, "Lower Break"
  , shape.labeldown
  , location.absolute
  , dnCss
  , text = "B"
  , textcolor = color.white
  , size = size.tiny)
 
TP = 0.02
SOS = 0.005
SL = 0.15
 
///longs//
if ta.change(direction) < 0 
    strategy.entry("My Long Entry Id", strategy.long) 
    alert("close MATICUSDT a=USDM \nbuy MATICUSDT q=500% a=USDM")

if ((strategy.position_size > 0) and close > strategy.position_avg_price * (1 + TP))
    strategy.exit(id = "TP" , stop = low)
    alert("close MATICUSDT a=USDMM")

if ta.change((direction) < 0 and upos > upos[1] and strategy.position_size == 0)
    strategy.entry("My Long Entry Id", strategy.long)
    alert("buy MATICUSDT q=500% a=USDM")
 

//shorts//
if ta.change(direction) > 0
    strategy.entry("My Short Entry Id", strategy.short)
    alert("close MATICUSDT a=USDM \nsell MATICUSDT q=500% a=USDM")

if ((strategy.position_size < 0) and close < strategy.position_avg_price * (1 - TP))
    strategy.exit(id = "TP" ,stop = high)
    alert("close MATICUSDT a=USDMM")
 
if ta.change((direction) > 0 and dnos > dnos[1] and strategy.position_size == 0)
    strategy.entry("My Short Entry Id", strategy.short)
    alert("sell MATICUSDT q=500% a=USDM")`

help pls
thank youuu


i just want regular alerts without any repainting or false signals

Upvotes: 0

Views: 102

Answers (1)

Gu5tavo71
Gu5tavo71

Reputation: 1214

  1. Your strategy.exit has stop, but no limit
    You must use limit for TP

  2. Your code has six strategy.entry. That's why you have multiple entries.
    It should have one strategy.entry for long, and one strategy.entry for short.
    Now it has two long entries, and two short entries.

Upvotes: 0

Related Questions