Array and looping questions

#1
New member here, 1st time post. Found this site via google search, am from taiwan, trading US options/futures. New to Amibroker.
yahoo amibroker forum seems very 'restricted' to new members. I found this site much more friendly to new comers.

I am fairly good with programming and coming from Ninja, AFL's arry style is new to me. Need to twist my brain sometimes to get it working.

My question is:

Is there anything can't be done with pure array operation without looping?
(at least that was so claimed in some of the articles in yahoo amibroker forum)

A simple counter example is:
assume I am at bar i, the goal is to find the 1st previous (or to the left) bar j, where bar j's value is lower than bar i.

Obviously using things like

ValueWhen( L<=L, BarIndex(), 1) does not make sense at all, and there's no other way except looping.

So my point is, pure AFL style array operation fails in cases where you need to reference multiple array elements in the SAME array, and the relative positions of the two elements is unknown. i.e., you can't use Ref().

Can any AFL seniors elighten me if my point is not correct?


A follow up question then is, is there any 'conflict' of loop code in backtest/scan?

All the sample codes I've seen so far, loops only exist in indicators but not in scan/backtest-able code. This is one area that's still muddy to me,

so the general question is, what's the difference between indicator code, and scan/backtest-able code.


Since my system is primarily pattern based (vs. the quantitative based like MACD/RSI cross over etc), these questions will be important for me while evaluating if Amibroker is suitable as an auto-trading platform.


Much thanks!
 

asnavale

Well-Known Member
#2
New member here, 1st time post. Found this site via google search, am from taiwan, trading US options/futures. New to Amibroker.
yahoo amibroker forum seems very 'restricted' to new members. I found this site much more friendly to new comers.

I am fairly good with programming and coming from Ninja, AFL's arry style is new to me. Need to twist my brain sometimes to get it working.

My question is:

Is there anything can't be done with pure array operation without looping?
(at least that was so claimed in some of the articles in yahoo amibroker forum)

A simple counter example is:
assume I am at bar i, the goal is to find the 1st previous (or to the left) bar j, where bar j's value is lower than bar i.

Obviously using things like

ValueWhen( L<=L, BarIndex(), 1) does not make sense at all, and there's no other way except looping.

So my point is, pure AFL style array operation fails in cases where you need to reference multiple array elements in the SAME array, and the relative positions of the two elements is unknown. i.e., you can't use Ref().

Can any AFL seniors elighten me if my point is not correct?


A follow up question then is, is there any 'conflict' of loop code in backtest/scan?

All the sample codes I've seen so far, loops only exist in indicators but not in scan/backtest-able code. This is one area that's still muddy to me,

so the general question is, what's the difference between indicator code, and scan/backtest-able code.


Since my system is primarily pattern based (vs. the quantitative based like MACD/RSI cross over etc), these questions will be important for me while evaluating if Amibroker is suitable as an auto-trading platform.


Much thanks!

Hi Balance,

Yes, looping is necessary. But it depends on the criterion you are applying. When you are looking for a particular bar, then looping is required. If the condition you use is having a fixed reference to array members then it is not necessary to loop. Obviously the condition like ValueWhen(L<=L, BarIndex(), 1) does not help because the value of the bar is referencing itself. L is array of Low price and L <= L references itself. Whereas, if you are trying to compare , say, fifth previous bar Low to current bar then you can use Ref(L, -5) without looping. This is because the reference is fixed, that is the refrence is always the previous fifth bar. But if you are looking for a bar with a particular condition, the position of the bar which satisfies the condition is unknown. Let me give an example to clarify this:

Suppose I want to find a bar whose High is higher than the current bar. If on Dec 1, High is 10.5 and the High of all the previous three bars are less than 10.5 and the fourth previous bar is 10.6, then I am looking at the bar on Nov 27. But, If the same condition is applied on Nov 27, the condition may be satisfied by the previous day's bar itself, that on Nov 26th. That means the distance between the bars is not constant. In such cases looping has to be carried out.

As far as scan and indicator are concerned. There should not be a conflict. The calculations required to evaluate the indicator value are performed on each bar. Similarly the calculations for scanning also are carried out on each bar. So, there should not be any conflict. As you may be knowing, for scan, the BUY/SELL should be defined but for Indicator it is not necessary unless you want to display the BUY/SELL arrows on the chart.

-Anant
 
#3
Thanks for the reply, the same as my thoughts too.
I've asked the 'experts' on yahoo group and found the same answer.
I.e., AFL's array only operation is not a complete language, in the sense that, there're things can not be done with just array operation. My case is one of them.

On the 2nd part of my question, my original question is from my puzzle of, if I need to loop thru all bars in my code, what happen when the code is run as scan or backtest in the AA windows?

Below I've found the answer, and I think it is critical to AB developers to understand so I am posting here to give positive feedback to this community.

http://www.mail-archive.com/[email protected]/msg50281.html

In essence, AB is a 'run once' backtest platform, i.e., it will run your code for ALL elements in the array, or thru all the bars in the backtest range, have everything ready, then loop thru each bar just to compute the 'trade' part to get the backtest P/L result. This is why they emphasize so much of not to use codes with 'look into the future'.
The look into the future problem does not exists for systems with bar-by-bar style backtest engine.
This also explains why AB is so fast in their backtest performance.

With this in mind, my question is answered, i.e., my looping code will only be run once for all bars, even in the backtest/scan environments.
 

Similar threads