Hi Keith,
Let me try to explain why it works. First some abbreviations: H20 = High 20 periods ago HHV20 = Highest High Value over the last 20 periods The original MetaStock logic (using my terminology) is: If (H20 >= HHV20, HHV20, if(H20 < HHV20, PREV, 0)); The conditions H20 >= HHV20 and H20 < HHV20 are mutually exclusive, in other words when H20 >= HHV20 is false, H20 < HH20 is always true. This means the third result of 0 in the original equation can never be true and thus the following equation is equivalent to the original: If (H20 >= HHV20, HHV20, PREV); The MetaStock PREV concept is a little difficult to grasp. In your formula, PREV is not the previous bar's value of HHV20 as you had coded in your original email. PREV is the previous bar's value of the statement "if (H20 >= HHV20, HHV20, PREV)". I could have coded the function as a loop which may be more understandable because it does not introduce new AFL functions into the mix: H20 = Ref(H,-20); HHV20 = HHV(H,20); High_DOTS = Null; for (i=1 i<BarCount; i++ { if (H20[i] >= HHV20[i]) High_DOTS[i] = HHV20[i]; // true condition else High_DOTS[i] = High_DOTS[i-1]; // false condition = MetaStock PREV } My approach in my last post uses the AFL function ValueWhen() to implement the MetaStock PREV condition. I'll add comments to the code to try to explain what it's doing: // In this equation, I substituted the PREV function with the value 0. Thus the tmp array has the value HHV20 when the condition // H20 >= HHV20 is true and 0 when the PREV function is false; tmp = IIf(Ref(High,-HH)>=HHV(High,HH),HHV(High,HH),0); // The ValueWhen() function updates all tmp array 0 values with the last HHV20 value of the tmp array when // the condition H20 >= HHV20 was true. Thus the tmp2 array is the tmp array with all the 0's replaced by the // last HHV20 value when the condition H20 >= HHV20 was true tmp2 = ValueWhen(Ref(High,-HH)>=HHV(High,HH),tmp); // This formula represents the original MetaStock formula where the true condition gets it's value from the HHV20 array // and the false condition gets it's value from the tmp2 array. High_DOTS = IIf(Ref(High,-HH)>=HHV(High,HH),HHV(High,HH),tmp2); It's all a little difficult to explain, but hopefully by understanding how I simplified the MetaStock logic, showing how the PREV function works in a loop, and by adding comments to my original AFL code you can understand what I did. It's really not so genius after all. :-) Regards, David _____ From: amibroker@yahoogroups.com [mailto:[EMAIL PROTECTED] On Behalf Of Keith Osborne Sent: 02/20/2007 10:23 PM To: amibroker@yahoogroups.com Subject: Re: [amibroker] Help with coding PREV alternative David, I have now raised you to Guru status! :-) :-) Thank you. It works, now I just have to understand why it does. Regards............Keith dbw451 wrote: Hi Keith, I had to read up on the PREV function in MetaStock. That is one of the strangest coding implementations for a programming language syntax I've ever seen: The PREV constant is used in a custom indicator to reference the previous output of the same formula. The PREV constant represents the value for the previous time period ONLY FOR THE STATEMENT IN WHICH IT EXISTS. Since the PREV function needs the previous bar's value of itself it's actually a recursive function. I suggest you determine the values for the first condition, fill in the rest of the values with the last good value of the first condition, then use that array for your second condition values: IH_Periods = Param("Initial High", 21, 10, 100,1); HH = IH_Periods - 1; tmp = IIf(Ref(High,-HH)>=HHV(High,HH),HHV(High,HH),0); tmp2 = ValueWhen(Ref(High,-HH)>=HHV(High,HH),tmp); High_DOTS = IIf(Ref(High,-HH)>=HHV(High,HH),HHV(High,HH),tmp2); Plot(High_DOTS, " DOTS", colorBlack, 8+16); I'm not sure if it's what you're looking for because your logic text can be interpreted a couple different ways, but it fits your logic and explanation as I understand it. I'm guessing you use this indicator as a trailing stop for shorts because as I understand it, the values (Dots) only change with changes in the high 20 periods ago. Regards, David _____ From: [EMAIL PROTECTED] <mailto:amibroker@yahoogroups.com> ps.com [mailto:amibroker@ <mailto:amibroker@yahoogroups.com> yahoogroups.com] On Behalf Of Keith Osborne Sent: 02/20/2007 7:45 PM To: [EMAIL PROTECTED] <mailto:amibroker@yahoogroups.com> ps.com Subject: Re: [amibroker] Help with coding PREV alternative Paul/Graham, have tried both suggestions and I cannot get it to work. Let me explain in more detail as I did a pretty poor job last time: :-( I have a metastock formula for a 21 day initial high entry trigger that I am trying to convert. In words the logic is: "If the HIGH of 20 days ago is >= to the highest value in the last 20 periods then use the highest high else If the HIGH of 20 periods ago is < highest high in the last 20 periods then use the prior bars high." In other words it is looking for 20 days where the all the highs are lower than the high of 21 days ago. It is looking for the initial high reversal. the metastock formula is IH = Input("Initial High", 1, 300, 21); HH = IH - 1; if(Ref(HIGH,-HH)>=HHV(HIGH,HH),HHV(HIGH,HH),if(Ref(HIGH,-HH)<HHV(HIGH,HH),PR EV,0)); My attempt at converting this, including Graham's suggestion is as follows: IH_Periods = Param("Initial High", 21, 10, 100,1); HH = IH_Periods - 1; A = Ref(H,-HH); // High of 20 periods ago B = HHV(H,HH); // Highest High in 20 periods Z = HHV(H,HH); PREV = Z; for(i=1;i<BarCount;i++) { if(A[i] < B[i]) PREV[i] = Z[i]; else PREV[i] = Z[i-1]; // PREV is previous value of Z. } High_DOTS = IIf(A >= B, B, IIf(A < B,prev, 0)); Plot(High_DOTS, " DOTS", colorBlack, 8+16); Plot(C, "Price", ColorBlack, StyleBars); The problem is that it (see attachment) bar at A is less than 20 days (13 days) from Y therefore the dots should have continued at the X level and not stepped up to the higher value. B (14 days) and C (15 days) should also be at the X level. Appreciate the help. Keith ps. this is my first attempt at a loop and also converting a Metastock formula. Paul Ho wrote: > you have assign z = 0; the whole z array equal to 0, so z[i -1] is of > course 0; > try z[0] = 0 instead and use prev[i] instead of constant PREV. > > ---------------------------------------------------------- > *From:* [EMAIL PROTECTED] <mailto:amibroker%40yahoogroups.com> ps.com [mailto:[EMAIL PROTECTED] <mailto:amibroker%40yahoogroups.com> ps.com] > *On Behalf Of *Keith Osborne > *Sent:* Tuesday, 20 February 2007 2:54 PM > *To:* [EMAIL PROTECTED] <mailto:amibroker%40yahoogroups.com> ps.com > *Subject:* [amibroker] Help with coding PREV alternative > > Hi, I am attempting to convert a Metastock formula with a PREV > statement. I have read a number of messages in Amibroker database and my > attempt is as follows. > > IH_Periods = Param("Initial High", 21, 10, 100,1); > > HH = IH_Periods - 1; > > Z = HHV(H,HH); > Z = 0; // Initialize > for(i=1;i<BarCount;i++) > { > PREV = Z[i-1]; // PREV is previous value of Z. > } > > When I use PREV in a AB formula such as iif(A > B, XX, PREV) I get 0 > (Zero) when A<B.......rather than the previous value of Z. Can I assign > a HHV to Z?) > > I hope I have explained this. > > Any help would be appreciated. > > TIA .... Keith > >