A typical way of exporting quotation data involves using the explore function with filter = 1 in the amibroker code. If you used this approach with a portfolio, the saved file is a matrix with ticker symbol in the first column, Date in the second column and other following columns taking care of open, high....etc. All I need are closing prices (C). Is there some way of arranging the output so that the first column is named Date and the remain columns are named by the ticker symbols in the portfolio.
Yes, you can but you would have run exploration on symbol having all data since you have to run it on "Apply to" setting -> "Current" e.g. running exploration on an index such as SP500 being the selected symbol and the watchlist of symbols chosen in code.
/// Read here what the code is about and how to apply:
/// @link https://forum.amibroker.com/t/formatting-quotation-data-export-to-a-file/10509/2
/// sample EXPLORATION code by fxshrat@gmail.com
listnum = 0; // select Watchlist number
if ( GetOption( "ApplyTo" ) != 1 )
Error( "Set 'Apply to' to 'Current'!" );
cs = 0; // don't touch
tickerlist = CategoryGetSymbols(categoryWatchlist, listnum);
symcount = StrCount(tickerlist, ",")+1;
SetOption( "NoDefaultColumns", True );
AddColumn(DateTime(), "DateTime", formatDateTimeISO);
for ( i = 0; (ticker = StrExtract(tickerlist, i, ',')) != ""; i++ ) {
SetForeign(ticker, 0);
cs += IsNull(C);
AddColumn(C, Name());
// or e.g.
//AddColumn(IIf(IsNull(C), Ref(C,-1), C), Name());
RestorePriceArrays();
Filter = cs != symcount; // only output if at least one symbol has data
}
RestorePriceArrays and Filter can be moved outside of loop (I was writing/posting sample code too quickly).
/// Read here what the code is about and how to apply:
/// @link https://forum.amibroker.com/t/formatting-quotation-data-export-to-a-file/10509/2
/// sample EXPLORATION code by fxshrat@gmail.com
listnum = 0; // select Watchlist number
if ( GetOption( "ApplyTo" ) != 1 )
Error( "Set 'Apply to' to 'Current'!" );
cs = 0; // don't touch
tickerlist = CategoryGetSymbols(categoryWatchlist, listnum);
symcount = StrCount(tickerlist, ",")+1;
SetOption( "NoDefaultColumns", True );
AddColumn(DateTime(), "DateTime", formatDateTimeISO);
for ( i = 0; (ticker = StrExtract(tickerlist, i, ',')) != ""; i++ ) {
SetForeign(ticker, 0);
cs += IsNull(C);
AddColumn(C, Name());
// or e.g.
//AddColumn(IIf(IsNull(C), Ref(C,-1), C), Name());
}
RestorePriceArrays();
Filter = cs != symcount; // only output if at least one symbol has data
Many thanks for your elegant solution to the data export problem I described. For some applications it would be beneficial to have a workaround to the restriction of having to set the ApplyTo function to "current". What say you to the possibility of using the addtocomposite function to operate on a watchlist member and call that member with the ApplyTo setting as "Current"?