Remove "Early Close" Bars at 13:00

Is it possible to remove "Early Close" bars from all calculations and considerations in AFL? Usually, the market closes at 16:00, but there are days in the year such as 24-11-2017 and 3-7-2017 when the market closes at 13:00. I do not want Amibroker to take the bars at 13:00 for indicator calculations at all. For 16:00 market close it is enough to set the Day Sessions to end at 15:59 in the Database > Intraday Settings os no bars after 16:00 show up, but this does not work for Early Close bars. What can be done here?

Is it possible to do something like this with NULL bars maybe? http://www.amibroker.com/kb/2014/12/31/drawing-indicators-on-a-subset-of-visible-bars/

Putting Null at the end is not good.

Just use Ref( array, -1 ) for your calculations and that way you will use all bars, except the last (incomplete one).

Or just ignore last bar signal.

See also: When is a bar complete?

Is it possible to skip/remove specific bars through AFL code if I have the specific date for it?

Yes it is possible by using SparseCompress/SparseExpand functions.

Tomasz, thank you for the suggestion. Do you think you could give me a code example of how to prevent Amibroker from using the bar at ā€œ2017-11-24 13:00ā€ in all calculations/indicators/plots - making it nonexistent through AFL even though it would still be in the database? Iā€™m not sure how to apply SparseCompess here.

Coding example is in the manual: http://www.amibroker.com/guide/afl/sparsecompress.html

Yes, I looked at this, but not sure how to use this code. How do you remove a whole Bar? The example only uses the Close price.

No example from the guide shows exactly how to do that. Most indicators use close only but ANY array can be used identical way including OHLC and Volume. There are plenty of examples on the forum. Just use search function http://forum.amibroker.com/search?q=SparseCompress

What's more you could just copy-paste example from the manual and change just FIRST line and you would get what you want:

only_when = DateTime() != StrToDateTime( "2017-11-24 13:00:00" );

x = SparseCompress( only_when, Close ); // compact sparse data

y = MA( x, 10 ); // regular calculation

y = SparseExpand( only_when, y ); // expand sparse data 
1 Like

If you want to remove also from plot, you need to plot it compressed, but be aware that datetime stamp will not be the real one. (I should be more specific, plot of the candle)

No, you don't need to plot it compressed. If you want to remove it from PLOT simply use Null.

only_when = DateTime() != StrToDateTime( "2017-11-24 13:00:00" );

x = SparseCompress( only_when, Close ); // compress sparse data

y = MA( x, 10 ); // regular calculation

y = SparseExpand( only_when, y ); // expand sparse data 

Plot( IIF( only_when, y, Null ), "Plot WITHOUT one bar", colorDefault );

The timestamps will be perfectly fine.

Also other arrays can be processed the very same way:

only_when = DateTime() != StrToDateTime( "2017-11-24 13:00:00" );
xo = SparseCompress( only_when, Open ); // compact OPEN
xh = SparseCompress( only_when, High); // compact HIGH
xl = SparseCompress( only_when, Low ); // compact LOW

If you need to use indicator that makes implicit use of OHLC arrays, you can simply overwrite built-in arrays

only_when = DateTime() != StrToDateTime( "2017-11-24 13:00:00" );
Close = SparseCompress( only_when, Close ); // compress Close
Open = SparseCompress( only_when, Open ); // compress OPEN
High = SparseCompress( only_when, High); // compress HIGH
Low = SparseCompress( only_when, Low ); // compress LOW

// any indicator code you want

result = ATR( 15 ) ; // makes implicit use of all OHLC

result  = SparseExpand( only_when, result  ); // expand sparse data 

Plot( IIF( only_when, result , Null ), "ATR WITHOUT one bar", colorDefault );

// if you need original OHLC arrays you can restore them by calling
// RestorePriceArrays();
6 Likes

Tomasz, thank you for this! Regarding this code:

only_when = DateTime() != StrToDateTime( "2017-11-24 13:00:00" );
Close = SparseCompress( only_when, Close ); // compress Close
Open = SparseCompress( only_when, Open ); // compress OPEN
High = SparseCompress( only_when, High); // compress HIGH
Low = SparseCompress( only_when, Low ); // compress LOW

It replaces OHLC of 2017-11-24 13:00:00 with previous bar's OHLC instead of removing the bar 2017-11-24 13:00:00 with all of its information. Is it possible to do that?

Close = IIF(only_when, Ref(Close,-1) , Close );

This does the same thing: References previous close instead of removing the bar.

No. Ref and SparseCompress are technically different. If you can't see fundamental difference, I can't help. You need to find the understanding yourself. You need to start thinking outside the box. You are locked onto thinking on "removing the bar". You are too close to the tree and you don't see the forest. Once you step back and start looking further you will see that the code I provided removes bar for purposes of indicator CALCULATION. And that is all what counts.

There is an excercise for you: Do the calculation the way I did. Then go to Quote Editor and physically remove that particular bar. Then do the calculation again. Compare results.

Try the example below and see if you can understand the diference

NormalMA = MA(C, 5);
Plot(NormalMA, "NormalMA", colorLightBlue, styleNoLine| styleDots);

// "delete" all bars which has hour == 13
OkToInclude = Hour() != 13;//DateTime() != StrToDateTime( "2017-11-24 13:00:00" );

Cc = SparseCompress(OkToInclude, Close);
CompressedMA = MA(Cc, 5);
CompressedMA = SparseExpand(OkToInclude, CompressedMA);
Plot(CompressedMA, "CompressedMA", colorRed, styleThick);

FirstValid = ValueWhen(OkToInclude, Close);
Ciif = IIF(OkToInclude, Close, FirstValid);
IIFMA = MA(Ciif, 5);
Plot(IIFMA, "IIFMA", colorGreen, styleDashed | styleThick);