'PineScript: plot a line with custom values
I'm a newbie and I'd like to create a simple indicator I update every day with my numeric values and that plots one line chart. I tried this but it plots 3 single separate lines that don't have neither start nor end.
Could you help me please?
study("Study")
8may22 = 3
9may22 = 1
10may22 = 2
plot(8may22)
plot(9may22)
plot(10may22)
Solution 1:[1]
Plot can "plot" a Series. So you need to build a Series which happens automatically.
Pine runs your code on every bar (this is what usually wraps the newbies' heads around) and most importantly it runs your code on every tick on the current bar (when you sit in front of Tradingview and just watch the chart).
When your code sets a variable in a run, Pine will automatically save the value from the run, and append it to a Series behind the scenes.
So the only thing you need is to do is to set a single variable (which is a Series, you just don't realize it) to a value and Pine will record the value of that variable in every run, making you a Series.
my_variable = close * 2 - open / high //random calculated nonsense
plot(my_variable)
Apply this study to a chart (low timeframe) and watch how the plotted line will move around on the current bar as it is being constantly recalculated and repainted. When the bar is closed, and no more data is being added to it, the plot will "finalize" there and a new bar will be opened.
Your example of 8may22 = 3
worked the same way, it was setting the variable to '3' in every run and Pine saved that value in every run, effectively creating a line with a constant value.
UPDATE: Here is a simple code I hope it helps to understand some internals of Pine. Apply this script to a 1H/1m current FTX:MOVE contract ('BTCMOVE0516' today). MOVE contracts disappear daily, and new contracts come to life with modified names. They live only 2 days, so they are good to experiment with as their starting point is easily reachable:
// © karatedog
//@version=5
indicator("Custom series", overlay = false)
custom_series = bar_index < 15 ? 3.0 : 5.3
plot(custom_series)
You will see the plot's value is 3.3 at bar #0 and it will change to 5.3 at bar #15, whatever time frame you chose (as long as it has 15 bars in it). So I set a custom value at #0 and I modified it at another point in time (bar #15).
When you have a chart, which has, say, 5500 bars, you need to imagine your 'variables' as if every one of them were an Array with 5500 elements and every index position in the array is a single value to that index-th bar. This is why variables are referred as Series, they are not singular values, but series of values.
This is what happens when you plot close
. You have a big Array, every item is a specific close
value from a specific bar. But as close
is changing every time (in other words, Pine will calculate it for you), you have a nice, ever-changing graph.
With custom values, you have to do the calculation yourself and generate a value for that variable for every bar. There are multiple ways to do it, the one I put in above is not the best in Pine's term but for a first approach it is very easy to understand.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |