In AmiBroker, you can retrieve and print the name of a watchlist using its number via AFL (AmiBroker Formula Language). Watchlists are managed under category 0 in AmiBroker's category system. The key function you'll use is CategoryGetName(), which retrieves the name of an item in a specified category.
Here's a step-by-step guide on how to do this:
Step 1: Understand the Function
CategoryGetName(category, item): This function returns the name of the item in the specified category.
For watchlists, the category is always 0.
The item parameter is the watchlist number (e.g., 5 for Watchlist 5).
Step 2: Write the AFL Code
You can use this in an Exploration formula to "print" (or display) the watchlist name. Explorations are a common way to output results in AmiBroker.
Here's a simple AFL code snippet:
afl
// Define the watchlist number you know
watchlistNum = 5; // Replace 5 with your actual watchlist number
// Get the name of the watchlist
watchlistName = CategoryGetName(0, watchlistNum);
// Set up a filter to run the exploration (this will include all symbols or bars)
Filter = 1; // This ensures the exploration runs and outputs results
// Add columns to display the results
AddColumn(watchlistNum, "Watchlist Number", 1.0); // 1.0 for integer format
AddColumn(watchlistName, "Watchlist Name");
Step 3: Run the Exploration
Open the AFL Formula Editor in AmiBroker (go to Tools > Send to Analysis or create a new formula file).
Paste the code into the editor.
Go to Analysis > Automatic Analysis (or press Ctrl+A).
In the Automatic Analysis window:
Select your formula in the "Formula" dropdown.
Choose "Exploration" as the type.
Set the "Range" to something simple, like "1 recent bar" or "All quotes."
Click "Explore."
The results will appear in the Exploration grid, showing the watchlist number and its name.
Example Output
If you have a watchlist numbered 5 named "My Favorites," the Exploration results might look like this:
Watchlist Number
Watchlist Name
5
My Favorites
Notes:
Error Handling: If the watchlist number doesn't exist, CategoryGetName() might return an empty string. You can add a check in your code if needed:
afl
if (StrLen(watchlistName) == 0)
{
watchlistName = "Watchlist not found";
}
AddColumn(watchlistName, "Watchlist Name");
Alternative Ways to Display:
In a Chart: If you want to display it on a chart, you can use the Title or GraphTitle functions. For example:
Apply this formula to a chart, and the title will show the name.
Using printf for Logging: If you're running a backtest or optimization, you can use printf to output to the log:
afl
watchlistNum = 5;
watchlistName = CategoryGetName(0, watchlistNum);
printf("The name of watchlist %g is %s\n", watchlistNum, watchlistName);
This will print to the AmiBroker log window when the formula is executed in a backtest or exploration.
Limitations: This method works well for explorations and charts but isn't meant for real-time console output like in some programming languages. AmiBroker's AFL is designed for analysis, so outputs are typically visualized in its interface.
If you encounter any issues, double-check that the watchlist exists in your AmiBroker setup (you can view them in the Symbols window under Watchlists). Let me know if you need further clarification!