What am I doing wrong with StaticVarSet and StaticVarGet?

I was using Amibroker version 6.40.4 X64 until this morning and everything was working fine as of this morning.

This morning, I have upgraded Amibroker to version 6.90.1 X64.

I have a batch file in which I set static variable in the first script and get the static variable in the subsequent scripts so that only scripts that have data as of the date of index are returned.

if (Status("stocknum") == 0){
	if (GetDatabaseName() == "InEOD-DhanHQ"){
		StaticVarRemove("InEOD-DhanHQ-LTD");
		SetForeign("NIFTY 50");
		dt = DateNum();
		ltd = LastValue(dt);	
		//LTD stands for last trade date in DateNum format
		StaticVarSet("InEOD-DhanHQ-LTD", ltd, persist = True);
		RestorePriceArrays();
	}
	if (GetDatabaseName() == "USEOD-Polygon"){
		StaticVarRemove("USEOD-Polygon-LTD");
		SetForeign("SPY");
		dt = DateNum();
		ltd = LastValue(dt);	
		//LTD stands for last trade date in DateNum format
		StaticVarSet("USEOD-Polygon-LTD", ltd, persist = True);
		RestorePriceArrays();
	}
}

When I run the above script in AFL editor and immediately execute the next script (see below) in batch file from AFL editor using the analysis window, I am getting results as expected.

//Get tickers whose last traded Date is same as index last traded date
if (GetDatabaseName() == "InEOD-DhanHQ"){
	dn_index_lastdataDate = StaticVarGet("InEOD-DhanHQ-LTD");
}
else if (GetDatabaseName() == "USEOD-Polygon"){
	dn_index_lastdataDate = StaticVarGet("USEOD-Polygon-LTD");
}

dt_index_lastdataDate = DateTimeConvert(2, dn_index_lastdataDate);

symbol = Name();
dt = DateTime();
dt_symbol_lastdataDate = LastValue(dt);

iscurrentdate = DateTimeDiff(dt_index_lastdataDate, dt_symbol_lastdataDate) == 0;

//filtering the data
if (StrFind(GetDatabaseName(), "USEOD")){
	Filter = ROC(Close, 21) > 20 AND Close > 5 AND LLV(Volume, 10) > 100000 AND iscurrentdate;
}
else if (StrFind(GetDatabaseName(), "InEOD")){
	Filter = ROC(Close, 21) > 20 AND Close > 5  AND LLV(Volume, 10) > 10000 AND iscurrentdate;
}

AddColumn(Close, "Close", 1.2);
AddColumn(ROC(C, 21), NumToStr(21, 1.0)+" DPerf", 1.2);
AddColumn(dt_index_lastdataDate, "Index Last Date", formatDateTime);
AddColumn(dt_symbol_lastdataDate, "Symbol Last Date", formatDateTime);
AddColumn(DateTimeDiff(dt_index_lastdataDate, dt_symbol_lastdataDate)/(24*3600), "Date Difference", 1.0);

SetSortColumns(4);

When I make a batch file and run the 2 scripts in sequence, I dont get any results as the date is being set to previous bar date.

This started happening after the upgrade. After trying all the methods I can, I have downgraded back to my original version but the issue still persists.

I made a small youtube video explaining the issue: https://youtu.be/wxfa1o0Wkco

Please advise how this issue can be resolved as I am not able to run any of my scripts.

@Tomasz I tried again with today's data and ran into the same situation. If I am not wrong, this is a issue related to the upgrade. Can you please take a look?

I made a small youtube video explaining the issue: https://youtu.be/wxfa1o0Wkco

Thanks for your time.

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?

Put _TRACE into your formulas and use to analyse what your formula is doing.

Also, use "Divide and Conquer" method. Make things SIMPLE if you have trouble understanding what you are doing.

Make your Filter as simple as:

Filter = 1; // display everything

and display ALL values including "iscurrentdate" variable.

Once you know what iscurrentdate variable holds you can use

Filter = iscurrentdate;

Other remarks:

  1. Generally it is not good idea to have formula that works differently depending on database name.
  2. Check your "pad and align" settings.
  3. You need to understand that your code does NOT READ last date from SPY or NIFTY.
    It reads last date from FIRST SYMBOL (stocknum==0) in the Analysis. So the entire SetForeign code in the first formula MAKES absolutely NO SENSE.

I have put _TRACE into my code, rewritten the code to make it the simplest to troubleshoot and made the below video.

https://www.youtube.com/watch?v=srfbrC2nOn8

I have used below code to set the static variable to the last day of the ticker NIFTY 50.

if (Status("stocknum") == 0){
	if (GetDatabaseName() == "InEOD-DhanHQ"){
		SetForeign("HDFCBANK");
		_TRACE("Name of the symbol is " + Name());
		dt = DateNum();
		ltd = LastValue(dt);	
		//LTD stands for last trade date in DateNum format
		_TRACE("Static Variable InEOD-DhanHQ-LTD1 set to " + LastVisibleValue(dt));
		StaticVarSet("InEOD-DhanHQ-LTD1", LastVisibleValue(dt));
		RestorePriceArrays();
	}
}

Then I am using the following code to get the static variable.

if (Status("stocknum") == 0)
{
	if (GetDatabaseName() == "InEOD-DhanHQ"){
		dn_index_lastdataDate = StaticVarGet("InEOD-DhanHQ-LTD1");
	}

	_TRACE("Static Variable InEOD-DhanHQ-LTD1 in datenum format is  " + dn_index_lastdataDate);
}

I have been running this script as part of a batch job for close to 2-3 months without any issue.

I am still running into the same issue. This was working without any issues until the upgrade to version 6.90.1 X64.

I have spent considerable amount of time to figure out what the issue is by changing lot of things but I couldn't figure out as there isn't much in the code. No matter what the last date of NIFTY 50 data (I have shown QUOTE Editor in the video as well), I am only getting 1240718 as the value.

Any help in resolving this is highly appreciated as I am not able to get my screeners to work properly. Please advise.

@vdtedla1 I think you try to store the ltd value (not the last visible one)

I apologize.

Code I have executed in the youtube link is as below.

if (Status("stocknum") == 0){
	if (GetDatabaseName() == "InEOD-DhanHQ"){
		SetForeign("NIFTY 50");
		_TRACE("Name of the symbol is " + Name());
		dt = DateNum();
		ltd = LastValue(dt);	
		//LTD stands for last trade date in DateNum format
		_TRACE("Static Variable InEOD-DhanHQ-LTD1 set to " + LastValue(dt));
		StaticVarSet("InEOD-DhanHQ-LTD1", LastValue(dt));
		RestorePriceArrays();
	}
}

I have been trying so many variations, that I am completely lost when I pasted the code in my previous post.

Code I have posted in this post is the correct code that I have executed in the youtube link.

Sorry for any confusion that I have created.

I read the entire thread and in particular the above answer by @Tomasz.

I think you should focus on the last 2 points above.

If your StaticVars are set using an exploration check that your "pad and align" is NOT set to any symbol (otherwise the last date you get is the one of the symbol used to "pad").

Remove also the SetForeign/RestorePriceArray lines and check for the Name() == "NIFTY 50"
Something along this snippet:

dt = DateNum();
ltd = LastValue(dt);	
if (GetDatabaseName() == "InEOD-DhanHQ")
{
	if (Name() == "NIFTY 50") 
	{
		_TRACEF("Selected symbol: %s - Last date: %1.0f", Name(), ltd);
		_TRACE("Static Variable InEOD-DhanHQ-LTD1 set to " + ltd);
		StaticVarSet("InEOD-DhanHQ-LTD1", ltd);
	}
	else
	{
		_TRACEF("%s - Last date: %1.0f", Name(), ltd);
	}
}

(test it with "pad and align" not selected and with a watchlist that includes "NIFTY 50")

1 Like

Thanks @beppe for your help and providing alternative code.

I didnt pay enough attention to the above point by @Tomasz until you highlighted it in your last post. That is the mistake I have done.

Though the issue started occurring after the upgrade, based on above point by Tomasz, the first ticker in the list stopped trading after July 18th. As this first started after the upgrade, I strongly believed that upgrade caused the issue and thinking only along those lines and not from other angles.

As I understood the issue, I created a watchlist with the index as the only ticker, used that watchlist in the analysis filter and able to make everything work.

Thanks once again to you (@beppe) and @Tomasz.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.