User Error. Was: Static Variable not working properly

Hi

I am trying to store static variable .Its not working properly .Its store last value only .I have attached my code .
Need To fine-tune this AFL this AFL any can help me.

_SECTION_BEGIN("Price1");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

SetOption( "DisableRuinStop", True );
//******************************************************************

dat = DateNum() ; 
d   = Day() ;  
Buycnd = 0; sellcon = 0;
Shortcnd=0; covercnd = 0;

Buy = Sell = Short = Cover = 0;
position = 0;
entrydate = 0; 
entrytime = 0; 
MarketPosition = 0;

//*******************************************************
listname = CategoryFind("AxisbankOP", categoryWatchlist );
//******************************************************
symnu = 0;
maxoisymno = 1000;
symbolno = 0;
tets=0;
syno = 0;
entryprice = 0;
exitprice = 0;
entrydate = 0; 
entrytime = 0; 
dat = DateNum() ; 
dt = DateTime();
TM  = TimeNum(); 
List = "";MaxOI = 0; symbolname = "";
entrysymbol = "";
for( k = 1; k < BarCount; k++ )  
{ 
	entrydate = dat[k];
	entrytime = TM[k];
	if ( Status( "stocknum" ) == 0 )  
	{
		List = CategoryGetSymbols(categoryWatchlist, listname );
		for(n = 0; (Symbol = StrExtract(List, n))  != "";  n++) 
		{
			f =Foreign( Symbol, "I");
			MaxOI = IIf(n == 0, f, Max(f, MaxOI));
			if(MaxOI[k] == f[k] )
			{
				maxoisymno = n;
			}
			symnu[k] = maxoisymno;
			symbolname = StrExtract( List, symnu[k] );
			StaticVarSetText("sym",symbolname);	
		}
	}
	entrysymbol = StaticVargetText("sym");
	_TRACE ( DateTimeToStr( dt[k], 0 )+"  "+entrysymbol);
}
//***************************************
Filter = Status( "stocknum" ) == 0;

AddColumn( DateTime(), "Date", formatDateTime , colorDefault, bkgndColor = colorDefault, width = 150, barchart = Null);

AddColumn(Foreign(StrExtract( List, 1 ), "I"),"440CE",1.2, colorDefault, bkgndColor = colorDefault, width = 75, barchart = Null);
AddColumn(Foreign(StrExtract( List, 2 ), "I"),"450CE",1.2, colorDefault, bkgndColor = colorDefault, width = 75, barchart = Null);
AddColumn(Foreign(StrExtract( List, 3 ), "I"),"460CE",1.2, colorDefault, bkgndColor = colorDefault, width = 75, barchart = Null);
AddColumn(Foreign(StrExtract( List, 4 ), "I"),"470CE",1.2, colorDefault, bkgndColor = colorDefault, width = 75, barchart = Null);

AddColumn(MaxOI,"HighestOI",1.2, colorDefault, bkgndColor = colorDefault, width = 75, barchart = Null);
AddColumn(symnu,"HighestOINum",1.2, colorDefault, bkgndColor = colorDefault, width = 100, barchart = Null);
AddTextColumn(entrysymbol,"HighestOISymbol",1.2, colorDefault, bkgndColor = colorDefault, width = 150);


Result page
tempsnip1

StaticVarSetText() stores a single string. When you call this code:

StaticVarSetText("sym",symbolname);	

from inside a loop, you are simply assigning the static variable named "sym" over and over again. Therefore, when you exit the loop, the static variable contains only what you assigned on the final iteration of the loop.

2 Likes

Please don't make a mess and continue your original thread:

Are you going to create a new thread with each reply? It doesn't make sense! Please respect the forum's rules!

1 Like

FYI: Duplicate posts and duplicate thread have been deleted. @Ambi123 please respect forum rules. This is public place. We do not allow vandalism/spam.

If you started one thread, continue within the thread that you started, i.e. within this thread.

@mradtke gave you very good response already.

1 Like

Now I'm change to number array .Please look on my code and let me know what is the problem in my code .

_SECTION_BEGIN("Price1");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 
_SECTION_END();

SetOption( "DisableRuinStop", True );
//******************************************************************

dat = DateNum() ; 
d   = Day() ;  
Buycnd = 0; sellcon = 0;
Shortcnd=0; covercnd = 0;

Buy = Sell = Short = Cover = 0;
position = 0;
entrydate = 0; 
entrytime = 0; 
MarketPosition = 0;

//*******************************************************
listname = CategoryFind("AxisbankOP", categoryWatchlist );

//******************************************************
symnu = 0;
maxoisymno = 1000;
symbolno = 0;
tets=0;
syno = 0;
entryprice = 0;
exitprice = 0;
entrydate = 0; 
entrytime = 0; 
dat = DateNum() ; 
dt = DateTime();
TM  = TimeNum(); 
List = "";MaxOI = 0; symbolname = "";
symbolnum = "";

for( k = 1; k < BarCount; k++ )  
{ 
	entrydate = dat[k];
	entrytime = TM[k];
	if ( Status( "stocknum" ) == 0 )  
	{
		List = CategoryGetSymbols(categoryWatchlist, listname );
		for(n = 0; (Symbol = StrExtract(List, n))  != "";  n++) 
		{
			symbolno[k] = n;
			f = Foreign( Symbol, "I");
			MaxOI = IIf(n == 1, f, Max(f, MaxOI));
			if(MaxOI[k] == f[k] )
			{
				maxoisymno = n;
			}
			RestorePriceArrays(); 
			symnu[k] = maxoisymno;
			symbolname = StrExtract( List, symnu[k] );
			StaticVarSet("sym",symnu[k]);
		}
	}
		symbolnum = StaticVarGet("sym");
		_TRACE (DateTimeToStr( dt[k], 0 )+"  "+symbolnum);
}

You can't expect people to fix your code constantly. If you write a poem you can't expect people to fix your grammar. It is your poem and your formula. Put some effort.

@mradtke already told you to watch for variable types and what you are storing.

With this:

StaticVarSet("sym",symnu[k]); // overwrites "sym" static variable with single number

You are storing SINGLE number, not array. You are storing single value of k-th element of the array. So "sym" static variable that you stored gets overwritten and it stores just a number (scalar), not array.

Another thing that is wrong is entire loop for( k = 1; k < BarCount; k++ ).
It should be removed. What you are trying to do should be written WITHOUT that loop at all.

You should start with reading this http://www.amibroker.com/guide/h_understandafl.html
then How do I learn AFL? and this How do I debug my formula?

Should you used this advice:

you would easily find the variable types yourself and you will see that you are storing a number, not array.

4 Likes