# Finance

Daily closing prices fall for 300 days and then rise. A golden cross is the 50-day simple moving average of the price crossing above the 200-day one, a death cross crossing below it.

Parameters (in the page address as ?fast=…):

- Fast average over (`fast`): 5 to 150 Samples in steps of 5, default 50
- Slow average over (`slow`): 50 to 400 Samples in steps of 10, default 200

The site cannot run the default parameters: (JavaScript) TypeError: Cannot read properties of undefined (reading 'length')

Java source at the default parameters:

```java
import io.github.romanhaendler.rbed.*;
import java.util.Map;

Channel price = Channel.named("price");

Rule deathCross = Rule.named("DeathCross").when(
    price.averageOverSamples(50).below(price.averageOverSamples(200)));
Rule goldenCross = Rule.named("GoldenCross").when(
    price.averageOverSamples(50).above(price.averageOverSamples(200)));
Detector detector = Detector.builder()
    .rules(deathCross, goldenCross)
    .build();

for (int day = 0; day < 600; day++) {   // daily closing prices: falling, then rising from day 300
  double close = day < 300 ? 200 - day / 3.0 : 100 + (day - 300) / 2.0;
  detector.push(day * 86_400_000L, Map.of(price, close));
}
detector.close();
```

## The Rules

- **DeathCross**: the fast average below the slow one.
- **GoldenCross**: the fast average above the slow one.

`averageOverSamples(n)` is defined once `n` values exist, so the first Event starts once the longer of the two averages exists: on day 199 here, later with a longer one.

At the defaults, `DeathCross` runs from day 199 to day 366. It is Truncated at its start: when both averages first exist, on day 199, the 50-day one is already below, so nobody knows when it crossed. `GoldenCross` follows from day 366, the day the example also finds by brute force over the two averages. It still holds when the data ends on day 599, so its end is Truncated.

### Things to try

- Raise *Slow average over* to 300 Samples: the slow average first exists on day 299, so `DeathCross` starts there, Truncated, and `GoldenCross` follows on day 388.

## Reference

The example is [TradingSignalsExample](https://github.com/romanhaendler/rbed/blob/main/src/test/java/io/github/romanhaendler/rbed/examples/TradingSignalsExample.java), a test with its expected Events, listed in the README's [Examples](https://github.com/romanhaendler/rbed#examples).

Methods used: [`Channel.named`](https://romanhaendler.github.io/rbed/api/io.github.romanhaendler.rbed/io/github/romanhaendler/rbed/Channel.html#named%28java.lang.String%29), [`Rule.named`](https://romanhaendler.github.io/rbed/api/io.github.romanhaendler.rbed/io/github/romanhaendler/rbed/Rule.html#named%28java.lang.String%29), [`Rule.when`](https://romanhaendler.github.io/rbed/api/io.github.romanhaendler.rbed/io/github/romanhaendler/rbed/Rule.html#when%28io.github.romanhaendler.rbed.Condition%29), [`ChannelExpression.averageOverSamples`](https://romanhaendler.github.io/rbed/api/io.github.romanhaendler.rbed/io/github/romanhaendler/rbed/ChannelExpression.html#averageOverSamples%28int%29), [`Expression.below`](https://romanhaendler.github.io/rbed/api/io.github.romanhaendler.rbed/io/github/romanhaendler/rbed/Expression.html#below%28io.github.romanhaendler.rbed.Expression%29), [`Expression.above`](https://romanhaendler.github.io/rbed/api/io.github.romanhaendler.rbed/io/github/romanhaendler/rbed/Expression.html#above%28io.github.romanhaendler.rbed.Expression%29), [`Detector.builder`](https://romanhaendler.github.io/rbed/api/io.github.romanhaendler.rbed/io/github/romanhaendler/rbed/Detector.html#builder%28%29), [`Detector.Builder.rules`](https://romanhaendler.github.io/rbed/api/io.github.romanhaendler.rbed/io/github/romanhaendler/rbed/Detector.Builder.html#rules%28io.github.romanhaendler.rbed.Rule...%29), [`Detector.Builder.build`](https://romanhaendler.github.io/rbed/api/io.github.romanhaendler.rbed/io/github/romanhaendler/rbed/Detector.Builder.html#build%28%29), [`Detector.push`](https://romanhaendler.github.io/rbed/api/io.github.romanhaendler.rbed/io/github/romanhaendler/rbed/Detector.html#push%28long,java.util.Map%29), [`Detector.close`](https://romanhaendler.github.io/rbed/api/io.github.romanhaendler.rbed/io/github/romanhaendler/rbed/Detector.html#close%28%29).

The concepts at work, each on a page of its own: [Conditions and Expressions](https://romanhaendler.github.io/rbed/conditions-and-expressions/), [The Event lifecycle](https://romanhaendler.github.io/rbed/event-lifecycle/).
