Skip to content

Commit 2b1a694

Browse files
committed
update bollinger docs
1 parent f25e9f3 commit 2b1a694

File tree

2 files changed

+13
-32
lines changed

2 files changed

+13
-32
lines changed

docs/marks/bollinger.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const k = ref(2);
1212

1313
# Bollinger mark <VersionBadge version="0.6.10" pr="1772" />
1414

15-
The **bollinger mark** is a [composite mark](../features/marks.md#marks) consisting of a [line](./line.md) representing a moving average and an [area](./area.md) representing volatility as a band; the band thickness is proportional to the deviation of nearby values. The bollinger mark is often used to analyze the price of financial instruments such as stocks.
15+
The **bollinger mark** is a [composite mark](../features/marks.md#marks) consisting of a [line](./line.md) representing a moving average and an [area](./area.md) representing volatility as a band; the band thickness is proportional to the deviation of nearby values. The bollinger mark is often used to [analyze the price](https://en.wikipedia.org/wiki/Bollinger_Bands) of financial instruments such as stocks.
1616

1717
For example, the chart below shows the price of Apple stock from 2013 to 2018, with a window size *n* of {{n}} days and radius *k* of {{k}} standard deviations.
1818

docs/transforms/map.md

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,8 @@ import * as d3 from "d3";
55
import {ref} from "vue";
66
import aapl from "../data/aapl.ts";
77

8-
const N = ref(20);
9-
const K = ref(2);
10-
11-
function bollingerBandY(N, K, options) {
12-
return Plot.map({y1: bollinger(N, -K), y2: bollinger(N, K)}, options);
13-
}
14-
15-
function bollinger(N, K) {
16-
return Plot.window({k: N, reduce: (Y) => d3.mean(Y) + K * d3.deviation(Y), strict: true, anchor: "end"});
17-
}
8+
const n = ref(20);
9+
const k = ref(2);
1810

1911
</script>
2012

@@ -61,18 +53,18 @@ Is shorthand for this:
6153
Plot.map({y: "cumsum"}, {y: d3.randomNormal()})
6254
```
6355

64-
As a more practical example, we can use the map transform to construct [Bollinger bands](https://en.wikipedia.org/wiki/Bollinger_Bands), showing both the price and volatility of Apple stock.
56+
As a more practical example, we can use the map transform to construct [Bollinger bands](../marks/bollinger.md), showing both the price and volatility of Apple stock.
6557

6658
<p>
6759
<label class="label-input">
68-
<span>Periods (N):</span>
69-
<input type="range" v-model.number="N" min="2" max="100" step="1">
70-
<span style="font-variant-numeric: tabular-nums;">{{N.toLocaleString("en-US")}}</span>
60+
<span>Window size (n):</span>
61+
<input type="range" v-model.number="n" min="1" max="100" step="1" />
62+
<span style="font-variant-numeric: tabular-nums;">{{n.toLocaleString("en-US")}}</span>
7163
</label>
7264
<label class="label-input">
73-
<span>Deviations (K):</span>
74-
<input type="range" v-model.number="K" min="0" max="10" step="0.1">
75-
<span style="font-variant-numeric: tabular-nums;">{{K.toLocaleString("en-US", {minimumFractionDigits: 1})}}</span>
65+
<span>Radius (k):</span>
66+
<input type="range" v-model.number="k" min="0" max="10" step="0.1" />
67+
<span style="font-variant-numeric: tabular-nums;">{{k.toLocaleString("en-US")}}</span>
7668
</label>
7769
</p>
7870

@@ -83,26 +75,15 @@ Plot.plot({
8375
grid: true
8476
},
8577
marks: [
86-
Plot.areaY(aapl, Plot.map({y1: bollinger(N, -K), y2: bollinger(N, K)}, {x: "Date", y: "Close", fillOpacity: 0.2})),
87-
Plot.lineY(aapl, Plot.map({y: bollinger(N, 0)}, {x: "Date", y: "Close", stroke: "blue"})),
78+
Plot.areaY(aapl, Plot.map({y1: Plot.bollinger({n, k: -k}), y2: Plot.bollinger({n, k})}, {x: "Date", y: "Close", fillOpacity: 0.2})),
79+
Plot.lineY(aapl, Plot.map({y: Plot.bollinger({n})}, {x: "Date", y: "Close", stroke: "blue"})),
8880
Plot.lineY(aapl, {x: "Date", y: "Close", strokeWidth: 1})
8981
]
9082
})
9183
```
9284
:::
9385

94-
```js
95-
function bollinger(N, K) {
96-
return Plot.window({
97-
k: N,
98-
reduce: (V) => d3.mean(V) + K * d3.deviation(V),
99-
strict: true,
100-
anchor: "end"
101-
});
102-
}
103-
```
104-
105-
The `bollinger` map method above is implemented atop the [window transform](./window.md), computing the mean of values within the rolling window, and then offsetting the mean by a multiple of the rolling deviation.
86+
The [bollinger map method](../marks/bollinger.md#bollinger) is implemented atop the [window map method](./window.md#window), computing the mean of values within the rolling window, and then offsetting the mean by a multiple of the rolling deviation.
10687

10788
The map transform is akin to running [*array*.map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) on the input channel’s values with the given map method. However, the map transform is series-aware: the data are first grouped into series using the **z**, **fill**, or **stroke** channel in the same fashion as the [area](../marks/area.md) and [line](../marks/line.md) marks so that series are processed independently.
10889

0 commit comments

Comments
 (0)