oscillator

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

I have written this ob/os oscillator for securities analysis. The only
thing is that the result is always negative. Such as -96. I have looked over
and over this code and it looks fine to me. Is there any trouble?

#include <stdio.h>
#include <stdlib.h>

double os(double price,double ma)
{ double os;
os=price-ma/ma*100;
return os;
}
int main(int argc,char *argv[])
{ if (argc!=3)
{fprintf(stderr,"usage error\n");
exit(EXIT_FAILURE);
}
double price,ma;
price=strtod(argv[1],NULL);
ma=strtod(argv[2],NULL);
printf("%f.2\n",os(price,ma));
return 0;
}

Thanks.
 
W

Walter Roberson

I have written this ob/os oscillator for securities analysis. The only
thing is that the result is always negative. Such as -96. I have looked over
and over this code and it looks fine to me. Is there any trouble?
double os(double price,double ma)
{ double os;
os=price-ma/ma*100;
return os;
}

When ma is a double, ma/ma is always going to be 1 (unless ma is 0
or infinity or NaN), so your os routine is always going to return
price - 100 .

*Possibly* you meant something like, os = (price - ma) / ma * 100; ??
 
B

Bill Cunningham

When ma is a double, ma/ma is always going to be 1 (unless ma is 0
or infinity or NaN), so your os routine is always going to return
price - 100 .

*Possibly* you meant something like, os = (price - ma) / ma * 100; ??
Ah I see. Thanks much Walter. That must be the problem. Not quite used
to all C's parenthesis yet :)

Bill
 
K

Keith Thompson

Bill Cunningham said:
I have written this ob/os oscillator for securities analysis. The only
thing is that the result is always negative. Such as -96. I have looked over
and over this code and it looks fine to me. Is there any trouble?

#include <stdio.h>
#include <stdlib.h>

double os(double price,double ma)
{ double os;
os=price-ma/ma*100;
return os;
}
int main(int argc,char *argv[])
{ if (argc!=3)
{fprintf(stderr,"usage error\n");
exit(EXIT_FAILURE);
}
double price,ma;
price=strtod(argv[1],NULL);
ma=strtod(argv[2],NULL);
printf("%f.2\n",os(price,ma));
return 0;
}

I don't know what an "ob/os oscillator" is, or what it's supposed to do.

Show us one or more examples of arguments passed to the program, the
output it produces, and the output you expected (and *why* you
expected that particular output).

And please fix your code formatting. Run it through "indent" if you
must.
 
B

Bill Cunningham

[snip]
When ma is a double, ma/ma is always going to be 1 (unless ma is 0
or infinity or NaN), so your os routine is always going to return
price - 100 .

*Possibly* you meant something like, os = (price - ma) / ma * 100; ??
Also for some reason though I have printf("%f.2\n"... in my code I get
numbers like this

44.000000.2

There must be something else broken.

Bill
 
B

Bill Cunningham

I don't know what an "ob/os oscillator" is, or what it's supposed to do.

overbought/oversold oscillator. price of security -ma or moving
average/moving average *100. The formula produces buy and sell signals for
things like stocks.
Show us one or more examples of arguments passed to the program, the
output it produces, and the output you expected (and *why* you
expected that particular output).

And please fix your code formatting. Run it through "indent" if you
must.

Oh yes forgot to run it through.
 
K

Keith Thompson

Bill Cunningham said:
[snip]
When ma is a double, ma/ma is always going to be 1 (unless ma is 0
or infinity or NaN), so your os routine is always going to return
price - 100 .

*Possibly* you meant something like, os = (price - ma) / ma * 100; ??
Also for some reason though I have printf("%f.2\n"... in my code I get
numbers like this

44.000000.2

There must be something else broken.

"%f" specifies the format used to print the argument. The ".2"
following it is just plain text that's printed normally. So
you're printing a lvaue in "%f" format:
44.000000
immediately followed by
.2

Try "%.2f".

And you get numbers like that *with what inputs*? See my other
message.
 
B

Bill Cunningham

[snip]
Show us one or more examples of arguments passed to the program, the
output it produces, and the output you expected (and *why* you
expected that particular output).
os 4 5 result -20
os 5 4 result 25
os 25 35 result -28.57

It seems to be working pretty well now. The results seem to be correct.

Bill
 
R

Richard

Bill Cunningham said:
Ah I see. Thanks much Walter. That must be the problem. Not quite used
to all C's parenthesis yet :)

Bill

Might I suggest you not posting until you understand what is potentially
the easiest thing in C to understand. Or alternatively stop
trolling. Read other peoples code and learn to indent and use your
braces clearly and consistently.

While I have a lot of time for people who really want to learn your
continual posts show you rarely, if ever, listen to what is told to you
and the ridiculously poor quality of your code suggests that you must be
trolling.
 
B

Bill Cunningham

Might I suggest you not posting until you understand what is potentially
the easiest thing in C to understand. Or alternatively stop
trolling. Read other peoples code and learn to indent and use your
braces clearly and consistently.

While I have a lot of time for people who really want to learn your
continual posts show you rarely, if ever, listen to what is told to you
and the ridiculously poor quality of your code suggests that you must be
trolling.

I am honestly not quite sure how to answer this post. I understand there
are always going to be people who believe you have some sinister motive
behind everything and nothing can really be said to change that. But I will
give it a shot. I usually use indent to post my code and have been doing
that for awhile to make things easier to read and I forgot this time. If you
had been following my posts (which is up to you) you would know that. That
makes me have some doubts to your motives Richard. In my understanding clc
is a place to go for help now true; I might have been able to pick up a
reference and correct that %.2f thing but I doubt the parenthesis I would
have caught on my own.

As to level of understanding, I don't profess to be a C expert or I
wouldn't need clc. As for the term "Troll" this is my understanding of it.

From wikipedia there was an instance where a man pretended to be a woman
and so on and there was a big problem. On clc I doubt seriously that it is
possible to "troll". No one truely knows nor will ever expect to meet anyone
else here. That's common sense. And all we discuss is C. And nothing else.
How much damage can be possibly done.

Bill
 
R

Richard

Bill Cunningham said:
I am honestly not quite sure how to answer this post. I understand there
are always going to be people who believe you have some sinister motive
behind everything and nothing can really be said to change that. But I
will

You are seemingly intelligent and well spoken and polite. I refuse to
believe that you do not understand C scoping rules with regard to
braces. It is the easiest thing in existence. I also do not believe you
do not have the common sense to correctly *MANUALLY* indent your code so
it is laid out nicely and is easy on the eye. Do not rely on indent. Do
it yourself.
give it a shot. I usually use indent to post my code and have been doing
that for awhile to make things easier to read and I forgot this
time. If you

Why? Do it yourself.
had been following my posts (which is up to you) you would know that. That
makes me have some doubts to your motives Richard. In my understanding clc
is a place to go for help now true; I might have been able to pick up
a

It is not the place to continually make the same mistakes regardless of
advice. You will then just become cannon fodder for the pedants in this
group of which there are many. I suspect most have you killfiled already
as many people consider you to be a troll and just here to waste peoples
time.
reference and correct that %.2f thing but I doubt the parenthesis I would
have caught on my own.

Why? It is the easiest thing in the world. Indent your code. Do not have
multiple statements on one line like Falconer does. Be
consistent. Personally I prefer the original K&R style e.g (code not
compiled or meant to do anything other than show layout)

,----
| int i;
| for(i=0;i<10;i++){
| doSomething(i);
| doSomethingElse(i);
| }
`----

I like it because the boundaries are easy to see. Heathfield advocates
the opening brace on the next line e.g

,----
| int i;
| for(i=0;i<10;i++)
| {
| doSomething(i);
| doSomethingElse(i);
| }
`----

Personally I do not like that but it is clear and easy to spot missing
braces. I would have no problem with anyone working on any projects of
mine using such and can understand why he, and others, prefer it.
As to level of understanding, I don't profess to be a C expert or I
wouldn't need clc. As for the term "Troll" this is my understanding of
it.

You do not have to be a C expert to understand braces. Practice. Do not
post your code until you have checked your parenthesis/braces. Really
Bill, you will not learn anything by constantly posting the same errors
and then glibly stating that "you are not quite familiar with or fully
understanding braces". How can you not be?
From wikipedia there was an instance where a man pretended to be a woman
and so on and there was a big problem. On clc I doubt seriously that it is
possible to "troll". No one truely knows nor will ever expect to meet anyone
else here. That's common sense. And all we discuss is C. And nothing else.
How much damage can be possibly done.

The only damage is that people who can guide you and help you will
killfile you as they feel their advice is falling on deaf ears.
 
R

Richard Tobin

*Possibly* you meant something like, os = (price - ma) / ma * 100; ??
[/QUOTE]
Ah I see. Thanks much Walter. That must be the problem. Not quite used
to all C's parenthesis yet :)

They're not C's parentheses. It's just the normal notation used
in mathematics. You should have learned it at primary school!

-- Richard
 
B

Bill Cunningham

[snip]
do not have the common sense to correctly *MANUALLY* indent your code so
it is laid out nicely and is easy on the eye. Do not rely on indent. Do
it yourself.

I agree with you on style. But too many people complained. So I learn
from indent that indents for me. Then people complained about that. Nothing
can be done right for some.

[snip]
The only damage is that people who can guide you and help you will
killfile you as they feel their advice is falling on deaf ears.
That's fine. As long as there are a few that are willing to help me I
will learn their tricks of the trade so to speak. I can't do it everyone's
way. There a several people who are willing to teach their style. I don't
need 100s disagreeing with each other and confusing me still. That's just a
mess.

Bill
 
B

Bill Cunningham

Richard Tobin said:
They're not C's parentheses. It's just the normal notation used
in mathematics. You should have learned it at primary school!
I'll take the scolding. Actually I think Jr High School for me.

Bill
 
R

Richard

Bill Cunningham said:
[snip]
do not have the common sense to correctly *MANUALLY* indent your code so
it is laid out nicely and is easy on the eye. Do not rely on indent. Do
it yourself.

I agree with you on style. But too many people complained. So I learn
from indent that indents for me. Then people complained about that. Nothing
can be done right for some.

No. You posted a complete mess. Would you please READ what is directed
at you.
[snip]
The only damage is that people who can guide you and help you will
killfile you as they feel their advice is falling on deaf ears.
That's fine. As long as there are a few that are willing to help me I
will learn their tricks of the trade so to speak. I can't do it everyone's
way. There a several people who are willing to teach their style. I don't
need 100s disagreeing with each other and confusing me still. That's just a
mess.

The only mess is in your head. Indent your damn code properly and then
come back. Do not use "indent". Do it yourself and a light will come on
as to WHY clear indentation is needed.
 
M

Martin Ambuhl

Bill said:
I have written this ob/os oscillator for securities analysis. The only
thing is that the result is always negative. Such as -96. I have looked over
and over this code and it looks fine to me. Is there any trouble?

Other than the fact that your formula 'price-ma*ma/100' must be wrong,
not really (if we don't count the incorrect printf specifier, the
allergy to white space, and the failure to have a useful error message).
Run the following version of your code and see what happens:

#include <stdio.h>
#include <stdlib.h>

double os(double price, double ma)
{
/* mha: note that ma / ma * 100 == 100 always */
printf("sanity check on the value computed:\n"
"price = %g, ma = %g\n"
"ma / ma * 100 = %g\n"
"price - ma / ma * 100 = %g\n",
price, ma, ma / ma * 100, price - ma / ma * 100);

return price - ma / ma * 100;
}

int main(int argc, char *argv[])
{
double price, ma; /* mha: moved to beginning of block for
pre-C99 compilers */
if (argc != 3) {
fprintf(stderr, "usage error: "
"price and 'ma' arguments needed\n");
exit(EXIT_FAILURE);
}
price = strtod(argv[1], NULL);
ma = strtod(argv[2], NULL);
printf("added sanity check on input:\n"
" price = %.2f, ma = %.2f\n", price, ma);

printf("%.2f\n", os(price, ma)); /* mha: was "%f.2" */
return 0;
}
 
B

Bill Cunningham

The only mess is in your head. Indent your damn code properly and then
come back. Do not use "indent". Do it yourself and a light will come on
as to WHY clear indentation is needed.

I'll try this.

int main(void) {

printf("indent\n");

}

Is this more what you mean braces on lines by them selves do look better.

Bill
 
R

Richard

Bill Cunningham said:
I'll try this.

int main(void) {

printf("indent\n");

}

Is this more what you mean braces on lines by them selves do look better.

Bill

Q: why didn't you indent the printf line? Think before you answer.
 
B

Bill Cunningham

Other than the fact that your formula 'price-ma*ma/100' must be wrong, not
really (if we don't count the incorrect printf specifier, the allergy to
white space, and the failure to have a useful error message). Run the
following version of your code and see what happens:

#include <stdio.h>
#include <stdlib.h>

double os(double price, double ma)
{
/* mha: note that ma / ma * 100 == 100 always */
printf("sanity check on the value computed:\n"
"price = %g, ma = %g\n"
"ma / ma * 100 = %g\n"
"price - ma / ma * 100 = %g\n",
price, ma, ma / ma * 100, price - ma / ma * 100);

return price - ma / ma * 100;
}

int main(int argc, char *argv[])
{
double price, ma; /* mha: moved to beginning of block for
pre-C99 compilers */
if (argc != 3) {
fprintf(stderr, "usage error: "
"price and 'ma' arguments needed\n");
exit(EXIT_FAILURE);
}
price = strtod(argv[1], NULL);
ma = strtod(argv[2], NULL);
printf("added sanity check on input:\n"
" price = %.2f, ma = %.2f\n", price, ma);

printf("%.2f\n", os(price, ma)); /* mha: was "%f.2" */
return 0;
}

I don't quite know what you're doing here but it does look interesting.

Bill
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top