Search in sources :

Example 36 with SecurityPrice

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));
}
Also used : SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) LocalDate(java.time.LocalDate)

Example 37 with SecurityPrice

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);
}
Also used : SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) LocalDate(java.time.LocalDate)

Example 38 with SecurityPrice

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));
}
Also used : ChartLineSeriesAxes(name.abuchen.portfolio.ui.views.ChartLineSeriesAxes) SimpleMovingAverage(name.abuchen.portfolio.ui.views.SimpleMovingAverage) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) Security(name.abuchen.portfolio.model.Security) LocalDate(java.time.LocalDate) Test(org.junit.Test)

Example 39 with SecurityPrice

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));
}
Also used : ChartLineSeriesAxes(name.abuchen.portfolio.ui.views.ChartLineSeriesAxes) SimpleMovingAverage(name.abuchen.portfolio.ui.views.SimpleMovingAverage) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) Security(name.abuchen.portfolio.model.Security) LocalDate(java.time.LocalDate) Date(java.util.Date) LocalDate(java.time.LocalDate) Test(org.junit.Test)

Example 40 with SecurityPrice

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));
}
Also used : ChartLineSeriesAxes(name.abuchen.portfolio.ui.views.ChartLineSeriesAxes) SimpleMovingAverage(name.abuchen.portfolio.ui.views.SimpleMovingAverage) SecurityPrice(name.abuchen.portfolio.model.SecurityPrice) Security(name.abuchen.portfolio.model.Security) LocalDate(java.time.LocalDate) Date(java.util.Date) LocalDate(java.time.LocalDate) Test(org.junit.Test)

Aggregations

SecurityPrice (name.abuchen.portfolio.model.SecurityPrice)52 Security (name.abuchen.portfolio.model.Security)34 LocalDate (java.time.LocalDate)24 PortfolioTransaction (name.abuchen.portfolio.model.PortfolioTransaction)22 ArrayList (java.util.ArrayList)21 Test (org.junit.Test)20 TestCurrencyConverter (name.abuchen.portfolio.TestCurrencyConverter)15 LatestSecurityPrice (name.abuchen.portfolio.model.LatestSecurityPrice)11 Client (name.abuchen.portfolio.model.Client)10 List (java.util.List)7 Values (name.abuchen.portfolio.money.Values)6 OpenDialogAction (name.abuchen.portfolio.ui.dialogs.transactions.OpenDialogAction)6 MessageFormat (java.text.MessageFormat)5 BiFunction (java.util.function.BiFunction)5 Function (java.util.function.Function)5 Collectors (java.util.stream.Collectors)5 AccountTransaction (name.abuchen.portfolio.model.AccountTransaction)5 Taxonomy (name.abuchen.portfolio.model.Taxonomy)5 Watchlist (name.abuchen.portfolio.model.Watchlist)5 Factory (name.abuchen.portfolio.online.Factory)5