# Bollinger bands

***

This example computes Bollinger bands using a custom-provided function packaged as a jar using the Joule SDK.

## Use case configuration

*File:* app-bollingerbands-influxdb.env

```bash
SOURCEFILE=conf/sources/stockQuoteStream.yaml
ENGINEFILE=conf/usecases/slidingEventWindowBollingerBandsAnalytics.yaml
PUBLISHFILE=conf/publishers/influxdbBollingerBands.yaml
```

## Pipeline configuration <a href="#processor-configuration" id="processor-configuration"></a>

This pipeline will compute the [Bolinger Bands](https://www.investopedia.com/terms/b/bollingerbands.asp) for the `CVCO` symbol over a sliding window of 5 events.

```yaml
processing unit:
  pipeline:
    - filter:
      expression: "symbol == 'CVCO'"
      - user defined function:
          bollinger bands:
            parameters:
              deviations: 2
          fields: [ ask ]
          event history: 5

emit:
  select: "symbol, 
               ask_upper_BollingerBand, 
               ask_middle_BollingerBand, 
               ask_lower_BollingerBand"
```

## Function Implementation

This function uses the same implementation pattern as the EMA function.

{% code overflow="wrap" %}

```java
@AnalyticDefinition(
        id = "bollingerband",
        stateless = true,
        useRawColumn = true,
        description = "Bollinger Bands are a momentum indicator used in technical analysis."
)
@JsonRootName(value = "bollinger bands")
public class BollingerBands extends AnalyticsFunction<Map<String, Double>>  {

    private int deviations = 2;
    public BollingerBands(){
        super();
    }

    @Override
    public Map<String, Double> compute(Number[] values, Number previousValue, Context context) {

        Double mean = Arrays.stream(values).mapToDouble(d-> (double) d).sum() / values.length;
        double sqrtmean = 0.0;
        for(int i=0; i<values.length; i++){
            double s =  values[i].doubleValue() - mean;
            sqrtmean += s * s;
        }

        sqrtmean = Math.sqrt( sqrtmean / values.length);
        double band = deviations * sqrtmean;

        Map<String, Double> results = new HashMap<>();
        results.put("upper", mean + band);
        results.put("middle", mean );
        results.put("lower", mean - band);
        return results;
    }

    @Override
    public void setParameters(Properties parameters) {
        if( parameters != null && parameters.containsKey("deviations")) {
            deviations = Integer.parseInt(parameters.get("deviations").toString());
        }
    }

    @Override
    public String getVariablePostFixID() {
        return "bollingerband";
    }
```

{% endcode %}

For Joule to load and initialised the component the analytics class needs to be defined within the `plugins.properties` file under the `META-INF/services` directory

#### Example

```properties
# Change and add lines for your analytics classes
com.fractalworks.examples.banking.analytics.BollingerBands
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.fractalworks.io/joule/use-case-examples/banking/bollinger-bands.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
