Australian (ASX) Stock Market Forum

# Amibroker FAQ

#### rnr

Hi Guys,

Is there an easier way to code the below?

and is it possible to define a search criteria for REF or timeframegetprice?
IE i would like to be able to return the last 20 highs when the stochastics above 50?

Code:
``````//get last 20 highs

wh1 = Ref( H, -1 );
wh2 = Ref( H, -2 );
wh3 = Ref( H, -3 );
wh4 = Ref( H, -4 );
wh5 = Ref( H, -5 );
wh6 = Ref( H, -6 );
wh7 = Ref( H, -7 );
wh8 = Ref( H, -8 );
wh9 = Ref( H, -9 );
wh10 = Ref( H, -10 );
wh11 = Ref( H, -11 );
wh12 = Ref( H, -12 );
wh13 = Ref( H, -13 );
wh14 = Ref( H, -14 );
wh15 = Ref( H, -15 );
wh16 = Ref( H, -16 );
wh17 = Ref( H, -17 );
wh18 = Ref( H, -18 );
wh19 = Ref( H, -19 );
wh20 = Ref( H, -20 );
//get last 20 lows

wl1 = Ref( L, -1 );
wl2 = Ref( L, -2 );
wl3 = Ref( L, -3 );
wl4 = Ref( L, -4 );
wl5 = Ref( L, -5 );
wl6 = Ref( L, -6 );
wl7 = Ref( L, -7 );
wl8 = Ref( L, -8 );
wl9 = Ref( L, -9 );
wl10 = Ref( L, -10 );
wl11 = Ref( L, -11 );
wl12 = Ref( L, -12 );
wl13 = Ref( L, -13 );
wl14 = Ref( L, -14 );
wl15 = Ref( L, -15 );
wl16 = Ref( L, -16 );
wl17 = Ref( L, -17 );
wl18 = Ref( L, -18 );
wl19 = Ref( L, -19 );
wl20 = Ref( L, -20 );

//20 period average
tadr = (Sum(H,20) - Sum(L,20)) / 20;

or

#### kastin

thanks for the quick replys guys , sorry for my vagueness, i think the valuewhen syntax is what i was after
I have to keep all the “days” (wo1 and wo2 and so forth) separate as there are further calculations to follow that require them to be that way,

Problem 2 -
I am finding that when i select a bar on the chart when the stochastic turn or basically when i want the IIF statement to change the parameters of “indis” the otohigh and otolow don’t seem to change very dramatically which leads me to believe i am missing something from my formula
Cheers
Jon

Code:
``````_SECTION_BEGIN("unknown");
sto1 = Ref(StochK(20,14),-0 );
sto2 = Ref(StochK(20,14),-1 );

IIf(sto1>=sto2, indic=sto1>=sto2, indic=sto1<=sto2);

dire = WriteIf(sto1>=sto2, "L" , "S");
indis=indic;

//get last 20 opens

wo1 = ValueWhen(indis,O, -1 );
wo2 = ValueWhen(indis,O, -2 );
wo3 = ValueWhen(indis,O, -3 );
wo4 = ValueWhen(indis,O, -4 );
wo5 = ValueWhen(indis,O, -5 );
wo6 = ValueWhen(indis,O, -6 );
wo7 = ValueWhen(indis,O, -7 );
wo8 = ValueWhen(indis,O, -8 );
wo9 = ValueWhen(indis,O, -9 );
wo10 = ValueWhen(indis,O, -10 );
wo11 = ValueWhen(indis,O, -11 );
wo12 = ValueWhen(indis,O, -12 );
wo13 = ValueWhen(indis,O, -13 );
wo14 = ValueWhen(indis,O, -14 );
wo15 = ValueWhen(indis,O, -15 );
wo16 = ValueWhen(indis,O, -16 );
wo17 = ValueWhen(indis,O, -17 );
wo18 = ValueWhen(indis,O, -18 );
wo19 = ValueWhen(indis,O, -19 );
wo20 = ValueWhen(indis,O, -20 );
//get last 10 weekly lows

wl1 = ValueWhen(indis,L, -1 );
wl2 = ValueWhen(indis,L, -2 );
wl3 = ValueWhen(indis,L, -3 );
wl4 = ValueWhen(indis,L, -4 );
wl5 = ValueWhen(indis,L, -5 );
wl6 = ValueWhen(indis,L, -6 );
wl7 = ValueWhen(indis,L, -7 );
wl8 = ValueWhen(indis,L, -8 );
wl9 = ValueWhen(indis,L, -9 );
wl10 = ValueWhen(indis,L, -10 );
wl11 = ValueWhen(indis,L, -11 );
wl12 = ValueWhen(indis,L, -12 );
wl13 = ValueWhen(indis,L, -13 );
wl14 = ValueWhen(indis,L, -14 );
wl15 = ValueWhen(indis,L, -15 );
wl16 = ValueWhen(indis,L, -16 );
wl17 = ValueWhen(indis,L, -17 );
wl18 = ValueWhen(indis,L, -18 );
wl19 = ValueWhen(indis,L, -19 );
wl20 = ValueWhen(indis,L, -20 );

wh1 = ValueWhen(indis,H, -1 );
wh2 = ValueWhen(indis,H, -2 );
wh3 = ValueWhen(indis,H, -3 );
wh4 = ValueWhen(indis,H, -4 );
wh5 = ValueWhen(indis,H, -5 );
wh6 = ValueWhen(indis,H, -6 );
wh7 = ValueWhen(indis,H, -7 );
wh8 = ValueWhen(indis,H, -8 );
wh9 = ValueWhen(indis,H, -9 );
wh10 = ValueWhen(indis,H, -10 );
wh11 = ValueWhen(indis,H, -11 );
wh12 = ValueWhen(indis,H, -12 );
wh13 = ValueWhen(indis,H, -13 );
wh14 = ValueWhen(indis,H, -14 );
wh15 = ValueWhen(indis,H, -15 );
wh16 = ValueWhen(indis,H, -16 );
wh17 = ValueWhen(indis,H, -17 );
wh18 = ValueWhen(indis,H, -18 );
wh19 = ValueWhen(indis,H, -19 );
wh20 = ValueWhen(indis,H, -20 );

wc1 = ValueWhen(indis,C, -1 );
wc2 = ValueWhen(indis,C, -2 );
wc3 = ValueWhen(indis,C, -3 );
wc4 = ValueWhen(indis,C, -4 );
wc5 = ValueWhen(indis,C, -5 );
wc6 = ValueWhen(indis,C, -6 );
wc7 = ValueWhen(indis,C, -7 );
wc8 = ValueWhen(indis,C, -8 );
wc9 = ValueWhen(indis,C, -9 );
wc10 = ValueWhen(indis,C, -10 );
wc11 = ValueWhen(indis,C, -11 );
wc12 = ValueWhen(indis,C, -12 );
wc13 = ValueWhen(indis,C, -13 );
wc14 = ValueWhen(indis,C, -14 );
wc15 = ValueWhen(indis,C, -15 );
wc16 = ValueWhen(indis,C, -16 );
wc17 = ValueWhen(indis,C, -17 );
wc18 = ValueWhen(indis,C, -18 );
wc19 = ValueWhen(indis,C, -19 );
wc20 = ValueWhen(indis,C, -20 );

//Open to low av
otolow =    (wo1-wl1 + wo2-wl2 + wo3-wl3 + wo4-wl4 + wo5-wl5 + wo6-wl6 + wo7-wl7 + wo8-wl8 + wo9-wl9 + wo10-wl10 + wo11-wl11 + wo12-wl12 + wo13-wl13 + wo14-wl14 + wo15-wl15 + wo16-wl16 + wo17-wl17 + wo18-wl18 + wo19-wl19 + wo20-wl20) /20;
//Open to high av
otohigh =   (wh1-wo1 + wh2-wo2 + wh3-wo3 + wh4-wo4 + wh5-wo5 + wh6-wo6 + wh7-wo7 + wh8-wo8 + wh9-wo9 + wh10-wo10 + wh11-wo11 + wh12-wo12 + wh13-wo13 + wh14-wo14 + wh15-wo15 + wh16-wo16 + wh17-wo17 + wh18-wo18 + wh19-wo19 + wh20-wo20)/20;
//Open to close av crude
otoclosec = (wo1-wc1 + wo2-wc2 + wo3-wc3 + wo4-wc4 + wo5-wc5 + wo6-wc6 + wo7-wc7 + wo8-wc8 + wo9-wc9 + wo10-wc10 + wo11-wc11 + wo12-wc12 + wo13-wc13 + wo14-wc14 + wo15-wc15 + wo16-wc16 + wo17-wc17 + wo18-wc18 + wo19-wc19 + wo20-wc20)/20;

Title = EncodeColor(colorWhite)+"  "+  Date()+"   "+EncodeColor(colorWhite)+ dire + "\n"+

EncodeColor(37) + "  open to low    =   "	+ 	EncodeColor(37)+ otolow + " " +"\n"+
EncodeColor(37) + "  open to high    =   "	+ 	EncodeColor(37)+ otohigh + " " +"\n"+
EncodeColor(37) + "  open to close crude    =   "	+ 	EncodeColor(37)+ otoclosec + " " +"\n";
GraphXSpace=5;
_SECTION_END();``````

#### edilsonsantos

Some genius is able to translate my "exit" of the long position to AB code?
I want a stop loss to 10% of the historical volatility below of the price of entry; when the price of stock reaches the first profit target (10% of the historical volatility above of the price of entry), I want to sell (Sell) part of the position and reposition the stop to the price of the entry. From there, the stop should move and be repositioned at the low (L) of the previous bar.

Edilson

#### howardbandy

Hi Edilson --

The most straightforward way is for you to program a loop, process all bars of data, and handle the conditions and prices within the loop code.

Thanks,
Howard

#### edilsonsantos

Hi Howard,
Thanks for the suggestion.
I'm thinking of using as model the code of Fig. 7.10 (scaling out) of his book (QTS). I believe it is a good alternative. I'll work on that idea today and I'll post my findings here.

Thanks.
Edilson

#### Synergy

I'm trying to find the maximum result of several statements.
It appears the MAX statement is only good for 2 conditions?

#### rnr

Try nesting the Max functions.

Max(Value1,Max(Value2,Value3))

I don't know if there is a limit on the number of times the Max function can be nested.

#### Synergy

Cheers guys I'll do some playing. The looping looks cool, but I think if I can get away with nesting that may be easier this time.

#### edilsonsantos

Hi Howard.
Regarding the programming loop to play in the AFL language a stop loss (10% below the buyprice), scaling out (10% above the buyprice), repositioning the stop to the buyprice and continue as a trailing stop at the low of previous bar.
Yesterday I tried to use the code of the Fig 7.10 (book QTS). I didn't have success. I'm trying to understand how AFL works with array and single number in loop. Some material on this subject, beyond the basic help in the AB (User Guide) and "Looping in AmiBroker AFL", June 2007, will be welcome.

Thanks.
Edilson

#### howardbandy

Hi Edilson --

The reference material, including that you have already read, is pretty good. I would not know where to begin a general discussion of looping in a forum posting -- the topic is broad, would require several long postings, and even then might not address the areas you need help understanding.

Do you have a specific question?

Thanks,
Howard

#### edilsonsantos

Hi Howard.
My current phase is to acquire basic knowledge in the broad sense. Specifically, I am trying to understand the use of arrays and single number in the loop. Eg., into the code in Figure 7.10 you used "PriceAtBuy" to store "BuyPrice" which was set at the beginning of the code as "C". From what I understand "BuyPrice" is a reserved word of the AFL language and could not be used inside the loop to store a single number. Hence the need to create a new variable ("PriceAtBuy"). Is that correct?

Thanks.
Edilson

#### Synergy

I seem to be getting caught up on the simplest things!

I have a running stop that should not get smaller. For some reason, mine does get smaller.
Here's the line causing me grief:

All I'm trying to do is take the maximum of a range of 'stop choices' and the previous runningstop, but alas, I fail.

#### Synergy

Seems I've had a bit of an understanding fail.

appears I need to use looping to get my stops to work.
I had assumed that 'buyprice' was a fixed number once a buy was taken, and it could simply be refered to as buyprice from then on in, but not the case.

Much to learn it seems.

#### edilsonsantos

Hi all,
In order to better understand the functioning of the loop in Amibroker, I decided to work towards a single system. The exit (where the loop will be developed) is my main focus.
When opening an operation (long), I place a protective stop (lower limit of the Bollinger Bands) and a profit objective (upper limit of the Bollinger Bands). In the Backtest this code generates exit always in the next bar of the entry occurred (Buy). I can not understand? Someone could help me clarify. What could be wrong? (The code is below).

///////////////////////////////////////////////////////////////////////////////
SetTradeDelays( 0, 0, 0, 0 );
SellPrice = C;
Sell = 0;

MA20 = MA( C, 20 );
MA200 = MA( C, 200 );

Cond = C > MA200;
Buysign = Cross( C, MA20 );

//Variables for loop

StopLoss = 0;
ProfitTarget = 0;
LSBB = BBandTop( C, 21, 2 );
LIBB = BBandBot( C, 21, 2 );

//Loop

for ( i = 0; i < BarCount; i++ )
{
{
StopLoss = LIBB;//simple number (no array)
ProfitTarget = LSBB;//simple number (no array)
}

if ( PriceAtBuy > 0 AND L < StopLoss )
{
Sell = 1;
StopLoss = 0;
ProfitTarget = 0;
}

if ( PriceAtBuy > 0 AND H > ProfitTarget )
{
Sell = 1;
StopLoss = 0;
ProfitTarget = 0;
}
}

#### captain black

In the line above, BuyPrice is an array not a simple number so it changes with each bar.

#### edilsonsantos

In the line above, BuyPrice is an array not a simple number so it changes with each bar.
I think that no, captain black. The command "if" doesn't accept array.
The main problem with this code has been mentioned in previous post. It works, but not the way that I want. It is the question.

Thanks.
Edilson

#### captain black

I think that no, captain black. The command "if" doesn't accept array.
The main problem with this code has been mentioned in previous post. It works, but not the way that I want. It is the question.

Thanks.
Edilson
It takes an exit the next bar after the buy because the variable "BuyPrice" is a reserved variable which is an array. You assign "Close" to the BuyPrice at the beginning of the formula outside the loop so on every iteration of the loop the variable "PriceatBuy" is assigned the close price of that particular bar. That's why your exits are triggered immediately after your entry.

#### Caveroute

It takes an exit the next bar after the buy because the variable "BuyPrice" is a reserved variable which is an array. You assign "Close" to the BuyPrice at the beginning of the formula outside the loop so on every iteration of the loop the variable "PriceatBuy" is assigned the close price of that particular bar. That's why your exits are triggered immediately after your entry.
Do we mean:

That sets price_at_buy to the close of the day the buy signal was generated, assuming no delays are configured.

#### boofis

Would anyone be up for trying to help me get up and running with amibroker? I'm just trying to import free data and can't even get that to work. Have read through but keep getting error messages, pm if available, that'd be great!