About mix timeframe

Ask about the programming of mixed time frames

When I create a subroutine file, func1.afl contains a function

Func1( .... ) ;

In the function, Timenum() is used:

Local iti ;

Iti = TimeNum( ) / 100 ;

Because I have to set some variables by time.

Then I build a second program, func2.afl

This program is mainly a program that controls time frames.
When in my program

First

Suppose the time frame is 1m.

#include "func1.afl"

TimeFrameSet( inXMinute );

SELxM = func1(...) ;

.
.
.

TimeFrameRestore();

SEL = TimeFrameExpand( SELxM, inXMinute);

Excuse me, the program in func1 uses what time frame to calculate data.

Your function name is func1 and your AFL file is named func1.afl too.
So which func1 do you actually mean??
Another case of imprecise question.


If you mean func1() (the function) in func1.afl, then in "func1.afl" file the function func1() does not calculate/return anything if it is not called there but then it is just defined as function in that file.

If that function func1() is called in that file ("func1.afl") e.g.

f1 = func1();

then it calculates on selected interval's data (if f1 is not called within timeframe functions there).


If you mean the call of func1() (the function) in func2.afl of your upper example then calculations are switched to different interval derived from selected interval's data (switched if inXminute > selected interval). If inXminute <= selected interval then calculations remain in selected interval (e.g. your 1-minute interval being selected chart or analysis interval).

If func1() contains iterations through array then read here for example


Now, if you mean func1.afl (the file) when you say "the program in func1" then again, if that program in file func1.afl is not executed within timeframe functions then it is executed on selected interval's data. If that program of func1.afl is executed within timeframe functions in func1.afl then calculations are switched to different interval derived from selected interval (switched if inXminute > selected interval).


As for documentation on multi timeframe support read here.

1 Like

Sorry , i change the afl file name ,
the func1( ) in AFL1.afl
and func2( ) in AFL2.afl

Excuse me, the program in func1 uses what time frame to calculate data.

Evaluate the flow of control yourself, It is not about TimeFrame in respective AFL files but what is the timeframe when a function is called.

Calling a Function does not execute the entire AFL, however, an include command will when its called.

That is why headers files are always declared at the start, not at the end. Is this logical to you ?

If you have TimeFrame calls within the called ( or Callee ) function, then it will change it accordingly otherwise the called function will execute in the scope of the caller function (Logically answered to best of my knowledge).

What don't you write a complete proper code of AFL 1 and 2, demonstrate the control flow instead ?

Not that I don't write the complete code, it's the code, it's very complicated, so I only write code for the code related problem points.

My strategic thinking is about this

Fuc1 is X1Timeframe and func2 is X2Timeframe

Func1 determines the direction of the order,

Func2 determines the point in time when the order is placed

I put these two func1 func2 in the AFL1.afl AFL2.afl program file.

AFL1.afl like this :

A = 1; // func1 related initial value setting

Func1( c , d )
{
x = c * A / d ;
...
Return x;
}

AFL2.afl:

B = 1; // func2 related initial value setting
C = 2;

Func2( e, f )
{

x = e * B / f ;
y = avg(f, C);
...
z = func1( x , y );

Return z
}

Moderator comment: I added required code tags!

Finally add a Strategy.afl to place the integration program.

So the program for Strategy.afl is as follows

#include "AFL1.afl"
#include "AFL2.afl"

Order = func2( );

If ( Order1 == 1 ) {
putBuyOrder( );
}
Else if ( Order == -1 ) {
putSellOrder( );
} else {
}

This is the whole picture of my program.

I'm also saying, why don't you put code in more presentable way like this:

// AFL1.afl
function func1(varC, varD)
{
    x = varC * A / varD;
    // some computation logic
    return x;
}

use code tags, it doesn't cost anything.

-> Calling a Function does not execute the entire AFL, however, an include command will when its called.

-> That is why headers files are always declared at the start, not at the end. Is this logical to you ?

This statement is a bit interesting to me.
My include afl file contains the associated initial value settings, plus the TimeFrame setting.
Then create a variable for the next afl file

Is there a problem with this?

PS : i also use loop in the afl

https://www.amibroker.com/guide/h_timeframe.html

Time-frame functions do not change the BarCount - they just squeeze the arrays so you have first N-bars filled with NULL values and then - last part of the array contains the actual time-compressed values.
This is why it is essential to expand the data back to the original frame with TimeFrameExpand.

The original setting in Chart TF or Periodicity will govern the AFL variable BarCount
Obviously, you cannot go to Lower TF than this, only higher an in proper multiples.
so with 5m TF, don't expect to extrapolate 12m TF.

Array values will be populated according to the respective TF in which that bit of code is run.

periodicity set to daily and you will see how "weekly close compressed" column contains empty values at the beginning and weekly compressed data at the end of array.

so you need to deal with these NULL values either using AFL functions or your own logic (if you're capable of handling it).

Why do you guys repeat same things that are covered in post #2 already?

Read POST #2 carefully! Loop and time frame is covered there already.
Why asking the same questions over and over again?

It is hilarious again that people don't take time reading.

One can either read 1 post twice, or read 1 post once and ask twice :smiley:
You know all about it for a long time :stuck_out_tongue:

1 Like

I am afraid that you misunderstood my problem.
I will ask again, this time I will separate the questions and write the program.

The following program, I am in the 1min interval, to get the gold cross value of 2min timeframe

Please help me correct the wrong code.

_SECTION_BEGIN( "CrossOverNm" ) ;

function CrossOverNm(inNmin, Lavg, Savg)
{
	inMinute = inNmin * in1Minute;

	TimeFrameSet( inMinute ); // switch to N minute frame

	LongAvg = MA(C, Lavg);
	ShortAvg = MA(C, Savg);
	BuySell = IIf( ShortAvg > LongAvg , 1, -1 );
	
	TimeFrameRestore();
	
	Co = TimeFrameExpand(BuySell, inMinute);
	
	return Co; 
}

Co2m = CrossOverNm(2, 20, 3);

_SECTION_END( ) ;

Nothing has been misunderstood. You don't understand what has been already explained in detail here in this thread.

You do not need to add function definition within Time Frame functions.
So:

_SECTION_BEGIN( "CrossOverNm" ) ;
// Function DEFINITION before
// (Alternativally move that custom function to include file)
function CrossOverNm()
{
	LongAvg = MA(C, 20);
	ShortAvg = MA(C, 3);
	BuySell = IIf( ShortAvg > LongAvg , 1, -1 );
	
	return BuySell; 
}

in2Minute = 2 * in1Minute;

TimeFrameSet( in2Minute ); // switch to 2 minute frame
Co2m = CrossOverNm();
TimeFrameRestore();

Co2 = TimeFrameExpand( Co2m, in2Minute);

Plot( Co2, "Co2", colorRed );
_SECTION_END( ) ;

EDIT: Now I have seen that you have edited your upper post to this one all of the sudden:

And what is supposed to be wrong there? And where is loop you have problems with?


Excuse me but the OP clearly has not bothered reading the second post (carefully) at all! (I even doubt that he continued reading after first paragraph.)

In first post he asked:

Excuse me, the program in func1 uses what time frame to calculate data.

Then he got detailed response (having taken time to create) for almost every case of func1... Besides I have already suspected in 2nd post that looping code in his func1() function could the probable main issue and being the possible reason for asking "which time frame is used". That's why I have added example there dealing with looping code used within Time Frame functions.

And then in 3rd post he ask exactly the same question again:

Excuse me, the program in func1 uses what time frame to calculate data.

I consider that incredibly ignorant and big waste of time.


Fact remains your further posts are just repetition and that you post same links and quotes that have been posted already just tells me that you did not bother reading/acknowledging 2nd post yourself. Otherwise you would have mentioned that almost everything has been posted in this thread already including link(s) to manual and actual loop example.

If he would have read 2nd post carefully then where is his looping code within timeframe functions to ask further specific questions about?

Again, he has not bothered reading first response at all. Period.

Instead same questions and different functions and then one post or more posts even without any use of time frame functions in his code examples all of the sudden. A big imprecise mess.


As for your (rather hypocritical) last post (which encourages others to similarly not bother reading carefully too)... from now on I will use that one as a quote when you will complain yourself again in posts similar to these ones:

etc.

You're welcome! :stuck_out_tongue:

I wouldn't argue with you, that's not what I meant if perceived that way, ppl don't absorb what is posted by others.
This doesnt mean that the post was missing anything or incorrect.
This is why I wrote "You know all about it for a long time"

1 Like

the new version with Loop

_SECTION_BEGIN( "CrossOverNm" ) ;

function CrossOverNm(inNmin, Lavg, Savg)
{
	inMinute = inNmin * in1Minute;

	TimeFrameSet( inMinute ); // switch to N minute frame
	
	iti = TimeNum() / 100;
	
	LongAvg = MA(C, Lavg);
	ShortAvg = MA(C, Savg);
	
	for ( i = 0; i < BarCount ; i++ ) { 
	
		if ((iti[i] < (846 + (Lavg * inNmin))) || (iti[i] > 1400)) {
			BuySell[i] = 0 ; 		  				 // I want the BuySell if the time befort bar count set to 0 
		} else { 
			if ( ShortAvg[i] > LongAvg[i] ) { 
				BuySell[i] = 1;
			} else {
				BuySell[i] = -1;
			}
		}
	}	
	
	TimeFrameRestore();
	
	Co = TimeFrameExpand(BuySell, inMinute);
	
	return Co; 
}

Co2m = CrossOverNm(2, 20, 3);

_SECTION_END( ) ;

This is not the final version I want. The program I want is that we need to reset all variables when we open the mark every day, but I can't think of how to write it. For example, when we open 0845 with 2min K,

when Savg == 3
845 => ShortAvg = ma(c,1);
847 => ShortAvg = ma(c,2);
849 => ShortAvg = ma(c,3);
851 => ShortAvg = ma(c,3) ;
...
Lavg = 10
845 => LongAvg = ma(c,1);
847 => LongAvg = ma(c,2);
...
903 => LongAvg = ma(c,9);
905 => LongAvg = ma(c,10);
907 => LongAvg = ma(c,10);
...