How to assign current date to StaticVarGetRankedSymbols during backtesting

Hi there,

I've been trying to work this problem out for over a week, but I haven't found a solution. I'm currently trying to create a buy condition based on my current symbol being in a strong sector of its market at any point in time.

I've been using the StaticVarGenerateRanks to perform the ranking and this is my code so far below to retrieve the top sectors, and then check to see if the current symbol's sector is in that group of top sectors.

symlist = "DSBM,DSNC,DSCY,DSFN,DSHC,DSIN,DSTL";

ThisSymbolsSector = "DSCY"; // Hardcoded for this example
StrongSector = Null;

// delete static variables
StaticVarRemove( "ItemScore*" );

// fill input static arrays

for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
    SetForeign( sym );
    Value = ROC( C, 10 );
    RestorePriceArrays();
    StaticVarSet( "ItemScore" + sym, Value );
}

// perform ranking
TopSectorCount = 3;

StaticVarGenerateRanks( "rank", "ItemScore", 0, 1224 ); // normal rank mode

StaticVarGenerateRanks( "top", "ItemScore", TopSectorCount, 1224 ); // top-N mode

StaticVarGenerateRanks( "bot", "ItemScore", -3, 1224 ); // bottom-N mode

// read ranking
for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
    Plot( StaticVarGet( "RankItemScore" + sym ), sym, colorCustom10 + i );
}

sdt = SelectedValue( DateTime() );

// plot
Title = "{{NAME}} -{{DATE}} - {{VALUES}} TOP: " + StaticVarGetRankedSymbols( "top", "ItemScore", sdt ) +
        " BOT: " + StaticVarGetRankedSymbols( "bot", "ItemScore", sdt ) ;
 
// Get top ranked sectors  
TopSectors = StaticVarGetRankedSymbols( "top", "ValuesToSort", sdt );

for ( i = 0; i < TopSectorCount; i++)
{
	_TRACE(StrExtract(TopSectors, i) + DateTimeFormat("%Y%m%d", sdt) + "\n");
	TopSingleSector = StrExtract(TopSectors, i);
	TopSingleSector = StrToUpper(TopSingleSector);
	if (ThisSymbolsSector == TopSingleSector)
	{
		//printf(TopSingleSector + " is a strong sector "+ DateTimeFormat("%Y%m%d", sdt) + "\n");
		StrongSector = True;
		}
}

Buy = StrongSector & C>MA(C,52);
Sell = C<MA(C,52);

As per the documentation, I've found that sdt = SelectedValue(DateTime()) returns the last date of my analysis range, rather than the date of the current bar during backtesting. So its only checking the ranking on the last date, rather than the current bar being backtested.

It is possible to get the sdt variable to contain the date of the current bar that its being processed while undergoing backtesting? I've tried ValueWhen, but that retrieves an array, whilst StaticVarGetRankedSymbols requests a number for a date.

I've had a look at the portfolio backtester reference, but it seems that with the backtester I can only get date of the current bar once a signal occurs, whereas I'm looking to get the current date of the bar to generate a signal. I hope that all makes sense.

Any help would be much appreciated.

Thanks

To get array of StrongSector you would have to iterate Barcount.

So instead of this

something like this

/// https://forum.amibroker.com/t/how-to-assign-current-date-to-staticvargetrankedsymbols-during-backtesting/25631/2
ThisSymbolsSector = "DSCY"; // Hardcoded for this example
ThisSymbolsSector = ","+StrToLower(ThisSymbolsSector)+",";

dt = DateTime();

if ( GetChartID() > 0 ) {
	bir = Status("barvisible");
} else {
	bir = Status("barinrange");
}

StrongSector = Null;
for ( i = 0; i < Barcount; i++) {
	if ( bir[i] ) {
		// Get top ranked sectors  per bar
		TopSectors = ","+StaticVarGetRankedSymbols( "top", "ValuesToSort", dt[i] )+",";
		//_TRACE(StrExtract(TopSectors, i) + DateTimeFormat("%Y%m%d", dt[i]) + "\n");
		if (StrFind(TopSectors,ThisSymbolsSector))
		{
			//printf(TopSingleSector + " is a strong sector "+ DateTimeFormat("%Y%m%d", dt[i]) + "\n");
			StrongSector[i] = True;
		}
	}
}

Note: Could be slow.


BTW, instead of this

Rather do

// perform ranking
TopSectorCount = 3;

if ( Status("stocknum") == 0 ) {
	// delete static variables
	StaticVarRemove( "ItemScore*" );

	// fill input static arrays
	for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
	{
		SetForeign( sym );
		Value = ROC( C, 10 );
		RestorePriceArrays();
		StaticVarSet( "ItemScore" + sym, Value );
	}
	
	StaticVarGenerateRanks( "rank", "ItemScore", 0, 1224 ); // normal rank mode
	StaticVarGenerateRanks( "top", "ItemScore", TopSectorCount, 1224 ); // top-N mode
	StaticVarGenerateRanks( "bot", "ItemScore", -3, 1224 ); // bottom-N mode
}

Hi fxshrat,

Thanks for the example and also the tip about making the ranking run once.

I can get the dates now, but yes unfortunately it is slow.

I'll have to think about different faster workaround, but thanks anyway for showing how it works.

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