test automail
test automail

Reputation: 13

Signals getting generated multiple times

I have one strategy, which is working well. I want to convert it into study (indicator), while doing that I am facing issue of signals getting generated multiple times.

I am not sure what's wrong I am doing here.

Strategy code

//@version=4
strategy("Debug_Strategy", overlay=true)

// inputs:
inp_lkb = input( title='Lookback Period', defval=300)
percen_above_hh = input( title='% above HH (SL) (0-100)', defval=1)
percen_above_vwap = input( title='% From spike made (Exit) (1-100)', defval=1)
perc_price_change = input( title='% change (1-100)', defval=5)
inp_diff=input(title='min. diff. between MAs (0-5)',defval=0.25)
// Price change
// study(title="change in full %", shorttitle="%c", overlay=false, resolution="")
change = ((close - close[inp_lkb]) / close[inp_lkb]) * 100
// plot(change, color=color.green,title='Percentage Change from Start to High', linewidth=2)
// Price change

// SMAs
fastMALen = input(title="Fast SMA Length", defval=5)
slowMALen = input(title="Slow SMA Length", defval=18)
fastMA = sma(close, fastMALen)
slowMA = sma(close, slowMALen)
// 

// Stop input
hiHighs = highest(high, inp_lkb)[1]
lowLows= lowest(low,inp_lkb)[1]
pos_size = 1000
// 
spikeMadeEntry=hiHighs-lowLows
tpEntry =  hiHighs- (spikeMadeEntry*percen_above_vwap/100)
diff=abs(fastMA-slowMA)/((fastMA+slowMA)/2) *100

//(have included a calculation but using the built in vwap function for speed, as you are on 1 second)
if change >= perc_price_change and close > vwap(hlc3)  and diff>inp_diff  and crossunder(fastMA, slowMA) and close>tpEntry
    strategy.entry("sell", strategy.short, 1000, when=strategy.position_size <= 0) 
    
//take_profit = close * (1+percen_above_vwap)
sold = strategy.position_size[0] < strategy.position_size[1]
sl = valuewhen(sold, hiHighs*(1+percen_above_hh/100), 0)
//sl = valuewhen(bought, hiHighs, 0)
spikeMade=valuewhen(sold,hiHighs-lowLows,0)
tp =  valuewhen(sold,hiHighs- (spikeMade*percen_above_vwap/100),0)

if  high >= sl or low <= tp
    strategy.entry("buy", strategy.long, 0, when=strategy.position_size < 0) 
//plot(strategy.equity)

plot(sl, color=color.orange,title='StopLoss', linewidth=1)
plot(tp, color=color.blue,title='Target Price', linewidth=1)
plot(hiHighs, color=color.green,title='PastHigh', linewidth=1)
plot(lowLows, color=color.white,title='PastLow', linewidth=1)
//plot(vwap(hlc3), color=color.green,title='VWAP', linewidth=1)
plot(slowMA, color=color.yellow, title='Slow MA', linewidth=1)
plot(fastMA, color=color.red, title='Fast MA', linewidth=1)
//plot(change, color=color.white, title='debig', linewidth=1)

Strategy settings

Strategy trades

Study (indicator) code

//@version=4
study("Debug_v2", overlay=true)

var isLong = false
var isShort = false


// inputs:
inp_lkb = input( title='Lookback Period', defval=300)
percen_above_hh = input( title='% above HH (SL) (0-100)', defval=1)
percen_above_vwap = input( title='% From spike made (Exit) (1-100)', defval=1)
perc_price_change = input( title='% change (1-100)', defval=5)
inp_diff=input(title='min. diff. between MAs (0-5)',defval=0.25)
// Price change
// study(title="change in full %", shorttitle="%c", overlay=false, resolution="")
change = ((close - close[inp_lkb]) / close[inp_lkb]) * 100
// plot(change, color=color.green,title='Percentage Change from Start to High', linewidth=2)
// Price change

// SMAs
fastMALen = input(title="Fast SMA Length", defval=5)
slowMALen = input(title="Slow SMA Length", defval=18)
fastMA = sma(close, fastMALen)
slowMA = sma(close, slowMALen)
// 

// Stop input
hiHighs = highest(high, inp_lkb)[1]
lowLows= lowest(low,inp_lkb)[1]
pos_size = 1000
// 
spikeMadeEntry=hiHighs-lowLows
tpEntry =  hiHighs- (spikeMadeEntry*percen_above_vwap/100)
diff=abs(fastMA-slowMA)/((fastMA+slowMA)/2) *100

sellSignal = not isShort and change >= perc_price_change and close > vwap(hlc3)  and diff>inp_diff  and crossunder(fastMA, slowMA) and close>tpEntry

//(have included a calculation but using the built in vwap function for speed, as you are on 1 second)
if sellSignal
    isLong := false
    isShort := true
//else
//    isLong := false
//    isShort := false
    
//take_profit = close * (1+percen_above_vwap)
sold = isShort
sl = hiHighs*(1+percen_above_hh/100)
//sl = valuewhen(bought, hiHighs, 0)
spikeMade=hiHighs-lowLows
tp =  hiHighs- (spikeMade*percen_above_vwap/100)


buySignal = not isLong  and high >= sl or low <= tp

if  buySignal
    isLong := true
    isShort := false
//else
//    isLong := false
//    isShort := false

plot(sl, color=color.orange,title='StopLoss', linewidth=1)
plot(tp, color=color.blue,title='Target Price', linewidth=1)
plot(hiHighs, color=color.green,title='PastHigh', linewidth=1)
plot(lowLows, color=color.white,title='PastLow', linewidth=1)
//plot(vwap(hlc3), color=color.green,title='VWAP', linewidth=1)
plot(slowMA, color=color.yellow, title='Slow MA', linewidth=1)
plot(fastMA, color=color.red, title='Fast MA', linewidth=1)
//plot(change, color=color.white, title='debig', linewidth=1)

plotshape(series=buySignal, title="BUY", text="BUY", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(series=sellSignal, title="SELL", text="SELL", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)

same settings used for indicator. Symbol used. "WIMI", Time frame used. 1 min.

Indicator result

Upvotes: 0

Views: 268

Answers (1)

vitruvius
vitruvius

Reputation: 21302

Your low <= tp condition is almost always true. Since your buySignal have that condition with an or operator, buySignal is evaluated to true. That's why you always have the "BUY" signals.

Add this to your chart. This is how you can debug. I basically plot all the conditions for buySignal and then see which of those are true and false:

//@version=4
study("Debug_v3", overlay=false)

var isLong = false
var isShort = false


// inputs:
inp_lkb = input( title='Lookback Period', defval=300)
percen_above_hh = input( title='% above HH (SL) (0-100)', defval=1)
percen_above_vwap = input( title='% From spike made (Exit) (1-100)', defval=1)
perc_price_change = input( title='% change (1-100)', defval=5)
inp_diff=input(title='min. diff. between MAs (0-5)',defval=0.25)
change = ((close - close[inp_lkb]) / close[inp_lkb]) * 100

// SMAs
fastMALen = input(title="Fast SMA Length", defval=5)
slowMALen = input(title="Slow SMA Length", defval=18)
fastMA = sma(close, fastMALen)
slowMA = sma(close, slowMALen)

// Stop input
hiHighs = highest(high, inp_lkb)[1]
lowLows= lowest(low,inp_lkb)[1]
pos_size = 1000

spikeMadeEntry=hiHighs-lowLows
tpEntry =  hiHighs- (spikeMadeEntry*percen_above_vwap/100)
diff=abs(fastMA-slowMA)/((fastMA+slowMA)/2) *100

sellSignal = not isShort and change >= perc_price_change and close > vwap(hlc3)  and diff>inp_diff  and crossunder(fastMA, slowMA) and close>tpEntry

//(have included a calculation but using the built in vwap function for speed, as you are on 1 second)
if sellSignal
    isLong := false
    isShort := true

sold = isShort
sl = hiHighs*(1+percen_above_hh/100)
spikeMade=hiHighs-lowLows
tp =  hiHighs- (spikeMade*percen_above_vwap/100)


buySignal = not isLong  and high >= sl or low <= tp

if  buySignal
    isLong := true
    isShort := false

plot(not isLong ? 1 : 0)
plot(high >= sl ? 3: 2)
plot(low <= tp ? 5 : 4)

enter image description here

Upvotes: 1

Related Questions