Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bollinger mark & transform #1772

Merged
merged 4 commits into from
Aug 4, 2023
Merged

bollinger mark & transform #1772

merged 4 commits into from
Aug 4, 2023

Conversation

mbostock
Copy link
Member

Fixes #548.

@mbostock mbostock requested a review from Fil July 29, 2023 23:46
);
}

export function bollingerX(n, k, options) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure n and k should be required positional arguments here. Probably would be more consistent to have a bollingerOptions argument, and allow a single combined options, too. And maybe a single number could be used to specify n; n defaults to 20 and k defaults to 2.

if (x === undefined && x1 === undefined && x2 === undefined) options = {...options, x: (x = identity)};
const outputs = {};
if (x != null) outputs.x = bollinger(n, k);
if (x1 != null) outputs.x1 = bollinger(n, -k);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By negating k here, bollingerX behaves like mapX, and can be used for either area or line!

@mbostock
Copy link
Member Author

Nice, we get a build failure now when we don’t document something. 😁 I’ll add that to the PR.

@Fil
Copy link
Contributor

Fil commented Aug 1, 2023

Nice! It’s a useful and sound method for general time series analysis.

I'm just wondering if we should keep that name and defaults of the transform. “Bollinger” seems to be confined to finance (according to wikipedia), and has the default multiplier = 2 (not a bad choice, but mathematically surprising).

Reading https://www.bollingerbands.com/bollinger-bands, I see that "bands" is plural—they represent the three lines y = moving average MA, y2 = MA+2 Dev, and y1 = MA-2 Dev. Whereas how I see it in our usage, we're considering a singular band, the area between y1 and y2.

Also, there is a trademark on the name.

Even in finance, people seem to be using “volatility” often (shown as a 1-sigma band). The 1-sigma band excludes 31.8% of the distribution on a normal model, so it makes sense to use a 2-sigma band as the default (4.6% outliers), but it's also legit to do 3-sigma (0.3%), etc. Other data science domains seem to be using words like “rolling window variance” or “rolling window deviation”, and plotting them with 1-sigma, 2-sigma, 3-sigma lines or bands, or adapting the sigmas to match 10%, 5%, 1%….

Copy link
Contributor

@Fil Fil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only blocking remark is that there is a TODO in the documentation.

Non-blocking remarks & questions:

  1. To apply in log-space rather than in linear-space (e.g., to avoid negative values if the multiplier × the deviation is high), one can do:
Plot.bollingerY(aapl, { x: "Date", y: (d) => t(d.Close), k }).plot({
  y: { transform: (d) => t.invert(d), domain: [0, 300], nice: true }
})

with t = d3.scaleLog() 👍. (Note that the difference on aapl with the default parameters is totally imperceptible.)

  1. Should we add the anchor option? For time series we obviously don't want to use “values from the future“, and if the data is coming in reverse chronological order (e.g. data = d3.reverse(aapl), it seems important to either anchor: "start" or use an explicit sort: "Date". Similarly, it could be useful to anchor “middle” if x is not time, and we want to gauge local variance.

  2. In that vein I'd also want to expose the underlying strict option, which would be particularly useful to set to false when the data has gaps (e.g., interval: "day" on the aapl dataset). (If we support that option and default it to true, we should document it as it is the converse of windowY's default.)

@mbostock mbostock force-pushed the mbostock/bollinger branch from 67fbf48 to b0afd6c Compare August 2, 2023 17:12
@mbostock mbostock requested a review from Fil August 2, 2023 23:59
@mbostock mbostock force-pushed the mbostock/bollinger branch from 200f911 to a0f9de6 Compare August 4, 2023 22:29
@mbostock mbostock enabled auto-merge (squash) August 4, 2023 22:29
@mbostock mbostock merged commit c73fa05 into main Aug 4, 2023
@mbostock mbostock deleted the mbostock/bollinger branch August 4, 2023 22:32
Fil pushed a commit that referenced this pull request Aug 21, 2023
* bollinger mark & transform

* strict & anchor for bollinger

* fancy candlesticks 🕯️

* more documentation
chaichontat pushed a commit to chaichontat/plot that referenced this pull request Jan 14, 2024
* bollinger mark & transform

* strict & anchor for bollinger

* fancy candlesticks 🕯️

* more documentation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Built-in Bollinger band transform
2 participants