Regarding mapcreate, using 6.9, I read documentation and searched further. I learn best from others, and would appreciate if anyone could post some more complete examples of how they used mapcreate in their code.
I was initially planning to use it to map a custom sector coding to a new variable, to facilitate a x2 sort allowing creation of sector averages of stock movements (addtocomposite or statvarset). However, I got bogged down trying to it right. I can post my poor coding if useful.
OK, perhaps it will spur conversations if I post some non-working code. This was a 1st step, attempting to validate how the mapcreate could work, with the idea to then use the new variable as input into the x2 loop of symbol & industry name for averages by industry (It has error in an If test). I predict that are other better ways to go about this :), but I am much better at research/analysis than coding.
// Define the watchlist name
watchlistName = "MF";
// Retrieve the index of the specified watchlist
watchlistIndex = CategoryFind(watchlistName, categoryWatchlist);
// Retrieve a comma-separated list of symbols in the watchlist
symbolList = CategoryGetSymbols(categoryWatchlist, watchlistIndex);
// Initialize a Map to group symbols by industry
industryMap = MapCreate();
if (Status("action") == actionScan)
{
// Loop through each symbol in the watchlist
for (i = 0; (symbol = StrExtract(symbolList, i)) != ""; i++)
{
// Set the current symbol context
SetForeign(symbol);
// Retrieve the industry name associated with the symbol
industryName = IndustryID(1); // Parameter 1 returns the industry name
// Restore the previous symbol context
RestorePriceArrays();
// Check if the industry already exists in the Map
existingSymbols = industryMap[industryName];
// If the industry exists, append the symbol; otherwise, create a new entry
if (IsNull(existingSymbols))
{
industryMap[industryName] = symbol;
}
else
{
industryMap[industryName] = existingSymbols + "," + symbol;
}
}
}
// Example: Access symbols in a specific industry
specificIndustry = "Technology";
symbolsInIndustry = industryMap[specificIndustry];
if (!IsNull(symbolsInIndustry))
{
printf("Symbols in the %s industry: %s", specificIndustry, symbolsInIndustry);
}
else
{
printf("No symbols found in the %s industry.", specificIndustry);
}
I don't see any reason to use map to somehow duplicate existing functionality of categories. Or duplicating anything that already exists. FWIW: I see lots of codes that abuse SetForeign inside loops for no reason. I could not stress it enough that SetForeign in a loop should be avoided. This is way too heavy function to be used that way. SetForeign does not set the context. It performs resource hungry data request to main thread and fills all OHLCV arrays with tons of data. Don't use it lightly for not good reason.
Ok, get that.
Back to original question Tomasz (or anyone) - any example of an application/solution that Mapcreate provides (I did review the user guide); just wanted to think about it more deeply.
Thanks
Maps are syntactic sugar. They can be used to make code prettier/shorter/more readable. For example if you have some parameters that depend on symbol you can use switch, you can use if-else, or you can use map to do that. Maps can also be thought as equivalent of objects without methods (something like struct in C/C++) so they can encapsulate multiple properties in single variable. Using a map as return value for function allows to return many things at once.
Encapsulate multiple properties - nice. Could I bug you for a couple of lines of code that would illustrate that?
Thanks so much - this is very helpful.
something like this, another user wanted more SetOptions()
Store the MAP in a file until it is supported as StaticVariable in the future
/* include in an afl file, OR store Map Keys as a static variable(when avaialble) */
mapOptions = Map();
// Define your Options here, and many more...
// 0=LONG, 1=SHORT, 2=LONGSHORT
mapOptions["LONGSHORT"] = 2;
mapOptions["Setting1" ] = 0;
mapOptions["Setting2"] = 1;
// more settings
//// When static variable supports Map()
//StaticVarSet( "MyOption", mapOptions, True);
Then, you can have Options that are global or per project basis depending on paramter you pass.
Set like this MySetOption( "LONGONLY", 1 "global");
mapOptions = StaticVarGet( "MyOption" );
function MySetOption( field, value, projectname )
{
global mapOptions;
if( typeof( field ) != "string" )
{
Error("Not valid KEY string");
result = NULL;
}
else
{
optInt = mapOptions[ field ];
if (IsNull( optInt ))
{
Error("KEY not found in Options");
result = NULL;
}
else
{
optionString = "MyOption"+ projectname + field;
StaticVarSet( optionString, value, True);
result = 1;
}
}
return result;
}
use like this opt1 = MyGetOption( "LONGONLY", "global");
function MyGetOption( field, projectname )
{
global mapOptions;
if( typeof( field ) != "string" )
{
Error("Not valid KEY string");
value = NULL;
}
else
{
optInt = mapOptions[ field ];
if (IsNull( optInt ))
{
Error("KEY not found in Options");
value = NULL;
}
else
{
optionString = "MyOption" + projectname + field;
value = StaticVarGet( optionString );
}
}
return value;
}