Skip to content

Commit 0da2feb

Browse files
committed
Standard deviation due to popular demand.
1 parent 98de52f commit 0da2feb

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

src/org/yccheok/jstock/analysis/StockRelativeHistoryOperator.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public enum Function
4040
MFI,
4141
MACD,
4242
MACDSignal,
43-
MACDHist
43+
MACDHist,
44+
StandardDeviation
4445
}
4546

4647
public enum Type
@@ -188,15 +189,15 @@ public void calculate(StockHistoryServer stockHistoryServer)
188189
return;
189190
}
190191

191-
java.util.List<Stock> stocks = new java.util.ArrayList<Stock>();
192-
java.util.List<Double> values = new java.util.ArrayList<Double>();
192+
java.util.List<Stock> stocks = new java.util.ArrayList<>();
193+
java.util.List<Double> values = new java.util.ArrayList<>();
193194

194195
// For MFI usage.
195-
java.util.List<Double> highs = new java.util.ArrayList<Double>();
196-
java.util.List<Double> lows = new java.util.ArrayList<Double>();
197-
java.util.List<Double> closes = new java.util.ArrayList<Double>();
196+
java.util.List<Double> highs = new java.util.ArrayList<>();
197+
java.util.List<Double> lows = new java.util.ArrayList<>();
198+
java.util.List<Double> closes = new java.util.ArrayList<>();
198199
// TODO: CRITICAL LONG BUG REVISED NEEDED.
199-
java.util.List<Long> volumes = new java.util.ArrayList<Long>();
200+
java.util.List<Long> volumes = new java.util.ArrayList<>();
200201

201202
final int size = stockHistoryServer.size();
202203
/* Fill up stocks. */
@@ -350,6 +351,24 @@ public void calculate(StockHistoryServer stockHistoryServer)
350351
v = TechnicalAnalysis.createMACDFix(values, day).outMACDHist;
351352
break;
352353

354+
case StandardDeviation:
355+
if ((dataSize - 1) <= 0) {
356+
Object oldValue = this.value;
357+
this.value = null;
358+
if (Utils.equals(oldValue, value) == false) {
359+
this.firePropertyChange("value", oldValue, this.value);
360+
}
361+
return;
362+
}
363+
364+
double mean = average(values);
365+
double squareSum = 0.0;
366+
for (Double _value : values) {
367+
squareSum += Math.pow(_value - mean, 2);
368+
}
369+
v = Math.sqrt((squareSum) / (dataSize - 1));
370+
break;
371+
353372
default:
354373
assert(false);
355374
}

0 commit comments

Comments
 (0)