Percentage change formula implementation help

Hi,

I am a relatively new AFL proogrammer and very much appreciate the flexibility offered by this platform programmatically.

However, I have not been able to print the output of daily per_change_since_open formula i.e calculate every 1 min change from last day's close

//Percentage change since open
TimeFrameSet(inDaily);
prevD_close=Ref(Close,-1);
TimeFrameRestore();

//Expanding the timeframe
prevD_close_exp=TimeFrameExpand(prevD_close,inDaily);

Filter=1;
per_change=100*(Close-prevD_close_exp)/prevD_close_exp;
AddColumn(per_change,"Percentage change since open",1.2);

My current time frame is 1min (Intraday). I read previous posts and have avoided the TimeFrameExpand mistake. Still I am not able to get this right.

Here is the output I get in exploration window

Exploration_Output

Can some one please help me with what I am missing or why it is not working?

Thanks
Rahul

Hi,

Writing this down for any future users who may stumble on the same issue.
I figured the reason myself. The issue was that

prevD_close_exp if simply referred as what I wrote above will be an intraday long array with Null value in all indices except for the at BarCount-1.So one need to explicitly refer the value at BarCount-1 and subtract it from Close(1min array) like one would subtract a constant

per_change=100*(Close- prevD_close_exp[BarCount-1])/prevD_close_exp[BarCount-1];

Good Luck coding!!

@ramo1986, Welcome to the Forum.

Glad you figured out your problem. Much better learning the lesson.

Next you need to get your "Verified Badge". Search it and follow the instructions.

2 Likes

@ramo1986 there are usually multiple paths to achieving your goals in AmiBroker. One alternative for you to consider,

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

// calculate the % change since that previous daily Close
pctChange = 100 * ( Close - dClose ) / dClose;

Resulting in this type of calculation,
image

6 Likes

Thank you @snoopy.pa30. Will check for the instruction.

Thanks @portfoliobuilder. Your solution definitely look cleaner and simpler than mine :slight_smile: