# Counter don't start at zero

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
arnie
Posts: 1590
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 479 times
Been thanked: 509 times

### Counter don't start at zero

Hi guys.

Can you help me here?

This a simple study that counts how many bars, in this case, MACD, fills above and below zero line.
The problem is that the counter starts always at 1 and never at zero. I can't understand why. I'm starting the count at zero everytime the bars crosses zero line.

Another strange reaction is although I'm requesting to count the up bars, above zero, it actually counts the down bars, below zero.

Code: Select all

`inputs:   fastLength   (14),   slowLength   (6);variables:   counterUpBars      (0),   counterDownBars   (0),   upCount      (false),   downCount      (false),   myMACD      (0);      myMACD = MACD(close, fastLength, slowLength);if myMACD > 0 then begin   upCount = true;   counterUpBars = 0;endelse begin   downCount = true;   counterDownBars = 0;end;if upCount then   counterUpBars = counterUpBars + 1;if downCount then    counterDownBars = counterDownBars + 1;plot1(counterUpBars);//plot2(counterDownBars);`

As usual, I'm missing something.

Regards,
Fernando

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Counter don't start at zero

arnie wrote:Hi guys.

Can you help me here?

This a simple study that counts how many bars, in this case, MACD, fills above and below zero line.
The problem is that the counter starts always at 1 and never at zero. I can't understand why. I'm starting the count at zero everytime the bars crosses zero line.
...

because as soon as the condition is true, you added 1 to the counter.

Code: Select all

`...if upCount then   counterUpBars = counterUpBars + 1;...`

there are many ways to fix this,
the easiest way would be to initialize the counter to -1:

Code: Select all

`if myMACD > 0 then begin   upCount = true;   counterUpBars = -1;  //<--- initialize at -1end`

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Counter don't start at zero

arnie wrote:...
Another strange reaction is although I'm requesting to count the up bars, above zero, it actually counts the down bars, below zero.
...
As usual, I'm missing something.

Regards,
Fernando

because when downCount = true
you DID NOT tell the computer that upcount is now FALSE.
thus the computer keeps on counting...

Code: Select all

`...else begin   upcount = FALSE;   //<--- add this line !!!   downCount = true;   counterDownBars = 0;end;...`

arnie
Posts: 1590
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 479 times
Been thanked: 509 times

### Re: Counter don't start at zero

Hi TJ.

there are many ways to fix this,
the easiest way would be to initialize the counter to -1:

Yes, that was it.

because when downCount = true
you DID NOT tell the computer that upcount is now FALSE.
thus the computer keeps on counting...

Although this makes perfect sense, adding that line the study will plot zero.

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Counter don't start at zero

arnie wrote:....
As usual, I'm missing something.

Regards,
Fernando

my suggestions:

before you start writing a single line of code, always start by:

1. WRITING DOWN what you want to achieve (in signals as well as in benefits)
2. draw a mock up diagram/chart of how the indicator would look like (with lines and arrows and notes to illustrate your vision)
3. draw a flow chart... showing each IF-THEN-ELSE branches
ie. IF myMACD > 0, what should happen to each variable:
you must account for ALL the variables at each juncture -- counterUpBars, upCount, counterdownBars, downCount , etc.,
4. look for mutually exclusive conditions... and make sure they do not interfere with each other. (using ELSE will prevent some of the cross overs)

HTH

arnie
Posts: 1590
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 479 times
Been thanked: 509 times

### Re: Counter don't start at zero

Finally!

Code: Select all

`inputs:   fastLength   (14),   slowLength   (6);variables:   counterUpBars      (0),   counterDownBars   (0),   upCount      (false),   downCount      (false),   myMACD         (0);      myMACD = MACD(close, fastLength, slowLength);if myMACD > 0 then begin   upCount = true;   downCount = false;endelse begin      upCount = false;   downCount = true;end;if upCount = true then begin   counterUpBars = counterUpBars + 1;   counterDownBars = 0;end;if downCount = true then begin   counterUpBars = 0;   counterDownBars = counterDownBars + 1;end;plot1(counterUpBars);plot2(counterDownBars);//plot3(myMACD);`

Why such simple things takes ages to be understood

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Counter don't start at zero

arnie wrote:Finally!

Congratulations !

arnie wrote:Why such simple things takes ages to be understood

There is a first time for everything.
It will be faster next time.

Posts: 353
Joined: 30 Jul 2010
Location: Chicago, IL
Has thanked: 155 times
Been thanked: 215 times

### Re: Counter don't start at zero

Over time, you will learn how to think like a computer thinks, and it will become easier (although not foolproof), to detect these issues.

arnie
Posts: 1590
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 479 times
Been thanked: 509 times

### Re: Counter don't start at zero

Yes, with time.

The problem is that we, the ones with no programming skills, we don't deal with programming language on a daily basis, and this can be deadly when you are learning, because you tend to forget stuff if you stay a way from it for to long

Also, one of the things manuals lack is a comprehensive language when explaining stuff regarding programming.
Easy language manuals assume that the reader knows about programming languages, hence the simple explanations on functions and reserved words. For a programmer, that explanation is sufficient for him to understand what can be done with a specific function, but for a newbie, a person that don't know how a computer talks, he will find it very difficult.

There should be a manual where newbies could learn the computer way. How to think like a computer, how to manage our thoughts and ideas with a computer language, in this case, easy language.

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Counter don't start at zero

arnie wrote:Yes, with time.

The problem is that we, the ones with no programming skills, we don't deal with programming language on a daily basis, and this can be deadly when you are learning, because you tend to forget stuff if you stay a way from it for to long

Also, one of the things manuals lack is a comprehensive language when explaining stuff regarding programming.
Easy language manuals assume that the reader knows about programming languages, hence the simple explanations on functions and reserved words. For a programmer, that explanation is sufficient for him to understand what can be done with a specific function, but for a newbie, a person that don't know how a computer talks, he will find it very difficult.

There should be a manual where newbies could learn the computer way. How to think like a computer, how to manage our thoughts and ideas with a computer language, in this case, easy language.

The ebook on functions and reserved words is a reference, it is meant to be straight to the point.

The other ebooks* are actually not bad; with well explained examples on each key topic, they are all you needed to learn to program in EasyLanguage.

*other ebooks:
Getting Started with EasyLanguage is the book you need if you're thinking about using EasyLanguage but don't know where to start.
EasyLanguage Reference Guide could easily be named "Everything you wanted to know about EasyLanguage but were afraid to ask."
The essential EasyLanguage programming guide allows you to quickly look up usage and syntax concepts and examples for the most commonly used features of EasyLanguage.

arnie
Posts: 1590
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 479 times
Been thanked: 509 times

### Re: Counter don't start at zero

Hi.

TJ your words are very appreciated, though continuing my work on the study I got stuck, once again. I just can't find nothing in the manuals that could share some light on this.

I twisted a bit the initial study, and I wanted to be able to plot on the bar chart the price range when MACD is above and below zero line.

As we can see on the attached chart, I have the ranges calculations already done, but look how the text is being plotted.

I just want to be able to see the price range for each MACD side plotted once.
Each new bar for the MACD side the text will plot the price range made so far.

Code: Select all

`inputs:   fastLength         (14),   slowLength         (6);variables:   counterUpBars      (0),   counterDownBars      (0),   upCount         (false),   downCount         (false),   myMACD         (0),   highUpMACD         (0),    lowUpMACD         (0),    highDnMACD         (0),   lowDnMACD         (0),   highMACD         (0),   lowMACD         (0),   upSwing         (0),   dnSwing         (0),   txtAbove         (-1),   txtBelow         (-1);      myMACD = MACD(close, fastLength, slowLength);if myMACD > 0 then begin   upCount = true;   downCount = false;endelse begin      upCount = false;   downCount = true;end;//___ MACD above zero line calculations ___if upCount = true then begin   if high > highUpMACD then      highUpMACD = high;   if low < lowUpMACD then      lowUpMACD = low;            highMACD = highUpMACD;      lowMACD = lowUpMACD;      upSwing = highMACD - lowMACD;            highDnMACD = -999999;       lowDnMACD = +999999;          txtAbove = Text_New_S(Date, Time, 0, " ");      Text_SetSize(txtAbove , 10);      Text_SetColor(txtAbove , yellow);      Text_SetStyle(txtAbove , 2, 1);         text_setstring(txtAbove , numtostr(upSwing, 4));   text_setlocation_s(txtAbove , date, time_s, highMACD);end;//___ MACD below zero line calculations ___if downCount = true then begin   if high > highDnMACD then      highDnMACD = high;   if low < lowDnMACD then      lowDnMACD = low;         highMACD = highDnMACD;      lowMACD = lowDnMACD;      dnSwing = highMACD - lowMACD;            highUpMACD = -999999;       lowUpMACD = +999999;          txtBelow = Text_New_S(Date, Time, 0, " ");      Text_SetSize(txtBelow , 10);      Text_SetColor(txtBelow , yellow);      Text_SetStyle(txtBelow , 2, 1);         text_setstring(txtBelow , numtostr(dnSwing, 4));   text_setlocation_s(txtBelow , date, time_s, myMACD);end;plot1(upSwing);plot2(dnSwing);`
Attachments
MACD_study.png (91.12 KiB) Viewed 705 times

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Counter don't start at zero

arnie wrote:Hi.
...
I just want to be able to see the price range for each MACD side plotted once.
Each new bar for the MACD side the text will plot the price range made so far.
...

if you write out your thoughts...
one line at a time,
one action per line...
(as if you were teaching a little kid how to draw)

you will come to your code logic quickly.

Let me show you...

for every bar
I want to print the price range
when a new bar is created
I want to delete the OLD price range in the previous bar
then I want to print the NEW price range on the new bar

to do the above,
one easy way is to delete the old text before your TEXT_NEW.
note: I say "easy way" because there are more than one way to do this.

Code: Select all

`...   text_delete(txtAbove);   // <--- add this line   txtAbove = Text_New_S(Date, Time, 0, " ");...`

arnie
Posts: 1590
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 479 times
Been thanked: 509 times

### Re: Counter don't start at zero

Unfortunately is a bit more complicated than that.

If I wanted the range to be plotted just on the current bar I could also done this:

Code: Select all

`if currentbar = 1 then begin   txtAbove = Text_New_S(Date, Time, 0, " ");      Text_SetSize(txtAbove , 10);      Text_SetColor(txtAbove , yellow);      Text_SetStyle(txtAbove , 2, 1);   txtBelow = Text_New_S(Date, Time, 0, " ");      Text_SetSize(txtBelow , 10);      Text_SetColor(txtBelow , yellow);      Text_SetStyle(txtBelow , 2, 1);end;`

That is my idea. To retain previous text plots.

So I have each price range delimited. What I need is to move the text each new bar on that price range, holding the highest or lowest price value til the end of that same range (ie. til MACD reverses).
Attachments
my_idea.png (65.53 KiB) Viewed 703 times

TJ
Posts: 6873
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 984 times
Been thanked: 1976 times

### Re: Counter don't start at zero

arnie wrote:Hi.
...
I twisted a bit the initial study, and I wanted to be able to plot on the bar chart the price range when MACD is above and below zero line.

arnie wrote:Unfortunately is a bit more complicated than that.

If I wanted the range to be plotted just on the current bar I could also done this:
....

the pot thickens...

see instructions in my posts #5 and #12.

arnie
Posts: 1590
Joined: 11 Feb 2009
Location: Portugal
Has thanked: 479 times
Been thanked: 509 times

### Re: Counter don't start at zero

... after 2 days screaming, banging my head over the keyboard, insulting the easy language manuals, and 2 nights well slept...

Code: Select all

`inputs:   fastLength         (14),   slowLength         (6);   variables:   counterUpBars      (0),   counterDownBars      (0),   upCount         (false),   downCount         (false),   myMACD         (0),   highUpMACD         (0),    lowUpMACD         (0),    highDnMACD         (0),   lowDnMACD         (0),   highMACD         (0),   lowMACD         (0),   upSwing         (0),   dnSwing         (0),   txtAbove         (-1),   txtBelow         (-1),   upMACD         (0),   dnMACD         (0),   upMACDstarted      (false),   dnMACDstarted      (false);      myMACD = MACD(close, fastLength, slowLength);if myMACD <  0 then begin   upCount = true;   downCount = false;   //plot1(0,"Trend");   //SetPlotColor(1, green);endelse begin      upCount = false;   downCount = true;      //plot1(0,"Trend");   //SetPlotColor(1, red);end;//___ MACD above zero line calculations ___if upCount = true then begin   counterUpBars = counterUpBars + 1;   counterDownBars = 0;   if upCount <> upCount[1] then begin      upMACD = upMACD + 1;      upMACDstarted = true;                     txtAbove = text_new(Date, Time, 0, " ");      //txtAbove = text_new_self(Date, Time, 0, " ");         Text_SetSize(txtAbove , 10);         Text_SetColor(txtAbove , yellow);   end;   if high > highUpMACD then      highUpMACD = high;   if low < lowUpMACD then      lowUpMACD = low;            highMACD = highUpMACD;      lowMACD = lowUpMACD;      upSwing = highMACD - lowMACD;      upSwing = FracPortion(upSwing) * 10000;             highDnMACD = -999999;       lowDnMACD = +999999;          if upMACDstarted then begin      text_setstring(txtAbove , numtostr(upSwing, 0));      text_setlocation(txtAbove , date, time, highMACD);      //text_setlocation(txtAbove , date, time, 0);      Text_SetStyle(txtAbove , 2, 1);      //Text_SetStyle(txtAbove , 0, 1);   end;      end;//___ MACD below zero line calculations ___if downCount = true then begin   counterDownBars = counterDownBars + 1;   counterUpBars = 0;   if downCount <> downCount[1] then begin      dnMACD = dnMACD + 1;      dnMACDstarted = true;                     txtBelow = text_new(Date, Time, 0, " ");      //txtBelow = text_new_self(Date, Time, 0, " ");         Text_SetSize(txtBelow , 10);         Text_SetColor(txtBelow , yellow);   end;   if high > highDnMACD then      highDnMACD = high;   if low < lowDnMACD then      lowDnMACD = low;         highMACD = highDnMACD;      lowMACD = lowDnMACD;      dnSwing = highMACD - lowMACD;      dnSwing = FracPortion(dnSwing) * 10000;             highUpMACD = -999999;       lowUpMACD = +999999;       if dnMACDstarted then begin      text_setstring(txtBelow , numtostr(dnSwing, 0));      text_setlocation(txtBelow , date, time, lowMACD);      //text_setlocation(txtBelow , date, time, 0);      Text_SetStyle(txtBelow , 0, 0);      //Text_SetStyle(txtBelow , 0, 0);   end;end;   plot2(highMACD);plot3(lowMACD);//plot4(counterUpBars);//plot5(counterDownBars);`

First made the version where the text is plotted on the bar chart, but since I'm in favour of a clean chart, made a second version to be plotted on a subchart.