Last active
October 27, 2024 18:15
-
-
Save rajvermacas/fa3b96b0bf2af5681c91941b9be8d2fb to your computer and use it in GitHub Desktop.
debugPinescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//@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