# Correlation, loop and timeframeset

Good morning,

I was trying to make a weighted ranking with ROC and Correlation in monthly timeframe using timeframeset to check the daily drawdown. There is not problem with ROC rank, but the calcule of correlation do not realize the loop and only take a value for month. The code is based on Trendxplorer approach of Tactical asset allocation. Thank you.

``````if ( ( Status( "stocknum" ) == 0 OR Status("stocknum") == -1 ) )
{
StaticVarRemove( "Mom*" );
StaticVarRemove( "Cor*" );
StaticVarRemove( "MC*" );
StaticVarRemove( "Rank*" );

// --- stocks universe --- //

for ( i = 0; ( symbol = StrExtract( watchlist, i ) )  != "";  i++ )
{
SetForeign( symbol );

TimeFrameSet(inMonthly);

Mom_monthly = ROC(C, Mom_lenght);

TimeFrameRestore();

Mom = TimeFrameExpand(Mom_monthly, inMonthly);

TimeFrameSet(inMonthly);

Cor_monthly = -Correlation(C, Foreign( "\$SPX", "C"), Cor_lenght);

TimeFrameRestore();

Cor = TimeFrameExpand(Cor_monthly, inMonthly);

RestorePriceArrays();

// --- store values ---
StaticVarSet( "Mom"    + symbol, Mom    );
StaticVarSet( "Cor" + symbol, Cor );

}

// --- generate ranks for stocks ---
StaticVarGenerateRanks( "Rank", "Mom", 0, 1224 );
StaticVarGenerateRanks( "Rank", "Cor", 0, 1224 );
StaticVarGenerateRanks( "Rank", "Vol", 0, 1224 );

for ( i = 0; ( symbol = StrExtract( Watchlist, i ) )  != "";  i++ )

{

RankMom  = StaticVarGet( "RankMom"  +  symbol );
RankCor  = StaticVarGet( "RankCor"  +  symbol );

// in case of tie, RankMom is decisive through + 0.001
MC = RankCor * (Wc +0.01) + RankMom * Wm;

StaticVarSet( "MC" + symbol, -MC);

}

// generate master ranking
StaticVarGenerateRanks( "Rank", "MC", 0, 1224 );

}

symbol  =  Name();

Mom     =  StaticVarGet( "Mom"     +  symbol );
Cor     = 	-StaticVarGet( "Cor"     +  symbol );
MC      = 	-StaticVarGet( "MC"     +  symbol );
RankMom =  StaticVarGet( "RankMom" +  symbol );
RankCor =  StaticVarGet( "RankCor" +  symbol );
RankMC  = 	StaticVarGet( "RankMC"     +  symbol );

// Posicionamos los ETF

PositionSize = -100 / posiciones;

PositionScore = Max(1/RankMC, 0);

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

// --- exploration filter ---
ExploreFilter = ParamToggle( "ExploreFilter", "LastBarInTest|All", 1 );
if ( ExploreFilter )
Filter = 1;
else
Filter = Status( "LastBarInTest" );

// --- sort for exploration only (not on backtest) ---
if ( Status( "actionex" ) == actionExplore )
{

SetSortColumns( 2, 3 );

AddColumn( Cor    , "Cor"  , 1.3                  );
AddColumn( Mom    , "Mom"  , 1.3                  );
AddColumn( RankMom, "RankMom"    , 1.0                  );
AddColumn( RankCor, "RankCor"    , 1.0                  );
AddColumn( MC    , "MC   "     , 3.3                  );

}

``````

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