Git Product home page Git Product logo

Comments (6)

denernun avatar denernun commented on August 20, 2024 1

from technicalindicators.

episage avatar episage commented on August 20, 2024

Also, SMA relies on approximate, running algorithm. It is efficient but the results will differ when compared to other software.

from technicalindicators.

quanxufeng avatar quanxufeng commented on August 20, 2024

When calling SMA or other indicators relying on LinkedList the LinkedList items never get garbage collected. This is due to the structure of LinkedList that always contains reference to the next and previous element.

In NodeJS you must remove reference to the unused items for them to be disposed by GC.

so how can I solve this problem? could you please tell me How can I modify the code to fix it?

from technicalindicators.

episage avatar episage commented on August 20, 2024

@quanxufeng

To solve this problem, you can create a custom LinkedList class that has built-in garbage collection support. This class will automatically remove references to unused items, allowing them to be properly disposed of by the garbage collector. Here's an example implementation:

class GCLinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
    this.length = 0;
  }

  append(value) {
    const newNode = { value, prev: null, next: null };

    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      newNode.prev = this.tail;
      this.tail.next = newNode;
      this.tail = newNode;
    }

    this.length++;
    return this;
  }

  remove(value) {
    let currentNode = this.head;

    while (currentNode) {
      if (currentNode.value === value) {
        if (currentNode.prev) {
          currentNode.prev.next = currentNode.next;
        } else {
          this.head = currentNode.next;
        }

        if (currentNode.next) {
          currentNode.next.prev = currentNode.prev;
        } else {
          this.tail = currentNode.prev;
        }

        currentNode.prev = null;
        currentNode.next = null;

        this.length--;
        return currentNode.value;
      }

      currentNode = currentNode.next;
    }

    return null;
  }

  removeFirst() {
    if (!this.head) {
      return null;
    }

    const removedNode = this.head;
    this.head = removedNode.next;

    if (this.head) {
      this.head.prev = null;
    } else {
      this.tail = null;
    }

    removedNode.next = null;
    this.length--;

    return removedNode.value;
  }

  removeLast() {
    if (!this.tail) {
      return null;
    }

    const removedNode = this.tail;
    this.tail = removedNode.prev;

    if (this.tail) {
      this.tail.next = null;
    } else {
      this.head = null;
    }

    removedNode.prev = null;
    this.length--;

    return removedNode.value;
  }
}

Now, you can use this custom LinkedList class to store your SMA or other indicator values. Whenever you no longer need a value, call the remove or removeFirst method, depending on your use case, to remove it from the list. This will ensure that the garbage collector disposes of the unused items properly.

For example:

const smaList = new GCLinkedList();

// Add new SMA values
smaList.append(smaValue);

// Remove old SMA values when no longer needed
smaList.removeFirst();

By managing your LinkedList items this way, you can ensure that unused items are correctly garbage collected, preventing memory leaks and improving your application's performance.

from technicalindicators.

quanxufeng avatar quanxufeng commented on August 20, 2024

@quanxufeng

To solve this problem, you can create a custom LinkedList class that has built-in garbage collection support. This class will automatically remove references to unused items, allowing them to be properly disposed of by the garbage collector. Here's an example implementation:

class GCLinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
    this.length = 0;
  }

  append(value) {
    const newNode = { value, prev: null, next: null };

    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      newNode.prev = this.tail;
      this.tail.next = newNode;
      this.tail = newNode;
    }

    this.length++;
    return this;
  }

  remove(value) {
    let currentNode = this.head;

    while (currentNode) {
      if (currentNode.value === value) {
        if (currentNode.prev) {
          currentNode.prev.next = currentNode.next;
        } else {
          this.head = currentNode.next;
        }

        if (currentNode.next) {
          currentNode.next.prev = currentNode.prev;
        } else {
          this.tail = currentNode.prev;
        }

        currentNode.prev = null;
        currentNode.next = null;

        this.length--;
        return currentNode.value;
      }

      currentNode = currentNode.next;
    }

    return null;
  }

  removeFirst() {
    if (!this.head) {
      return null;
    }

    const removedNode = this.head;
    this.head = removedNode.next;

    if (this.head) {
      this.head.prev = null;
    } else {
      this.tail = null;
    }

    removedNode.next = null;
    this.length--;

    return removedNode.value;
  }

  removeLast() {
    if (!this.tail) {
      return null;
    }

    const removedNode = this.tail;
    this.tail = removedNode.prev;

    if (this.tail) {
      this.tail.next = null;
    } else {
      this.head = null;
    }

    removedNode.prev = null;
    this.length--;

    return removedNode.value;
  }
}

Now, you can use this custom LinkedList class to store your SMA or other indicator values. Whenever you no longer need a value, call the remove or removeFirst method, depending on your use case, to remove it from the list. This will ensure that the garbage collector disposes of the unused items properly.

For example:

const smaList = new GCLinkedList();

// Add new SMA values
smaList.append(smaValue);

// Remove old SMA values when no longer needed
smaList.removeFirst();

By managing your LinkedList items this way, you can ensure that unused items are correctly garbage collected, preventing memory leaks and improving your application's performance.

Hi, episage, thanks for your reply, is there a method or a way that I can directly remove values or empty the list? can I access to the reference of the values list in this library?

from technicalindicators.

episage avatar episage commented on August 20, 2024

Yes, you can add a method to the custom GCLinkedList class to clear the list and remove all references to the values. Here's the updated class with the new clear method:

class GCLinkedList {
  // ... (previous code)

  clear() {
    let currentNode = this.head;

    while (currentNode) {
      let nextNode = currentNode.next;
      currentNode.prev = null;
      currentNode.next = null;
      currentNode = nextNode;
    }

    this.head = null;
    this.tail = null;
    this.length = 0;
  }
}

This clear method iterates through the list, removing references to the prev and next nodes, effectively clearing the list. After calling this method, the garbage collector will be able to dispose of the unused items properly. To use the clear method, simply call it on your GCLinkedList instance:

const smaList = new GCLinkedList();

// Add new SMA values
smaList.append(smaValue);

// Clear the list and remove all references
smaList.clear();

Regarding direct access to the values list, the custom GCLinkedList class does not store the values in a separate list or array, as it uses a linked list structure with nodes referencing each other. However, you can create a method to retrieve all values as an array:

class GCLinkedList {
  // ... (previous code)

  getValues() {
    const values = [];
    let currentNode = this.head;

    while (currentNode) {
      values.push(currentNode.value);
      currentNode = currentNode.next;
    }

    return values;
  }
}

Now you can access the list values as an array:

const smaList = new GCLinkedList();

// Add new SMA values
smaList.append(smaValue);

// Get the values as an array
const valuesArray = smaList.getValues();
console.log(valuesArray);

This will output an array containing the values of the nodes in the list.

from technicalindicators.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.