# Intraday ROC from yesterday's close

I would like to get the rate of change from a point in time to certain intraday bars. For example, the ROC from yesterday's close till 7:30, 8:30, 2:30, etc. The problem is the second argument in the ROC function only takes a number, not an array. I originally wrote this for a 1 minute time frame and it worked because I used = when referencing a TimeNum, but it only worked on 1 minute. I then saw I could make it work on many intraday time frames by referencing the first bar prior to a TimeNum. However, I cannot figure out how to use the ROC function. The line of code causing the error is `RODROC = ROC (C, BarsSincePDClose);`
TIA,
James

``````Time = TimeNum();
BI = BarIndex();

EndTime = 150000;
RODTime = 143000;

RODBar = Time < RODTime;
EndBar = Time == 150000;

PDClose = TimeFrameGetPrice("Close",inDaily,-1);
RODClose = ValueWhen( RODBar, C, 1 );
RODChange = RODClose - PDClose;
RODROC = ROC (C, 5);

Filter = EndBar;

AddColumn (PDClose, "Previous Day Close", 1.2);

//SetBarsRequired( -2, -2 );
"Previous Day Close: " + NumToStr( PDClose, 1.2 );
"ROD Close: " + NumToStr( RODClose, 1.2 );
"ROD Change: " + NumToStr( RODChange, 1.2 );
"Bars Since: " + NumToStr( BarsSincePDClose, 1.0 );
"ROD ROC: " + NumToStr( RODROC, 1.2 );
``````

@iwonder I don't trade intraday but I think you should be able to calculate the rate of change without the built in indicator, bar-by-bar each day if you want.

Perhaps something like this

``````// get Previous Daily Close
dClose 	= TimeFrameGetPrice( "Close", inDaily, -1 );

// calculate each bar, the % change since previous daily Close
pctChange = 100 * ( Close - dClose ) / dClose;
``````
1 Like

This won't work because TimeFrameGetPrice returns already expanded data.
ROC needs to work on compressed data to yield daily result.

To calculate one-day rate-of-change (ROC) when input data is intraday, you can use TimeFrameSet, TimeFrameRestore and TimeFrameExpand functions.

First, you need to set the time frame to daily using TimeFrameSet function. Then, you can calculate the rate-of-change using the ROC() function with periods set to 1. After that, you need to restore the original time frame using TimeFrameRestore and expand the ROC array to match the original time frame using TimeFrameExpand. Here's an example code:

``````TimeFrameSet(inDaily); // set time frame to daily
roc_daily = ROC(Close, 1); // calculate 1-day ROC
TimeFrameRestore(); // restore original time frame
roc_intraday = TimeFrameExpand(roc_daily, inDaily); // expand ROC array to match intraday time frame
``````

This code calculates the one-day ROC of the closing prices, expands the ROC array to match the intraday time frame, and stores it in the 'roc_intraday' variable. You can modify it to calculate ROC of other inputs or with different periods.

1 Like

I am not looking for the one-day rate of change. I am looking for the rate of change from point a to point b on any intraday time frame. For example, the ROC from yesterdays close (TimeNum = 150000) to 7:30 AM today. If I only wanted to find one ROC on one timeframe, I could just count the bars. Perhaps this isolated code will explain better:

``````EndBar = Time == 150000;
``````

So regardless if I am in 1, 3, or 5 minute, I will have the bar count to the last EndBar and if I could pass that value to the second argument of the ROC function, I could always get the ROC from the last EndBar or any TimeNum I define.
James

@iwonder what I believe @Tomasz is writing to you is to use the different time intervals available in the MuptiTimeFrame functionality of AmiBroker.

Change it to whatever timeframe you want. If you are still looking for a variable period ROC function, this has been discussed on the forum in the past. Something like this.

``````NewDay 				= datenum() != ref( datenum(), -1 );

function VarROC( array, periods )
{
prev = Ref( array, -periods );
return 100 * ( array - prev ) / prev;
}

``````

So on a 5 minute chart you may see these variable calculations. Or on a 1 minute bar (notice in my default settings, a 5 minute bar has the 09:30 bar ending at the close of the 09:34 bar. Similarly the 5 minute bar that begins with 09:35 ends at the close of the 09:39 bar. Depends on how you have set up your Amibroker)

PS I am just making some educated guesses here as I mentioned before I do not trade intraday, so maybe all the above is wildly incorrect.

2 Likes

Thank you portfoliobuilder, I think this is the answer, a ROC function that accepts an array. Your results are what I am looking for. I knew it was easy, I just got a mind blank because I haven't used the function feature often.

1 Like

Here is a useful post to review.

1 Like

OK, I was referring to reply by @portfoliobuilder suggesting TimeFrameGetPrice.

But if you want intraday change from last day close you can also use ValueWhen, so

``````EndBar = TimeNum() == 150000;
EndValue = ValueWhen( EndBar, Close );
RODROC = 100*(Close - EndValue)/EndValue;
``````

This is shorter and more efficient.

Actually there is a KB article on very similar topic:

1 Like