PureBytes Links
Trading Reference Links
|
Terry --
Thanks for your help. Your second sentence is the real KEY to the
thing. Unfortunately it took me quite a while to get it into the
correct corner of my brain. Below is my original code followed by the
new working code.
-- Keith
/* ================== Original none working code
====================
// doesn't work because "You can't assign to an array by referencing
// prior values of that same array inside and IIF statement." (Terry)
// is price falling
isfalling = 0; // initialize array
isfalling = IIf(C<Ref(C,-1) OR C==Ref(C,-1) AND Ref(isfalling, -1),
1, 0);
//extra code for troubleshooting
//Plot(IIf(isfalling,500,400), "isfalling", colorRed, styleLine); //
below recent RUT2K
// if price just changed to falling then last value was a peak
// set value of recentpeak to the peak and follow it until next peak
recentpeak[0] = C[0]; // initialize to value higher than recent RUT2K
price
recentpeak = IIf(isfalling AND NOT Ref(isfalling,-1), Ref(C,-1),
Ref(recentpeak,-1));
//PROBLEM: recentpeak is set to proper value but then goes to 650 on
the next bar!!!!
// This means that if conditional statement is false then recentpeak
is NOT set
// to Ref(recentpeak,-1) as it should be but just stays at initial
value.
==================== End of Original none working code ==============*/
// This code works
isfalling[0] = 0; // initialize array
recentpeak[0] = C[0];
for(i=1; i<BarCount; i++){ // notice, use '<' and not "<="
if(C[i]<C[i-1] OR C[i]==C[i-1] AND isfalling[i-1])
isfalling[i]=1;
else{
isfalling[i]=0;
}
if(isfalling[i] AND NOT isfalling[i-1])
recentpeak[i] = C[i-1];
else
recentpeak[i] = recentpeak[i-1];
}
GraphXSpace=5;
Plot(C, "Close", colorBlack, styleLine);
Plot(recentpeak, "Recent Peak", colorGreen, styleLine);
Terry wrote:
Just going to the 2nd line of code. You can't assign to an array by
referencing prior values of that same array inside and IIF statement.
In your case, your third test will always be false since the value of
isfalling "at the time of evaluating your 2nd line of code" will be 0
based
on your first line of code.
The only way to check previous values as you are trying to do is to use
a
loop that evaluates each bar of the array one at a time, thus the prior
value IS then available to test against.
This is why you had to "initialize" the array first to get rid of the
error
"..array not initialized". It just makes it all zeros and that is what
is
referred to while line 2 is executing.
PS: Here's a simplified portion of the first part of line 2...
isfalling = IIf(C <= Ref(C,-1)...
instead of
isfalling = IIf(C<Ref(C,-1) OR C==Ref(C,-1)...
Terry
--
> I am still having a problem using IIf() and arrays to track peaks
and
> valleys. I just tried to simplify and comment the code so people
> could more easily see my problem. TIA for your help. -- Keith
>
> ======= code starts here ================
> // is price falling
> isfalling = 0; // initialize array
> isfalling = IIf(C<Ref(C,-1) OR C==Ref(C,-1) AND Ref(isfalling,
-1),
> 1, 0);
> //extra code for troubleshooting
> Plot(IIf(isfalling,500,400), "isfalling", colorRed, styleLine); //
> below recent RUT2K
>
>
> recentpeak = 650; // initialize to value higher than recent RUT2K
> price
> // if price just changed to falling then last value was a peak
> // set value of recentpeak to the peak and follow it until next
peak
> recentpeak = IIf(isfalling AND NOT Ref(isfalling,-1), Ref(C,-1),
Ref
> (recentpeak,-1));
> //PROBLEM: recentpeak is set to proper value but then goes to 650
on
> the next bar!!!!
> // This means that if conditional statement is false then
recentpeak
> is NOT set
> // to Ref(recentpeak,-1) as it should be but just stays at
inital
> value of 650.
>
> GraphXSpace=5;
> Plot(C, "Close", colorBlack, styleLine); // testing with RUT which
> is near 500
> Plot(recentpeak, "Recent Peak", colorGreen, styleLine);
>
>
Check AmiBroker web page at:
http://www.amibroker.com/
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html
Check AmiBroker web page at:
http://www.amibroker.com/
Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html
Yahoo! Groups Sponsor |
ADVERTISEMENT
| |
|
Yahoo! Groups Links
|
|