Use StaticVarGetText if you want string, Exploration using Static Variables

I'm sure I am doing everything wrong here but I can't seem to figure it out. I am trying to set up an exploration of data that I have brought in via static variables. This data is working just fine to output into my Interpretation Window but I can't get it to populate the Exploration and am getting errors that I can't seem to work around. I have tries AddTextColumn and AddColumn, and I have tried with and without the StrReplace lines, and other configurations to try and get this data called into Exploration. At this point I am happy to just get it to display without any type of sorting or filtering. I can figure that out later. I am able to get the headers but no data. Can anyone point me in the right direction? I am sure Im missing something simple.

_SECTION_BEGIN ("Weighted Alpha w/ Earnings Surprise");

_Sector = StaticVarGet(Name() + "_Sector");
_WeightedAlpha = StaticVarGet(Name() + "_WeightedAlpha");
_LQESurprise = StaticVarGet(Name() + "_LQESurprise");
_LQESurprise = StrReplace(_LQESurprise, "%", "%%");
_SecondQESurprise = StaticVarGet(Name() + "_SecondQESurprise");
_SecondQESurprise = StrReplace(_SecondQESurprise, "%", "%%");
_ThirdQESurprise = StaticVarGet(Name() + "_ThirdQESurprise");
_ThirdQESurprise = StrReplace(_ThirdQESurprise, "%", "%%");
_FourthQESurprise = StaticVarGet(Name() + "_FourthQESurprise");
_FourthQESurprise = StrReplace(_FourthQESurprise, "%", "%%");
_52WkAveVol  = StaticVarGet(Name() + "_52WkAveVol");
_DailyPercentPriceChange = StaticVarGet(Name() + "_DailyPercentPriceChange");
_DailyPercentPriceChange = StrReplace(_DailyPercentPriceChange, "%", "%%");
_PercentVolume = StaticVarGet(Name() + "_PercentVolume");
_PercentInsider = StaticVarGet(Name() 	+ "_PercentInsider");
_PercentInsider = StrReplace(_PercentInsider, "%", "%%");
_PercentInstitutional = StaticVarGet(Name() + "_PercentInstitutional");
_PercentInstitutional = StrReplace(_PercentInstitutional, "%", "%%");
_Float = StaticVarGet(Name() + "_Float");
_Float = StrReplace(_Float, "%", "%%");
_LastEarningsDate = StaticVarGet(Name() + "_LastEarningsDate");
_NextEarningsDate = StaticVarGet(Name() + "_NextEarningsDate");
 
 
AddColumn(_Sector, "Sector", 1);
AddColumn(_WeightedAlpha, "Weighted Alpha", 1.2);
AddColumn(_LQESurprise, "Last Qtr Surprise", 1.2);
AddColumn(_SecondQESurprise, "2nd Qtr Surprise", 1.2);
AddColumn(_ThirdQESurprise, "3rd Qtr Surprise", 1.2);
AddColumn(_FourthQESurprise, "4th Qtr Surprise", 1.2);
 

_SECTION_END();

Screen Shot 2020-07-15 at 11.19.53 PM

Is the stored static var really a string? Or is it number?

But that being said. It might be that static var is empty. StaticVarGet would work with string too if string was stored via StaticVarSetText. But if it is empty (variable not available) then it will return number. And as such StrReplace would show error message.

Also if the values where stored via StaticVarSet() then they are not string originally otherwise StaticVarSet would complain about incorrect var type. So have you stored via StaticVarSet or StaticVarSetText?

So if stored via StaticVarSetText then instead use StaticVarGetText (if it was stored as string) to call variable.
Also in case of string use AddTextColumn.

BTW, before storage (and before call of var) you can check type of variable via typeof oprator.

x = "1234";
printf("type of x %s\n",typeof(x));

y = 1234;
printf("typeof y %s\n", typeof(y));
printf("y: %g", y);

Checking for type in if statements e.g.

if ( typeof(x) == "string" ) {
    // do something
}
1 Like

If you expect a string and want to use static variable as STRING (for StrReplace) you should just use StaticVarGetText(), not StaticVarGet(). StaticVarGetText() is guaranteed to work because it will return empty STRING in case static variable does not exist. For this reason type checking is not needed if you use StaticVarGetText()

OK so I did modify the Exploration to use StaticVarGetText() and used the AddtextColumn. This did allow me to run the exploration but still did not populate anything but the column headers. So a step in the right direction but still not there yet. Below is the table scrape I use to bring in the data and it does populate parts my Interpretation Window successfully. @fxshrat I havent tried the code you offered to check type because I am not exactly sure where it goes or what I am looking for. Sorry for not quite understanding.


//Author: RioL
//Original Date: 2020/07/11 (YYYY/MM/DD)
//Revision 00.001.200711
/////////////////////////////////////////

//Set Variables
path 		= "C:\\Program Files\\AmiBroker\\IBDeTables";
filename	= "WeightedAlpha.csv";

//open the file
filepath	= path + "\\" + filename;
fh = fopen(filepath, "r" );


{
// Prepare Watchlist by clearing it
BarchartList = CategoryFind("Weighted_Alpha_Ranked", categoryWatchlist);
// retrive comma-separated list of symbols in watch list
MLlist = CategoryGetSymbols( categoryWatchlist, BarchartList );
//iterate through watchlist members and remove
for( i = 0; ( sym = StrExtract( MLlist, i ) ) != ""; i++ )
{
	CategoryRemoveSymbol( sym, categoryWatchlist, BarchartList );
}
}

// prep file position for loop by jumping over the header (copied from @rocketpower)
for(i = 0; i < 1; i++)
	CL = fgets( fh );

// loop through list
if( fh ) 
{ 
   while( ! feof( fh ) ) 
   { 
      line = fgets( fh ); // read a line of text
      _TRACE( line ); 
      _Ticker 	= StrExtract(line, 0);
      _Sector	= StrExtract(Line, 1);
      _WeightedAlpha 	= StrExtract(line, 2);
      _LQESurprise 	= StrExtract(line, 3);
      _SecondQESurprise 	= StrExtract(line, 4);
      _ThirdQESurprise 	= StrExtract(line, 5);
      _FourthQESurprise 	= StrExtract(line, 6);
      _52WkAveVol	= StrExtract(Line, 7);
      _DailyPercentPriceChange = StrExtract(Line, 8);
      _PercentVolume = StrExtract(Line, 9);
      _PercentInsider = StrExtract(Line, 10);
      _PercentInstitutional = StrExtract(Line, 11);
      _Float = StrExtract(Line, 12);
      _LastEarningsDate = StrExtract(Line, 13);
      _NextEarningsDate = StrExtract(Line, 14);
      
      
      StaticVarSetText( _Ticker + "_Ticker",		 _Ticker,		True);
      StaticVarSetText( _Ticker + "_Sector",		 _Sector,		True);
      StaticVarSetText( _Ticker + "_WeightedAlpha",	 _WeightedAlpha,	True);
      StaticVarSetText( _Ticker + "_LQESurprise",	 _LQESurprise,	True);
      StaticVarSetText( _Ticker + "_SecondQESurprise",	 _SecondQESurprise,	True);
      StaticVarSetText( _Ticker + "_ThirdQESurprise",	 _ThirdQESurprise,	True);	
      StaticVarSetText( _Ticker + "_FourthQESurprise",	 _FourthQESurprise,	True);
      StaticVarSetText( _Ticker + "_52WkAveVol",	 _52WkAveVol,	True);
      StaticVarSetText( _Ticker + "_DailyPercentPriceChange",	 _DailyPercentPriceChange,	True);
      StaticVarSetText( _Ticker + "_PercentVolume",	 _PercentVolume,	True);
      StaticVarSetText( _Ticker + "_PercentInsider",	 _PercentInsider,	True);
      StaticVarSetText( _Ticker + "_PercentInstitutional",	 _PercentInstitutional,	True);
      StaticVarSetText( _Ticker + "_Float",	 _Float,	True);
      StaticVarSetText( _Ticker + "_LastEarningsDate",	 _LastEarningsDate,	True);
      StaticVarSetText( _Ticker + "_NextEarningsDate",	 _NextEarningsDate,	True);
      
      
      
      
      // Add ticker to watchlist
CategoryAddSymbol(_Ticker, categoryWatchlist, BarchartList);

   }

	fclose( fh ); 
} 
else 
{ 
   Error("ERROR: file can not be open"); 
}

This is the most recent change I tried and again while it does fill the column headers and allows for the Exploration to run, it does not populate the Exploration. Again see above for the actual scrape of data from the .csv file that does work for the Interpretation Window.( I might also add that in the code for calling data into the Interpretation Window, it uses StaticVarget(Name() in it.)

_SECTION_BEGIN ("Weighted Alpha w/ Earnings Surprise");

_Sector = StaticVarGetText("_Sector");
_WeightedAlpha = StaticVarGetText("_WeightedAlpha");
_LQESurprise = StaticVarGetText("_LQESurprise");
_SecondQESurprise = StaticVarGetText("_SecondQESurprise");
_ThirdQESurprise = StaticVarGetText("_ThirdQESurprise");
_FourthQESurprise = StaticVarGetText("_FourthQESurprise");
_52WkAveVol  = StaticVarGetText("_52WkAveVol");
_DailyPercentPriceChange = StaticVarGetText("_DailyPercentPriceChange");
_PercentVolume = StaticVarGetText("_PercentVolume");
_PercentInsider = StaticVarGetText("_PercentInsider");
_PercentInstitutional = StaticVarGetText("_PercentInstitutional");
_Float = StaticVarGetText("_Float");
_LastEarningsDate = StaticVarGetText("_LastEarningsDate");
_NextEarningsDate = StaticVarGetText("_NextEarningsDate");
 
 
AddtextColumn(_Sector, "Sector", 1);
AddtextColumn(_WeightedAlpha, "Weighted Alpha", 1.2);
AddtextColumn(_LQESurprise, "Last Qtr Surprise", 1.2);
AddtextColumn(_SecondQESurprise, "2nd Qtr Surprise", 1.2);
AddtextColumn(_ThirdQESurprise, "3rd Qtr Surprise", 1.2);
AddtextColumn(_FourthQESurprise, "4th Qtr Surprise", 1.2);
 

_SECTION_END();

It does not populate with any meaningful text because your static variable is EMPTY (not set). This can be because static variable name that you are attempting to read is different than the one you used when writing data. Check your static variable names. Chances are that you have a SPACE (trailing/leading) somewhere.
Static variables created with StaticVarSet can have spaces in identiifers, and the variable name "ABC xyz" is different from "ABCxyz" .

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

When I use the DebugView program to view the scrape, it appears to me that the static vars is being filled (unless I dont really know what I am looking at which is quite possible). I have attached a pic of that below. I have checked all of the names as you have suggested and dont find any issues there either. I know I must be missing something. I will say also that I tried running the code for the exploration, and could not get it to view in the DebugView program which seems odd and may point to that as the problem. I can say that right now, in the scrape I have used StaticVarSetText() and in the Exploration I have used StaticVarGetText() and AddTextColumn().

Screen Shot 2020-07-17 at 10.51.26 PM

I already wrote you what the problem is:

static variable name that you are attempting to read is different than the one you used when writing data

That is precisely what is happening and you should use that advice.

The screenshot just shows (what I guess from what I see) the content of the text file you are reading. It does not show what static variables are created.

Guessing from the code that you included earlier, it seems that you are writing static variables that have SYMBOL APPENDED at front of variable name. Such as
like "AAPL_WeightedAlpha" but when you are READING you are NOT prepending symbol. You are just doing this:

_WeightedAlpha = StaticVarGetText("_WeightedAlpha");

So are reading variable named "_WeightedAlpha" which is NOT the same as "AAPL_WeightedAlpha". Since you are reading non-existing variable "_WeightedAlpha" you get empty string.

That is just what I wrote you

static variable name that you are attempting to read is different than the one you used when writing data

You would need to be doing this:

// PREPEND SYMBOL as you do when writing !!!!
_WeightedAlpha = StaticVarGetText( Name() + "_WeightedAlpha");

to get proper symbol name. That is why I told you to run UNDER DEBUGGER.

If you followed steps that were advised earlier you would understand that you are calling functions with different (incorrect) parameters.

it is crucial to ALWAYS follow debugging steps explained here How do I debug my formula?
because you really MUST understand what is happening in YOUR OWN code.

Also, it is recommended to use proper types. If you have numbers you should store them as numbers not as text. If you read from text file you can convert strings to numbers (StrToNum)

  1. If you are storing NUMBERS / ARRAYS you should be using StaticVarSet/StaticVarGet
  2. If you are storing TEXT/STRING you should be using StaticVarSetText/StaticVarGetText

Your original question was about StrReplace because you tried to READ the static variable as NUMBER and passed the result to StrReplace that expects STRING. That is why you got error message. So I told you:

If you expect a string and want to use static variable as STRING (for StrReplace) you should just use StaticVarGetText() , not StaticVarGet()

The IF word is crucial here. Only IF you store/read STRING you should use StaticVarSetText/StaticVarGetText

1 Like

Thanks for your help yet again @Tomasz. I am still working on this and am not finding the issue no matter how many times I read your advice. I did make the usggested change (I beleive) and still not getting the right info into the Exploration. I'll keep working on it though. Thanks again. If I solve it I'll let you know but until then, I'll keep studying.

//_SECTION_BEGIN ("Weighted Alpha w/ Earnings Surprise");

_Ticker = StaticVarGetText( Name() + "_Ticker");
_Sector = StaticVarGetText( Name() + "_Sector");
_WeightedAlpha = StaticVarGetText( Name() + "_WeightedAlpha");
_LQESurprise = StaticVarGetText( Name() + "_LQESurprise");
_SecondQESurprise = StaticVarGetText( Name() + "_SecondQESurprise");
_ThirdQESurprise = StaticVarGetText( Name() + "_ThirdQESurprise");
_FourthQESurprise = StaticVarGetText( Name() + "_FourthQESurprise");
_52WkAveVol  = StaticVarGetText( Name() + "_52WkAveVol");
_DailyPercentPriceChange = StaticVarGetText( Name() + "_DailyPercentPriceChange");
_PercentVolume = StaticVarGetText( Name() + "_PercentVolume");
_PercentInsider = StaticVarGetText( Name() + "_PercentInsider");
_PercentInstitutional = StaticVarGetText( Name() + "_PercentInstitutional");
_Float = StaticVarGetText( Name() + "_Float");
_LastEarningsDate = StaticVarGetText( Name() + "_LastEarningsDate");
_NextEarningsDate = StaticVarGetText( Name() + "_NextEarningsDate");
 

AddTextColumn(_Sector, "Sector", 1.2);
AddTextColumn(_WeightedAlpha, "Weighted Alpha", 1.2);
AddTextColumn(_LQESurprise, "Last Qtr Surprise", 1.2);
AddTextColumn(_SecondQESurprise, "2nd Qtr Surprise", 1.2);
AddTextColumn(_ThirdQESurprise, "3rd Qtr Surprise", 1.2);
AddTextColumn(_FourthQESurprise, "4th Qtr Surprise", 1.2);
 

//_SECTION_END();

Hi Mark,

if the code you posted is the only code for the exploration, then it probably lacking a filter. Just read the manual regarding the exploration and recreate it for better understanding: https://www.amibroker.com/guide/h_exploration.html

Thanks @RioL ! That is what it was. Can't believe I missed something so simple. Good catch. That definitely gets me a step closer. Definitely have more work to do though now that I can actually see the output. Now I can see that there are more characters than just the numbers AND being stored as StaticVarText, Im not sure exactly how the exploration will be able to filter. I'll have to look into that. Thanks again for the help!

Screen Shot 2020-07-29 at 10.29.54 PM