Jansen Marc
Jansen Marc

Reputation: 21

How to create a self defined series in Pine on TradingView?

i'm pretty new to pine scripts, and i have a very simple problem: i want to multiply each value of a series by a different value, e.g., i have the series sma(close, 10) and i want to multiply each of its values with a different value. Therefore, i thought to generate another series with the different factors and multiply the two series.

values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

result = sma(close, 10) * values

This is what i tried so far, but i get an error when i try to initiate the series with [. Any help on how to construct such a series?

Cheers

Marc

Upvotes: 2

Views: 8383

Answers (2)

jkr
jkr

Reputation: 660

Go back to last 10 candles and start incrementing the counter.

var count = 0
if last_bar_index - bar_index < 10
    count := count + 1

Use the counter as index into array to get values. The values in the series will be 'na' prior to last 10 candles.

series = array.size(values)>0 and (last_bar_index - bar_index) < 10? array.get(values ,count) : na

The series variable above can be used in any ta functions to do calculations.

daily_func() =>
    var count = 0
    values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    if last_bar_index - bar_index < 10
        count := count + 1
    series = array.size(values)>0 and (last_bar_index - bar_index) < 10? array.get(values ,count) : na
    [series]

[series] = request.security('SPX', 'D', daily_func())

Upvotes: 0

alexgrover
alexgrover

Reputation: 1043

In pinescript the syntax [a,b,...] is used when a function return a multi dimensional array, that is: [a,b] = f(x), where f(x) => [x+1,x+2] for example, so its not like with other programming languages.

Also note that in your case you must assume that your series sma(close,10) contain only 10 values, which is not necessarily the case. So one way to possibly do it is to simply use sma(close,10)*(bar_index+1) where your wanted results will appear when bar_index+1 is inferior to 11.

Another way is to use:

a = bar_index%10 + 1
result = sma(close,10)*a

Here a is will start at 1 and increase by 1 until a = 10, then it will be equal to 1 once again, this process will repeat itself until the last bar.

Upvotes: 2

Related Questions