Monthly ATR on daily data – last day in a month shows different value


#1

Hello,

I am trying to calculate monthly ATR that I use on daily bars using following code:

TimeFrameSet(inMonthly);
mATR = ATR(5);
TimeFrameRestore();
MATR=TimeFrameExpand( mATR, inMonthly, expandLast);
Plot(MATR, "",colorBlue,styleLine);

What is confusing me that the value of ATR does not switch at the end of month directly to new level. Instead I get linear change from the day before the end till the end.

atr

How should I change my code to have stable ATR value through the whole month?


#2

@peterfin you appear to be mistaking what the "lines" on a chart represent. The data points are discrete, and only exist on each bar. The line is imaginary connecting the data points. Check your actual values and you will see the number stays continuous for the entire month and then "jumps" one bar into the new month.

If you plot your indicator as a discrete data point (without lines) you will see what is going on. Or run an Exploration on your monthly ATR and you will see the values are just fine.
image

Similar discussion worth reading here,


PlotOHLC Color Bleed
#3

@portfoliobuilder thank you for your answer. You are right.
Now I realize that I was confused by the fact, that the ATR value changes already within the last trading day of "current" month. I was expecting to see the new value first trading day of new month.

Can you give me hint how to setup the code so I can see new ATR value of previous month on the first day of next month?


#4

@peterfin With the default setting in TimeFrameExpand being expandLast you are getting the January value for your ATR on the last bar of January (after the close of the last trading day of January). I think that it would help you to review the MultiTimeFrame functions but particularly the TimeFrameExpand
http://www.amibroker.com/guide/afl/timeframeexpand.html

It will be difficult to interpret what is going on by looking at the 5 month ATR so I suggest that you experiment by looking at some values that are easy to interpret like the Monthly Open and Monthly Close as in this Exploration code,

// Explore the Monthly Open and Close on a daily bar Exploration
// Try all three options of expandLast, expandPoint and expandFirst

TimeFrameSet( inMonthly );
OpenM = Open;
CloseM = Close;
TimeFrameRestore();

// lets create some variables that will enable the Monthly calculations to be
// available to the Daily charts and Explorations
// Note: expandLast - the compressed value is expanded starting from
// last bar within given period (so for example Monthly close/high/low
// is available on last trading day of month bar)


// the default is expandLast
OpenMonthlyToDaily = TimeFrameExpand( OpenM, inMonthly );
OpenMonthlyToDaily_useExpandPoint = TimeFrameExpand( OpenM, inMonthly, expandPoint );
OpenMonthlyToDaily_useExpandFIRST  = TimeFrameExpand( OpenM, inMonthly, expandFirst  );


CloseMonthlyToDaily = TimeFrameExpand( CloseM, inMonthly );
CloseMonthlyToDaily_useExpandPoint = TimeFrameExpand( CloseM, inMonthly, expandPoint );
CloseMonthlyToDaily_useExpandFIRST = TimeFrameExpand( CloseM, inMonthly, expandFirst );

// Note: expandPoint - the resulting array gets not empty values only for the last bar
// within given period (all remaining bars are Null (empty))

// to help illustrate in our Exploration
// let us identify the Start of a new month
newMonth = Month() != Ref( Month(), -1 );

/////////////////
// Exploration //
/////////////////
Filter = 1;
dynamic_fg_Color = IIf( newMonth, colorWhite, colorDefault );
dynamic_Bk_Color = IIf( newMonth, colorBlue, colorDefault );
AddColumn( newMonth, "New Month", 1.0, dynamic_fg_Color, dynamic_Bk_Color );
AddColumn( Open, "Daily Open" );
AddColumn( Close, "Daily Close" );


AddColumn( OpenMonthlyToDaily, "Monthly OPEN expandLast" );
AddColumn( OpenMonthlyToDaily_useExpandPoint, "Monthly OPEN expandPoint" );
AddColumn( OpenMonthlyToDaily_useExpandFIRST, "Monthly OPEN expandFIRST" );

AddColumn( CloseMonthlyToDaily, "Monthly CLOSE expandLast" );
AddColumn( CloseMonthlyToDaily_useExpandPoint, "Monthly CLOSE expandPoint" );
AddColumn( CloseMonthlyToDaily_useExpandFIRST, "Monthly CLOSE expandFIRST" );

image
In the screen capture I have highlighted in yellow the Monthly Close and you can see how the different options pick it up (after the month is done, it looks very different in the middle of the month, try it and see).

A similar Exploration was run on an earlier thread and it may be worth a look as a learning example too,

Oh wow, after posting all of that stuff, on reading your question a second time I just realized you are looking for a way of delaying your reading?

I am not sure but you could try something like this to "delay" it one bar, or at least see it one bar later

CloseMonthlyToDaily = TimeFrameExpand( CloseM, inMonthly );
DelayItOneBar = Ref(CloseMonthlyToDaily, -1);

image


#5
TimeFrameSet(inMonthly);
mATR = ATR(5);
TimeFrameRestore();
MATR=TimeFrameExpand( mATR, inMonthly, expandLast);
EndOfMonth = Month() != Ref(Month(), -1);
MATR = ValueWhen(EndOfMonth, MATR);
Plot(MATR, "",colorBlue,styleLine);