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)
- Forget what I previously wrote; my brain obviously crashed. Now rebooted.
- ROC=(previous value / current value−1)∗100
Rate of Change (ROC)
- Forget what I previously wrote; my brain obviously crashed. Now rebooted.
- ROC=(previous value / current value−1)∗100
Rate of Change (ROC)
No worries - forced mine to reboot also :)
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello
Has anyone tried to code the Price Rate of Change in an Advisor?
Can you please share the code.