Page 1 of 1
Call a function from inside a loop
Posted: Wed Mar 23, 2022 1:47 pm
by Alex100
I'd like to call a function from inside a 'for' loop. I wrote the code below, but I get an 'undeclared identifier' error.
I tried lots of things to make it run, but couldn't find any solution.
Can someone please help me?
Alex
Code: Select all
//@version=5
indicator("My function")
my_function(i) =>
// code here...
[i]
for i = 1 to 10 by 1
[result] = my_function(i)
plot (result)
Re: Call a function from inside a loop
Posted: Fri Mar 25, 2022 9:19 pm
by processingclouds
Hey,
If you can complete your code a bit more, it would be easier to help you out in this.
Error "Undeclared Identifier" means you need to initialize the variable before you can use it.
Re: Call a function from inside a loop
Posted: Sat Mar 26, 2022 3:55 pm
by Alex100
I already tried to declare the 'result' variable, but I still received an error.
If I declare it at the top of the script I get this error...
Shadowing variable 'result' which exists in parent scope.
and if I declare it right beneath the 'for' instruction, I get this one...
'result' is already defined.
I tried mostly everything I could think of, but nothing seemed to work.
Alex
Re: Call a function from inside a loop
Posted: Sun Mar 27, 2022 2:55 pm
by processingclouds
When you declare the variable outside , and than want to use it again , you need to make sure you use
not just plain
Code: Select all
float result = na
result = 1.0 // WRONG
result := 1.0 // CORRECT
Re: Call a function from inside a loop
Posted: Sun Mar 27, 2022 6:53 pm
by Alex100
Ok, I no longer receive the two errors in my previous message, but a new one is showing up now, and I can't get rid of it:
line 11: Syntax error
I don't see any syntax error on line 11 (which is 'result := 1.0'), so what am I doing wrong? The code is this:
Code: Select all
//@version=5
indicator("My function")
float result = na
my_function(i) =>
// code here...
[i]
for i = 1 to 10 by 1
result := 1.0
[result] = my_function(i)
plot (result)
Re: Call a function from inside a loop
Posted: Mon Mar 28, 2022 2:25 pm
by processingclouds
Do you have more code, so i can better understand exactly what you are trying to do ? It is not clear , whether you are creating arrays, or how your my_function works.... rather than // code here, if you can provide more code and explain what you intend it to do. Even if it has errors, i will atleast be able to understand the logic.
Re: Call a function from inside a loop
Posted: Tue Mar 29, 2022 1:57 pm
by Alex100
Since I first posted my initial question, I did find a better solution that I used to replace this function, so I no longer have any code for it.
But to give you a basic example, the function can calculate the percentage of a number from another number. And in case you need something like this in multiple locations within a script (including a 'for' loop), you can simply call the function instead of writing the same code multiple times.
I'm not trying to create any arrays, I just need the basic functionality of any function: access the same code from multiple locations within a script.
Re: Call a function from inside a loop
Posted: Tue Mar 29, 2022 10:38 pm
by processingclouds
Ok, so you are looking for a way to have a global variable that can be changed by a function. Here is a small indicator to show this :
Code: Select all
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © processingclouds
//@version=5
indicator("My function", overlay=true)
result = array.new_float(1, 0.0)
my_function(i) =>
oldValue = array.get(result, 0)
array.set(result,0, oldValue + i)
for i = 1 to 10 by 1
my_function(i*1.1)
if barstate.islast
label.new(bar_index, high, str.tostring(array.get(result,0)), textcolor=color.white)
Re: Call a function from inside a loop
Posted: Wed Mar 30, 2022 1:39 pm
by Alex100
Works great, thank you very much!