Optimising for different timeframes/intervals

I want to optimise for different time frames let's say 6 min up to 15 min. It seems to be hard/impossible using pure AFL since that is only for mixing different timeframes (from what I've read on this forum), which is not my goal for as of now. I also read you need to change the periodicity settings to accomplish what I'm trying to do but then I want to be able to programmatically (OLE or pure AFL) change this, manually changing my custom time intervals constantly is time consuming and won't work for testing/optimising at scale.

Am I missing something, is there a way to accomplish this?

Thank you in advance!

You can do this programmatically, but not in "pure" AFL.
You can use OLE automation interface to write a Javascript that runs a series of backtest each using different periodicity.

Documentation with examples (at the end) is in the Users' Guide
https://www.amibroker.com/guide/objects.html

Hi, I've tried the following:

Changing the value of ActiveDocument.Interval (Using OLE)
Changing the value of RunEveryInterval in the analysis file .apx (programmatically using python)
Changing the value of ChartInterval in the analysis file .apx (programmatically using python)
Changing the value of Periodicity in the analysis file .apx (programmatically using python)

Only changing the Peridodicity seemed to make any difference for the results being different.

The problem with doing that is the value of Periodicity is just an index to (I'm assuming) the internal list of preset periodicities within Amibroker AKA the list of which you can customise with few of your own values. This does not allow for true customisation of the periodicity programmatically only programmatically switching between the preset values.

Am I missing something? Do I need to change a different value?

Thank you in advance again!

"RunEverInterval" is for "run every" feature (auto-repeat).
"ChartInterval" as name says, it is interval for relevant chart, not analysis

"Periodicity" is the only parameter that you should use.

Yes, you need to define custom intervals in the Tools->Preferences, "Intraday" tab. Then you can use any interval that you defined by specifying their index in the .APX file.

What if I want to use more than 5 custom intervals?

And this means the only way to do loads of optimisations/backtests for different intervals is to change the 5 custom intervals manually? Really :sweat_smile:?

I was thinking I could maybe change the "C:\Program Files\AmiBroker\broker.prefs" to be able to change the 5 custom intervals programmatically but it's a file format I do not understand/know how to change.

And how do I know what periodicity is linked to what index?

Where does it start?

0 = ?
1 = ?
2 = ?

It seems kind off random...

And is there a way to know what custom periodicities are currently set (through OLE)?

I was thinking I could figure this out through reading in the broker.prefs maybe?

You only use "custom" slots for non-standard intervals.

Intervals such as 1-second, 5-second, 15-second, 1-minute, 5-minute, 15-minute, 1-hour (60 minute) are built-in and you don't need to define so there is
a lot more than just 5 intervals.

It is not random by any means, it is consecutive.
You could find indices by simply storing different settings to project file.
Those values are SUBJECT TO CHANGE as AmiBroker changes, because we ADD FEATURES and do NOT document those as documenting them would require keeping them unchanged. So don't rely on this list - it is subject to change. Anyway current values are:

0 - daily
1 - day/night
2 - hourly
3 - 15-minute
4 - 5 - minute
5 - 1-minute
6 - 15 - second
7 - 5 - second
8 - 1 - second
9 - tick
10-15 - custom minute intervals
16-20 - custom tick/range/volume intervals

We may add more custom intervals in the future or add a way to specify interval just in seconds. For the time being I am writing what is available right now.

Optimizing non-standard timeframes is possible also in different ways, for example, if you wanted to do this for individual symbol, you could just use Equity() function directly as it is backtester in a box and performs in-place backtest within formula.

I might also consider extending analysis window to allow to run any action (Scan/Exploration/Backtest/Optimization) on range of intervals, or add that to batch window. There are many possibilities.

General remark is that what you are thinking of doing is straight way to overfitting. If your system works on 4-minute data but doesn't work on 5-minute it means that your system is overfitted and it is really BAD IDEA to trade it. You will lose money that way. I guarantee that.

2 Likes

Thank you for the in depth reply!

I'm taking a lot more into consideration than just the timeframe :slight_smile: I appreciate the warning though!

I have just read up on the documentation on the Equity function (a few times now) and I can't really see how this would allow me to test for multiple custom intervals...

Would you mind sharing a small example?

Thank you for your time in advance!

PS: Being able to set a custom interval in seconds would be amazing in the future!

First of all, do you want to backtest single symbol (or multiple symbols but individually, not as portfolio)? Only then Equity() would be possible alternative.

For single symbol backtest, an example is similar to this posted in the Users guide in SetForeign docs AFL Function Reference - SETFOREIGN

Instead of SetForeign you would call TimeFrameSet, something like that:

// the code is quick and dirty hack, for illustration purposes only, don't rely on it
for( k = 1; k < 10; k++ )
{
    period = k * Interval( 0 ); // test integer multiple of selected interval
    TimeFrameSet( period );
    // need to set price arrays to compressed values
    BuyPrice = SellPrice = ShortPrice = CoverPrice = Close;
    // the "system"
    Buy = Cross( MACD(), Signal() );
    Sell = Cross( Signal(), MACD() );
    SetOption( "PriceBoundChecking", False ); // don't run price checks
    e = Equity(); // single-security backtest is done here in-place
    e0 = e[ NullCount( e ) ];
    _TRACEF( "Interval %g, Initial Equity %.2f, Final Equity %.2f\n", period, e0, LastValue( e ) );
    RestorePriceArrays( True );
}

CAVEAT EMPTOR: the code above is a quick and dirty hack. I wrote it just out of head and did not test it. It uses the TimeFrameSet in "wrong" way, turns off price bound checking and abuses the fact that Equity does not really care about timestamps. Those functions should NOT be used like that. It serves only as illustration of hacking possibilities, not as "production quality" code.

1 Like

I would like to backtest multiple symbols individually as portfolio (so the example you sent would not work right?).

I tried the example either way and even if this is what I wanted, I'm assuming I would have to get all the statistics out manually, right?

As described here: Equity function and chart under Can I calculate system statistics using Equity function?

This is exactly what I'm trying to accomplish:

Have a system, with certain optimisation parameters. I would than also want the interval to be an optimisation parameter, for example:

After initially reading this: AFL Function Reference - TIMEFRAMESET this is what I was thinking:

optimised_interval_parameter = Optimize("interval", 1, 6, 15, 1);
current_interval = in1Minute * optimised_interval_parameter;

TimeFrameSet( current_interval ); // switch to optimized time frame

ma_13 = MA( C, 13 );

TimeFrameRestore(); // restore time frame to original

// buy sell short cover HERE based on current optimisation parameters (including the current timeframe/interval)

(I know this is incorrect but it's an example of what I would like to accomplish, you do not have to explain why this wouldn't work I understand it doesn't after reading about it on the forums :slight_smile: )

Is there a similar/alternative way to accomplish this?

I greatly thank you for your time yet again.

1 Like

The answer (with code) is provided already in Optimising for different timeframes/intervals - #8 by Tomasz

Regarding statistics: optimization is not to produce gazillions of reports (because it would be slow and eat up all your hard disk space). Optimization is for finding optimum parameter set. Once you find your optimum parameter value(s) just run regular backtest to get all stats for that optimum. It is all already available.

It does not solve my problem though :sweat_smile:
Like I said I would like to get portfolio level results (just as you would when using regular individual optimisation with a few parameters or a regular backtest).

And as you stated:

First of all, do you want to backtest single symbol (or multiple symbols but individually , not as portfolio)? Only then Equity() would be possible alternative.

So what I want does not work with the example you provided.

This means using the Equity function would not be sufficient for my use case.

(Please do correct me if I'm wrong of course)

Besides what we already discussed as potential solutions are there any other ways of accomplishing what I want?

Earlier I proposed I could edit the 'broker.prefs' file programmatically to change the 5 custom intervals, could this be a solution? If so could you point me towards some data or anything on how to decipher the format of this file?

I appreciate your time and thank you yet again for considering my question :innocent: