Use NorgateConstitution values as array in IF-Statement

I try to use NorgateIndexConstituentTimeSeriesOther in an IF-Statement

I rank all Stocks from the Dynamic Norgate Watchlichst and try to save the return value of NorgateIndexConstituentTimeSeriesOther in an array: "InList".

Than i try to use "InList" in an If Statement, but i have Problems to address the right value.
Because i don't know the right array field.

In AddColumn i try different array field but all are wrong.

I also have problems with _trace,It is executed only once, on the last day of the analysis period.


#include_once "Formulas\Norgate Data\Norgate Data Functions.afl"

maxPosLong = 10;
maxPosShort = 0;
maxPos = maxPosLong + maxPosShort;
entryDelay = 0;
SetOption( "InitialEquity", 30000 );
SetOption( "CommissionMode", 2 );
SetOption( "CommissionAmount", 4 );

SetOption( "MaxOpenLong", maxPosLong );

SetOption( "MaxOpenPositions", maxPos );
SetOption( "UsePrevBarEquityForPosSizing", True );
SetOption( "AllowPositionShrinking", True );
SetOption( "PriceBoundChecking", False );
PositionSize = ( -( 100 / maxPosLong ) );

//------------------------------------------------------------------------------

// ########      Einmal Ranking     ###########      Einmal Ranking
_SECTION_BEGIN( "1x Ranking" );

wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum ) ;
ListQty = StrCount( List, "," ) + 1;

if( Status( "stocknum" ) == 0 )
 {
   
    StaticVarRemove( "RawValue_*" );
    StaticVarRemove( "RankRawValue_*" );

    
    _Tracef("Date:");  // Give exacly 1 output. at the enddate of the analysis timespan
	InList=0;
    for( n = 0; ( Symbol = StrExtract( List, n ) ) != "";  n++ ) //
    {
		InList= IIf(NorgateIndexConstituentTimeSeriesOther("$SPX",Symbol),1,0);
		
		IstInList[n]=InList[n];
		Filter=1;
		
		if(Symbol=="ABS-200606")
		{
		 AddTextColumn(Symbol ,"Symbol");
		 
		 AddColumn(InList ,"InList");						//Correkt Value that the Symbol is in Index or not
		 
		 AddColumn(LastValue(InList) ,"LastValue");			// Output is every Time 0. Is this the last value of this array at last Bar ?
		 AddColumn(InList[BarCount-1] ,"BarCount-1");		// Output is every Time 0. Is this the last value of this array at last Bar ?
		 AddColumn(InList[BarCount-n+1] ,"BarCount-n+1");		// Output is every Time 0. Is this the last value of this array at last Bar ?
		 
		 AddColumn(InList[n] ,"n");							// Every Time empty
		 AddColumn(InList[n-1] ,"n-1");						// Every Time empty
		}
		
		//IF(InList) 
		//{
        SetForeign( Symbol, 0 );
  
        RankValue = 0;
        RankValue =IIf(InList, C,0);		//  Workaround that ist correkt
        
        RestorePriceArrays();
      
       StaticVarSet( "RawValue_"  +  Symbol, RankValue );
       //}
       
      
    }
    StaticVarGenerateRanks( "Rank", "RawValue_", 0, 1224 );
}
Rang  = StaticVarGet( "RankRawValue_" +  Name() );
    
Buy = Sell = Short = Cover = 0;
BuyPrice = ShortPrice = o;
SellPrice = CoverPrice = o;


B0 = IIf(Rang <= MaxPosLong,1,0);


Buy = B0;// AND NorgateIndexConstituentTimeSeries("S&P 500") ; 




ApplyStop( Type = 1, Mode = 1, Amount = 20 );
ApplyStop( Type = 0, Mode = 1, Amount =10 );


In the last 6 hours i try a lot more but i dont find an solution.....

I try to use NorgateIndexConstituentTimeSeriesOther in an IF-Statement
I rank all Stocks from the Dynamic Norgate Watchlichst and try to save the return value of NorgateIndexConstituentTimeSeriesOther in an array: "InList".
Than i try to use "InList" in an If Statement, but i have Problems to address the right value.
Because i don't know the right array field.
In AddColumn i try different array field but all are wrong.

I also have problems with _trace,It is executed only once, on the last day of the analysis period.

Main Problem: TO get the right value from InList, When this Line is in a for loop; InList=NorgateIndexConstituentTimeSeriesOther("$SPX",Symbol);

#include_once "Formulas\Norgate Data\Norgate Data Functions.afl"

maxPosLong = 10;
maxPosShort = 0;
maxPos = maxPosLong + maxPosShort;
entryDelay = 0;
SetOption( "InitialEquity", 30000 );
SetOption( "CommissionMode", 2 );
SetOption( "CommissionAmount", 4 );

SetOption( "MaxOpenLong", maxPosLong );

SetOption( "MaxOpenPositions", maxPos );
SetOption( "UsePrevBarEquityForPosSizing", True );
SetOption( "AllowPositionShrinking", True );
SetOption( "PriceBoundChecking", False );
PositionSize = ( -( 100 / maxPosLong ) );

//------------------------------------------------------------------------------

// ########      Einmal Ranking     ###########      Einmal Ranking
_SECTION_BEGIN( "1x Ranking" );

wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum ) ;
ListQty = StrCount( List, "," ) + 1;

if( Status( "stocknum" ) == 0 )
 {
    StaticVarRemove( "RawValue_*" );
    StaticVarRemove( "RankRawValue_*" );
	//StaticVarRemove( "InList_*" );
    bi = BarIndex();
	arrayitem = SelectedValue( bi ) - bi[ 0 ]; 
	My_Matrix=Matrix(BarCount,1500,0);
	
    for( n = 0; ( Symbol = StrExtract( List, n ) ) != "";  n++ ) //
    {
		InList=NorgateIndexConstituentTimeSeriesOther("$SPX",Symbol);
	   My_Matrix[arrayitem][n]=InList[arrayitem];

			if(InList[arrayitem])  //My_Matrix[arrayitem][n]  same values
			{
				SetForeign( Symbol, 0 );
	
				RankValue = Ranking2( RankNR );
				RawValue = IIf( RankInvert == 1, 10000 - RankValue, RankValue );
	
				RestorePriceArrays();
			  
			      StaticVarSet( "RawValue_"  +  Symbol,RawValue );
		     }
    }
    StaticVarGenerateRanks( "Rank", "RawValue_", 0, 1224 );
}
Rang  = StaticVarGet( "RankRawValue_" +  Name() );
    
Buy = Sell = Short = Cover = 0;
BuyPrice = ShortPrice = o;
SellPrice = CoverPrice = o;

B0 = IIf(Rang <= MaxPosLong,1,0);
Buy = B0;// AND NorgateIndexConstituentTimeSeries("S&P 500") ; 

ApplyStop( Type = 1, Mode = 1, Amount = 20 );
ApplyStop( Type = 0, Mode = 1, Amount =10 );

Is it your goal to only rank symbols that are members of the index on each bar? For example, you run your AFL against the S&P 500 Current & Past watchlist, but on each day of the backtest you only rank the 500 stocks that are members of the S&P 500 on that day.

Yes, that is my Goal.

a. Actual i run out of ideas. I even try to use StaticVariable for every Symbol but even that did not work.
b. I think the problem is that i overwrite the array for this candle ~1300(for loop) and i have to calculate how often i have do that... but cant imaginate that this is needend.
c. What drive me crazy is, that IIF(NorgateIndexConstituentTimeSeriesOther,1,0) work fine. But i cant find a methode to save/call this return.

In one topic Thomas write about how rewrite array like InList save the values, but i am sure i have try this...

Maybe i need a third ride through the topics.

PS: i could generate a extra List / array(same problem?) with all the symbols...but that would be a workaroud and not the array solution... i will try it. i am interesst what is faster...

This is my standard template for the type of ranking you're trying to accomplish. Perhaps you can modify this to fit your needs.

if (Status("stocknum") == 0 AND
	Status("actionex") != actionExAAParameters)
{
	StaticVarRemove("AvgTurnover*");
	
	SymList = CategoryGetSymbols(categoryWatchlist, GetOption("FilterIncludeWatchlist"));
	for (i = 0; (tkSym = StrExtract(SymList, i)) != ""; ++i) 
	{ 
		if (SetForeign(tkSym))
		{
			turnover = C * V;
			avgTurnover = MA(turnover, 21);
			
			RestorePriceArrays(); 			
		}

		// Only rank stocks when they are in the index
		inIndex = NorgateIndexConstituentTimeSeriesOther(tkIndex, tkSym);
 
		// Most desirable (highest) values have lowest rank numbers.
		StaticVarSet("AvgTurnover"+tkSym, IIf(inIndex, avgTurnover, -1e9));
	} 

	// Now actually do the rankings. Tied ROC values will produce the same ranking result
	StaticVarGenerateRanks("Rank", "AvgTurnover", 0, 1224); 

}
4 Likes

Thank you for your help. But this is only part of the solution.
I would like to calculate the rank value only for the valid shares and create the static variable.
That's why I want to put the validity check in an IF constraint.

The "validity check" is an array, so you would need to use IIf, not if. But it hardly seems worth it to put the result of the Norgate call into a static variable when you can just call it again later if you need that information. What are you intending to do with that static variable?

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