Help with "For" loop and different timeframes

#1
Friends, I made a system using Donchian Channel. The start of the code is simply the Donchian Formula:


periods = Optimize("Periods", 20, 0, 45, 5);
DHigh = HHV(Ref(H,-1),periods);
DLow = LLV(Ref(L,-1),periods);
DMiddle = (DHigh+DLow)/2;

After this, I begin my for loop code, and all the signals (buy, sell, stoploss, profit, breakeven) is made via this for loop.

My database is in 1m. When I backtest in 1m (settings > periodicity), it works just fine, the exactly way it should. But when I set a different time frame to my backtest, it returns messed results.

What am I doing wrong?

Thanks, friends! :)
 

trash

Well-Known Member
#2
So you expect readers to be psychics?
What loop, example to reproduce? Where do you set timeframe (periodicity or timeframe functions)?

If using loops you should take care of quickAFL
http://www.amibroker.com/kb/2008/07/03/quickafl/

and if you use timeframe functions you need to take care to skip null values

There are lots of things that you need to read from the manual. Specifically

http://www.amibroker.com/guide/h_timeframe.html
How does it work internally ?

Time-frame functions do not change the BarCount - they just squeeze the arrays so you have first N-bars filled with NULL values and then - last part of the
array contains the actual time-compressed values. This is why it is essential to expand the data back to the original frame with TimeFrameExpand. The following
simple exploration shows what happens after you switch to a higher timeframe. Run Exploration on current symbol, all quotations, periodicity set to daily and
you will see how "weekly close compressed" column contains empty values at the beginning and weekly compressed data at the end of array.

So, your loop does not create correct values because it does not take into account the fact that when you use TimeFrame functions
then INITIAL bars are filled with Nulls. You need to skip those Nulls in the loop

Your loop should start from NON-NULL value, ....



....

Secondly, if you use the code in the indicator and if you use loops then you need to take care about QuickAFL,
because otherwise, you will not get enough data to produce hourly bars because there are too few bars.

Please read this article from the Knowledge Base:
http://www.amibroker.com/kb/2008/07/03/quickafl/
that covers what is QuickAFL, how it works, and how to control it.

Best regards,
Tomasz Janeczko
amibroker.com

Hello,

Thank you very much for your e-mail.

Well, you can take custom indicators "as is" into different timeframes in majority of cases because all AmiBroker array functions
are aware of the fact that Nulls may (and usually do) appear at the initial bars and skip those Nulls automatically.
So as long as you are using array functions, you don't need to worry.

But.. when you are coding low-level loop and processing every bar on your own you need to take care of Null values by yourself.

Also array functions are aware of quickafl and request extra bars they need so again no worries if you use array functions
to construct custom indicators.

And again, if you are going low-level and using loop to process bar by bar, you need to use SetBarsRequired to tell quickAFL to give you more bars as explained
in the article I pointed out in my earlier response.

Best regards,
Tomasz Janeczko
amibroker.com
 
Last edited:

colion

Active Member
#3
Friends, I made a system using Donchian Channel. The start of the code is simply the Donchian Formula:


periods = Optimize("Periods", 20, 0, 45, 5);
DHigh = HHV(Ref(H,-1),periods);
DLow = LLV(Ref(L,-1),periods);
DMiddle = (DHigh+DLow)/2;

After this, I begin my for loop code, and all the signals (buy, sell, stoploss, profit, breakeven) is made via this for loop.

My database is in 1m. When I backtest in 1m (settings > periodicity), it works just fine, the exactly way it should. But when I set a different time frame to my backtest, it returns messed results.

What am I doing wrong?

Thanks, friends! :)
Why are you using a loop? From what you have said it is not clear that you need one.
 

Similar threads