rel strength index function

B

Bill Cunningham

I have written this function and changed it and re written it and it
just doesn't do what I want it too. It is supposed to calculate the RSI for
avg up and down prices of a commodity. I compile this into an .o file and
link with a .c file.

double rsi(double up, double down, int period)
{
double rsi, up_avg, down_avg;
up_avg = up / period;
down_avg = down / period;
rsi = ((up_avg + down_avg) / up_avg) * 100;
printf("%.3f\n", rsi);
return 0;
}

I have tried reversing the division of up and down_avg. There must be
something wrong here.

Bill
 
K

Keith Thompson

Bill Cunningham said:
I have written this function and changed it and re written it and it
just doesn't do what I want it too. It is supposed to calculate the RSI for
avg up and down prices of a commodity. I compile this into an .o file and
link with a .c file.

In effect, you haven't told us what your function is supposed to do.
Don't expect us to know what an RSI is.

Oh, I see you mentioned "rel strength index" in the subject. I almost
didn't notice, because you didn't mention it in the body of the
article. And I still don't know what it is.
double rsi(double up, double down, int period)
{
double rsi, up_avg, down_avg;
up_avg = up / period;
down_avg = down / period;
rsi = ((up_avg + down_avg) / up_avg) * 100;
printf("%.3f\n", rsi);
return 0;
}

I have tried reversing the division of up and down_avg. There must be
something wrong here.

You haven't given us a clue of what it's doing wrong, or of how you
know it's doing it. You've shown us a function, but you haven't shown
us a call to it or the output of any program.

I do see one possible problem. You've declared the function to return
a value of type double. What value does the function actually return?
("return" is the key word in that question.)
 
B

Bill Cunningham

It would make more sense to return rsi rather than 0,
given the type of the function.

rsi(4,3,2) is 175.000000

What do you think it should be instead?

return rsi isn't recursion? Ok passing these values to rsi should result
in this answer

rsi(1.03,1.24,14);

Answer should be ~45.39. I'm getting 220 and these huge numbers that should
never be above 100 or below 0. I'm using a simpler version than what I've
run into.

http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:relative_strength_in

Bill
 
B

Bill Cunningham

In effect, you haven't told us what your function is supposed to do.
Don't expect us to know what an RSI is.

Oh, I see you mentioned "rel strength index" in the subject. I almost
didn't notice, because you didn't mention it in the body of the
article. And I still don't know what it is.


You haven't given us a clue of what it's doing wrong, or of how you
know it's doing it. You've shown us a function, but you haven't shown
us a call to it or the output of any program.

I do see one possible problem. You've declared the function to return
a value of type double. What value does the function actually return?
("return" is the key word in that question.)

I never thought about return rsi. I get no error codes though.
http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:relative_strength_in
 
U

user923005

    I have written this function and changed it and re written it and it
just doesn't do what I want it too. It is supposed to calculate the RSI for
avg up and down prices of a commodity. I compile this into an .o file and
link with a .c file.

double rsi(double up, double down, int period)
{
    double rsi, up_avg, down_avg;
    up_avg = up / period;
    down_avg = down / period;
    rsi = ((up_avg + down_avg) / up_avg) * 100;
    printf("%.3f\n", rsi);
    return 0;

}

I have tried reversing the division of up and down_avg. There must be
something wrong here.

Here is RSI done right:
http://dronten.googlepages.com/xtrader
 
B

Ben Bacarisse

Bill Cunningham said:
I have written this function and changed it and re written it and it
just doesn't do what I want it too. It is supposed to calculate the RSI for
avg up and down prices of a commodity. I compile this into an .o file and
link with a .c file.

double rsi(double up, double down, int period)
{
double rsi, up_avg, down_avg;
up_avg = up / period;
down_avg = down / period;
rsi = ((up_avg + down_avg) / up_avg) * 100;
printf("%.3f\n", rsi);
return 0;
}

I have tried reversing the division of up and down_avg. There must be
something wrong here.

At first I thought there must be something wrong simply because the
value you calculate does not, in the end, depend on 'period' but then
I looked up the way the RSI is calculated and, indeed, it does not
even though every explanation I saw pointlessly uses it!

Anyway, the formula is sometimes given as

rsi = 100 - (100 / (1 + up/down));

and sometimes as

rsi = 100 / (1 + down/up);

which is the same thing. This is not what you calculate.

Here, the 'up' and 'down' values are the sums of the absolute values
of the gains and losses. It is always quoted in terms of "averages"
of the gains and losses but that averaging is (a) pointless and (b)
not a true average so it just complicates the definition.

Why does the function return 0? Surely you want to return the RSI?
 
B

Bill Cunningham

Why does the function return 0? Surely you want to return the RSI?
Do you think that is the problem? I'm not working on it again til
tomorrow. I'm tired of formulas and functions right now and frustrated.

I can calculate this very easily by hand but can't get a right result
from calling rsi from main. I get -199 and 120 and 220 but not the true
value.

Bill
 
G

Guest

    I have written this function and changed it and re written it and it
just doesn't do what I want it too. It is supposed to calculate the RSI for
avg up and down prices of a commodity. I compile this into an .o file and
link with a .c file.

double rsi(double up, double down, int period)
{
    double rsi, up_avg, down_avg;
    up_avg = up / period;
    down_avg = down / period;
    rsi = ((up_avg + down_avg) / up_avg) * 100;
    printf("%.3f\n", rsi);
    return 0;

}

I have tried reversing the division of up and down_avg. There must be
something wrong here.

I have some bilerplate text somehere, ah!

0. fix your layout
1. post your code
2. post your input data
3. post your output
4. explain why you don't like 3
5. check return values
6. RTFM
7. don't guess (see 6)

you've breached rules 2, 3 and 4
 
B

Ben Bacarisse

Bill Cunningham said:
Do you think that is the problem?

No, the problem is the formula is wrong. I gave two versions that
were written as C code -- you could just put that into your function
to get the right value. Of course, they depend on the parameters
being as I specified. You don't tell us what up and down are in your
code and there is evidence (see below) that you are not calculating
these correctly.

I can calculate this very easily by hand but can't get a right result
from calling rsi from main. I get -199 and 120 and 220 but not the true
value.

Then I suggest you back up a bit. Your formula

| up_avg = up / period;
| down_avg = down / period;
| rsi = ((up_avg + down_avg) / up_avg) * 100;

can only give a negative result if one of up or down is negative and
that is not possible if you are calculating RSI. It uses the sum of
the absolute values of gains and losses so it never uses any negative
data. You can't get the right result from the wrong initial data.

So, to summarise... Take the data for the period in question and sum
the gains into a variable 'up'. Do the same for losses, putting that
in 'down'. If the loss data are negative numbers, do

down = -down;

If the losses are given as positive numbers you don't need this step.
Then calculate:

rsi = 100 / (1 + down/up);
 
B

Bill Cunningham

[snip]
can only give a negative result if one of up or down is negative and
that is not possible if you are calculating RSI. It uses the sum of
the absolute values of gains and losses so it never uses any negative
data. You can't get the right result from the wrong initial data.

So, to summarise... Take the data for the period in question and sum
the gains into a variable 'up'. Do the same for losses, putting that
in 'down'. If the loss data are negative numbers, do

down = -down;

If the losses are given as positive numbers you don't need this step.
Then calculate:

rsi = 100 / (1 + down/up);

Could using fabs() help here? I guess I don't know how to code the
formula in C. Just by hand.

Bill
 
B

Ben Bacarisse

Bill Cunningham said:
[snip]
can only give a negative result if one of up or down is negative and
that is not possible if you are calculating RSI. It uses the sum of
the absolute values of gains and losses so it never uses any negative
data. You can't get the right result from the wrong initial data.

So, to summarise... Take the data for the period in question and sum
the gains into a variable 'up'. Do the same for losses, putting that
in 'down'. If the loss data are negative numbers, do

down = -down;

If the losses are given as positive numbers you don't need this step.
Then calculate:

rsi = 100 / (1 + down/up);

Could using fabs() help here?

Yes, but it is not needed. You know if the losses are negative or
not.
I guess I don't know how to code the
formula in C. Just by hand.

Which bit of the formula? From what you wrote before it sounded as
if you have the sums of the gains and losses. If so, I've written the
formula in C for you.
 
B

Barry Schwarz

I never thought about return rsi. I get no error codes though.

Of course not. There is nothing syntactically incorrect with
returning 0. (It will be converted to 0.0 but so what.) YOUR problem
with returning 0 is it's not the value you computed.

What would you think if sqrt always returned 0.0?
 
B

Barry Schwarz

return rsi isn't recursion? Ok passing these values to rsi should result
in this answer

Why on earth did you introduce the extra confusion of naming a
variable the same as the function? (Since the variable and function
name are in the same namespace, the variable name hides the function
name so recursion would not be possible even if you wanted it. It
wouldn't be recursion anyway since a function name without the
argument list is a pointer to the function, not an invocation of the
function.)
 
R

Richard Bos

Barry Schwarz said:
On Thu, 19 Feb 2009 22:00:03 -0500, "Bill Cunningham"


Why on earth did you introduce the extra confusion of naming a
variable the same as the function?

Possibly because he has previously failed to learn Pascal the same way
he is failing to learn C.

Richard
 
P

Phil Carmody

Possibly because he has previously failed to learn Pascal the same way
he is failing to learn C.

Whilst that may be a tad harsh, I will confess to letting out
a small snigger upon reading that.

I think it's true in most computer languages that using the
same name to refer to different things in overlapping scopes
may decrease the readability and maintainability of the code,
certainly not just the common imperative ones mentioned above.

Phil
 
K

Kaz Kylheku

Why on earth did you introduce the extra confusion of naming a
variable the same as the function? (Since the variable and function
name are in the same namespace, the variable name hides the function
name so recursion would not be possible even if you wanted it. It
wouldn't be recursion anyway since a function name without the
argument list is a pointer to the function, not an invocation of the
function.)

What?

A function name /with/ an argument list is also a pointer to the function.

A call thorugh a pointer can still be recursive; recursion does not hinge
on the use of a name.

void foo(int leaf)
{
void (*self)(int) = foo;

if (!leaf)
{
int foo; /* shadow */

self(1); /* recurse */
}
}
 
B

Barry Schwarz

What?

A function name /with/ an argument list is also a pointer to the function.

x = func; attempts to set x to the address of the function.

y = func(); attempts to set y to the value returned by the function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top