DUPLICATE: Search the forum before posting

I am trying to check a condition for all stocks in a loop. I have the basic code here from the forum. I have been using it for a few weeks to calculate ranks.
Now I noticed that a simple assignment of values to a static varibale in the loop does not give the same result as checking the conditions outside....
Also an exploration shows me differences

Both values, the ones assigned in the loop as well as the ones assigned outside should give the same result when used in "Buy". But they dont, THe Strategy resurlt variat

#include_once "Formulas\Norgate Data\Norgate Data Functions.afl" 
 
SetTradeDelays(1,1, 1, 1); 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
 
maxPosLong =20; 
maxPosShort = 7; 
maxPos = maxPosLong + maxPosShort; 
entryDelay = 0; 
SetOption( "InitialEquity", 100000 ); 
SetOption( "CommissionMode", 1 ); 
SetOption( "CommissionAmount", 0.5 ); 
SetOption("ActivateStopsImmediately",True); 
SetOption( "MaxOpenLong", maxPosLong ); 
 SetOption( "MaxOpenShort", maxPosShort );
SetOption( "MaxOpenPositions", 20 ); 
SetOption( "UsePrevBarEquityForPosSizing", True ); 
SetOption( "AllowPositionShrinking", True ); 
SetOption( "PriceBoundChecking", True ); 
PositionSize = (-(100/20));

Indize ="$SPX"; 
 

 if( Status( "stocknum" ) == 0) 
 { 
	wlnum = GetOption( "FilterIncludeWatchlist" ); 
	List = CategoryGetSymbols( categoryWatchlist, wlnum ) ; 
         StaticVarRemove( "RawValue_*" ); 
    StaticVarRemove( "RankRawValue_*" ); 
    for( n = 0; ( Symbol = StrExtract( List, n ) ) != "";  n++ ) // 
    {      
		IsInList=NorgateIndexConstituentTimeSeriesOther(Indize,Symbol); 
		 
		SetForeign( Symbol, 0 ); 
			
			Ranking =IIf(C>BBandTop(C,100,2) AND IsInList,1,0);    //<<<<----Value assignment in Loop
					
			if(Name()=="CI" OR Name()=="CL")
			{
				_TRACE("#In Loop Ranking:  "+Ranking);
				
			}
	 
			StaticVarSet( "RawValue_"  +  Symbol,Ranking);    //<<<<----Value assignment for StaticVar 	
		RestorePriceArrays();		 
    } 
    //StaticVarGenerateRanks( "Rank", "RawValue_", 0, 1224 );
}  

VarGetResult=StaticVarGet( "RawValue_"  + Name());   / 
OutLoop= C>BBandTop(C,100,2);


Filter=  Name()=="CI" OR Name()=="CL"; 
	AddColumn(StaticVarGet( "RawValue_"  +  Name()),"  Stativ var"); 
	AddColumn(C>BBandTop(C,100,2),"  Condition "); 
	
BuyPrice = ShortPrice = o; 
SellPrice = CoverPrice =o; 	
					
//buy =  VarGetResult;
		//	Both LInes should have the same effekt but they dont	
		  
buy = C>BBandTop(C,100,2)   AND  NorgateIndexConstituentTimeSeries(Indize)	;
sell = C<BBandBot(C,100,2)   OR  !NorgateIndexConstituentTimeSeries(Indize);

Deleting the stativar only in Custom Backtester change nothing.
The Exploration show me that most time the Values are the same.

I hope the question and the problem is clear and i havn#t oversee the solution in the last (ver long) two day....

Thank you. :grimacing:

Static variables work everywhere and they work perfectly fine.

But your code is wrong.

You MUST NOT use such codes anymore. Don't call SetForeign inside loop. It is extremely inefficient. AmiBroker screams with Warning 512 if you do that. Respect the warnings. They indicate that the code is bad. There are literally ZILLIONS of posts like yours on the forum. Search for 'SetForeign loop' read my explanations what you should do instead.
I don't really have time and energy to repeat the same thing every day 365 days a year.

SEARCH THE FORUM:
https://forum.amibroker.com/search?q=SetForeign%20loop

and

https://forum.amibroker.com/search?q=Warning%20512

One of posts in the result list of a search is:

1 Like