@fourier, unfortunately, I do not have a solution for you.
Anyway, today I reworked a little the exploration since I think it is still useful to understand how some things work in Amibroker
Actually, my code using the BarCount seems to work properly only in exploration mode.
I found this note of TJ in an old post in the AmiBrokerYahooGroup (message #114917)
BarCount does not represent the number of bars in the DATABASE,
but in the ARRAYs in the current execution.
The arrays have DYNAMIC size depending on zoom factor. That’s why BarCount may and will vary.
And it DOES NOT change when you call the SetForeign function so I had to discard its use (see the commented out code).
So, using my code, in order to set properly the “marketTradedDaysYTD” number you are forced to include the composite/market ticker in the list of the symbols used for the exploration (in my sample code it is hard-coded as “SPY”). It is actually set - with a specific conditional if - only when the exploration will reach/process this ticker. Far from an optimal solution…
To show its value, I simply use a sort at the end of the exploration to bring the only line with a value to the top; all the other tickers will have an empty column.
So, in the end, my sample exploration is only providing some “stats” about the current database tickers.
For instance, it could be used to see recent IPOs, not recently traded tickers (delisted?), infrequently traded names (with missing bars) and the long-standing ones.
// This is a code sample to be used as an exploration:
// Select "1 recent bar" and apply to "All Symbols" in Daily Interval
// Use it to see what tickers were recently introduced in the database (IPOs)
// and/or are no longer trading and to see the long-standing ones
Version(6.27);
SetOption("RequireDeclarations", True);
SetBarsRequired( sbrAll, sbrAll );
// Pass parameters by reference
procedure tradedDaysStats(tradedDays, tradedDaysYTD, IPO, firstTradedDate, lastTradedDate) {
local firstYear, lastYear, years, dt;
tradedDays = BarCount;
years = Year();
firstYear = years[0];
lastYear = years[BarCount - 1];
IPO = False;
dt = DateTime();
firstTradedDate = dt[0];
lastTradedDate = dt[BarCount-1];
if ((lastYear == firstYear ) AND( lastYear == Now( 8 ))) {
IPO = True;
tradedDaysYTD = tradedDays;
} else {
// Counter start at zero - we need to add 1
tradedDaysYTD = 1 + LastValue( BarsSince( Year() != Ref( Year(), -1 ) ) );
}
}
local IPO, marketTradedDaysYTD, firstTradedDate, lastTradedDate, tradedDays, tradedDaysYTD;
local chartTicker, isSpecialTicker;
global Filter;
IPO = False;
marketTradedDaysYTD = Null;
firstTradedDate = Null;
lastTradedDate = Null;
tradedDays = Null; // Total days in the database
tradedDaysYTD = Null;
if (Name() == "SPY") { // set it for your composite
tradedDaysStats(&tradedDays, &marketTradedDaysYTD, &IPO, &firstTradedDate, &lastTradeDate);
}
/* THIS DOES NOT WORK.....
// Cannot call it since BarCount will be the one of the currently processed ticker
SetForeign("SPY");
tradedDaysStats(&tradedDays, &marketTradedDaysYTD, &IPO, &firstTradedDate, &lastTradeDate);
RestorePriceArrays();
*/
// Explore sample
tradedDaysStats(&tradedDays, &tradedDaysYTD, &IPO, &firstTradedDate, &lastTradedDate);
chartTicker = StrLeft(Name(), 1); // Skip index some tickers / composites, etc.
isSpecialTicker = StrFind("~^", chartTicker);
Filter = 1 AND !(isSpecialticker);
AddTextColumn( StrFormat( "%3.0f", tradedDaysYTD), "Traded YTD days");
if (IsNull(marketTradedDaysYTD))
AddTextColumn( "", "Market YTD days" );
else
AddTextColumn( StrFormat( "%3.0f", marketTradedDaysYTD), "Market YTD days" );
AddTextColumn( StrFormat( "%1.0f", IPO), "IPO" );
AddTextColumn( StrFormat( "%5.0f", tradedDays), "Traded days" );
AddTextColumn( DateTimeToStr(firstTradedDate, 4), "First trade Date");
AddTextColumn( DateTimeToStr(lastTradedDate, 4), "Last trade Date");
SetSortColumns(-4, -5, 3); // order market YTD, then IPOs, then least traded
Note that in my user-definede procedure named “tradedDaysStats()” I use the pass by reference variables so it requires a recent version of AmiBroker.
I wish some true expert will join us to give you the proper solution and/or to further clarify how to properly accomplish what you want.