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....
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
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.
thanks.
L
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
Mmm, i see this is going to be a bit more complex. i'll do some more reading. ...
L
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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