Using Explore to calculate cumulative total returns of symbols by rank

Ok I mistakenly posted this question in the generic Amibroker forum earlier today by mistake, my apologies. I tried to delete it but was not able to.

I am trying to accumulate total returns of symbols by rank over time using Explore and output the total return of each rank position over the entire Explore period. So for example I want the total return over time for the number 1 ranked symbol, the number 2 ranked symbol, etc based on my ranking criteria. Historically I have done this using excel which is cumbersome and time consuming. I am hopoing to expand my horizons and learn to program this into my Explore script so I don't have to do post processing in excel.

The script I am using is below. I think I need to use Composite symbols to accumulate the returns for each rank (i.e. ~rank1, ~rank2, etc). My example below is only for ~rank1 as an example. My first issue is I don't know how to get the results for ~rank1 to display because the ~rank1 is not in my Watchlist. I am using Norgate Data and the Watchlist called "S&P 100 Current and Past" is created maintained by Norgate's plug-in (or software). My second issue is it will be cumbersome to create a Composite ticker for each rank, especially if I want to use the Russell 1000 index. Note that my return calculation stored in the Composite symbols is looking forward one day, which is intentional, because I want to calculate the one day forward return for the symbol in each ranked position. The ranking is in this case rebalanced daily.

Any ideas appreciated.


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

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( "values1*" );

     for ( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++    ) 
     { 
         SetForeign ( Symbol );
         include = NorgateIndexConstituentTimeSeriesOther("S&P 100", Symbol); 
         values1 = IIf(include, MA(C, 15)-MA(C, 50) , -1000);
         RestorePriceArrays();
         StaticVarSet (  "values1"  +  Symbol, values1 ); 
         _TRACE( Symbol );
     }

     StaticVarGenerateRanks( "rank1", "values1", 0, 1234 );
}

Symbol = Name();

values1 = StaticVarGet ( "values1" +  Symbol );
rank1 = StaticVarGet ( "rank1values1" +  Symbol );

AddToComposite(IIf(rank1 == 1, Ref(C, 1)/C-1, 0), "~rank1", "C", 1+16);

AddColumn(CumProd(1+C)-1, "CumProd", 1.4);

Filter = Name() == "~rank1";  

SetSortColumns(2, 3);

Instead of posting duplicates you can EDIT the post including editing category and subject line: How to use this site

Thank you for letting me know I can edit the category.

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