EveryDayBetter
Pine Script Master
Pine Script Master
Posts: 27
Joined: February 16th, 2022

How to transform this code into a long only strategy?

Hello, here ist the origin code:

Code: Select all

//@version=5
indicator("Mutiple Alerts", overlay=true)
i_T1 = input.symbol('BTCUSD')
i_T2 = input.symbol('AMC')
i_T3 = input.symbol('AMD')

f_strategy() =>
    smaFast = ta.sma(close, 21)
    smaSlow = ta.sma(close, 50)
    goLong = ta.crossover(smaFast, smaSlow)
    goShort = ta.crossunder(smaFast, smaSlow)
    signal = goLong ? 1 : goShort ? -1 : 0
    signal

f_screener(_ticker) =>
    message = ''


    [signal, _tickerClose, _OP, hi] = request.security(_ticker, timeframe.period, [f_strategy(), close, open, high])

    if signal == 1
        message := 'Buy ' + _ticker + '@' + str.tostring(_tickerClose)
        message
    else if signal == -1
        message := 'Sell ' + _ticker + '@' + str.tostring(_tickerClose)
        message

    if signal == 1 or signal == -1
        alert(message, alert.freq_once_per_bar_close)
    signal


t1_signal = f_screener(i_T1)
plot(t1_signal, title='T1 Signal')

t2_signal = f_screener(i_T2)
plot(t2_signal, title='T2 Signal')

t3_signal = f_screener(i_T3)
plot(t3_signal, title='T3 Signal')

plot(ta.sma(close, 21), color=color.new(color.red, 0))
plot(ta.sma(close, 50), color=color.new(color.green, 0))

When I remove the short signal like this, I get some "Mismatched input 'end of line without line continuation' expecting ':'." issues. How is it possible to transform it into a long only strategy an keep the alerts for long signal?

Code: Select all

f_strategy() =>
    smaFast = ta.sma(close, 21)
    smaSlow = ta.sma(close, 50)
    goLong = ta.crossover(smaFast, smaSlow)
 
     signal = goLong ? 1 
    signal

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: How to transform this code into a long only strategy?

It returns this error as you have left the if logic incomplete.
Try this
signal = goLong ? 1 : 0
This tells if goLong is true than return 1 else a 0.

You were missing the else part.

EveryDayBetter
Pine Script Master
Pine Script Master
Posts: 27
Joined: February 16th, 2022

Re: How to transform this code into a long only strategy?

Thank you very much. Now, it works.

Return to “Pine Script Q&A”