use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class BollingerBands method calculateBollingerBands.
/**
* Calculates the Simple Moving Average for the given range of days from the
* given startDate on. The method returns an object containing the X and Y
* Axes of the generated bollinger bands
*/
private void calculateBollingerBands() {
if (security == null)
return;
this.prices = security.getPricesIncludingLatest();
int index;
SecurityPrice startPrice = null;
if (prices == null || prices.size() < BollingerBandsDays + 3)
return;
if (startDate == null) {
startPrice = this.getStartPrice();
// in case no valid start date could be determined, return null
if (startPrice == null)
return;
index = prices.indexOf(startPrice);
if (index >= prices.size())
return;
} else {
startPrice = this.getStartPriceFromStartDate();
// in case no valid start date could be determined, return null
if (startPrice == null)
return;
index = prices.indexOf(startPrice);
if (index >= prices.size())
return;
}
for (; index < prices.size(); index++) {
if (index < BollingerBandsDays)
continue;
LocalDate nextDate = prices.get(index).getDate();
LocalDate isBefore = nextDate.plusDays(1);
LocalDate isAfter = prices.get(index - BollingerBandsDays + 2).getDate();
List<SecurityPrice> filteredPrices = this.getFilteredList(isBefore, isAfter);
if (filteredPrices.size() < calculatedMinimumDays)
// skip this date and try to calculate bollinger bands for next
continue;
// entry
double sum = filteredPrices.stream().mapToLong(SecurityPrice::getValue).sum();
double QuotePriceAverage = sum / Values.Quote.divider() / filteredPrices.size();
double tempQuotePriceVariance = 0;
for (int i = 0; i < filteredPrices.size(); i++) {
tempQuotePriceVariance += ((filteredPrices.get(i).getValue() / Values.Quote.divider()) - QuotePriceAverage) * ((filteredPrices.get(i).getValue() / Values.Quote.divider()) - QuotePriceAverage);
}
Double StandardDeviationBollingerBands = Math.sqrt(tempQuotePriceVariance / (filteredPrices.size() - 1)) * BollingerBandsFactor;
Double valueBollingerBandsLowerBands = QuotePriceAverage;
Double valueBollingerBandsUpperBands = QuotePriceAverage;
valueBollingerBandsLowerBands -= StandardDeviationBollingerBands;
valueBollingerBandsUpperBands += StandardDeviationBollingerBands;
valuesBollingerBandsLowerBands.add(valueBollingerBandsLowerBands);
valuesBollingerBandsMiddleBands.add(QuotePriceAverage);
valuesBollingerBandsUpperBands.add(valueBollingerBandsUpperBands);
datesBollingerBands.add(prices.get(index).getDate());
}
LocalDate[] tmpDates = datesBollingerBands.toArray(new LocalDate[0]);
Double[] tmpPricesLower = valuesBollingerBandsLowerBands.toArray(new Double[0]);
Double[] tmpPricesMiddle = valuesBollingerBandsMiddleBands.toArray(new Double[0]);
Double[] tmpPricesUpper = valuesBollingerBandsUpperBands.toArray(new Double[0]);
this.BollingerBandsLowerBand.setDates(TimelineChart.toJavaUtilDate(tmpDates));
this.BollingerBandsLowerBand.setValues(ArrayUtils.toPrimitive(tmpPricesLower));
this.BollingerBandsMiddleBand.setDates(TimelineChart.toJavaUtilDate(tmpDates));
this.BollingerBandsMiddleBand.setValues(ArrayUtils.toPrimitive(tmpPricesMiddle));
this.BollingerBandsUpperBand.setDates(TimelineChart.toJavaUtilDate(tmpDates));
this.BollingerBandsUpperBand.setValues(ArrayUtils.toPrimitive(tmpPricesUpper));
}
use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class BollingerBands method determineStartPrice.
private SecurityPrice determineStartPrice(LocalDate BollingerBandsPeriodEnd) {
// check if an bollinger bands can be calculated for this Date
List<SecurityPrice> filteredPrices = null;
LocalDate isBefore = BollingerBandsPeriodEnd.plusDays(1);
LocalDate isAfter = BollingerBandsPeriodEnd.minusDays(BollingerBandsDays);
LocalDate lastDate = prices.get(prices.size() - 1).getDate();
filteredPrices = this.getFilteredList(isBefore, isAfter);
int i = 1;
while (!this.checkListIsValidForBollingerBands(filteredPrices)) {
if (isBefore.plusDays(i).isAfter(lastDate) || isAfter.plusDays(i).isAfter(lastDate))
return null;
filteredPrices = this.getFilteredList(isBefore.plusDays(i), isAfter.plusDays(i));
i++;
}
return filteredPrices.get(filteredPrices.size() - 1);
}
use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class SimpleMovingAverageTest method testSufficientPriceDataPass.
@Test
public void testSufficientPriceDataPass() {
Security security = new Security();
LocalDate date = LocalDate.parse("2016-01-01");
for (int ii = 0; ii < 300; ii++) {
security.addPrice(new SecurityPrice(date, Values.Quote.factorize(10)));
date = date.plusDays(1);
}
ChartLineSeriesAxes SMALines = new SimpleMovingAverage(10, security, null).getSMA();
assertThat(SMALines, is(IsNull.notNullValue()));
assertThat(SMALines.getValues().length, is(security.getPrices().size() - 10 + 1));
}
use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class SimpleMovingAverageTest method testSecurityHasSparsePrice.
@Test
public void testSecurityHasSparsePrice() {
Security security = new Security();
LocalDate date = LocalDate.parse("2016-01-01");
for (int ii = 0; ii < 100; ii++) {
security.addPrice(new SecurityPrice(date, Values.Quote.factorize(10)));
date = date.plusDays(1);
}
security.addPrice(new SecurityPrice(LocalDate.parse("2017-01-01"), Values.Quote.factorize(12)));
LocalDate tmp = LocalDate.parse("2016-01-01");
tmp = tmp.plusDays(99);
Date lastSMADate = java.sql.Date.valueOf(tmp);
ChartLineSeriesAxes SMALines = new SimpleMovingAverage(10, security, null).getSMA();
assertThat(SMALines.getDates(), is(IsNull.notNullValue()));
assertThat(SMALines.getDates()[SMALines.getDates().length - 1], is(lastSMADate));
}
use of name.abuchen.portfolio.model.SecurityPrice in project portfolio by buchen.
the class SimpleMovingAverageTest method testSufficientPriceDataStartDate.
@Test
public void testSufficientPriceDataStartDate() {
Security security = new Security();
LocalDate date = LocalDate.parse("2016-01-01");
for (int ii = 0; ii < 300; ii++) {
security.addPrice(new SecurityPrice(date, Values.Quote.factorize(10)));
date = date.plusDays(1);
}
LocalDate startDate = LocalDate.parse("2016-06-01");
Date isStartDate = java.sql.Date.valueOf(startDate);
ChartLineSeriesAxes SMALines = new SimpleMovingAverage(10, security, startDate).getSMA();
assertThat(SMALines, is(IsNull.notNullValue()));
assertThat(SMALines.getDates()[0], is(isStartDate));
}
Aggregations