Counter function

Questions about MultiCharts and user contributed studies.
gorby
Posts: 2
Joined: 05 Oct 2010

Counter function

Postby gorby » 15 May 2011

I am having difficulty coding an indicator using the counter.

I have not been successful trying to code the number of days a stock is up during the past 20 days.

The code I am using is:


Inputs: length(20);
Vars: mysum(0), counter(0);

For counter = 0 to length -1 begin
mysum = 0;
If c > c[1] then mysum = 1 + mysum[1];
end;

Plot1(mysum,"posdays");

This code is correctly plotting each day the condition is met, but it does not provide a sum.

I would be most appreciative if anybody out there can tell me what I am doing incorrectly.

User avatar
TJ
Posts: 7740
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 1033 times
Been thanked: 2221 times

Re: Counter function

Postby TJ » 15 May 2011

I am having difficulty coding an indicator using the counter.

I have not been successful trying to code the number of days a stock is up during the past 20 days.

The code I am using is:

Code: Select all

Inputs: length(20);
Vars: mysum(0), counter(0);

For counter = 0 to length -1 begin
mysum = 0;
If c > c[1] then mysum = 1 + mysum[1];
end;

Plot1(mysum,"posdays");
This code is correctly plotting each day the condition is met, but it does not provide a sum.

I would be most appreciative if anybody out there can tell me what I am doing incorrectly.
you need to write out what you want to do -- in Plain English --- of what you want to do...
before you start coding.


now that you have done the coding,
you have to take the code line-by-line... and work out all the variables.

eg.
process step 1:
bar number (or date time) = 1
counter = 0
mysum = 0
c > c[1] = true
mysum[1] = 0
mysum = 1 + mysum[1] --> mysum = 1 + 0
what you want to plot ---> mysum = 0

process step 2:
bar number (or date time) = 1
counter = 1
mysum = ?
c > c[1] = ?
mysum[1] =
mysum = 1 + mysum[1] -->
what you want to plot ---> mysum = ?

process step 3:

... and so on

User avatar
RobotMan
Posts: 375
Joined: 12 Jul 2006
Location: Los Altos, California, USA
Has thanked: 31 times
Been thanked: 13 times
Contact:

Re: Counter function

Postby RobotMan » 15 May 2011

Hi Gorby,

you set mysum equal to 0 on each loop.
try moving the "mysum = 0;" to the line above the "For counter = 0..." and you should be good to go.

~Bob

gorby
Posts: 2
Joined: 05 Oct 2010

Re: Counter function

Postby gorby » 16 May 2011

Thanks for the help. But moving mysum doesn't solve the problem.

With this change, the indicator correctly identifies each day the condition is met and gives me the number of consecutive days that the condition has been met. However, it still doesn’t tell me how many times the condition has been met over the past twenty days.

What I am looking for is this:

Let's say that using a twenty day lookback the condition is met on days(bars) one, three, four, eight, twelve and thirteen. In this situation the indicator should give me a reading of six.

In other words, I need language that would allow me to sum up the readings that the indicator currently is giving me.

User avatar
RobotMan
Posts: 375
Joined: 12 Jul 2006
Location: Los Altos, California, USA
Has thanked: 31 times
Been thanked: 13 times
Contact:

Re: Counter function

Postby RobotMan » 16 May 2011

Did you read TJ's post?

You are just comparing today with yesterday 20 times per bar.
You need to re-visit the line:
"If c > c[1] then mysum = 1 + mysum[1];" and think about adding something more definitive to it. Think about it and it will come to you.

~Bob


Return to “MultiCharts”