Limiting Data in Backtesting till present Candle

I have developed a trading algo for intraday trading which runs on hourly data. It uses the high and low of the current day till the current candle to generate buy/sell signals. While trading in live market this works as intended.

But when I backtest this algo on hourly data instead of taking today's high/low till current candle (current hour) it takes high/low of the whole day which in turn pollutes the result of the backtest, since this uses the future data which will not be available while I am trading live.

For example:
Let's say that the market function between 9am to 3pm and I am trading on hourly chart at 1pm, so at 1 pm my algo will use the day's high/low till 1pm to generate buy/sell signal.

Whereas in backtest since I have the whole day's data for any day the same algo at 1pm will use the whole day's high/low which might be made after 1pm.

So is there a way in which I can limit my data till the current hourly candle while backtesting??

Any help will be highly appreciated.
Thanks,

@klivish how can anyone on the forum correct your code when you have not posted it?

Remember to use proper code block formatting for the forum.

In addition to @portfoliobuilder's comments, you should confirm that you have set your Periodicity to 1 hour in the Analysis Settings window. It sounds to me like perhaps you have 1-hour data in your database, but you're back testing using daily bars. That won't allow you to achieve what you want.

HighestSince() and LowestSince()

dn = DateNum();
newday = dn != Ref(dn, -1);

DayHHsince = HighestSince(newday, H);// "current" Highest High since start of day
DayLLsince = LowestSince(newday, L);// "current" Lowest Low since start of day

prevbarHH = Ref(DayHHsince, -1);// previous bar's Highest High
prevbarLL = Ref(DayLLsince, -1);// previous bar's Lowest Low

Plot( DayHHsince, "DayHHsince", colorGreen, styleStairCase );
Plot( DayLLsince, "DayLLsince", colorRed, styleStairCase );

0318

1 Like

Thanks for you replies. Below is a part of the code that I am using.

dmin1C=TimeFrameGetPrice( "C", inDaily, shift = -1, mode = expandFirst ); // to get yesterday's close
dmin1L=TimeFrameGetPrice( "L", inDaily, shift = -1, mode = expandFirst );  // to get yesterday's low
dmin1H=TimeFrameGetPrice( "H", inDaily, shift = -1, mode = expandFirst ); //  // to get yesterday's high


dH=TimeFrameGetPrice( "H", inDaily, shift = 0, mode = expandFirst ); // to get today's high till the present candle
dop=TimeFrameGetPrice( "O", inDaily, shift = 0, mode = expandFirst );  // to get today's open till the present candle
dC=TimeFrameGetPrice( "C", inDaily, shift = 0, mode = expandFirst ); // to get today's close till the present candle

Buy= (dmin1L< dop) and (dH>dmin1H);
BuyPrice=dC ;

//The above line is used to get the price at the time when the Buy condition is true. It works for live market but I face the same issue while backtesting

While back testing in the settings window I have set the Periodicity to Hourly. I am using the above code to access the daily high and low for past days and present days.

@klivish,

When you use the "inDaily" timeframe, you are essentially looking forward in time.

You will have to track the highs of your hourly bars in your code for the current day high up to the current bar.

Only when you use expandFirst mode.

When shift is zero, one should use expandLast. It is precisely documented in the manual http://www.amibroker.com/guide/h_timeframe.html

expandLast - the compressed value is expanded starting from last bar within given period (so for example weekly close/high/low is available on Friday's bar)
expandFirst - the compressed value is expanded starting from first bar within given period (so for example weekly open is available from Monday's bar)
expandPoint - the resulting array gets not empty values only for the last bar within given period (all remaining bars are Null (empty)).

Caveat: expandFirst used on price different than open may look into the future. For example if you create weekly HIGH series, expanding it to daily interval using expandFirst will enable you to know on MONDAY what was the high for entire week.

So in the above example if we use expandLast in a weekly chart and today is wednesday, so will it give high/low till Wednesday or will it give null?

It will give previous week data in such situation. Current week data (except Open) are not known until Friday's close.

But what I need is High/Low till Wednesday for that week and the price at which my conditions return TRUE.