@d3fc/d3fc-random-data

中文文档
4.0.2发布于 2024/06/13MITESM + CJS近 30 天下载 6.2万

Components for generating random data series based on stochastic processes

原始使用文档
文档目录5 节
  1. Installing
  2. API Reference
  3. Financial
  4. Financial Stream
  5. Geometric Brownian Motion

d3fc-random-data

Components for generating random data series based on stochastic processes.

Main D3FC package

Installing

npm install @d3fc/d3fc-random-data

API Reference

Financial

The random financial data generator component generates open-high-low-close-volume financial data. Prices are calculated using the geometric Brownian motion generator.


import { randomFinancial, randomSkipWeekends } from 'd3fc-random-data';

const generator = randomFinancial()
    .startDate(new Date(2016, 0, 1))
    .startPrice(100)
    .filter(randomSkipWeekends);

generator(4);

// [
//   {
//     date: 2016-01-01T00:00:00.000Z,
//     open: 100,
//     high: 100.37497903455065,
//     low: 99.9344064016257,
//     close: 100.13532170178823,
//     volume: 974
//   },
//   {
//     date: 2016-01-04T00:00:00.000Z,
//     open: 100.2078374019404,
//     high: 100.55251268471399,
//     low: 99.7272105851512,
//     close: 99.7272105851512,
//     volume: 992
//   },
//   {
//     date: 2016-01-05T00:00:00.000Z,
//     open: 99.7272105851512,
//     high: 101.06403178230532,
//     low: 99.7272105851512,
//     close: 101.00200313600685,
//     volume: 835
//   },
//   {
//     date: 2016-01-06T00:00:00.000Z,
//     open: 101.00200313600685,
//     high: 101.41129520567128,
//     low: 100.50311227829566,
//     close: 100.5536971451326,
//     volume: 1021
//   }
// ]

# fc.randomFinancial()

Constructs a new financial data generator.

# randomFinancial(points)

Run the generator. Returns an array with points number of objects with date, open, high, low, close and volume properties.

# randomFinancial.startDate([value])

If value is specified, sets the start date to the specified Date object and returns this generator instance. If value is not specified, returns the current start date, which defaults to the value of new Date() when the generator was constructed.

# randomFinancial.startPrice([value])

If value is specified, sets the start price to the specified number and returns this generator instance. If value is not specified, returns the current start price, which defaults to 100.

# randomFinancial.interval([value])

If value is specified, sets the time increment to the specified d3 time interval and returns this generator instance. If value is not specified, returns the current interval, which defaults to d3_time.timeDay.

# randomFinancial.intervalStep([value])

If value is specified, sets the number of intervals that returned points should have dates offset by to the specified integer number and returns this generator instance. If value is not specified, returns the current number of intervals, which defaults to 1. Internally, this value is supplied to the step argument of an interval's offset function.

# randomFinancial.steps([value])

Get/Set the number of steps used by the geometric Brownian motion simulation per intervalStep number of intervals. A higher number gives a slower, but higher resolution simulation.

# randomFinancial.mu([value])

Get/Set the drift used by the geometric Brownian motion simulation.

# randomFinancial.sigma([value])

Get/Set the volatility used by the geometric Brownian motion simulation.

# randomFinancial.random([value])

Get/Set the random function used by the geometric Brownian motion simulation.

# randomFinancial.unitInterval([value])

If value is specified, sets the time interval used for units of mu and sigma to the specified d3 time interval and returns this generator instance. If value is not specified, returns the current interval, which defaults to d3_time.timeYear.

# randomFinancial.unitIntervalStep([value])

If value is specified, sets the integer number of intervals used for units of mu and sigma to the specified number and returns this generator instance. If value is not specified, returns the current interval, which defaults to 1. For example, to have trading year units of mu and sigma rather than calendar year, set unitIntervalStep to 252 and unitInterval to d3_time.timeDay.

# randomFinancial.volume([value])

If value is specified, sets the function used return a point's volume to the specified function and returns this generator instance. Can be specified as either a function mapping an output object to a number, or a number. If value is not specified, returns the current volume, which defaults to a function sampling integers from a normal distribution centred around 1000.

# randomFinancial.filter([value])

If value is specified, sets the filter function to the specified function and returns this generator instance. Only output objects d for which filter(d) returns true will be included in the output array. If value is not specified, returns the current filter function, which defaults to (d) => true. To skip weekends, supply the pre-defined filter fc_random_data.skipWeekends.

Financial Stream

Use the streaming interface to have successive calls to generate data keep track of the latest date and price.


import { randomFinancial } from 'd3fc-random-data';

const generator = randomFinancial()
    .startDate(new Date(2016, 0, 1))
    .startPrice(100);

const stream = generator.stream();
let data = [];

data.push(stream.next());
// data.length -> 1

data = data.concat(stream.take(2));
// data.length -> 3

data = data.concat(stream.until(d => d.date > new Date(2016, 0, 10)));
// data.length -> 10

# randomFinancial.stream()

Constructs a new stream from an existing financial data generator instance.

# stream.next()

Returns a single output object with date incremented from the latest returned output object's date according to the generator instance's interval and intervalStep properties, or with startDate if this is the first call.

# stream.take(number)

Returns an array of length number of output objects, each object with date incremented according to the generator instance's interval and intervalStep properties, starting with the latest returned output objects's incremented date, or with startDate if this is the first call.

# stream.until(comparison)

Returns the array of objects constructed by repeatedly generating a single output object with date incremented according to the generator instance's interval and intervalStep properties until a generated object satisfies the condition of the supplied comparison function, appending to the output array only if the condition is not satisfied.

Geometric Brownian Motion

The geometric Brownian motion component creates a series of values based on the Geometric Brownian Motion stochastic process.


import { randomGeometricBrownianMotion } from 'd3fc-random-data';

const generator = randomGeometricBrownianMotion()
    .steps(10);

generator(10);

// [
//   10,
//   10.272847363463436,
//   10.423881104466574,
//   10.629316182766384,
//   10.7209321393133,
//   10.773722182206432,
//   10.229636144307582,
//   10.225282323984114,
//   10.488138829847468,
//   10.428118194568341,
//   10.848822656937935
// ]

# fc.randomGeometricBrownianMotion()

Constructs a new geometric Brownian motion generator.

# randomGeometricBrownianMotion(start)

Returns an array of price values following a geometric Brownian motion with the set drift and volatility, given a starting price of start. The first array value is the supplied start price, followed by steps number of values corresponding to the simulated price value at the end of each step.

# randomGeometricBrownianMotion.mu([value])

If value is specified, sets the percentage drift per period to the specified number and returns this generator instance. If value is not specified, returns the current drift, which defaults to 0.1.

# randomGeometricBrownianMotion.sigma([value])

If value is specified, sets the percentage volatility per period to the specified number and returns this generator instance. If value is not specified, returns the current volatility, which defaults to 0.1.

# randomGeometricBrownianMotion.period([value])

If value is specified, sets the interval length to the specified number of periods and returns this generator instance. If value is not specified, returns the current interval length, which defaults to 1.

randomGeometricBrownianMotion.steps([value])

If value is specified, sets the number of discrete steps to divide the interval into to the specified number and returns this generator instance. If value is not specified, returns the current number of steps, which defaults to 20.

randomGeometricBrownianMotion.random([value])

If value is specified, sets the function used for generating random numbers with a normal (Gaussian) distribution to the specified function and returns this generator instance. If value is not specified, returns the current random function, which defaults to d3_random.randomNormal.

版本与发布记录

@d3fc/d3fc-random-data 共发布 24 个版本,首次发布于 2018/08/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.0.2latest2024/06/132 年前
4.0.12020/07/146 年前
4.0.02020/04/166 年前
3.1.02020/03/196 年前
3.0.192019/11/286 年前
3.0.182019/09/047 年前

关于 @d3fc/d3fc-random-data 的常见问题

@d3fc/d3fc-random-data 是什么?

Components for generating random data series based on stochastic processes

如何安装 @d3fc/d3fc-random-data?

在项目目录运行 npm install @d3fc/d3fc-random-data,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:d3-random(*)、d3-time(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@d3fc/d3fc-random-data 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/d3fc__d3fc-random-data 是否存在。

@d3fc/d3fc-random-data 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@d3fc/d3fc-random-data 可以商用吗?

@d3fc/d3fc-random-data 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@d3fc/d3fc-random-data 的下载量有多少?

根据 npm 官方下载统计,@d3fc/d3fc-random-data 在 2026/10/11 – 2026/11/09 这 30 天内被下载 61,626 次,日均约 2,054 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@d3fc/d3fc-random-data 还在维护吗?

最新版本 4.0.2 发布于 2024/06/13(2 年前);至今共发布 24 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/08/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。