Rate of Change

 

Hello

Has anyone tried to code the  Price Rate of Change in an Advisor?

Can you please share the code.

 
William Roeder #:
When in doubt: think!
double roc = ( price[i] - price[j] ) / (i-j+1);

Somehow I felt this code it not right - provides incorrect results and risks divide by 0.

Maybe I am wrong but I would have done this

roc = (i-j != 0)? (price[i] - price[j]) / MathAbs(i - j) : 0;

Some test results to explain my thinking:

2022.08.10 17:09:54.770 roc = (price[i] - price[j]) / (i - j + 1)

2022.08.10 17:09:54.770 i=0 j=0 (i-j+1)=1 ROC = 0.00000 price[i]=1.3000 price[j]=1.3000 (price[i]-price[j]=0.0000)

2022.08.10 17:09:54.770 i=1 j=0 (i-j+1)=2 ROC = 0.00005 price[i]=1.3001 price[j]=1.3000 (price[i]-price[j]=0.0001)

2022.08.10 17:09:54.770 i=2 j=0 (i-j+1)=3 ROC = 0.00007 price[i]=1.3002 price[j]=1.3000 (price[i]-price[j]=0.0002)

2022.08.10 17:09:54.770 i=3 j=0 (i-j+1)=4 ROC = 0.00007 price[i]=1.3003 price[j]=1.3000 (price[i]-price[j]=0.0003)

2022.08.10 17:09:54.770

2022.08.10 17:09:54.770 roc = (i-j != 0)? (price[i] - price[j]) / MathAbs(i - j) : 0

2022.08.10 17:09:54.770 i=0 j=0 (i-j)=0 ROC = 0.00000 price[i]=1.3000 price[j]=1.3000 (price[i]-price[j]=0.0000)

2022.08.10 17:09:54.770 i=1 j=0 (i-j)=1 ROC = 0.00010 price[i]=1.3001 price[j]=1.3000 (price[i]-price[j]=0.0001)

2022.08.10 17:09:54.770 i=2 j=0 (i-j)=2 ROC = 0.00010 price[i]=1.3002 price[j]=1.3000 (price[i]-price[j]=0.0002)

2022.08.10 17:09:54.770 i=3 j=0 (i-j)=3 ROC = 0.00010 price[i]=1.3003 price[j]=1.3000 (price[i]-price[j]=0.0003)


 
  1. Forget what I previously wrote; my brain obviously crashed. Now rebooted.
  2. ROC=(previous value / current value1)100
              Rate of Change (ROC)
 
William Roeder #:
  1. Forget what I previously wrote; my brain obviously crashed. Now rebooted.
  2. ROC=(previous value / current value1)100
              Rate of Change (ROC)

No worries - forced mine to reboot also :)

Reason: