Beta Scanner Behave different in different Time Frame

Dear Senior,

I am using below Beta code to filter the set of Scrip in which i want my Buy condition to be true in 5 minute time frame, the idea was to select only those Scrip which met the volatility criteria of benchmark Index on daily time frame, however when i debug the code on set time frame it works fine, but the moment i switch to lesser time frame in periodicity keeping the set time frame as "indaily" it gives the different result, am i fundamentally wrong to understand the behavior of Beta or am i missing something on the code.

ticker=ParamStr( "Ticker", "NIFTY 50.NSE_IDX" );
P=Foreign(ticker,"C",1);
TimeFrameSet( inDaily );
N = Param("Period",21,1,252,1);
Beta=(( N * Sum(ROC( C,1) * ROC(P,1),N )) - (Sum(ROC(C,1),N) *Sum(ROC( P,1),N))) / 
((N * Sum((ROC(P,1)^2 ),N)) - (Sum(ROC(P,1 ),N)^2 ));
TimeFrameRestore();
Beta = TimeFrameExpand( Beta, inDaily,expandLast);
Beta_Cond = beta>=1 && beta<=3;

Filter = 1;
AddColumn (Beta, "Beta",1.2);
AddColumn (Beta_Cond, "Beta_Cond",1.0);

Any suggestion will be most welcome.
S-Shot%20-%205%20Min S-Shot%20-%20Daily Explore%20-%205%20Min Explore%20-%20Daily

If using TimeFrameSet you should rather use SetForeign not Foreign.

ticker=ParamStr( "Ticker", "NIFTY 50.NSE_IDX" );
N = Param("Period",21,1,252,1);

// TimeFrameSet for foreign symbol 
SetForeign(ticker, 1);
	TimeFrameSet( inDaily );
		P = C;
		rc_p = ROC(P,1);
		Sum_p = Sum(rc_p, N);
TimeFrameRestore();
// restore symbol and time frame
// but we keep variables compressed 

// TimeFrameSet for selected symbol 
TimeFrameSet( inDaily );
	rc_c = ROC(C,1);
	num = N*Sum(rc_c * rc_p,N) - Sum(rc_c,N)*Sum_p;
	denom = (N * Sum(rc_p^2, N) - Sum_p^2); 	
	Beta = num / denom;
TimeFrameRestore();

// Now expanding Beta
Beta = TimeFrameExpand( Beta, inDaily,expandLast);
Beta_Cond = beta>=1 && beta<=3;

Plot( Beta, "Beta", colorRed );

Filter = 1;
AddColumn (Beta, "Beta",1.2);
AddColumn (Beta_Cond, "Beta_Cond",1.0);

@fxshrat thanks for the revert, tried your suggestion it works well when the periodicity & set timeframe is equal but the problem still persist in lower periodicity time frame while keeping settimeframe on "indaily".

May be as stated earlier my fundamentals would be wrong as i am trying to find the stock on lower periodicity by bench-marking it on higher time frame beta.

attaching the screen shot for your reference.

Explore%20-%205%20Min%20Timeframe S-Shot%20-%205%20Min%20Time%20Frame

Your claim is incorrect! Check your facts first.

The code returns same (daily) BETA for all time frames in chart and in analysis.

5-min
7

15-min
8

1-hour
9

Daily

10

And e.g. 5-min periodicity in analysis
11

Once again check your facts first. I'm not posting for sake of posting and for wasting time.
If I post something then it works in 99% of cases.

EDIT: What's your entire analysis settings?
What is output in 15-min, hourly, daily in chart?
Is it different? Check whether you have pasted actual code of post #2.

@fxshrat thanks for taking effort as i said the code is working perfectly fine in similar time-frame however the beta value what you see in your screen shot is the last candle daily beta value when you change the time frame directly on chart it continue to show same last beta value irrespective of what time frame chart you have have selected, attaching the reference image.
ABC

However once you click on respective candle from the exploration there is difference of beta value both in screen & exploration for example on 22-08-2019 @ 3:25 PM exploration show the Beta value of 0.59 however once you click on same candle in chart its shows 0.62 attaching the screen shot of my screen.

SShot%20-%20With%20Problem

Also attaching the screenshot of analyses setting.
Untitled

Also to mention that I have found many of your solutions useful from this forum, so no doubts on the efficiency of the solutions.

  1. You have applied (1.) pad & align and (2. worse) on Yahoo EOD symbol!

  2. Next you are using expandLast in your code.
    Read the manual what it means.
    Everything is correct.

  3. Also check Param setting of Period in analysis! It will not use the same setting as chart if you change it in either one!

That's why I said check your facts first !

Once again and slowly...
The code... returns... the correct same ... and expected... results... in all time frames on all bars.

Please don't claim incorrect things and please don't make me waste more time (with more pictures).
The code works just fine. It is usage error on your end.

Last pictures (next do your homework and actually it has been your homework all the time):

5-min
7

same results as in analysis on all selected bars (check July in analysis picture)
1

1-hour
8

same results as in analysis on all selected bars (check July in analysis picture)
2

Daily (in shorter TFs (1-min, 5-min, .... hourly, ... it is shifted forward to output last longer TF's completed bar value because of expandLast and last bar in Daily TF will be updated if there is new bar until then same value as previous one). Check manual on timeframe functions and function arguments if you don't understand them.
9

2 Likes

@fxshrat Many thanks for identifying the mistakes and helping with the refine code, yes the problem was lying with these below 2 points now its working absolutely fine thanks a lot once again.