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);
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.
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:
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.
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.
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.