Skip to content

Commit

Permalink
Fixed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlam604 committed Feb 18, 2017
1 parent 85c93ef commit c69cff6
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 51 deletions.
34 changes: 12 additions & 22 deletions src/com/jasonlam604/stocktechnicals/util/HighestHigh.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package com.jasonlam604.stocktechnicals.util;

import java.util.stream.IntStream;

import org.apache.commons.lang3.ArrayUtils;

public class HighestHigh {

private Double[] doubles;

private int index;

private double value;
Expand All @@ -21,22 +15,18 @@ public class HighestHigh {
*/
public void find(double[] high, int startIndex, int endIndex) {

this.doubles = ArrayUtils.toObject(high);

if(startIndex < 0 || startIndex > endIndex)
startIndex = 0;

if(endIndex < startIndex || endIndex > (doubles.length - 1))
endIndex = doubles.length - 1;

IntStream.range(startIndex, endIndex).parallel().reduce((a, b) -> doubles[a] < doubles[b] ? b : a)
.ifPresent(ix -> setValues(ix));

}

private void setValues(int ix) {
this.index = ix;
this.value = doubles[ix];
this.value = high[startIndex];
this.index = startIndex;

for (int i=startIndex; i < endIndex+startIndex; i++) {

if (high[i] > this.value) {

this.value = high[i];
this.index = i;

}
}
}

public double getValue() {
Expand Down
40 changes: 15 additions & 25 deletions src/com/jasonlam604/stocktechnicals/util/LowestLow.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
package com.jasonlam604.stocktechnicals.util;

import java.util.stream.IntStream;

import org.apache.commons.lang3.ArrayUtils;

public class LowestLow {

private Double[] doubles;

private int index;

private double value;

/**
* Finds the lowest low from index 0 and to the indicated give range index
*
* @param high
* @param low
* array of high values
* @param range
*/
public void find(double[] high, int startIndex, int endIndex) {

this.doubles = ArrayUtils.toObject(high);

if(startIndex < 0 || startIndex > endIndex)
startIndex = 0;

if(endIndex < startIndex || endIndex > (doubles.length - 1))
endIndex = doubles.length - 1;

IntStream.range(startIndex, endIndex).parallel().reduce((a, b) -> doubles[a] > doubles[b] ? b : a)
.ifPresent(ix -> setValues(ix));

}

private void setValues(int ix) {
this.index = ix;
this.value = doubles[ix];
public void find(double[] low, int startIndex, int endIndex) {

this.value = low[startIndex];
this.index = startIndex;

for (int i=startIndex; i < endIndex+startIndex; i++) {

if (low[i] < this.value) {

this.value = low[i];
this.index = i;

}
}
}

public double getValue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void testHighestHighIndexZeroToTwo() {

@Test
public void testHighestHighIndexTwoToFour() {
double[] values = { 13.3, 19.9, 10, 21, 7.7 };
double[] values = { 13.3, 19.9, 10, 21, 7.7,9.9,12.4 };

HighestHigh highestHigh = new HighestHigh();
highestHigh = new HighestHigh();
Expand All @@ -28,10 +28,10 @@ public void testHighestHighIndexTwoToFour() {

@Test
public void testHighestHighUsingValueSize() {
double[] values = { 13.3, 19.9, 10, 21, 7.7 };
double[] values = { 13.3, 19.9, 10, 21, 7.7,9.9,12.4 };

HighestHigh highestHigh = new HighestHigh();
highestHigh.find(values, 0, 20);
highestHigh.find(values, 0, 5);
Assert.assertEquals(21, highestHigh.getValue(), 0);
Assert.assertEquals(3, highestHigh.getIndex(), 0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void testLowestHighIndexTwoToFour() {
double[] values = { 13.3, 19.9, 10, 21, 7.7 };

LowestLow lowestLow = new LowestLow();
lowestLow.find(values, 2, 4);
lowestLow.find(values, 2, 2);

Assert.assertEquals(10.0, lowestLow.getValue(), 0);
Assert.assertEquals(2, lowestLow.getIndex(), 0);
Expand Down

0 comments on commit c69cff6

Please sign in to comment.