addfirst() or removelast() issue in list for averaging

B137 internal Final: Intel Mac




I have been developing a custom indicator that detects fractals in price data and calculates a rolling average of the last `N` fractals. For this purpose, I have been using a `LinkedList` to store the most recent `N` fractals, adding new ones at the front and removing old ones from the end. It then calculates the average based on the fractals in the list.



However, I have encountered a strange issue with the averaging appearing to be incorrect. For de-bugging purposes I set the N value to 2 (no. of fractals to average) and after about 12 rounds of adding and removing fractals correctly, the indicator starts behaving incorrectly. It seems to start adding the new fractal to both slots in the list, effectively making the average the same as the value of the current fractal from that point onwards. 


If viewing the trades on the chart in AlgoWizard the values for the fractals and averages can be seen on the chart and the error is observable.



The error happens on all data so it doesn't matter which instrument you run the indicator on in AlgoWizard.


Im assuming this is the problematic section of the code:




if(bFoundHigh) {
    if(countUp == AvgLength){
        totalUp -= upFractals.removeLast();
        countUp--;
    }
    totalUp += dCurrentHigh;
    countUp++;
    upFractals.addFirst(dCurrentHigh);
    Up.set(0, dCurrentHigh);
}
AvgUp.set(0, countUp > 0 ? totalUp / countUp : 0);




I have tried several methods to debug this issue, including tracking the size of the list and the values being added and removed. The size of the list appears to be correct, but the values being added and removed start being incorrect after around 12 rounds. 


I have also tried using an `ArrayList` instead of a `LinkedList`, but the issue persists. 


The issue seems to occur regardless of the specific values of the fractals or the `AvgLength` parameter but is most prominent for de-bugging when at 2.





Attachments
FractalSR.java
(3.08 KiB)
  • Votes +1
  • Project Extending SQ
  • Type Bug
  • Status New
  • Priority Normal

History

JH
#1

Jabezz

25.07.2023 13:47

Task created

JH
#2

Jabezz

25.07.2023 13:52
Voted for this task.

Votes: +1

Drop files to upload

or

choose files

Max size: 5MB

Not allowed: exe, msi, application, reg, php, js, htaccess, htpasswd, gitignore

...
Wait please