Change day's 1st candle's (9.15 am) values in Amibroker with Pre-open's

I am new to Amibroker and learning as I go. This is my first post in the Forum.

I want to backtest an intraday strategy on 1 min time frame. The opening price of the the trading session on 1min (9.15 am) is the most important as it is compared with yesterday's closing price(EOD).

I have historical data (1 min) for backtesting, but the Open price of 9.15am Candle is incorrect. It should have been equal to the pre-open session's(9.08/9.07) Open/High/Low/Close.
Please see below screen shot for better idea:

Note: During the pre-open session no trading is done, it only begins at 9.15am.

For backtesting purpose, how do I replace the 9.15's Open with 9.07/9.08's price in Amibroker? Or any other solution ??


My answer is a bit partial for now and a little bit of IFs.

I get the same issue so i've already cleaned old data when i created the DB and now fix everything on a daily basis by running a script.

Ideally, anything can be achieved with code but i have preferred to rectify the data so there is no problem later.

  1. You can export data:
    AmiBroker Knowledge Base » How to export quotes to separate text files per symbol

  2. Use the script in step 1 and MODIFY it to export only timestamp of 9:07 or 9:08 but checking which one is valid. Also modify the time part of time stamp to 9:15 hardcoded.
    The DateTime can be changed to get only Date part.

  3. Import to overwrite. Script here using HYBRID mode.
    AmiBroker Knowledge Base » How to combine data from multiple input files

some other links that can help you understand.
Import ASCII only for specified fields? - AmiBroker - AmiBroker Community Forum

AmiBroker Knowledge Base » How to export quotations from AmiBroker to CSV file ?

Formatting quotation data export to a file - AFL Programming - AmiBroker Community Forum

1 Like

Thanks for your suggestions..

I tried to implement what you recommenced, but having trouble grasping it since I am totally new to programming.

In the live data feed I will not have to change the open price of 9.15 am candle , since it would be correct from the data vendor. I Only need to replace it for the historical data for backtesting.

Wouldn't it be easier to do it through AFLs rather than exporting the data ???

If your dataset ensures that for each trading session there exist:

  1. a bar before market open at 09:15:00 on same day, irrespective of its timestamp 09:07:00 or 09:07:30 or 09:08:00 which marks pre-open session closure, and,

  2. an available tick precisely at 09:15:00.

Then, below should work:

tn = TimeNum();
mrktOpenTime = 091500;

O = IIf( tn == mrktOpenTime, Ref( C, -1 ), O );

/* rest of your code */

Also in Database setting > Intraday Setting, you need to make sure, either:

  • Show 24 hours trading (no filtering), or,

  • Day Session's start time is set to Market Pre-Open Time 09:00 and not Market Open Time 09:15.

In my historical data, day's data started from 9.07/9.08am(pre open) and many times directly at 9.15 am.
However, using your logic I was able to resolve my issue:

I used the below :

tn= TimeNum();
dn= DateNum();
fc= dn != Ref (dn,-1);// todays date != ydays date,first 
Lc= dn != Ref (dn,1);//todays date != tomm date,last

printf("FC is  %g\n",fc);
count = BarsSince(FC) + 1;
printf("count is  %g\n",count);

cond = tn==091500 AND count == 2 ;

O= IIf(cond,Ref(O,-1),O);


However, there's still little ambiguity.

Instead of restricting to count == 2 always, how about count > 0 that would ensure there exist at least one bar before Live Market Open.

count = BarsSince( fc );
cond = tn == 091500 AND count > 0;

There is no certainty that pre-open session's last tick's Open == Close would be True always!

And you would want to have Pre-Session's Close as Live Market's Open.

So, O = IIf( cond, Ref( C, -1 ), O ) holds more valid.

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.