Skip to content

Instantly share code, notes, and snippets.

@rajvermacas
Last active October 27, 2024 18:15
Show Gist options
  • Save rajvermacas/fa3b96b0bf2af5681c91941b9be8d2fb to your computer and use it in GitHub Desktop.
Save rajvermacas/fa3b96b0bf2af5681c91941b9be8d2fb to your computer and use it in GitHub Desktop.
debugPinescript
//@version=5
indicator("Var Understanding", overlay=true)
myFunction() =>
var int counter = 0 // Initialized once per bar
counter := counter + 1
counter
// Scenario 1
// On the same bar, multiple calls will reset the var
int a = 0
a := myFunction() // First call on this bar: counter starts at 0, returns 1
a := myFunction() // Second call on this bar: counter starts at 0 again, returns 1
a := myFunction() // Third call on this bar: counter starts at 0 again, returns 1
// The var will retain its value across different bars
plot(a, color=color.red)
plot(a, color=color.blue)
plot(a, color=color.green)
buySignalTypes = array.from("ATR_CROSS", "RSI", "STOCHASTIC")
// Scenario 2
// Test each combination of buy and sell signals
int x = 0
for buyType in buySignalTypes
x := myFunction()
plot(x, color=color.yellow)
// The var keyword in Pine Script retains values across bars, NOT across multiple function calls.
// If the function calls are within a lopp then var variables are not reinitialized. It is only reinitialised if the functions are called explicitly by adding a new line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment