简体   繁体   English

使用 bin 中的第一行(而不是平均值)来计算百分比增益

[英]Using the first row in bin (instead of average) to calculate percentage gain

In the dc.js Nasdaq example , percentageGain is calculated as:在 dc.js Nasdaq 示例中,percentageGain 计算为:

(p.absGain / p.avgIndex) * 100

Here avgIndex is the average of all the day-averages.这里 avgIndex 是所有日平均值的平均值。

I'm more familiar with the equation:我更熟悉这个等式:

A. (Price - Prev period's Close) / Prev period's Close * 100 A. (价格 - 上一期收盘价) / 上一期收盘价 * 100

I'm not sure whether this is possible (with filters set and so on), the way crossfilter/dc works.我不确定这是否可行(设置了过滤器等),crossfilter/dc 的工作方式。 Therefor, an alternative and different equation,that might fit crossfilter/dc better and would still be meaningful, could be:因此,一个替代的和不同的方程可能更好地适合交叉滤波器/直流并且仍然有意义,可以是:

B. absGain of group / open of first day of group * 100 B. abs 组的增益 / 组第一天的开盘 * 100

B would also mean that: If only a filter is set on for example Q1, then only the absGain of Q1 is taken into account. B 还意味着:如果仅在 Q1 上设置了一个过滤器,则仅考虑 Q1 的 absGain。 The first day in this group is the the oldest Q1 date in the oldest year.该组中的第一天是最早的一年中最早的 Q1 日期。 Also, charts other than "yearly" with groups like quarter, month or day of the week should be able to display the value of this equation.此外,除“每年”之外的包含季度、月份或星期几等组的图表应该能够显示该等式的值。 For example in a month chart, the value of the month "June" is calculated by taking the open of the first day in the first June.例如,在月份图表中,“六月”月份的值是通过取 6 月第一天的开盘价来计算的。 The absGain is taken from all June months. absGain 取自六月的所有月份。 (of course working with all current filters in place) (当然可以使用所有当前的过滤器)

Question: Can A and/or B be solved the crossfilter/dc way and how (example)?问题: A 和/或 B 可以通过 crossfilter/dc 方式解决吗?如何(示例)?

Even if only B could be solved (naturally with crossfilter/dc), that would already be great.即使只有 B 可以解决(自然使用 crossfilter/dc),那也已经很棒了。 I want to use the dc.js example for other stocks that have the same underlying data structure (open, close, high, low, volume)我想将 dc.js 示例用于具有相同基础数据结构(开盘价、收盘价、最高价、最低价、成交量)的其他股票

thanks!谢谢!

I agree that Equation B is easier to define using crossfilter, so I figured out one way to do it.我同意方程 B 使用交叉过滤器更容易定义,所以我想出了一种方法来做到这一点。

Equation A could probably work but it's unclear which day's close should be used under filtering - the last day which is not in the current bin?等式 A 可能有效,但不清楚应该在过滤下使用哪一天的收盘价——最后一天不在当前仓位中? The day before the first day in the current bin?当前 bin 中第一天的前一天?

Equation B needs the earliest row for the current bin, and that requires maintaining the array of all rows for each bin.等式 B 需要当前 bin 的最早行,这需要维护每个 bin 的所有行的数组。 This is not built into crossfilter but it's a feature which we have talked about adding .这不是内置在 crossfilter 中的,但它是我们讨论过添加的一个特性

The complex reduce example does this, and we can reuse some of its code.复杂的 reduce 示例就是这样做的,我们可以重用它的一些代码。 It calculates the median/mode/min/max value from the arrays of rows which fall in each bin, using these functions to generate those arrays:它从落在每个 bin 中的行的 arrays 计算中值/众数/最小值/最大值,使用这些函数生成这些 arrays:

function groupArrayAdd(keyfn) {
    var bisect = d3.bisector(keyfn);
    return function(elements, item) {
        var pos = bisect.right(elements, keyfn(item));
        elements.splice(pos, 0, item);
        return elements;
    };
}

function groupArrayRemove(keyfn) {
    var bisect = d3.bisector(keyfn);
    return function(elements, item) {
        var pos = bisect.left(elements, keyfn(item));
        if(keyfn(elements[pos])===keyfn(item))
            elements.splice(pos, 1);
        return elements;
    };
}

It's somewhat inefficient to maintain all these arrays, so you might test if it has an impact on your application.维护所有这些 arrays 效率有点低,因此您可以测试它是否对您的应用程序有影响。 JS is pretty fast so it probably doesn't matter unless you have a lot of data. JS 非常快,所以除非你有大量数据,否则它可能并不重要。

Unfortunately there is no other way to compute the minimum for a bin other than to keep an array of all the items in it.不幸的是,除了保留其中所有项目的数组外,没有其他方法可以计算 bin 的最小值。 (If you tried to keep track of just the lowest item, or lowest N items, what would you do when they are removed?) (如果你试图只跟踪最低的项目,或者最低的 N 项目,当它们被删除时你会做什么?)

Using these arrays inside the group reduce-add function:在组内使用这些 arrays 减少-添加 function:

    (p, v) => {
        ++p.count;
        p.rowsByDate = rbdAdd(p.rowsByDate, v);
        p.absGain += v.close - v.open;
        // ...
        p.percentageGain = p.rowsByDate.length ? (p.absGain / p.rowsByDate[0].open) * 100 : 0;
        return p;
    },

In the reduce-remove function it's在减少删除 function 它是

        p.rowsByDate = rbdRemove(p.rowsByDate, v);

and the same percentageGain change.和相同的percentageGain变化。

Here is a demo in a notebook: https://jsfiddle.net/gordonwoodhull/08bzcd4y/17/这是笔记本中的演示: https://jsfiddle.net/gordonwoodhull/08bzcd4y/17/

第一天开放百分比收益

I only see slight changes in the Y positions of the bubbles;我只看到气泡的 Y 位置略有变化; the changes are more apparent in the values printed in the tooltip.这些变化在工具提示中打印的值中更加明显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM