Search code examples
pine-scriptpine-script-v5

I want to automate tradingview with capitalise.ai


I want to automate tradingview with capitalise.ai.

Now I get 2 signals for each candle, but I only want to get 1 signal if it is at a SellSignal or if it is at a BuySignal.

Can this be solved?

//@version=5
strategy("My Strategy", overlay=true)

//HMA
len6 = 125
src6 = close
hma = ta.wma(2 * ta.wma(src6, len6 / 2) - ta.wma(src6, len6), math.floor(math.sqrt(len6)))
hmacolor = close > hma ? #00ccff : #ff0055
plot(hma, title='HMA Line', color=color.new(hmacolor, 25), linewidth=5)

//Condition    
if (hmacolor == #00ccff and hmacolor[1] != #00ccff)
    strategy.entry("Long", strategy.long)
if (hmacolor == #ff0055 and hmacolor[1] != #ff0055)
    strategy.entry("Short", strategy.short)

//Alert
ttbuystring = 'Input your custom alert message here.'
ttsellstring = 'Input your custom alert message here.'
ttstopstring = 'Input your custom alert message here.'
usebuyalert = input.bool(defval=true, title='Use BUY Alert', group='Alert Messages')
buystring = input.string(title='Buy Alert Message', defval='BUY', confirm=false, group='Alert Messages', tooltip=ttbuystring)
usesellalert = input.bool(defval=true, title='Use SELL Alert', group='Alert Messages')
sellstring = input.string(title='Sell Alert Message', defval='SELL', confirm=false, group='Alert Messages', tooltip=ttsellstring)

if usebuyalert
    alert(buystring, alert.freq_once_per_bar)
if usesellalert
    alert(sellstring, alert.freq_once_per_bar)

Solution

  • //@version=5
    strategy("My Strategy", overlay=true)
    
    //HMA
    len6 = 125
    src6 = close
    hma = ta.wma(2 * ta.wma(src6, len6 / 2) - ta.wma(src6, len6), math.floor(math.sqrt(len6)))
    hmacolor = close > hma ? #00ccff : #ff0055
    plot(hma, title='HMA Line', color=color.new(hmacolor, 25), linewidth=5)
    
    //Variables for alerts
    ttbuystring = 'Input your custom alert message here.'
    ttsellstring = 'Input your custom alert message here.'
    ttstopstring = 'Input your custom alert message here.'
    usebuyalert = input.bool(defval=true, title='Use BUY Alert', group='Alert Messages')
    buystring = input.string(title='Buy Alert Message', defval='BUY', confirm=false, group='Alert Messages', tooltip=ttbuystring)
    usesellalert = input.bool(defval=true, title='Use SELL Alert', group='Alert Messages')
    sellstring = input.string(title='Sell Alert Message', defval='SELL', confirm=false, group='Alert Messages', tooltip=ttsellstring)
    
    //Condition    
    if (hmacolor == #00ccff and hmacolor[1] != #00ccff) and strategy.position_size < 0
        strategy.close("Short")
    
    if (hmacolor == #ff0055 and hmacolor[1] != #ff0055) and strategy.position_size > 0
        strategy.close("Long")
    
    if (hmacolor == #00ccff and hmacolor[1] != #00ccff) and strategy.position_size <= 0
        strategy.entry("Long", strategy.long)
        if usebuyalert
            alert(buystring, alert.freq_once_per_bar)
    
    if (hmacolor == #ff0055 and hmacolor[1] != #ff0055) and strategy.position_size >= 0
        strategy.entry("Short", strategy.short)
        if usesellalert
            alert(sellstring, alert.freq_once_per_bar)