# Thinking in Pine - var, varip and regular variables

Education

BINANCE:BTCUSDT   Bitcoin / TetherUS
This is our first video session on "Thinking in Pine" series. Before we start, we want to explain a bit about our new initiative.

🎲 What is "Thinking in Pine"?

In our journey to empower the trading community, we're excited to introduce "Thinking in Pine," a series of concise, 5-10 minute videos dedicated to unraveling the complexities of Pine Script®. We have our own list of topics to be covered, and we will start releasing the videos one by one. However, if you're grappling with any aspect of Pine Script® or stuck on an implementation, we encourage you to reach out to us or drop a comment here. We aim to address your queries by breaking down challenging concepts or implementations into easily digestible content.

What kind of videos are covered in "Thinking in Pine"?
1. Pine Script® Focus: We try to keep our focus on Pine Script® concepts and implementations.
2. General Utility: We prioritize topics that offer broader learning value. Though it's challenging to quantify this, we'll use our judgment to select topics that benefit the wider audience.
3. Time-Efficient Demonstrations: Ideally, we want to keep our demonstrations to 5–10 mins of time.

We're here to demystify Pine Script®, one topic at a time, making it accessible for everyone from beginners to advanced users. Stay tuned for insightful sessions with "Thinking in Pine"!

🎲 Demonstrating var, varip and regular variables in Pine Script®
In this video, we have demonstrated the difference between var, varip and regular variables by using an example implementation of OBV indicator.

🎯 Logic of OBV Calculation
• On each bar, add volume to the indicator if close price is higher than previous bar close price.
• On each bar, remove volume from the indicator is close price is lesser than previous bar close price

🎯 Highlights
• Regular variables are initialized separately on each bar and does not propagate value to next bar unless coded to do it.
• var variables are initialized once and then can be reassigned any number of times using := operator. The variables declared as var will propagate the current values to the next bar.
• varip variables are initialized once and then can be reassigned any number of times using := operator. varip will behave similar to var on historical bars. However, on real time bars, they are recalculated on every tick, and they remember the state of each tick.

🎯 Example Program Used

Here is the example program used in the demonstration.
```//Plot built-in OBV value for reference
plot(ta.obv, "OBV Built In", color=color.yellow)

//Volume multiplied by +-1 based on change in close price compared to previous bar.
volumeBySign = math.sign(nz(ta.change(close), 0))*volume

//Obv calculation by using regular variable. Code need to access and add last bar value using obvByRegular[1]
obvByRegular = 0.0
obvByRegular += nz(obvByRegular[1], 0) + volumeBySign
plot(obvByRegular, "OBV By Regular Variable", color=color.blue)

//Obv calculation using var variable. Since var variables propagate values to next bar,
// we do not need to use historical operator to get the last bar value
var obvByVar = 0.0
obvByVar += volumeBySign
plot(obvByVar, "OBV by var Variable", color = color.maroon)

//Obv implementation using varip. The OBV is calculated based on every tick. Histoical values will match the same as that of other implementation.
//However, in real time, the calculations are done based on the tick values
varip obvByVarip = 0.0
varip lastPrice = close
varip lastVolume = volume
if(barstate.isnew)
lastVolume := 0

obvByVarip += math.sign(close-lastPrice)*(volume-lastVolume)
lastPrice := close
lastVolume := volume

plot(obvByVarip, "OBV by varip Variable", color = color.purple)```

🎲 References:

Disclaimer