Bar counting – a Noob question

 

I cannot seem to get this counter to function. Tried several different methods as described in the help files as well as used in other EA’s i’ve looked at but with no success.

I need to count the number of bars from a certain event and reset the counter to zero when it occurs again. I refer to this counter further down in the code...

Currently it looks like this ..

if (iOpen(NULL,0,0)>0)

{

MACDBarCount = MACDBarCount+1;

if ((MACDPrevious<0 && MACDCurrent>0) || (MACDPrevious>0 && MACDCurrent<0))

{

MACDBarCount = 1;

// Alert (" Debug MACD Reset ", MACDBarCount);

}

// Alert (" Debug MACD Count ", MACDBarCount);

}

Second question – is there an easy way to insert a regression channel (previous day) 0:00 to 24:00?

L

 
Not sure if it's what your after, but I use iTime to give me the time of the bar that the event happened and then iBarshift to count back the bars. Next time event happens the time is updated and the count starts again. Hope that helps.
V
 

OK

Had a look and it looks like this could work. However, it seems my problem is not the function I use but rather the method of resetting a counter at a specific point and saving that value as future reference. In all the attempts I have made the counter will cont (bars), return a “0” (or the time in seconds) on the reset signal but then carry on counting from the last known number. The very next pass the reset counter will read zero instead of the last obtained value.

Code

if((MACDPrevious<0 && MACDCurrent>0) || (MACDPrevious>0 && MACDCurrent<0))

{

MACDBarCros = iTime(NULL,0,0);

Alert (" Debug MACD Cros ", MACDBarCros);

}

else

while (MACDBarCount < 5)

{

MACDBarCount = iBarShift(NULL,0,MACDBarCros);

Alert (" Debug MACD Count ", MACDBarCount);

continue;

}

Alert ( " Debug 2 - MACD Cros ", MACDBarCros);

Debug

07:30 GBPUSD,M15: Alert: Debug MACD Count 1031

07:30 GBPUSD,M15: Alert: Debug 2 - MACD Cros 0 – no reset yet

07:45 GBPUSD,M15: Alert: Debug MACD Count 1032

07:45 GBPUSD,M15: Alert: Debug 2 - MACD Cros 0

08:00 GBPUSD,M15: Alert: Debug MACD Cros 1270454400 - Reset timer giving time of the reset event

08:00 GBPUSD,M15: Alert: Debug 2 - MACD Cros 1270454400 – on completion of the cycle the value remains.

08:15 GBPUSD,M15: Alert: Debug MACD Count 1034 – counter did not reset.

08:15 GBPUSD,M15: Alert: Debug 2 - MACD Cros 0 – on the following pass the counter was reset ??!!

08:30 GBPUSD,M15: Alert: Debug MACD Count 1035

It should return - 1031, 1032, 0, 1, 2, 3, ...

PS

I have no programming background and have never written a script like this before. Played with “Basic” and “Pascal” some twenty-five years ago....

 
As I say, I'm new at this and I'm replying to aid in my own learning but from my limited experience I have always had issues mixing &&'s and || in the same if statement. I could never get them to work so I just split them out. A side benifit, is you can also tell which way the cross happened if that's important. I also added an = sign on one side... what happens if the lines touch and are equal for a couple of bars...

I also think your while statement won't work because the refresh of the counter happens inside the while... it needs to happen before you do the test. When the program initialises, time starts at zero so the barshift will be all the bars on the chart. you will then never get into barcount<5 and therefore never refresh the shift with the latest time stamp.

I thought it would work this way...hopefully the more skilled around here can correct me if I'm wrong.

if (MACDPrevious<0 && MACDCurrent>=0)            //cross up or touched from below
   {
   MACDBarCros = iTime(NULL,0,0);             // crossed up time
   Alert (" Debug MACD Crossed up ", MACDBarCros); // returns the time
   }
   
if (MACDPrevious>0 && MACDCurrent<=0)           // crossed down or touched from above
   {
   MACDBarCros = iTime(NULL,0,0);             // crossed down time
   Alert (" Debug MACD Crossed down ", MACDBarCros); // returns the time
   }

MACDBarCount = iBarShift(NULL,0,MACDBarCros);   // count the shift

if (MACDBarCount < 5)
   {
   Alert (" Debug MACD Count ", MACDBarCount);
   }

Alert ( " Debug 2 - MACD Cros ", MACDBarCros); 
Hope that helps
V
 
Leftless:
The very next pass the reset counter will read zero instead of the last obtained value.

Just thinking more... make sure the variable declerations are at the global scope. If you are declaring them in start() they will get reset to zero with every new tick.
 
Viffer wrote >>
Just thinking more... make sure the variable declerations are at the global scope. If you are declaring them in start() they will get reset to zero with every new tick.


Aha,, i think this is the cayuse of my problem. everything is defined under start() and not global. will play.

thanks.

L
 
global or static
Are you testing once per bar and not looking at the incomplete bar
int start (){                                static datetime Time0;
bool newBar = Time[shift] > Time0;   if (!newBar) return;    Time0 = Time[shift];

 

Thanks Viffer

Again you solved my problem. I moved the variable “MACDBarCount” to the global section. I think this was the big culprit. The counter runs as it should.

The only outstanding component is to draw a regression channel from 0:00 to 24:00 of the previous day and check for a breakout – any ideas on that??

Regards

L

 
Now you have me stumped. Some one else is going to have to take up the batton.
 

Mmm, i see this is going to be a bit more complex. i'll do some more reading. ...

L

 
WHRoeder wrote >>
global or static
Are you testing once per bar and not looking at the incomplete bar


I need to test once per bar. i'll check your method.

thanks


L