Ponderosa
Pine Script Scholar
Pine Script Scholar
Posts: 9
Joined: January 11th, 2023

Function not plotting as intended. PLEASE HELP.

So I got this script layout from a youtube video. I finally got it to send alerts, however it isn't triggering properly. The first plot function (yellow triangleup) is not working as intended. The second plot function (orange diamonds) seems to work correctly. I want them to print together. When I set this up for 'COINBASE:BTCUSD' it works perfectly, but for some reason on stocks, it gets out of wack. It almost seems like the time periods are offset by half an hour or something. I've been trying to figure this out for days now. If you have any ideas I'd love to hear them. Thank you!!

//@version=5
indicator('Test Script', overlay=true)

Timeframe = input.timeframe('60')

i_T26 = input.symbol('NYSE:DINO')

f_strategy() =>
test = close > open*1.001 and barstate.isconfirmed ? 1 : close < open*0.999 and barstate.isconfirmed ? -1 : 0

f_screener(_ticker) =>
message = ''
[signal, _tickerClose] = request.security(_ticker, Timeframe, [f_strategy(), close])
if signal == 1
message := 'Go Long ' + _ticker + '@' + str.tostring(_tickerClose)
else if signal == -1
message := 'Go Short ' + _ticker + '@' + str.tostring(_tickerClose)

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

t26_signal = f_screener(i_T26)
plotshape(t26_signal, title='Alert', style=shape.triangleup, location=location.belowbar,
color=color.yellow, size=size.large)

plotshape(f_strategy()==-1 or f_strategy()==1, title='debug', style=shape.diamond,
location=location.belowbar, color=color.orange, size=size.large)

Image Image

Ponderosa
Pine Script Scholar
Pine Script Scholar
Posts: 9
Joined: January 11th, 2023

Re: Function not plotting as intended. PLEASE HELP.

Also, had never written a line of code before about a month ago so please bear with my limited understanding on Pine. Thanks.

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: Function not plotting as intended. PLEASE HELP.

Hey Ponderosa,

Firstly when you copy code can you please indent if statements, custom functions and line continuations

Your code is not compiling because of the 3rd line below does not assign anything to signal
if signal == 1 or signal == -1
alert(message, alert.freq_once_per_bar_close)
signal

I'm also trying to understand your custom function f_screener(_ticker). Can you please exiplain what it is doing?

Thanks

Ponderosa
Pine Script Scholar
Pine Script Scholar
Posts: 9
Joined: January 11th, 2023

Re: Function not plotting as intended. PLEASE HELP.

Sorry, I'm pretty sure it was indented in the draft but they were realigned when submitted. I saw that immediately and was like aww what the heck?

The script does compile and like I say, it runs well on crypto markets but goes haywire on regular equities. The 'signal' line is part of the screener function; it will not compile without it. I really wish I could post a screenshot of my chart but here it is on twitter so you can see what I'm talking about: https://twitter.com/GrayMan1111/status/ ... XqEyw&s=19

The purpose of the script is to run alerts on multiple securities from a single alert. So I can add:
i_T1 = input.symbol('AAPL')
i_T2 = input.symbol('AFL')
i_T3 = input.symbol('ALLY')
i_T4 = input.symbol('AMD')
i_T5 = input.symbol('AMZN')
etcetera, and a plotshape function for each. Then just ignore the symbol the alert was generated on and read the message.

Thanks a lot for the reply Steve, I really really do appreciate it.

Ponderosa
Pine Script Scholar
Pine Script Scholar
Posts: 9
Joined: January 11th, 2023

Re: Function not plotting as intended. PLEASE HELP.

I'm sure you know this but just in case you overlooked it, the first plotshape function is pulling from the input.symbol input while the second plotshape function is pulling from the chart.

Ponderosa
Pine Script Scholar
Pine Script Scholar
Posts: 9
Joined: January 11th, 2023

Re: Function not plotting as intended. PLEASE HELP.

The issue also vanishes on the daily timeframe, so only happening on the shorter intraday timeframes. It definitely seems to be a timeframe issue to me but I have no idea how to remedy it.

Steve Burman
Moderator
Moderator
Posts: 109
Joined: January 13th, 2023

Re: Function not plotting as intended. PLEASE HELP.

Sorry I can't help you on this one. Hopefully someone else can.

RobAgrees
Pine Script Rookie
Pine Script Rookie
Posts: 3
Joined: March 17th, 2023
Contact: TradingView Profile

Re: Function not plotting as intended. PLEASE HELP.

Not sure what the issue was. I reformatted the script and it works on every timeframe and asset I tried

Image

Code: Select all

//@version=5
indicator('Test Script', overlay=true)

Timeframe = input.timeframe('60')

i_T26 = input.symbol('NYSE:DINO')

f_strategy() =>
    test = close > open*1.001 and barstate.isconfirmed ? 1 : close < open*0.999 and barstate.isconfirmed ? -1 : 0

f_screener(_ticker) =>
    message = ''

    [signal, _tickerClose] = request.security(i_T26, Timeframe, [f_strategy(), close])

    if signal == 1
        message := 'Go Long ' + _ticker + '@' + str.tostring(_tickerClose)
    else if signal == -1
        message := 'Go Short ' + _ticker + '@' + str.tostring(_tickerClose)

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

t26_signal = f_screener(i_T26)
plotshape(t26_signal, title='Alert', style=shape.triangleup, location=location.belowbar,color=color.yellow, size=size.large)

plotshape(f_strategy()==-1 or f_strategy()==1, title='debug', style=shape.diamond,location=location.belowbar, color=color.orange, size=size.large)

Ponderosa
Pine Script Scholar
Pine Script Scholar
Posts: 9
Joined: January 11th, 2023

Re: Function not plotting as intended. PLEASE HELP.

Thank you Rob. If you look closely, they don't always both plot, particularly late on the 8th. It's difficult to tell because it's so cluttered, but there are actually several instances where only one function plots.

Return to “Pine Script Q&A”