Duplicate - Self-referencing, was: Programming previous value of a "value"


I have used Metastock before, and Metastock has a function called PREV. It allows You to create self-referencing


In this formula PREV:s value is the previous value of cpc.

Is it possible to do the same thing in Amibroker?

Appreciate all help!


Have you tried using the REF function?


You can use the same variable name on the right and left sides of the assignment statement. So in your example, replace PREV with cpc.

@AsaLeffe, I have told you yesterday already that everything is possible.

Metastock software does not have looping that it why they have implemented such thing called PREV. AmiBroker allows looping as well as self referencing/recursive formulations (via loop or functions such as AMA(), AMA2(), ValueWhen(),…). Difference it is hundreds of times faster than PREV.

Below is loop example

pds = ...; // insert pds here

pdscond = pds != Ref(pds,-1); // equivalent to "<>" is "!="

dc = ...; // insert dc here

cpc = 0; // initialize cpc
for( i = 1; i < BarCount; i++ ) {
    PREV = cpc[ i - 1 ];
    cpc[ i ] = IIf( pdscond[ i ], 0, dc[ i ] + PREV );

Plot( cpc, "CPC", colorRed);

You actually don’t even need to loop in upper sample.
Here is ONE of several other non loop versions.

pds = ...; // insert pds here

pdscond = pds != Ref(pds,-1);

dc = ...; // insert dc here

cpc = ValueWhen( 1, SumSince(pdscond, dc) );

Plot( cpc, "CPC", colorRed );

and so on…

So rather ask at Metastock forum whether all the millions of millions of things being possible in AmiBroker are possible in Metastock too. But don’t waste your time. I can tell you the answer already. It is: NO. Metastock is an outdated dinosaur software.

Instead of asking “Is it possible…” all the time next time rather ask “How to do it…”.

@quantboy, Ref is not the same as PREV!


@fxshrat yes I guessed that REF was not the same. I meant to point out the user should learn how to use the REF function. I am sure you noticed the REF function was used by yourself in both of your solutions. Also as @mradtke pointed out, even that wasn’t necessary but for the poor Metastock user I felt he should learn about REF.

@quantboy, as to why I have used Ref… I have used it only because it was part of the original condition. That’s the sole reason. But in message it was not asked for Ref equivalent. So I could have used anything being stored to pdscond in order to explain equivalent of PREV such as:

pdscond = MACD() < 0;// insert condition

dc = ROC(C, 1) / 100 + 1; // insert dc here

cpc = Null; // initialize cpc
for( i = 1; i < BarCount; i++ ) {
    PREV = cpc[ i - 1 ];
    cpc[ i ] = IIf( pdscond[ i ], 1, dc[ i ] * PREV );

Plot( (cpc-1)*100, "% Growth since...", colorRed);

The thing in question has been “How to replicate PREV in AmiBroker”.


BTW the equivalent to upper % growth is

dc = ROC(C, 1)/100+1;
cond = MACD() < 0;
cpc = ProdSince( cond, dc);

Plot( (cpc-1)*100, "% Growth since...", colorRed ); 
1 Like

This topic is duplicate of existing one. Always search the forum before posting duplicates.
See this:

1 Like