Help for using TimeFrameSet with Static Variables

Hello,

I should be grateful if someone would help me to programme correctly the following part of a weekly system which is supposed to use the monthly value from a ranking.

// -- Watchlist --

Watchlistnumber       = GetOption("FilterIncludeWatchlist");
Watchlist      = GetCategorySymbols(categoryWatchlist, Watchlistnumber);

// -- Ranking --

MomentumPeriods = 6; 

TimeFrameSet(inMonthly);
if (Status("Stocknum") == 0 )
	{ 
	StaticVarRemove("ExponentialRegression*" ); 
	for (n = 0;(Symbol = StrExtract(Watchlist, n)) != ""; n++) 
		{ 
		SetForeign (Symbol);
		Slope = (exp(LinRegSlope(ln(C), MomentumPeriods))^52 - 1);
		RSQ = (Correlation(Cum(1), C, MomentumPeriods))^2; 
		Score = Slope * RSQ;  
		RestorePriceArrays();
		StaticVarSet ("ExponentialRegression" + Symbol, Score);
		} 
	StaticVarGenerateRanks("Ranking","ExponentialRegression", 0, 1224); 
	} 
	
Symbol = Name();
SymbolScoreM = StaticVarGet ("ExponentialRegression" + Symbol);  
SymbolRankingM = StaticVarGet ("RankingExponentialRegression" + Symbol);
TimeFrameRestore(); 

SymbolScore = TimeFrameExpand(SymbolScoreM, inMonthly);
SymbolRanking = TimeFrameExpand(SymbolRankingM, inMonthly);

Plot(SymbolRanking, "Ranking",colorBlack);

// -- Exploration --

Filter = 1;
AddColumn(SymbolRanking, "Ranking", 1.0);
AddColumn(SymbolScore, "Score", 1.2);

If I run an exploration in either weekly or monthly periodicity, the result should be the same for a given date. However it is not, but I am unable to know what I am doing wrong.

Thank in advance for your help.

Best regards

1 Like

@jptrader I can’t figure out why either. But I do think you can avoid the problem by using daily data and adjust your parameters accordingly. Remember the KISS principle. You can rotate weekly or monthly using daily data.

And also you have an unrelated error in your code, if you are using monthly data then you should not be raising to the power of 52 if you want an annualized measure. It should be a 12. Plus if this is a rotational training system why bother with StaticVarrAdd? PositionScore will rank your securities.

I’m pretty sure you should not be calling SetForeign from inside your TimeFrameSet...TimeFrameRestore block. See this knowledge base article: http://www.amibroker.com/kb/2014/10/20/foreign-timeframeset/

Matt

2 Likes

You can't get same results of monthly in weekly the same way as you can't get same result of 13-min in 5-min for example. You can get same result of monthly TF i.e. in daily though (using TF functions).

Just think about it logically... for example end of month could be 31st of May on Thursday. But Weekly Close is on Friday and new month. So how on earth do you want to extract Thursday's monthly close information on Friday on Weekly bars? So in weekly the (monthly) output will be the close of the latest friday of same month. So for example if monthly end is on Thursday 31st then Weekly will use Close of Friday 25th of same month as monthly close. Understood?

Just plot monthly price on daily, weekly, monthly TF and zoom out and compare few historical data points.

cl = TimeFrameGetPrice("C", inMonthly, 0, expandfirst);
Plot(cl, "Monthly Price", -1);
Plot(C, "Price of " + Interval(2),-1);
_N( Title = StrFormat( "{{NAME}} - {{INTERVAL}} - {{DATE}} Close %g, WKD: %g {{VALUES}}", C, DayOfWeek() ) );

Daily and monthly will be inline as regards to monthly close price. Weekly and monthly will not be fully inline.


Aynway, back to your code.... as been mentioned it was not properly.

Following one below will output same results in daily and monthly timeframe for example. But as been pointed out above on Weekly you will not get same results because of way of compression.

1

1

// -- Watchlist --

Watchlistnumber = GetOption("FilterIncludeWatchlist");
Watchlist = GetCategorySymbols(categoryWatchlist, Watchlistnumber);

// -- Ranking --

MomentumPeriods = 6; 

if (Status("Stocknum") == 0 )
{ 
	StaticVarRemove("ExponentialRegression*" ); 
	for (n = 0;(Symbol = StrExtract(Watchlist, n)) != ""; n++) 
	{ 
		SetForeign (Symbol);
		//
		TimeFrameSet(inMonthly);
			Slope = (exp(LinRegSlope(ln(C), MomentumPeriods))^52 - 1);
			RSQ = (Correlation(Cum(1), C, MomentumPeriods))^2; 
			Score = Slope * RSQ; 
		TimeFrameRestore();	
		//				
		Score = TimeFrameExpand(Score, inMonthly, expandfirst);
		StaticVarSet ("ExponentialRegression" + Symbol, Score);
	} 
	StaticVarGenerateRanks("Ranking","ExponentialRegression", 0, 1224); 
} 

Symbol = Name();
SymbolScoreM = StaticVarGet ("ExponentialRegression" + Symbol);  
SymbolRankingM = StaticVarGet ("RankingExponentialRegression" + Symbol);

Plot(SymbolRankingM, "Ranking",colorBlack);

// -- Exploration --
AddSummaryRows( 62, 1.2);

Filter = Month() != Ref(Month(),1) OR Status("lastbarinrange");
AddTextColumn( Interval(2), "Selected Interval", 1);
AddColumn(SymbolRankingM, "Monthly Ranking", 1.0);
AddColumn(SymbolScoreM, "Monthly Score", 1.2);
4 Likes

Quantboy, mrdtke, fxshrat,

Thank you for your help, now I understand my mistake and the code is working finally.

Regards.

Hi i am having an issue with using the same function (staticVarGenerateRanks and the TimeframeSet Function) when Im shifting from the daily to weekly Timeframe. The Code calculates Values properly but when i am plotting the ranks onto the chart it shows two Symbols having the same rank(even after setting the Rank function to 1234)

vp =5;
watchlist = 0;

List = CategoryGetSymbols(categoryWatchlist,watchlist);
if(Status("stocknum")==0)// Generate ranking when we are on the very first symbol
{
	StaticVarRemove( "value*" );

	for ( n = 0; ( Symbol = StrExtract( List, n ) )  != "";  n++    )
	{    
				
				
				
				SetForeign ( symbol );
				
				TimeFrameSet(inWeekly);
			
			    values = Ref(PercentRank(ATR(vp),100),-1);
		
				TimeFrameRestore();
				
				values = TimeFrameExpand(values,inWeekly,expandfirst);
				
				RestorePriceArrays();

				StaticVarSet (  "values" + symbol, values );
	
				_TRACE( symbol );
	}	
	
	StaticVarGenerateRanks( "rank", "values", 0, 1234 );
	


}
symbol = Name();
values = StaticVarGet ( "values" +  symbol );
rank = StaticVarGet ( "rankvalues" +  symbol );

So when i plot the above code, the ranks on the Weekly chart are perfect. But when i move to a daily timeframe, alot of the symbols have the same rank. How do i rectify this?