## Amibroker AFL EOD gap to trigger intraday trade

Hi All
I'm struggling with the following AFL and would appreciate some help. The system is based on an opening gap on the daily chart, but traded on an intraday periodicity, normally 5 or 15 minutes. The primary and simplified components of the system is as follows:

Condition 1. The difference (Gap) between the previous day close (17:30) and the current day open (08:30) must be in a certain range (between MinGap and MaxGap).
Condition 2. If Gap is positive Buy, if Gap is negative Sell.
Condition 3. Use the size of the Gap (less a few points) as both profit target (Gap is closed) and stop loss point.

The trade, either buy or short, must occur at the open of the first 5 minute bar of the new day or the open of the next 5 minute bar and must trade once only per day. Alternatively I would also like to test for the trade occuring at a specified time such as 09:00.

From viewing the exploration I can see that the daily open, closes and gap are calculated correctly, but the backtest buys and shorts all over the day.

Code snippet below.
Thank you

MinGap = Optimize( "MinGap", 50, 10, 300, 10 );
MaxGap = Optimize( "MaxGap ", 250, 10, 800, 10 );

TimeFrameSet( inDaily );
O1D = O;
C1D = C;
PC1D = Ref(C,-1);
TimeFrameRestore();

DailyOpen = TimeFrameExpand( O1D, inDaily );
DailyClose = TimeFrameExpand( C1D, inDaily );
DailyClosePrev = TimeFrameExpand( PC1D, inDaily );

Gap = DailyClosePrev - DailyOpen ;

BuyCond1 = abs( Gap ) >= MinGap AND abs( Gap ) <= MaxGap;
BuyCond2 = Gap > 0;