Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VWMA #133

Open
intothemoonlite opened this issue Sep 1, 2021 · 0 comments
Open

VWMA #133

intothemoonlite opened this issue Sep 1, 2021 · 0 comments

Comments

@intothemoonlite
Copy link

intothemoonlite commented Sep 1, 2021

@jbax I'm trying to add the VWMA indicator but I'm not able to get it to work just yet. My code is below:

public class VolumeWeightedMovingAverage extends SingleValueIndicator {

  private double value = 0;
  private int length = 0;
  private final CircularList price;
  private final CircularList volume;

  public VolumeWeightedMovingAverage(int length, TimeInterval interval) {
    this(length, interval, c -> c.close);
  }

  public VolumeWeightedMovingAverage(int length, TimeInterval timeInterval, ToDoubleFunction<Candle> valueGetter) {
    super(timeInterval, valueGetter);
    this.length = length;
    price = new CircularList(length);
    volume = new CircularList(length);
  }

  @Override
  protected Indicator[] children() {
    return new Indicator[0];
  }

  @Override
  protected boolean process(Candle candle, double value, boolean updating) {
    if (updating) {
      price.update(candle.close);
      volume.update(candle.volume);
    } else {
      price.add(candle.close);
      volume.add(candle.volume);
    }
    this.value = this.calculate(updating);
    return true;
  }

  private double calculate(boolean updating) {
    if (getAccumulationCount() <= this.length) {
      return 0;
    }

    double top = 0;
    double bottom = 0;
    int index = 0;
    for (double px : price.values) {
      top += price.get(index) * volume.get(index);
      bottom += volume.get(index);
      index++;
    }
    return top/bottom;
  }

  @Override
  public double getValue() {
    return this.value;
  }

  @Override
  public String signalDescription() {
    return super.signalDescription();
  }
}

I see there are various classes to extend for implementation but not too sure what to use for this indicator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant