PositionScore using combined rank1+rank2

I try to write the combined rank score. But it doesn't work.
Rank1 = ROC( Close, 10 );
Rank2 = ROC( Close, 100 );
PositionScore = Rank1 + Rank2;

I also want to optimize two lookback values.
Which folder does static variable store?(I want to see the static variable)
My code is below:
Could expert help me to modify my code?
Thank you so much.

SetOption("InitialEquity", 100000); 
SetOption("CommissionMode", 1); //count hand cost by how much future
SetOption("CommissionAmount", 0.5); //one future hand cost 50 dollars
SetOption("MaxOpenPositions",2);
SetBacktestMode( backtestRotational );

//The number of issues to hold at a time
NumberHeld=2;//Optimize("NumberHeld",1,1,10,2);

//Allocate funds equally among all issues
PositionSize = -100/(NumberHeld);

LookBack = Optimize("LookBack ",1,1,200,10);
LookBack1 = Optimize("LookBack1 ",1,1,200,10);
// watchlist should contain all symbols included in the test
wlnum = GetOption( "FilterIncludeWatchlist" );
List = CategoryGetSymbols( categoryWatchlist, wlnum ) ;

if( Status( "stocknum" ) == 0 )
{
    // cleanup variables created in previous runs (if any)
    StaticVarRemove( "rank*" );
    StaticVarRemove( "values*" );
    categoryList = ",";

    for( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++ )
    {
        SetForeign( symbol );

        // use sectors for ranking
        category = sectorID();

        // add sector to the list
        if( ! StrFind( categoryList, "," + category + "," ) ) categoryList += NumToStr( category, 1, 0 ) + ",";

        // write our ranking criteria to a variable
        // in this example we will use 10-bar rate-of-change
        value1 = Roc( Close, LookBack );
		value2 = Roc( Close, LookBack1 );
        RestorePriceArrays();

        // write ranked values to a static variable
        StaticVarSet( "value1" + category + "_" + symbol, value1 );
		StaticVarSet( "value2" + category + "_" + symbol, value2 );
    }

    // generate separate ranks for each category from the list
    for( i = 1; ( category = StrExtract( categoryList, i ) ) != ""; i++ )
    {
        StaticVarGenerateRanks( "rank1", "value1" + category + "_", 0, 1224 );
        StaticVarGenerateRanks( "rank2", "value2" + category + "_", 0, 1224 );
    }
}

category = sectorID();
symbol = Name();
m = Month();

value1 = StaticVarGet( "value1" + category + "_" + symbol );
rank1 = StaticVarGet( "rank1" + "value1" + category + "_" + symbol );

value1 = StaticVarGet( "value2" + category + "_" + symbol );
rank2 = StaticVarGet( "rank2" + "values1" + category + "_" + symbol );
// exploration code for verification
AddColumn( value1, "value1" );
AddColumn( rank1, "rank1" );
AddTextColumn( SectorID( 1 ), "Sector" );
AddColumn( SectorID(), "Sector No", 1 );
//Filter = rank <= 2;

if( Status( "Action" ) == actionExplore ) SetSortColumns( 2, 5, 4 );

// sample backtesting rules
SetBacktestMode( backtestRotational );
//score = IIf( rank <= 2, values, 0 );
score = rank1+rank2;
// switch symbols at the beginning of the month only
//PositionScore = IIf( m != Ref( m, -1 ), score, scoreNoRotate );
PositionScore = score;
//SetPositionSize( 1, spsPercentOfEquity );
Filter = 1;
1 Like

This is incorrect

Because it is wrong name.
Your set variable names contain "value1", "value2" but not "values1", "values2".


And this one too (name and number) -> should be "value2":


There is no folder for static variables (but for persistent variables which are saved to disk).
Static variables are in RAM.
But there is StaticvarInfo() function to get info about those variables.

3 Likes

In addition to what @fxshrat said.

Should be:
value2 = StaticVarGet( "value2" + category + "_" + symbol );

You need to set your Optimize default values to use in the Backtester.

LookBack = Optimize("LookBack", 10,1,200,10);
LookBack1 = Optimize("LookBack1", 100,1,200,10);

Also you have this in your code twice!

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