[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [amibroker] Help with coding PREV alternative



PureBytes Links

Trading Reference Links

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: amibroker@xxxxxxxxxxxxxxx [mailto:amibroker@xxxxxxxxxxxxxxx] On Behalf
Of Keith Osborne
Sent: 02/20/2007 7:45 PM
To: amibroker@xxxxxxxxxxxxxxx
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:* amibroker@xxxxxxxxx <mailto:amibroker%40yahoogroups.com> ps.com
[mailto:amibroker@xxxxxxxxx <mailto:amibroker%40yahoogroups.com> ps.com] 
> *On Behalf Of *Keith Osborne
> *Sent:* Tuesday, 20 February 2007 2:54 PM
> *To:* amibroker@xxxxxxxxx <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
>
> 

 


Content-Description: "AVG certification"
No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.441 / Virus Database: 268.18.3/694 - Release Date: 2/20/2007 1:44 PM