Rotational strategy using staticVarSet and StaticVarGenerateRanks

Hi,
I am trying to create a rotational strategy that ranks on different indicators. I have tried to use rotationalTrading but since I rotate on particular day and on different indicators I think I need to use the more custom way of StaticVarGenerateRanks. First I have created a watchlist of the stocks I want to rank. When I backtest this model I have some unexpected results. If the time range that I select has one or many stocks where the data is not complete (for example is removed from market) the stock does not loose its rank and if the rank is in the "Buy" range I get one stocks less traded for the rest of the period and the last one is just kept long forever. My "fix" that I tried in the bottom of the code is to force a sell at LastClose if there is no more data but it is still in the rank so no success. It also resulted in no "open long" positions.

It might also be an issue if I try to rank 100stocks and I have two year data for 10 of them and one year data for 90 of them. If I try to rank them for the first year my guess is that they get rank 1-10 + 11-100 instead of just 1-10. If this is the case and I am checking for momentum in this case I will at best have 10 positive and 90 negatives. OBS!! I haven’t checked this, this is just a guess and if someone is looking into this might want to double check how it is handled.

The last issue that I am facing is that even if a stock is ranked within the top 40 stocks for several consecutive weeks the system sells ang buy backs the same stock from week to week. I think it should be kept long.

Thanks for all the help or pointers that I can get. I think that I have googled and checked the forum several times but can’t find anything that can help me out.
All help is very appreciated.

SetOption("InitialEquity", 1000000); 
SetOption("CommissionMode", 2); //cost per trade
SetOption("CommissionAmount", 9); //one trade cost 9
SetTradeDelays( 1, 1, 1, 1 );
SetOption("AllowPositionShrinking", true );
SetOption("AllowSameBarExit", true );
SetOption("HoldMinBars", 1 );
RoundLotSize = 1;

if ( GetOption( "ApplyTo" ) == 2 )
{
     wlnum = GetOption( "FilterIncludeWatchlist" );
     List = CategoryGetSymbols( categoryWatchlist, wlnum ) ;
}
else
if ( GetOption( "ApplyTo" ) == 0 )
{
     List = CategoryGetSymbols( categoryAll, 0 );
}
else
{
     Error( "The formula works fine if your ApplyTo setting is 'Filter' or 'All' " );
}

if ( Status("stocknum") == 0 ) 
{
    StaticVarRemove( "values*" );
    StaticVarRemove( "rankvalues*" );

    for ( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++    )
    {
		if(Symbol == "~~~EQUITY") continue;
        SetForeign ( symbol );
       
        valuesRsi = RSI( 14 );
        valuesMdi = MDI( 14 );
        valuesAdx = ADX( 14);
        
        RestorePriceArrays();
        
        StaticVarSet (  "valuesRsi"  +  symbol, valuesRsi);
        StaticVarSet (  "valuesMdi"  +  symbol, valuesMdi);
        StaticVarSet (  "valuesAdx"  +  symbol, valuesAdx);
    }
	StaticVarGenerateRanks( "rank", "valuesRsi", 0, 1234 ); //1234
	StaticVarGenerateRanks( "rank", "valuesMdi", 0, 1234 ); //1234
	StaticVarGenerateRanks( "rank", "valuesAdx", 0, 1234 ); //1234
}

symbol = Name();
SetForeign(symbol);

valuesRsi = StaticVarGet ( "valuesRsi" +  symbol ); rankRsi = StaticVarGet ( "rankvaluesRsi" +  symbol );
valuesMdi = StaticVarGet ( "valuesMdi" +  symbol ); rankMdi = StaticVarGet ( "rankvaluesMdi" +  symbol );
valuesAdx = StaticVarGet ( "valuesAdx" +  symbol ); rankAdx = StaticVarGet ( "rankvaluesAdx" +  symbol );

AddColumn ( valuesRsi, "valuesRsi" );
AddColumn ( valuesMdi, "valuesMdi" );
AddColumn ( valuesAdx, "valuesAdx" );

AddColumn ( rankRsi, "rankRsi" );
AddColumn ( rankMdi, "rankMdi" );
AddColumn ( rankAdx, "rankAdx" );


SetSortColumns( 2, 7 ); 
Filter = 1;

nrPosMdi=0; nrTotMdi=0;

for( i = 0; ( sym = StrExtract( List, i ) )  != ""; i++ ) 
{ 	
	valDataMdi = StaticVarGet ( "valuesMdi" +  sym );
	nrPosMdi += IIf(IsTrue(valDataMdi) && valDataMdi <25, 1,0);  //Check
	nrTotMdi +=IIf(IsTrue(valDataMdi), 1,0);
}

trendMdi = nrPosMdi/nrTotMdi*100; AddColumn ( trendMdi, "TrendMdi" );

PlotGrid( trendLow, colorWhite, 8, 1, False );
Plot(trendMdi,"TrendMdi",colorBlue,styleLine);
Plot(rankMdi,"RankMdi",colorGreen,styleLine);
Plot(valuesMdi,"ValueMdi",colorRed,styleLine);
qty=40;
SetOption("MaxOpenPositions", qty); 

model = IIF(trendMdi <trendLow, 1 , 0 );
viktRsi = IIF(rankRsi<=qty/4,-100*4/qty,0);
viktAdx = IIF(rankAdx<=qty,-100/qty,0);

PositionSize = IIf(model ,viktRsi,viktAdx);

AddColumn ( model, "model" );
AddColumn ( viktRsi, "viktRsi" );
AddColumn ( viktAdx, "viktAdx" );
AddColumn ( PositionSize, "PositionSize" );

//This section feels wrong..., tries to handel my unexpected results.
AddColumn ( LastValue(o), "model" );
AddColumn ( LastValue(h), "model" );
AddColumn ( LastValue(l), "model" );
AddColumn ( LastValue(c), "model" );

LastTradeDay =  Ref(Open,1) == LastValue(Open) && Ref(High,1) == LastValue(High) && Ref(Low,1) == LastValue(Low) && Ref(Close,1) == LastValue(Close);

Buy = dayofweek() == 5 ;
Sell = dayofweek() == 5 || LastTradeDay ;

Your post would be more likely to get answers if it included actual QUESTION(s) you have.

Did you read documentation on
http://www.amibroker.com/guide/h_ranking.html
and
http://www.amibroker.com/f?enablerotationaltrading

with regards to "WorstRankHeld" option?

Usually you do NOT need StaticVarGenerateRanks at all.

One more thing that is very important:

You need to turn ON PADDING ("Pad and align to reference symbol").
Current versions (6.39) will display a note if you forgot to do that.

There is a lot of past posts about that: https://forum.amibroker.com/search?q=pad%20and%20align%20ranking

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