can't extract value from Function - function

function takes for example rsi and from declared for me historical bar 1 to 12 finds me maximum value ( integer or '00,0') of rsi.
//#version=5
indicator("loop", shorttitle="loop")
len = input.int(14, title="RSI Length")
src = input.source(close, "RSI Source")
Rsi = ta.rsi(src, len)
plot(Rsi*10, "RSI", color=#673ed8)
hline(65,linestyle= hline.style_dashed , color=color.new(color.red, 0))
minF = 1, maxF = 12, RsiFunction = Rsi
ff_loopMax(RsiFunction,minF, maxF) =>
var float Max = na
var float MaxOdpCena = na
var int MaxNrBar = na
for i=minF to maxF
if i == minF
Max := RsiFunction[i]
//MaxOdpCena := high[i]
//MaxNrBar := i
else
//MaxOdpCena := Max > RsiFunction[i] ? MaxOdpCena : high[i]
//MaxNrBar := Max > RsiFunction[i] ? MaxNrBar : i
Max := math.max(Max,RsiFunction[i])
// I want Max result from function
plot(Max, "rsiMax", color=#b63253)
Result = ff_loopMax(Rsi,5,15)
The code extracted from function should work alone. But I can't cope with function.

A function in pine script return the value of the last expression. In your case, the last expression is Max, but you can't access this directly. You call the function, and set this return to a variable.
You should change the last two lines to:
Result = ff_loopMax(Rsi,5,15) // Result is now equal to Max
plot(Result, "rsiMax", color=#b63253)

Related

Function in Pine Script with break

I have a function in pine script:
fun_check(Sell_Signal) =>
Base_Sell1 = Sell_Signal[0]
for i=0 to 150
if (Sell_Signal[i] == 1)
Base_Sell1 = 5
break
Base_Sell1
B_Sell = fun_check(Sell_Signal)
My function always returns the value as 1, but it should return a value as 5
You are missing : while assigning values to Base_Sell1 in the loop as Base_Sell1 is mutable variable
It should be:
Base_Sell1 := 5

Use varargin for multiple arguments with default values in MATLAB

Is there a way to supply arguments using varargin in MATLAB in the following manner?
Function
func myFunc(varargin)
if a not given as argument
a = 2;
if b not given as argument
b = 2;
if c not given as argument
c = a+b;
d = 2*c;
end
I want to call the above function once with b = 3 and another time while the previous one is running in the same command window with a = 3 and c = 3 and letting b take the default value in the function this time. How can it be done using varargin?
Here's the latest and greatest way to write the function (using arguments blocks from R2019b)
function out = someFcn(options)
arguments
options.A = 3;
options.B = 7;
options.C = [];
end
if isempty(options.C)
options.C = options.A + options.B;
end
out = options.A + options.B + options.C;
end
Note that this syntax does not allow you to say options.C = options.A + options.B directly in the arguments block.
In MATLAB < R2021a, you call this like so
someFcn('A', 3)
In MATLAB >= R2021a, you can use the new name=value syntax
someFcn(B = 7)
Here are two ways to do this which have been available since 2007a (i.e. a long time!). For a much newer approach, see Edric's answer.
Use nargin and ensure your inputs are always in order
Use name-value pairs and an input parser
nargin: slightly simpler but relies on consistent input order
function myFunc( a, b, c )
if nargin < 1 || isempty(a)
a = 2;
end
if nargin < 2 || isempty(b)
b = 2;
end
if nargin < 3 || isempty(c)
c = a + b;
end
end
Using the isempty check you can optionally provide just later arguments, for example myFunc( [], 4 ) would just set b=4 and use the defaults otherwise.
inputParser: more flexible but can't directly handle the c=a+b default
function myFunc( varargin )
p = inputParser;
p.addOptional( 'a', 2 );
p.addOptional( 'b', 2 );
p.addOptional( 'c', NaN ); % Can't default to a+b, default to NaN
p.parse( varargin{:} );
a = p.Results.a;
b = p.Results.b;
c = p.Results.c;
if isnan(c) % Handle the defaulted case
c = a + b;
end
end
This would get used like myFunc( 'b', 4 );. This approach is also agnostic to the input order because of the name-value pairs, so you can also do something like myFunc( 'c', 3, 'a', 1 );

How to add 3 number together?

There are 3 Uint 8 bits numbers. I want to sum up these numbers. How to describe it in chisel?
s = a + b + c // s is 10 bits number
If the only way to describe it as following, what's the benefits compare to traditional HDL?
s0 = a + b // s0 is 9 bits numebr
s1 = s0 + c // s1 is 10 bits number
I already try it in chisel, the result is not what I expect.
val in0 = Input(UInt(8.W))
val in1 = Input(UInt(8.W))
val p_out = Output(UInt(10.W))
io.p_out := io.in0 + io.in0 - io.in1
The generated RTL:
input [7:0] io_in0,
input [7:0] io_in1,
output [9:0] io_p_out
wire [8:0] _T_18;
wire [7:0] _T_19;
wire [8:0] _T_20;
wire [8:0] _T_21;
wire [7:0] _T_22;
assign io_p_out = {{2'd0}, _T_22};
assign _T_18 = io_in0 + io_in0;
assign _T_19 = _T_18[7:0]; // ??
assign _T_20 = _T_19 - io_in1;
assign _T_21 = $unsigned(_T_20); // ??
assign _T_22 = _T_21[7:0]; // ??
In order to keep the carry you should use the expanding operators +& and -&.
io.p_out := io.in0 +& io.in0 -& io.in1
https://chisel.eecs.berkeley.edu/doc/chisel-cheatsheet3.pdf

Why exactly does this function return a value to some power? (python 2.7)

def power(num, x = 1):
result = 1
for i in range(x):
result = result * num
return result
So I came across a tutorial on calling functions with 2 arguments and this one in the picture was used as an example to show how you could make a function called power(num, x=1) that takes an interval in the first argument and raises it to the power of the second argument. Can someone explain in laymen's terms why this happens and what exactly is going on in this function and 'for' loop?
First, range(x) is equivalent to range(0, x), and generates a sequence that ranges from 0 to x - 1. For example, with range(3) you get the sequence 0, 1, and 2, which has three elements. In general, range(x) generates a sequence that has x elements.
Second, for i in range(x) makes i iterates throught all the elements of range(x). Since range(x) has x elements, i will iterate through x different values, so the statements in the for loop will be executed x times.
With the above analysis, the body of the power function is equivalent to the following:
result = 1
result = result * num
result = result * num
// repeat x times
result = result * num
which is equivalent to:
result = 1 * num * num * ... * num // x nums here
which, apparently, is num raised to the power of x.
Update
Here's how this function works with specific input data. When num is 3 and x is 4, we have:
result = 1
result = result * num // = 1 * 3 = 3
result = result * num // = 3 * 3 = 9
reuslt = result * num // = 9 * 3 = 27
result = result * num // = 27 * 3 = 81 = 3^4
return result // 81 is returned
We can also show the execution process in more details:
result = 1
i = 0 // entering the loop
result = result * num // = 1 * 3 = 3
i = 1 // the second round of the loop begins
result = result * num // = 3 * 3 = 9
i = 2 // the third round of the loop begins
reuslt = result * num // = 9 * 3 = 27
i = 3 // the fourth and final round of the loop begins
result = result * num // = 27 * 3 = 81 = 3^4
// range(4) is exhausted, so the loop ends here
return result // 81 is returned

Arduino - waiting on function causes a number pile-up

I have a function filledFunction() that returns a float filled:
float filledFunction(){
if (FreqMeasure.available()) {
sum = sum + FreqMeasure.read();
count = count + 1;
if (count > 30) {
frequency = FreqMeasure.countToFrequency(sum / count);
a = frequency * x;
b = exp (a);
c = w * b;
d = frequency * z;
e = exp (d);
f = y * e;
float filled = c + f;
sum = 0;
count = 0;
return filled;
}
}
}
When I call this function with
while (1){
fillLevel = filledFunction();
int tofill = 500 - fillLevel;
Serial.print("fillLevel: ");
Serial.println(fillLevel);
Serial.print("tofill: ");
Serial.println(tofill);
The serial monitor should output two numbers that add up to 500 named fillLevel and tofill. Instead I get a repeating sequence of similar values:
http://i.imgur.com/Y9Wu8P2.png
The First two values are correct (410.93 + 89 = 500), but the following 60ish values are unknown to me, and do not belong there.
I am using an arduino nano
The filledFunction() function only returns a value if FreqMeasure.available() returns true AND count > 30. As stated in the answers to this question the C89, C99 and C11 standards all say that the default return value of a function is undefined (that is if the function completes without executing a return statement). Which really means that anything could happen, such as outputting arbitrary numbers.
In addition, the output that you're seeing starts off 'correct' with one of the numbers subtracted from 500, even when they have weird values such as 11699.00 and -11199 (which equals 500 - 11699.00). However, lower down in the output this seems to break down and the reason is that on Arduino Nano an int can only hold numbers up to 32767 and therefore the result of the subtraction is too big and 'overflows' to be a large negative number.
Fixing the filledFunction() function to explicitly return a value even if FreqMeasure.available() is false or count <= 30 and ensuring that it can't return a number greater than 500 will likely solve these issues.