Controlling Time interval with Afl script

Hi
I wanted to backtest my strategy in 3min,5min,10min interval . Is it possible to control this time interval variable in script, so that i can run optimizer with different time interval values and see all the results together at one place ?

Yes, it's possible using the various TimeFrame* functions. However, it would be a lot less work to either run your optimization three times or to set up a batch file to run the three optimizations for you, and then combine the results afterward.

1 Like

This is a code that I use to optimize between the daily, semi-weekly, weekly, bi-weekly and monthly time frames for rotational trading. You could modify it for your purposes:

//Rotate twice a week
SemiWeekly = DayofWeek() == 2 OR DayofWeek() == 5 ;	

//Rotate only at Friday's close
Weekly = DayofWeek() == 5 ;	

//Rotate only on Monday, every 2nd one
Mon = DayOfWeek() == 1; // identify Mondays
countMon = Cum( Mon ); // count Mondays
BiWeekly = Mon AND (countMon % 2 == 0 );

//Rotate only at monthly close	
Monthly = Month() != Ref(Month(), 1);

Timeframe = Optimize("Timeframe", 1,1,5,1); //have to check pad and align for this to work

Rotation = 0;

switch (TimeFrame)
{
	case 1:  Rotation = 1; break;
	Case 2:  Rotation = SemiWeekly; break;
	Case 3:  Rotation = Weekly; break;
	Case 4:  Rotation = BiWeekly; break; //have to check pad and align for this to work
	Case 5:  Rotation = Monthly; break;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

PositionScore = IIf(Rotation, posScore, scoreNoRotate );


Filter = 1;

AddColumn( DayOfWeek(), "Day of week", 1 );
AddColumn( Mon, "Monday", 1 );
AddColumn( countMon, "Monday counter", 1 );
AddColumn( countMon % 2 == 0, "division by 2 remainder", 1 );
AddColumn( Rotation, "condition", 1, colorDefault, IIf( Rotation, colorYellow, colorDefault) );
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
AddColumn( DayOfWeek(), "Day of week", 1 );
AddColumn( Mon, "Monday", 1 );
AddColumn( countMon, "Monday counter", 1 );
AddColumn( countMon % 2 == 0, "division by 2 remainder", 1 );
AddColumn( Rotation, "condition", 1, colorDefault, IIf( Rotation, colorYellow, colorDefault) );~~~
7 Likes

@marcel: I think @akshay.gupta55's case is a bit more complicated than yours because he will need to generate all of his signals in a timeframe other than his base timeframe, then expand his signals back to the base timeframe before the backtest executes.

1 Like

Thanks @Marcel. I was looking for something like that.

If you look on this webpage:
http://www.amibroker.com/guide/h_timeframe.html

You'll see on the top of the page:
"TimeFrame functions are NOT intended to replace Periodicity setting. To switch periodicity/interval you should only use Periodicity setting."

So changing the TimeFrame in your code is not the same as changing the Periodicity setting. I wanted to understand it myself, so I made this test code that can optimize your TimeFrame:

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

Timeframe = Optimize("Timeframe", 1,1,5,1); 
Time = 0;

switch (TimeFrame)
{
	case 1:  Time = 3*in1Minute ; break;
	Case 2:  Time = in5Minute ; break;
	Case 3:  Time = 10*in1Minute ; break;
	Case 4:  Time = in15Minute ; break; 
	Case 5:  Time = inHourly ; break;
	}

//Calculations
TimeFrameSet(Time); 

ShortMA = MA(C,10);
LongMA = MA(C,50);

Buy = Cross(ShortMA,LongMA); 
Sell = Cross(LongMA,ShortMA);
Short=Cover=0;

//Charting
Plot( C, "Price", colorDefault, styleCandle );
Plot( ShortMA, "ShortMA", colorblue, styleline );
Plot( LongMA, "LongMA", coloryellow, styleline );

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

TimeFrameRestore();

I tried running the code using different TimeFrame settings within the code. I then compared my results to running the code as is, but changing the Periodicity on the backtester settings. You'll see different results for the same time periods selected. @mradtke is right, it's better to just run your optimization three times with three different periodicity settings.

1 Like

You are not doing it right. That is why you are getting different results.
Corrected code below.

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

Timeframe = Optimize("Timeframe", 1,1,5,1); 

switch (TimeFrame)
{
	case 1:  Time = 3*in1Minute ; break;
	Case 2:  Time = in5Minute ; break;
	Case 3:  Time = 10*in1Minute ; break;
	Case 4:  Time = in15Minute ; break; 
	Case 5:  Time = inHourly ; break;
	default: Time = 0; break;
}

//Calculations
TimeFrameSet(Time); 

ShortMA = MA(C,10);
LongMA = MA(C,50);

TimeFrameRestore();

ShortMA = TimeFrameExpand(ShortMa, Time, expanLast);
LongMA = TimeFrameExpand(LongMA, Time, expanLast);

Buy = Cross(ShortMA,LongMA); 
Sell = Cross(LongMA,ShortMA);
Short=Cover=0;

//Charting
Plot( C, "Price", colorDefault, styleCandle );
Plot( ShortMA, "ShortMA", colorblue, styleline );
Plot( LongMA, "LongMA", coloryellow, styleline );

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
2 Likes

Thanks @codejunkie, I tested your corrected code and changing the TimeFrame in the code works the same as changing the Periodicity in the settings. Better yet, the optimization for the different TimeFrames will work too. I had to make minor typo corrections to the code, expanLast should be expandLast:

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

Timeframe = 0;//Optimize("Timeframe", 1,1,5,1); 

switch (TimeFrame)
{
	case 1:  Time = 3*in1Minute ; break;
	Case 2:  Time = in5Minute ; break;
	Case 3:  Time = 10*in1Minute ; break;
	Case 4:  Time = in15Minute ; break; 
	Case 5:  Time = inHourly ; break;
	default: Time = 0; break;
}

//Calculations
TimeFrameSet(Time); 

ShortMA = MA(C,10);
LongMA = MA(C,50);

TimeFrameRestore();

ShortMA = TimeFrameExpand(ShortMa, Time, expandLast);
LongMA = TimeFrameExpand(LongMA, Time, expandLast);

Buy = Cross(ShortMA,LongMA); 
Sell = Cross(LongMA,ShortMA);
Short=Cover=0;

//Charting
Plot( C, "Price", colorDefault, styleCandle );
Plot( ShortMA, "ShortMA", colorblue, styleline );
Plot( LongMA, "LongMA", coloryellow, styleline );

/* Plot Buy and Sell Signal Arrows */
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);