Lookup appears different

image

When running a backtest I get different results from when I use the same code when clicking on a chart. The code I run is simply dragged onto the screen as a new indicator,
The part of the code is

_TRACE("========= buy ======");
                _TRACE("symbol " + tradeSymbol);
                buyDate = buyTrades[i][1];
                _TRACE("buyDate " + buyDate);
                _TRACE(" " + typeof(buyDate));
                _TRACE("buyDate " + DateTimeToStr( buyDate, mode = 3 ));
                barI = Lookup( BarIndex(), buyDate);
                _TRACE("barI " + barI);


                if( IsNull( barI ) OR (barI == 0))
                    _TRACE( tradeName + " - barI " + barI + " NOT found" );
                else

Result from clicking the screen is
image

When running the backtest it is
image

The problem appears to be with the lookup function as the bar index is not found when using the backtest.

The part of the code is contained in an included function that reads from an external CSV file. I don't think this is the problem because it is exactly the same code executed in both cases.

I haven't uploaded the entire code as I just want some tips to try and sort it out. If I am unable I will post the code later.

Thanks for any ideas.

Short Answer: Dont use Bar index as it can be arbitrary because of QuickAFL. Chart thread and Analysis thread may be working on different subset of data.

Long answer explained in the link here:
AmiBroker Knowledge Base » QuickAFL facts

If we are using a plugin for Data, the historical data length may "over time" vary so even when using "All bars", we cannot guarantee the same Bar index number for a bar. This is a possible scenario only when data length may change.

Imo, you can lookup directly with timestamp.

Great answer.
I added

SetBarsRequired( sbrAll, sbrAll )

And it now works. I will follow up on the use of timestamp (and re visit the quickAFL info) but for now, it is solved. As I am using EOD processing I am not too concerned with the quicjAFL but will look into it.

Thanks,
Gary.

1 Like

Requesting all bars always is inefficient. It is like turning heating all the way up and opening all windows in your home at the same time. Waste of energy.

Thanks Tomasz,
I understand that and will revise the code a bit later.

What is your take on
"Imo, you can lookup directly with timestamp".

Cheers.

It is included in the Users guide, AFL Function Reference - LOOKUP
there are examples how to use lookup with timestamp

Reread the QuickAFL article, SetBarsRequired can be used intelligently without requesting ALL bars. You can request LESS.

barI = Lookup( BarIndex(), buyDate);

 barI = Lookup( BarIndex(), buyDate, 2);

Thanks,
I have added the 3rd parameter to the lookup and removed the

SetBarsRequired( sbrAll, sbrAll )

and it seems to be the answer. I will continue the tests.

I really appreciate the feedback,
Gary.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.