Variables and Data Types 📊
Basic Data Types
Pine Script has several fundamental data types:
// Numbers
float myPrice = 100.50
int myQuantity = 100
// Strings
string myText = "Hello"
// Booleans
bool isBullish = true
// Colors
color lineColor = color.blue
// Arrays
float[] prices = array.new_float(0)
Variable Declaration
Using 'var' Keyword
The var
keyword creates variables that persist across bars:
//@version=5
indicator("Variable Example")
// Regular variable - recalculated every bar
myVar = close
// Persistent variable - only initialized once
var myPersistent = close
Series vs. Simple Variables
// Simple (single value)
simpleValue = 100
// Series (value for each bar)
seriesValue = close
// Accessing historical values
previousClose = close[1] // Previous bar's close
twoBarBack = close[2] // Two bars back
Type Conversion
// String to float
stringPrice = "100.5"
floatPrice = str.tostring(stringPrice)
// Float to integer
floatNum = 100.5
intNum = int(floatNum)
// Number to string
price = 100.5
priceString = str.tostring(price)
Arrays
Creating Arrays
// Create new arrays
var float[] prices = array.new_float(0)
var string[] labels = array.new_string(0)
// Initialize with values
var int[] numbers = array.from(1, 2, 3, 4, 5)
Array Operations
//@version=5
indicator("Array Operations")
var float[] prices = array.new_float(0)
// Add value
array.push(prices, close)
// Get value
firstPrice = array.get(prices, 0)
// Set value
array.set(prices, 0, high)
// Get size
size = array.size(prices)
// Remove value
array.pop(prices)
Matrices
//@version=5
indicator("Matrix Example")
// Create a 2x2 matrix
var matrix<float> m = matrix.new<float>(2, 2, 0.0)
// Set values
matrix.set(m, 0, 0, 1.0)
matrix.set(m, 0, 1, 2.0)
matrix.set(m, 1, 0, 3.0)
matrix.set(m, 1, 1, 4.0)
// Get value
value = matrix.get(m, 0, 0)
Scope
Global Scope
//@version=5
indicator("Scope Example")
// Global variable
globalVar = 100
// Function using global variable
myFunction() =>
globalVar + 50
Local Scope
myFunction() =>
// Local variable
localVar = 100
localVar + 50
// localVar is not accessible here
Practice Exercises
1. Price History Array
//@version=5
indicator("Price History")
// Store last 5 closing prices
var float[] closePrices = array.new_float(5, close)
// Update array
array.unshift(closePrices, close)
array.pop(closePrices)
// Calculate average
sum = array.sum(closePrices)
avg = sum / array.size(closePrices)
plot(avg, "5-bar Average")
2. Dynamic Labels
//@version=5
indicator("Dynamic Labels")
var string[] labels = array.new_string(0)
if (close > high[1])
array.push(labels, "New High")
if (close < low[1])
array.push(labels, "New Low")
if (array.size(labels) > 10)
array.shift(labels)
label.new(bar_index, high, array.get(labels, array.size(labels)-1))
Pro Tips
- Use
var
for variables that should persist - Be mindful of type conversion
- Arrays are great for historical data
- Keep track of array sizes
Common Pitfalls
Watch Out For
❌ Forgetting to initialize arrays ❌ Mixing types without conversion ❌ Not handling empty arrays ❌ Ignoring scope rules
Next Steps
Ready to learn about operators and control flow? Move on to the next chapter! 🚀