Indicator using data from a day of week

Hi. If I want to create an indicator on 10 minute bars using calculation below…

prevclose=ref(close,-1);

prevclose2barback = ref(close,-2);

logchange = Ln(prevclose / prevclose2barback);

StandDev=StDEV(logchange,20); // standard deviation of last 20 bars.

Is it possible for me to only include closing prices for Mondays for indicator #1, then create a new indicator # 2 using same formula above but only containing bars from Tuesdays… up to indicator #5 only for Fridays?

Or even better create 1 indicator “X” where the value of the indicator for the bar lines up with the corresponding day. For example, if the 10 minute bar is a Tuesday bar, the value of this indicator X is last 20 price changes only calculated from Tuesday bars… if 10 minute bar is a Thursday , then indicator comprised only of thursday data,etc.

Thanks.! :slight_smile:

Sure it’s possible.
Use SparseCompress/SparseExpand functions.
https://www.amibroker.com/guide/afl/sparsecompress.html
https://www.amibroker.com/guide/afl/sparseexpand.html

And yes, you can create single code for all days. Just a little bit of looping (five iterations) required. Easy.


BTW, your code can be written shorter, like so

logchange = Ref( Ln( ROC(C,1)/100+1 ), -1);
StandDev = StDev(logchange,20); // standard deviation of last 20 bars.
4 Likes

Thanks fxshrat. You helped me with an AB issue like 2 years back (your handle is hard to forget…lol). So the sparsearraycompress and expand works the same way as timeframeset then timeframeexpand- to align the databars of different periodicities?

1 Like
// the indicator that is calculated only from Monday's closing prices
// coding example
/// @link http://forum.amibroker.com/t/indicator-using-data-from-a-day-of-week/2368
monday = DayOfWeek() == 1;

/* compress */
monday_closes = SparseCompress( monday, Close );

/* your indicator here */
monday_prevclose = Ref( monday_closes, -1 );
monday_prevclose2barback = Ref( monday_closes, -2 );
monday_logchange = ln( monday_prevclose / monday_prevclose2barback );
monday_StandDev = StDEV( monday_logchange, 20 ); // standard deviation of last 20 bars.

/* expand */
monday_indicator = SparseExpand( monday, monday_StandDev );
3 Likes

Thanks Tomasz.
I was able to make it work on a daily chart but it seems to have some issues on intra-day data. Occasionally I get null values. Does sparsecompress /expand have limitations on timeframes ?

SpikeByDay=Null;
DayNumber=DayOfWeek();
PrevCl1bb = Ref(Close,-1);
logaGaps= ln(Open/ Prevcl1bb); //close to open for overnight gaps action
GapsSigma = StDev(logaGaps,20);//chart the past 20  day changes using gap bars
DollarSigmaGaps = GapsSigma * Open;
GapsSpikeChart = abs((PrevCl1bb - Open )/DollarsigmaGaps);
DlySpike =  SparseCompress( Daynumber==2, GapsSpikechart); // compact sparse data 
AvgSpike = MA(DlySpike,5);
SpikebyDay = iif( DayNumber==2, SparseExpand(Daynumber==2, AvgSpike), SpikeByDay );

Plot(gapsSpikeChart,"Gaps_Spike",colorOrange,styleLeftAxisScale);
dynamic_color =colorWhite;// IIf( ( DayOfWeek()==1 OR DayOfWeek()==5) , colorGreen, colorwhite );  //green bar on monday, friday
Plot(SpikebyDay,"Gaps Spike By Day",dynamic_color,stylehistogram); 

If you look at chart the cursor is pointed to an empty "gaps spike by day".

image

Lastly, a very generous user might offer a suggestion on a fix but if the functions itself have limitations, I figure I might as well run it with the maker of the software thru this forum. I hope I am not in violation of Spamming rules.

Your modifications are WRONG.
You should compress CLOSE price and use COMPRESSED Close price for your indicator as ORIGINALLY SHOWN in my post.
Instead you are compressing the result of your calculation - it is WRONG.
You wanted to make calculations ON MONDAYS only.
Instead after your modifications you are doing calculation on ALL DAYS, but only picking
MONDAY indicator value which is COMPLETELY DIFFERENT THING.

You guys need to do MATH 101 course AGAIN.

And the code for obvious reasons is for DAILY interval. You would need to use timeframe functions to compress to daily first if you want to use on intraday source data.

2 Likes

It works . Thanks Tomasz and fxshrat.