This question is a continuation of this one.
My goal is to find the turning points in stock price data.
So far I:
Tried differentiating the smoothed pri
That's works Patrick87, Thanks. Following are java function to implement the same:
Assume StockPrices has a map of key date and value StockPrice (price, average where x = 5)
private double getCx(StockPrices stockPrices, LocalDate executionDate, int x, double m) { return Math.abs(getFx(stockPrices, executionDate) - getGx(stockPrices, executionDate)) - m * getHx(stockPrices, executionDate, x); }
private double getGx(StockPrices stockPrices, LocalDate executionDate) {
return stockPrices.getAvg(executionDate, 5);
}
private double getFx(StockPrices stockPrices, LocalDate executionDate) {
return stockPrices.getPrice(executionDate);
}
public double getHx(StockPrices stockPrice, LocalDate localDate, int x) {
//standard deviation
return Math.sqrt(getVariance(stockPrice, localDate, x));
}
private double getVariance(StockPrices stockPrice, LocalDate localDate, int x) {
double sum = 0;
int count = 0;
for (int i = - (x / 2); i <= (x / 2) ; i++) {
LocalDate date = localDate.with(BusinessDay.add(localDate, i, stockPrice.getPriceMap(), 2));
double avg = stockPrice.getAvg(date, 5);
double price = stockPrice.getPrice(date);
if (price != 0.0) {
sum += Math.pow((price - avg), 2);
count++;
}
}
return sum / count;
}