5 minute and 1 second time frame exploration

What I'm trying to accomplish:
An Exploration trading strategy that incorporates a 5 minute moving avg crossover and some actions based on 1 second bars.

Using a 1-second IQFeed database, and Analysis Periodicity set at 5 minutes, I ran an Optimization to find the best short and long moving averages (MA1 and MA2). I then plugged the MA1 and MA2 values in and ran an Exploration. With Periodicity set to 5 minutes, the resulting rows in the Exploration output are in 5 minute increments. So far so good. However, because I also want to evaluate 1 second bars, I thought it made sense to set Periodicity to 1 second, and use the TimeFrameSet function configured as in5minutes, so I could get both 1 second and 5 minute bars.

Where I'm confused:
I thought that the results of running these two Optimizations would produce the same MA1 and MA2 values:

  1. A 1 second interval database with Periodicity = 5 minutes. See AFL 1 below.

  2. A 1 second interval database with Periodicity = 1 minutes, and TimeFrameSet = to 5 minutes (in5minutes). See AFL 2 below.What I found is that the Optimization results are not the same. Should the results be the same? If not can somebody explain why... that might get me to finally understand. Did I code the timeframeset and timeframeexpand incorrectly???

I've been struggling with this for several days and have read all the relevant references and posts and I just can't seem to figure this out. This would be easy if all I wanted was 5-minute bars. It's the combo of 5 minute and 1 second bars that has me stumbling. Can somebody help me get my lights turned on?

One other thing worth mentioning... I've done some chart trading using real-time IQFeed and IB data during trading hours. Under that circumstance, it was easy to get real-time tick data, do some evaluations on it, write results to text files, and incorporate the results in the 5 minute moving avg crossover.. I'm struggling to do the same with real-time Explorations and I'm quite sure the issue is me and not Amibroker.

// AFL 1 - Periodicity set to 5min
MALength1 = Optimize("MA Length 1", 2, 2, 50, 5);
MALength2 = Optimize("MA Length 2", 4, 2, 250, 5);

    ema1 = EMA( C, MALength1 );
    ema2 = EMA( C, MALength2 );

    Buy = Cross( ema1, ema2 );
    Sell = Cross( ema2, ema1 );
    Short = Sell;
    Cover = Buy;

    Buy = ExRem( Buy, Sell );
    Sell = ExRem( Sell, Buy );
    Short = ExRem( Short, Cover );
    Cover = ExRem( Cover, Short );

// AFL 2 - Periodicity set to 1sec

MALength1 = Optimize("MA Length 1", 2, 2, 50, 5);
MALength2 = Optimize("MA Length 2", 4, 2, 250, 5);

      ema1_5min = EMA( C, MALength1 );
      ema2_5min = EMA( C, MALength2 );

    Buy = Cross( TimeFrameExpand(ema1_5min, in5Minute), TimeFrameExpand(ema2_5min, in5Minute) );
    Sell = Cross( TimeFrameExpand(ema2_5min, in5Minute), TimeFrameExpand(ema1_5min, in5Minute) );
    Short = Sell;
    Cover = Buy;

    Buy = ExRem( Buy, Sell );
    Sell = ExRem( Sell, Buy );
    Short = ExRem( Short, Cover );
    Cover = ExRem( Cover, Short );

The trades are the same but the report metrics may differ...
Reason: higher granularity -> 1-second bar having higher granularity than 5-min bar -> more data information. Metrics are calculated from equity.

Your signals are at 5-min Close.
So e.g. max drawdown may differ because on 1-second interval you have more data information available for closing price equity that you do not see the more you compress data. I.e. if you look at 5-minute bar from 1-second interval then you can see what is going on intra-bar in between one 5-min close to the next 5-min close. Contrary to that if you look at 5-min bar from 5-min periodicity setting then you just have information of two consecutive 5-min prices -> compression. The rest of price action in between the close prices is not visible then.

Think about it when you look at below max. drawdown equation

EQ       = Close;//  equity
MaxEQ    = Highest( EQ );// highest equity
DD       = EQ - MaxEQ;// "current" equity minus "current" highest equity
MaxDD    = Lowest( DD );// maximum drawdown

The EQ and highest equity (MaxEQ) on 1 second interval may be in between 5-min close (intrabar) -> more data information. But on 5-min interval EQ and MaxEQ are always at 5-min close.

Similar for other metrics.

Thanks fxshrat. As usual, your posts contain helpful information. I began using 1 second bars (increased detail/granularity) to help me find events that provide additional exit opportunities in between the 5 minute crossovers. So at a high level (but not your level) I understand why the granularity of 1-second vs 5-minute bars would result in different metrics. When I ran both scenarios, the trades and profit differed significantly between AFL01 code and AFL02 code. AFL01 with Periodicity set to 5min in the Analysis settings window is more than 3x more profitable than AFL02 (with the TimeFrameSet statements) and with Periodicity set to 1sec.

The challenge I can't seem to overcome... Is there a way to use Optimization to determine optimal MA1 and MA2 5-minute crossover points when Periodicity is set to 1-second and while using a 1-second interval database? That is really what I'm trying to accomplish and my attempt with TimeFrameSet doesn't seem to have helped. If I could do this, I would expect that my Explorations would produce 1 row every second instead of one row every 5 minutes, and within those rows I would see buy and sell signals generated by the 5-minute crossovers.

My brain breaks at this point. I don't see how to determine Optimized 5-min crossovers with 1-sec data and Periodicity set to 1 second. I've tried all kinds of hilariously stupid (also maddening and anxiety-producing) things over the last several days trying to figure this out.

I look forward to the day when I'm more knowledgeable about Amibroker and I can get things done faster and maybe even help somebody else.


Thanks fxshrat...
I see now that my use of TimeFrameSet was ok in the AFL2 example code.

When Filter = (Buy OR Sell), then the trades are the same when I run Explorations for the AFL1 and AFL2 code examples.

When I run an Exploration of the AFL2 example with Filter = 1, then one row for each second is displayed and I can also see the trades generated via the 5-minute TimeFrameSet.

I have no idea what goofy things I did previously but after re-editing the Analysis settings and re-saving the APX files, the Explorations display the expected results. The backtests for AFL1 and AFL2 are not the same but I think that is because backtesting uses the Periodicity setting and thus the 1-sec vs 5-min granularity differences likely produce the different results. I need to read more about that.

Thanks again.