Stuck with Timeframeset() again

SetChartOptions(0,chartShowArrows|chartShowDates|chartWrapTitle);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}} ", O, H, L, C, SelectedValue( ROC( C, 1 ))) );
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle(), Null, Null, 0, 1, 1); 

OneMinute=in1Minute*1;
TimeFrameSet(OneMinute);
global n1, n2, n3, TenkanSen, KijunSen, ChinkouSpan, Cks, SenkouSpanA, SpA, SenkouSpanB, SpB;
n1 = 9;//Param("TenkanSen",9,1,200,1); 
n2 = 26;//Param("KijunSen",26,1,400,1); 
n3 = 52;//Param("Senkou",52,1,600,1); 

TenkanSen   =(HHV(H,n1)+LLV(L,n1))/2;            
KijunSen    =(HHV(H,n2)+LLV(L,n2))/2;            
ChinkouSpan =Ref(C,-n2);                         
Cks         = Close;                             
SenkouSpanA =Ref((KijunSen+TenkanSen)/2,-n2);    
SpA         =(KijunSen+TenkanSen)/2;             
SenkouSpanB =Ref((HHV(H,n3)+LLV(L,n3))/2,-n2);   
SpB         =(HHV(H,n3)+LLV(L,n3))/2;  

TenkanSen=TimeFrameExpand(TenkanSen, OneMinute);
KijunSen=TimeFrameExpand(KijunSen, OneMinute);
ChinkouSpan=TimeFrameExpand(ChinkouSpan, OneMinute);
Cks=TimeFrameExpand(Cks, OneMinute);
SenkouSpanA=TimeFrameExpand(SenkouSpanA, OneMinute);
SpA=TimeFrameExpand(SpA, OneMinute);
SenkouSpanB=TimeFrameExpand(SenkouSpanB, OneMinute);
SpB=TimeFrameExpand(SpB, OneMinute);
TimeFrameRestore();

Plot (TenkanSen, "Tenkan-sen",colorOrange, styleNoLabel+styleNoTitle,Null,Null,0,0,.05); 
Plot (KijunSen, "Kijun-sen", colorRed, styleNoLabel+styleNoTitle,Null,Null,0,0,.05); 
PlotOHLC (SenkouSpanA, SenkouSpanA, SenkouSpanB, SenkouSpanB, "Cloud", IIf (SenkouSpanA>SenkouSpanB, ColorBlend( colorBlue, colorWhite, 0.65 ),
ColorBlend(colorPink, colorWhite, 0.65 )), styleCloud+ styleNoTitle, 10, 10, 0, 0,.05);

 

 TimeFrameSet(OneMinute);
I1 = TenkanSen;
I2 = KijunSen;
I3 = SenkouSpanA;
I4 = SenkouSpanB;

NearestAboveDiff = 1e9;
NearestBelowDiff= 1e9;
for( n = 1; n  <= 4; n++ ) 
{
  i = Ref(VarGet( "I"+ n), -1);
  Diff = i - Ref(C, -1);
  
  Above = IIF( Diff > 0, Diff, 1e9);
  Below = IIF( Diff < 0, -Diff, 1e9);
  NearestAboveDiff = Min(NearestAboveDiff, Above);
  NearestBelowDiff = Min( NearestBelowDiff, Below);  
}
NearestAboveInd=NearestAboveDiff+Ref(C, -1);
NearestBelowInd=Ref(C, -1)-NearestBelowDiff;
TimeFrameRestore();

confirmationLevelForCloudSupport=TimeFrameExpand(NearestAboveInd, OneMinute);
confirmationLevelForCloudResistance=TimeFrameExpand(NearestBelowInd, OneMinute);

_TRACE("!CLEAR!"); 
_TRACE("confirmationLevelForCloudResistance="+confirmationLevelForCloudResistance+" confirmationLevelForCloudSupport="+confirmationLevelForCloudSupport);

I expect the values to be same in a 1 minute chart and a 1 second chart but I am getting different values. I don’t understand I did all the calculations inside timeframeset() but still not getting the correct values. You would need 1 second data to debug this.

You need to get better structure to your code. It is a mess so far.
I have recommended to you in PM already to better not use multiple TimeFrameSet procedures but just one per each higher interval.

Structure your code based on IPO rule.
So first input then processing and then output.

And please read help again how to write timeframe function code properly.
https://www.amibroker.com/guide/h_timeframe.html
https://www.amibroker.com/guide/afl/timeframeset.html
https://www.amibroker.com/guide/afl/timeframerestore.html
https://www.amibroker.com/guide/afl/timeframeexpand.html

Here is quick (untested) fix

n1 = 9;//Param("TenkanSen",9,1,200,1);
n2 = 26;//Param("KijunSen",26,1,400,1);
n3 = 52;//Param("Senkou",52,1,600,1); 

OneMinute=in1Minute*1;
TimeFrameSet(OneMinute);
	TenkanSen   =(HHV(H,n1)+LLV(L,n1))/2;            
	KijunSen    =(HHV(H,n2)+LLV(L,n2))/2;            
	ChinkouSpan =Ref(C,-n2);                         
	Cks         = Close;                             
	SenkouSpanA =Ref((KijunSen+TenkanSen)/2,-n2);    
	SpA         =(KijunSen+TenkanSen)/2;             
	SenkouSpanB =Ref((HHV(H,n3)+LLV(L,n3))/2,-n2);   
	SpB         =(HHV(H,n3)+LLV(L,n3))/2; 

	I1 = TenkanSen;
	I2 = KijunSen;
	I3 = SenkouSpanA;
	I4 = SenkouSpanB;

	NearestAboveDiff = 1e9;
	NearestBelowDiff= 1e9;
	for( n = 1; n  <= 4; n++ ) 
	{
		i = Ref(VarGet( "I"+ n), -1);
		Diff = i - Ref(C, -1);

		Above = IIF( Diff > 0, Diff, 1e9);
		Below = IIF( Diff < 0, -Diff, 1e9);
		NearestAboveDiff = Min(NearestAboveDiff, Above);
		NearestBelowDiff = Min( NearestBelowDiff, Below);  
	}
	NearestAboveInd=NearestAboveDiff+Ref(C, -1);
	NearestBelowInd=Ref(C, -1)-NearestBelowDiff;
TimeFrameRestore();

TenkanSen=TimeFrameExpand(TenkanSen, OneMinute);
KijunSen=TimeFrameExpand(KijunSen, OneMinute);
ChinkouSpan=TimeFrameExpand(ChinkouSpan, OneMinute);
Cks=TimeFrameExpand(Cks, OneMinute);
SenkouSpanA=TimeFrameExpand(SenkouSpanA, OneMinute);
SpA=TimeFrameExpand(SpA, OneMinute);
SenkouSpanB=TimeFrameExpand(SenkouSpanB, OneMinute);
SpB=TimeFrameExpand(SpB, OneMinute);

confirmationLevelForCloudSupport=TimeFrameExpand(NearestAboveInd, OneMinute);
confirmationLevelForCloudResistance=TimeFrameExpand(NearestBelowInd, OneMinute);

/// NOW PLOT (OUTPUT) FOLLOWS
SetChartOptions(0,chartShowArrows|chartShowDates|chartWrapTitle);

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}} ", O, H, L, C, SelectedValue( ROC( C, 1 ))) );

Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle(), Null, Null, 0, 1, 1); 

Plot (TenkanSen, "Tenkan-sen",colorOrange, styleNoLabel+styleNoTitle,Null,Null,0,0,.05); 
Plot (KijunSen, "Kijun-sen", colorRed, styleNoLabel+styleNoTitle,Null,Null,0,0,.05); 
senkcolor = IIf(SenkouSpanA>SenkouSpanB, ColorBlend( colorBlue, colorWhite, 0.65 ), ColorBlend(colorPink, colorWhite, 0.65 ));
PlotOHLC (SenkouSpanA, SenkouSpanA, SenkouSpanB, SenkouSpanB, "Cloud", senkcolor, styleCloud+ styleNoTitle, 10, 10, 0, 0,.05);

_TRACE("!CLEAR!"); 
_TRACE("confirmationLevelForCloudResistance="+confirmationLevelForCloudResistance+" confirmationLevelForCloudSupport="+confirmationLevelForCloudSupport);
1 Like

I am sorry I forgot what you said about having only one timeframeset()…timeframerestore block. If i understand correctly compressing and expanding multiple times will mess up the values. Is that right? Thank you again. Your fix seems to work.