TradingView
deimosaffair
23 אוק׳ 2016 10:14

Back to zero: Understanding series 

GOLD / U.S. DOLLARICE

תיאור

type: pine series basic example
time required: 10 minutes
level: medium (need to know the "array" data variable as a generic programming concept, basic Pine syntax)

tl;dr how variables and series work in Pine

Pine is an array/vector language. That's something that twists how it behaves, and how we have to think about it. A lot of misunderstandings come from forgetting this fact. This example tries to clear that concept.

First, you need to know what an array is, and how it works in a programmig language. Also, having javascript under your belt helps too. If you don't, google "javascript array basic tutorial" is your friend :)

So, in pine arrays are called "series". Every variable is an array with values for each candle in the chart. if we do:

myVar = true


this is not a constant. It is a series of values for each candle, { true, true,....., true }
In practice, the result is the same, but we can access each of the values in the series, like myVar{0}, myVar{7}, myVar{anyNumber}....
Again, it is not a constant, since you can access/modify the each value individually

so, lets show it:
plot (myVar, clolor = gray)

this plots an horizontal line of value 1 ( 1 is equal to true ) so it's all good.

On to a more usual series:

tipicalSeries = close > open ? true : false
plot(tipicalSeries, color= blue)


This gives the expected result, a tipical up and down line with values at 1 or 0. Naturally, "tipicalSeries" is an array, the "ups" and "downs" are all stored under the same variable, indexed by the candles.
In Pine, the ZERO position in the array is the last one, which corresponds to the last candle on the right. Say you have a chart with 12 candles. The close[12] would be the closing value of what we intuitively think as first candle, the one on the left. then close[11]... and so on.... until close[0], the value of the "last" candle, the one on the right. It actually helps to start thinking of the positions backwards, counting down to zero, rocket launch style :)

And back to our series. The myVar will also be the same size, from myVar[12] to myVar[0].

When we do some operation with them, something simple like
if ( myVar == tipicalSeries)
what is really happening is that internally, Pine is checking each of the indexes, as in myVar[12] == tipicalSeries[12] , myVar[11] == tipicalSeries[11] .... myVar[] == tipicalSeries[0]
And we can store that stuff to check it. simply:
result = (myVar == tipicalSeries) ? true : false //yes, this is the same as tipicalSeries, but we're not in a boolean logic tut ;)
plot (result)


The reason we can plot the result is that it is an array, not a single value. The example indicator i provide shows a plot where the values are obtained from different places in the array, this line here:

mySeries3 = mySeries2[1] and mySeries1[1]

this creates a series that is the result of the PREVIOUS values stored (the zero index is the one most at the right, or the "current" one), which here just causes a shift in the plotted line by one candle.

Go ahead, grab a copy of my code, try to change the indexes and see the results. Understanding this stuff is critical to go deeper into Pine :)
תגובות
Blumsden
Can you create you own series? Do all series relate to candle? (Newbie question)
LixxChartz
doesnt work anymore
fpspqr
Thank you! But how the hell do you work operators on series? AFAIK a simple if statement demands a numerical value? And throws an syntax error at input 'if'? What am I missing here to create an if statement that checks if a series is postive (> 0) or not?!
nathansonic
I really appreciate this info! Thanks man!
affiorentini
Thanks deimosaffair !!!
deimosaffair
testing... myvar[0] myvar[ 0 ] myvar\[0\]

deimosaffair
ok, so square brakets just cannot be used in any way, either in main text or in comments. nice ..... ;P

there are a lot of missing pieces in my text, wich changes it's sense upside down. where you see myvar in the main text, you should see myvar{somenumber}. and, not with curly brakets, but with square brakets. just look at the code, it's the most clear, simple, best example of it all :)
deimosaffair
EDIT: it appears the square bracket '[' is a special char in the text, so i had to change to '{' and '}'.
So, in you mind please change the {}'s for "those damn square brackets i cannot use" :)
עוד