Troubles regarding a code

Dear Amibroker forum, I've been trying for a whole week to figure out how to convert this Metastock code into AFL. I must sadly admit that I'm stuck :dizzy_face:

The code I would like help to converted is as follows:
(Metastock non-logical language)

Lower line = If(LinearReg(C,13)>PREV,If(LinearReg(C,13)-(ATR(13)*2.5)>PREV,LinearReg(C,13)-(ATR(13)*2.5),PREV),LinearReg(C,13));

This is supposed to draw a line under the price as a kind of visual stoploss line, but not trigger the stoploss function (only as a visual guidance).

I'm currently stuck, mostly because of the fact that I'm a novice regarding coding langauge. I don't understand the Metastock formula function. I've read through knowledge base and searched members forum ( and read Metastock reference guide formula primer) and narrowed the solution (perhaps :thinking: ) to these to answers:

  1. As Tomasz writes: https://forum.amibroker.com/t/convert-metastock-indicator-to-afl/7131/4
  • but I cant figure out how to "breakdown" the Metastock formula like mentioned above.
  1. Or does the code need to converted into a loop like this example:
/* a sample low-level implementation of max-loss stop in AFL: */ 
priceatbuy=0; 
for( i = 0; i < BarCount; i++ ) 
{ 
     

     if( priceatbuy > 0 AND Low[ i ] < 0.98 * priceatbuy ) 
     { 
       Sell[ i ] = 1; 
       SellPrice[ i ] = 0.98 * priceatbuy; 
       priceatbuy = 0; 
     } 
     else 
       Sell[ i ] = 0; 

      if( priceatbuy == 0 AND Buy[ i ] ) 
           priceatbuy = BuyPrice[ i ]; 

}

I just can't fully understand the loop function and how "breakdown" the Metastock code.

Thanks in advance!

Kind regards

First you should store multiple same calls to a variable... for speed and better readability reasons.
Next, yes one may use loop to do convert to AFL. There isn't looping in Metastock. But AFL looping being much faster than MetaStock PREV.

lin_reg = LinearReg(C,13);
my_atr = ATR(13);
indis_diff = lin_reg - my_atr*2.5;

Lower_line = Null;
for ( i = 1; i < BarCount; i++ ) {
	PREV = Lower_line[ i - 1 ];
	Lower_line[ i ] = IIf(lin_reg[ i ] > PREV, IIf(indis_diff[ i ] > PREV, indis_diff[ i ], PREV), lin_reg[ i ]);
}

Plot( Lower_line, "Lower_line", colorRed );
2 Likes

Thank you @fxshrat for taking your time to code. :+1:

To understand it correct, the PREV in the AFL code above is not the same as the PREV FUNCTION in MS right? So it should not cause the entire formula to be wrapped in implicit loop causing re-execution that many times as there are bars in the data?

Contrary to MS code, PREV in @fxshrat code is just an ordinary variable. And in fact you don't need to use it at all. @fxshrat used it to show you the "logical equivalent" of MS code, but you could just use Lower_line[ i - 1 ] directly in all places where the code was using PREV and remove all PREV occurrences.

It does NOT cause any side effects or any implicit actions nor executions.

2 Likes

@Tomasz Thank you for clarifying it.
Also thanks for the great software and the development of it!