是否有已知的技术可以生成逼真的虚假股票数据?

发布于 2024-12-22 10:13:04 字数 229 浏览 1 评论 0原文

我最近编写了一些 Javascript 代码来生成随机的虚假股票数据,因为我想显示一个乍一看看起来像真实股票数据的图表 - 但我想出的只是 相当点头。我只是想知道是否有一些资源可以解释如何“正确”地完成此操作,即,以便您获得具有与真实股票数据中看到的相同模式的逼真数据?

I recently wrote some Javascript code to generate random fake stock data as I wanted to show a chart that at first glanced looked like real stock data - but all I came up with was pretty noddy. I was just wondering if there are some resources that explain how this might be done "properly" i.e. so you get realistic looking data that has the same patterns that you see in real stock data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

枕梦 2024-12-29 10:13:04

一个简单的算法是使用一个简单的波动率数字来限制股票在给定时期(例如一天)内可以变化的程度。数字越高,波动性越大。因此,每天您可以通过以下方式计算新价格:

rnd = Random_Float(); // generate number, 0 <= x < 1.0
change_percent = 2 * volatility * rnd;
if (change_percent > volatility)
    change_percent -= (2 * volatility);
change_amount = old_price * change_percent;
new_price = old_price + change_amount;

稳定股票的波动率可能为 2%。 10% 的波动率会显示出相当大的波动。

并不完美,但看起来很现实。

示例

在此处输入图像描述

A simple algorithm is to use a simple volatility number that restricts how much the stock can change within a given period (say, a single day). The higher the number, the more volatile. So each day you can compute the new price by:

rnd = Random_Float(); // generate number, 0 <= x < 1.0
change_percent = 2 * volatility * rnd;
if (change_percent > volatility)
    change_percent -= (2 * volatility);
change_amount = old_price * change_percent;
new_price = old_price + change_amount;

A stable stock would have a volatility number of perhaps 2%. A volatility of 10% would show some pretty large swings.

Not perfect, but it could look pretty realistic.

Samples

enter image description here

べ繥欢鉨o。 2024-12-29 10:13:04

我有一本书分形市场分析(最近刚刚摆脱它),其中讨论了股票价格的统计特性。对于投资来说不是很有用,但它可能会对您有所帮助。

您需要使用所需的统计属性对随机过程进行建模。随机过程的两个示例是高斯白噪声维纳过程(后者模拟布朗运动,也是小步随机游走的极限)。

如果我记得在《分形市场分析》一书中,有一个断言,即股票价格的对数具有类似于所谓的“1/f 噪声”或"粉红噪声",因此您可以尝试查找有关软件中粉红噪声生成的文章。 (然后获取结果并将它们插入 e^x) (编辑:哎呀,我记错了。看起来更像是 分数布朗运动

(这是一个 不错的可读文章,讲述了分形研究背后的历史随机过程——以及尼罗河泛滥与股市的关系——不幸的是它没有涉及技术数据,但也许有像赫斯特指数可以帮助您入门。)

如果您需要多个系列股票数据,问题就会变得更加困难。 (在这种情况下,股票之间存在一些相关性,这取决于各种共同因素,例如国民经济、行业类型等)我不确定您如何做到这一点,但首先从一个随机过程开始。

I had a book Fractal Market Analysis (just got rid of it recently) that talked about the statistical properties of stock prices. Not very useful for investing, but it might have been able to help you.

You'll need something that models a random process with desired statistical properties. Two examples of random processes are Gaussian white noise and a Wiener process (the latter which models Brownian motion and is also the limit of a random walk with small steps).

If I remember right from the Fractal Market Analysis book, there was an assertion that the logarithm of stock prices had characteristics similar to so-called "1/f noise" or "pink noise", so you could try looking for articles on pink noise generation in software. (and then take the results and plug them into e^x) (edit: oops, I misremembered. Looks like it's more like fractional Brownian motion)

(Here's a nice readable essay that talks about the history behind the study of fractal random processes -- and how the flooding of the Nile relates to the stock market -- unfortunately it doesn't get into technical data, but maybe there are search terms like Hurst exponent that can get you started.)

The problem becomes more difficult if you need multiple series of stock data. (in which case there is some correlation between stocks that depends on various common factors e.g. national economy, industry type, etc.) I'm not sure how you could go about that, but start with one random process first.

烛影斜 2024-12-29 10:13:04
# The following is an adaptation from a program shown at page 140 in
# "Stochastic Simulations and Applications in Finance",
# a book written by Huynh, Lai and Soumaré.
# That program was written in MatLab and this one was written in R by me.
# That program produced many price paths and this one produces one.
# The latter is also somewhat simpler and faster.

# Y is the time period in years, for instance 1 (year)
# NbSteps is the number of steps in the simulation,
# for instance 250 (trading days in a year).
# DeltaY is the resulting time step.

# The computations shown implement the exact solution
# to the stochastic differential equation for
# the geometric Brownian motion modelling stock prices,
# with mean mu and volatility sigma, thus generating a stochastic price path
# such as that exhibited by stock prices when price jumps are rare.

PricePath <- function(Y,NbSteps,mu,sigma,InitPrice) {
    DeltaY <- Y/NbSteps; SqrtDeltaY <- sqrt(DeltaY)
    DeltaW <- SqrtDeltaY * rnorm(NbSteps)
    Increments <- (mu-sigma*sigma/2)*DeltaY + sigma*DeltaW
    ExpIncr <- exp(Increments)
    PricePath <- cumprod(c(InitPrice,ExpIncr))
    return(PricePath)
}

该程序的输出图看起来非常像股票价格路径:

# The following is an adaptation from a program shown at page 140 in
# "Stochastic Simulations and Applications in Finance",
# a book written by Huynh, Lai and Soumaré.
# That program was written in MatLab and this one was written in R by me.
# That program produced many price paths and this one produces one.
# The latter is also somewhat simpler and faster.

# Y is the time period in years, for instance 1 (year)
# NbSteps is the number of steps in the simulation,
# for instance 250 (trading days in a year).
# DeltaY is the resulting time step.

# The computations shown implement the exact solution
# to the stochastic differential equation for
# the geometric Brownian motion modelling stock prices,
# with mean mu and volatility sigma, thus generating a stochastic price path
# such as that exhibited by stock prices when price jumps are rare.

PricePath <- function(Y,NbSteps,mu,sigma,InitPrice) {
    DeltaY <- Y/NbSteps; SqrtDeltaY <- sqrt(DeltaY)
    DeltaW <- SqrtDeltaY * rnorm(NbSteps)
    Increments <- (mu-sigma*sigma/2)*DeltaY + sigma*DeltaW
    ExpIncr <- exp(Increments)
    PricePath <- cumprod(c(InitPrice,ExpIncr))
    return(PricePath)
}

The plot of the output from this program looks very much like a stock price path:

司马昭之心 2024-12-29 10:13:04

有几个答案给出了相当教科书式的答案:使用几何布朗运动来模拟股票价格。但有一个主要原因可以认为这是错误的。真实股票价格的表现与几何布朗运动(GBM)完全不同。我稍后会解释一下。

教科书中使用 GBM 来模拟股票价格过程的原因是为了简单起见。它可以帮助您将理论付诸实践并得出一些似乎“本质上”正确的基本结果。但这并不意味着您应该认为这就是股价“看起来像”的样子。这就像导出一个忽略摩擦力的运动方程(这在理论上非常有用),然后认为这就是现实生活中运动的样子,例如每个人都像溜冰鞋一样穿着鞋子滑动。

GBM 理论上最有用的特性之一是未来的变化独立于过去的变化。股票价格也是如此吗?没有。一点也不。序列相关性随处可见。不仅如此,大幅下跌通常伴随着波动性增加,而大幅上涨通常伴随着波动性下降。

我想我可能会被指责吹毛求疵,但这些程式化的事实是投资者和经济学家众所周知的,所以我认为可以公平地说,GBM 对于任何熟悉股票市场行为的人来说看起来并不现实。

计量经济学家提出了大量的股票价格模型。在很多情况下似乎都有效的模型是条件均值的自回归模型与波动率的 (G)Arch 类型模型相结合。对于波动率模型,具有肥尾分布(如 Student t)的不对称 GARCH 似乎最适合各种金融市场。

There are several answers that give a fairly textbook answer: use geometric brownian motion to model stock prices. But there's one major reason to consider this wrong. Real stock prices do not behave anything like geometric brownian motion (GBM). I'll explain this in a bit.

The reason GBM is used in textbooks to model a stock price process is for simplicity. It helps you get the theory off the ground and derive some basic results which seem to be "essentially" correct. This doesn't mean you should think that's what stock prices "look like" however. That would be like deriving an equation of motion neglecting friction (which is theoretically very useful) and then thinking this is what motion looks like in real life, e.g. everyone slides around on their shoes like ice skates.

One of the theoretically most useful properties of GBM is that future changes are independent of past changes. Is this true of stock prices? Nope. Not at all. Serial correlation occurs everywhere. Not only that, large decreases are usually followed by increased volatility while large increases are usually followed by decreased volatility.

I suppose I might be accused of nitpicking, but these stylized facts are commonly known to investors and economists, so I think it's fair to say GBM doesn't look realistic to anybody that is familiar with stock market behavior.

Econometricians have come up with plenty of models for stock prices. The one that seems to work in a lot of situations is an autoregressive model for the conditional mean combined with an (G)Arch type model for the volatility. For the volatility model, an assymetric GARCH with a fat-tail distribution (like Student's t) seems to work the best for a variety of financial markets.

江南烟雨〆相思醉 2024-12-29 10:13:04

受 Peter P. 的回复启发,我写了一个快速的脏 JavaScript 版本。我需要创建每周、每年和总体趋势,因此它接受一系列参数并覆盖这些参数以获得更复杂的(假)趋势。

  function getRandomData(numPoints, center, min, max, cycles)
{
    var result = [];
    var phase = Math.random() * Math.PI;
    var y = center;

    function randomPlusMinus() { return (Math.random() * 2) - 1; }

    $.each(cycles, function(i,thisCycle) {
        thisCycle.phase = Math.random() * Math.PI;
        thisCycle.increment = Math.PI / thisCycle.length;
    });

    for (var i = 0; i < numPoints; i++)
    {
        $.each(cycles, function(i,thisCycle) {
            thisCycle.phase += thisCycle.increment * randomPlusMinus();
            y += (Math.sin(thisCycle.phase) * (thisCycle.variance / thisCycle.length) * (randomPlusMinus() * thisCycle.noise)) + (thisCycle.trend / thisCycle.length);

        });
        if (min) y = Math.max(y,min);
        if (max) y = Math.min(y,max);
        result.push(y);
    }

    return result;
}

var data = getRandomData(365,80,20,100,
                      [{ length: 7, variance: 50, noise: 1, trend: 0},
                       { length: 365, variance: 30, noise: 1, trend: 0},
                       { length: 700, variance: 2, noise: 0, trend: 100}]);

我在那里放了一个图表来显示结果: http://jsfiddle.net/z64Jr/3/

I wrote a quick an dirty javascript version inspired by Peter P.'s response here. I needed to create weekly, yearly and overall trends so this accepts an array of parameters and overlays these to get a more complex (fake) trend.

  function getRandomData(numPoints, center, min, max, cycles)
{
    var result = [];
    var phase = Math.random() * Math.PI;
    var y = center;

    function randomPlusMinus() { return (Math.random() * 2) - 1; }

    $.each(cycles, function(i,thisCycle) {
        thisCycle.phase = Math.random() * Math.PI;
        thisCycle.increment = Math.PI / thisCycle.length;
    });

    for (var i = 0; i < numPoints; i++)
    {
        $.each(cycles, function(i,thisCycle) {
            thisCycle.phase += thisCycle.increment * randomPlusMinus();
            y += (Math.sin(thisCycle.phase) * (thisCycle.variance / thisCycle.length) * (randomPlusMinus() * thisCycle.noise)) + (thisCycle.trend / thisCycle.length);

        });
        if (min) y = Math.max(y,min);
        if (max) y = Math.min(y,max);
        result.push(y);
    }

    return result;
}

var data = getRandomData(365,80,20,100,
                      [{ length: 7, variance: 50, noise: 1, trend: 0},
                       { length: 365, variance: 30, noise: 1, trend: 0},
                       { length: 700, variance: 2, noise: 0, trend: 100}]);

I put a chart on there to show the result: http://jsfiddle.net/z64Jr/3/

熊抱啵儿 2024-12-29 10:13:04

我想回复吉姆·米歇尔上面的帖子(https://stackoverflow.com/a/8597889/1360592),但自从我想包含代码,我被迫将我的回复放在这里。

根据 Jim Mischel 的算法,我进行了以下 Java 实现,它很好地满足了我的需求,生成的数字在绘制图表时产生了视觉上吸引人、逼真的股票行情价格。

Java:

private float getNextPrice(float oldPrice)
{
    // Instead of a fixed volatility, pick a random volatility
    // each time, between 2 and 10.
    float volatility = _random.nextFloat() * 10 + 2;

    float rnd = _random.nextFloat();

    float changePercent = 2 * volatility * rnd;

    if (changePercent > volatility) {
        changePercent -= (2 * volatility);
    }
    float changeAmount = oldPrice * changePercent/100;
    float newPrice = oldPrice + changeAmount;

    // Add a ceiling and floor.
    if (newPrice < MIN_PRICE) {
        newPrice += Math.abs(changeAmount) * 2;
    } else if (newPrice > MAX_PRICE) {
        newPrice -= Math.abs(changeAmount) * 2;
    }

    return newPrice;

}

请注意,正如wiggles 在他的评论中指出的那样,在声明changeAmount 变量时我需要将百分比除以100。

I wanted to reply to Jim Mischel's post above (https://stackoverflow.com/a/8597889/1360592) but since I wanted to include code, I am forced to put my reply here.

Based on Jim Mischel's alorithm, I did the following Java implementation, and it worked well for my needs, generating numbers that when graphed, produced visually appealing, realistic-looking stock ticker prices.

Java:

private float getNextPrice(float oldPrice)
{
    // Instead of a fixed volatility, pick a random volatility
    // each time, between 2 and 10.
    float volatility = _random.nextFloat() * 10 + 2;

    float rnd = _random.nextFloat();

    float changePercent = 2 * volatility * rnd;

    if (changePercent > volatility) {
        changePercent -= (2 * volatility);
    }
    float changeAmount = oldPrice * changePercent/100;
    float newPrice = oldPrice + changeAmount;

    // Add a ceiling and floor.
    if (newPrice < MIN_PRICE) {
        newPrice += Math.abs(changeAmount) * 2;
    } else if (newPrice > MAX_PRICE) {
        newPrice -= Math.abs(changeAmount) * 2;
    }

    return newPrice;

}

Note that, as wiggles pointed out in his comment, I needed to divide percentage by 100 when declaring the changeAmount variable.

灵芸 2024-12-29 10:13:04

看看雅虎财经,他们提供来自证券交易所和图表的免费延迟数据。

这是一篇关于使用 feed 的文章:
http://www.codeproject.com/KB/aspnet/StockQuote.aspx

您将需要 JQuery,或者您可以只使用 XMLHttpRequest 来使用该服务。仅供参考,JQuery 有一个用于处理 CSV 的插件:http://code.google.com/ p/js-tables/

Take a look at yahoo finance, they offer free delayed data from the stock exchange and charts.

Here's an article about using the feed:
http://www.codeproject.com/KB/aspnet/StockQuote.aspx

You'll need JQuery or you can just use XMLHttpRequest to comsume the service. FYI, there's a plugin for JQuery to process a CSV: http://code.google.com/p/js-tables/

人生百味 2024-12-29 10:13:04

我需要为我正在开发的一款模拟游戏创建一些虚拟市场数据。我需要数据看起来像市场数据,但仍保持在一定范围内,以便可以根据当天的起始价格、最高/最低价格进行预测。

最后,我组合了不同频率的正弦波,然后添加了一些随机性,结果不仅看起来不错,而且是一致的(你不会得到任何看起来奇怪的东西)。即使在可以感知到正弦波图案的地方,它看起来仍然没问题。

随机生成的市场数据

代码是用 BASIC 脚本语言编写的,但应该非常简单理解并转换为您想要的任何语言。获得标准化数据数组后,将这些值乘以您想要获得有界数据集的任何最大值。

dim values[] as float
dim offsets[] as integer
dim frequencies[] as float

function GetPoint(x#, f#, a#, o#)

    f# = 360.0 / f#

    x# = FMod(x# + o#, f#)
    angle# = (x# / f#) * 360.0

    r# = Sin(angle#) * a#

endfunction r#

function Generate()

    // Empty arrays
    offsets.Length = -1
    frequencies.Length = -1
    values.Length = -1

    offsets.Insert(Random(0, 359))
    offsets.Insert(Random(0, 359))
    offsets.Insert(Random(0, 359))

    f# = Random(100, 300)
    f# = f# / 1000.0
    frequencies.Insert(f#)
    f# = Random(500, 1000)
    f# = f# / 1000.0
    frequencies.Insert(f#)
    f# = Random(2000, 4000)
    f# = f# / 1000.0
    frequencies.Insert(f#)

    c# = 0
    for i = 0 to 1919
        v# = 0
        v# = v# + GetPoint(i, frequencies[0], 190, offsets[0])
        v# = v# + GetPoint(i, frequencies[1], 85, offsets[1])
        v# = v# + GetPoint(i, frequencies[2], 40, offsets[2])

        r# = Random(0, 40)
        r# = r# - 20.0

        c# = Clamp(c# + r#, c# - 40, c# + 40)
        v# = v# + c#

        values.Insert(v#)
    next i

    start# = values[0]
    max# = 0.0
    for i = 0 to values.Length
        values[i] = values[i] - start#
        if Abs(values[i]) > max#
            max# = Abs(values[i])
        endif
    next i

    // Normalize
    for i = 0 to values.Length
        values[i] = (values[i] / max#)
    next i

endfunction

function Clamp(v#, min#, max#)

    if v# < min#
        exitfunction min#
    elseif v# > max#
        exitfunction max#
    endif

endfunction v#

I needed to create some dummy market data for a sim game I was working on. I needed the data to look like market data yet stay within certain ranges so it was predictable in terms of starting price, maximum / minimum for the day.

In the end, I combined sine waves of varying frequencies and then added in some randomness and the results don't just look good but are consistent (you don't get anything that looks odd). Even where the sine wave pattern can be perceived, it still looks okay.

Random generated market data

The code is written in a BASIC scripting language, but it should be very simple to understand and convert to whatever language you want. Once you've got the array of normalised data, multiply the values by whatever maximum value you want to get a bounded dataset.

dim values[] as float
dim offsets[] as integer
dim frequencies[] as float

function GetPoint(x#, f#, a#, o#)

    f# = 360.0 / f#

    x# = FMod(x# + o#, f#)
    angle# = (x# / f#) * 360.0

    r# = Sin(angle#) * a#

endfunction r#

function Generate()

    // Empty arrays
    offsets.Length = -1
    frequencies.Length = -1
    values.Length = -1

    offsets.Insert(Random(0, 359))
    offsets.Insert(Random(0, 359))
    offsets.Insert(Random(0, 359))

    f# = Random(100, 300)
    f# = f# / 1000.0
    frequencies.Insert(f#)
    f# = Random(500, 1000)
    f# = f# / 1000.0
    frequencies.Insert(f#)
    f# = Random(2000, 4000)
    f# = f# / 1000.0
    frequencies.Insert(f#)

    c# = 0
    for i = 0 to 1919
        v# = 0
        v# = v# + GetPoint(i, frequencies[0], 190, offsets[0])
        v# = v# + GetPoint(i, frequencies[1], 85, offsets[1])
        v# = v# + GetPoint(i, frequencies[2], 40, offsets[2])

        r# = Random(0, 40)
        r# = r# - 20.0

        c# = Clamp(c# + r#, c# - 40, c# + 40)
        v# = v# + c#

        values.Insert(v#)
    next i

    start# = values[0]
    max# = 0.0
    for i = 0 to values.Length
        values[i] = values[i] - start#
        if Abs(values[i]) > max#
            max# = Abs(values[i])
        endif
    next i

    // Normalize
    for i = 0 to values.Length
        values[i] = (values[i] / max#)
    next i

endfunction

function Clamp(v#, min#, max#)

    if v# < min#
        exitfunction min#
    elseif v# > max#
        exitfunction max#
    endif

endfunction v#
z祗昰~ 2024-12-29 10:13:04

这是我对红宝石的尝试! :) 这将输出一个字符串,您可以复制并粘贴到谷歌图表中。我允许数据出现正向、负向或无趋势。该代码可能会针对随机性/规律性进行优化和/或调整。

Google 图表:https://code.google.com/apis/ajax /playground/?type=visualization#line_chart

# In order to generate a semi-realistic looking graph behavior
# we use a sine function to generate period behavior.  In order to avoid
# a graph that is too regular, we introduce randomness at two levels:
# The delta between steps across the x-axis is random, but within a range(deltavariance)
# The wavelength of the sine function is varied by randomly incrementing the index we pass
# to the sine function(sine_index)

# CONFIGURATION VARIABLES
yvalue = 1 # start value
range = 100 # y-range
deltavariance = 10 # allowable variance between changes
sine_index, wavelength = 0, 0.33 #index into our sine function that determines whether we change direction or not
i, maxi = 0, 100 # our counter and its maximum
data = {sine_index => yvalue} # seed our data structure with its first value
trend = :positive # :negative, :none # do we want the graph to trend upwards, downwards or neither
periodmin, periodmax = 0, 0 # vars to enforce trending
direction = 1 # start in a positive direction, -1 for negative

# DO NOT EDIT BELOW THIS LINE
while(i < maxi)

  olddirection = direction
  direction = Math.sin(sine_index).to_f
  direction = direction < 0 ? direction.floor : direction.ceil

  delta = rand(deltavariance) 
  yvalue += delta * direction

  if trend == :positive 
    yvalue = periodmin if yvalue < periodmin
    periodmin = yvalue if olddirection < direction
  elsif trend == :negative
    yvalue = periodmax if yvalue > periodmax
    periodmax = yvalue if olddirection > direction

  end

  data[sine_index] = yvalue
  sine_index += Math.sin(rand) # Math.sin(rand) will give random numbers from -1..1
  i += 1
end

code = <<-CODE
function drawVisualization() {
  // Create and populate the data table.
  var data = google.visualization.arrayToDataTable([
    ['x', 'Cats'],
    DATASTR
  ]);

  // Create and draw the visualization.
  new google.visualization.LineChart(document.getElementById('visualization')).
      draw(data, {curveType: "function",
                  width: 500, height: 400,
                  vAxis: {maxValue: 10}}
          );
}
CODE

datastr = data.collect{|k,v|  "[#{k},#{v}]"}.join(",")
code = code.gsub('DATASTR', datastr)
puts code

Here's my attempt in ruby! :) This will output a string you can copy and paste into google charts. I allow for positive, negative or no trending of the data. This code could probably be optimized and/or tweaked for randomness/regularity.

Google charts: https://code.google.com/apis/ajax/playground/?type=visualization#line_chart

# In order to generate a semi-realistic looking graph behavior
# we use a sine function to generate period behavior.  In order to avoid
# a graph that is too regular, we introduce randomness at two levels:
# The delta between steps across the x-axis is random, but within a range(deltavariance)
# The wavelength of the sine function is varied by randomly incrementing the index we pass
# to the sine function(sine_index)

# CONFIGURATION VARIABLES
yvalue = 1 # start value
range = 100 # y-range
deltavariance = 10 # allowable variance between changes
sine_index, wavelength = 0, 0.33 #index into our sine function that determines whether we change direction or not
i, maxi = 0, 100 # our counter and its maximum
data = {sine_index => yvalue} # seed our data structure with its first value
trend = :positive # :negative, :none # do we want the graph to trend upwards, downwards or neither
periodmin, periodmax = 0, 0 # vars to enforce trending
direction = 1 # start in a positive direction, -1 for negative

# DO NOT EDIT BELOW THIS LINE
while(i < maxi)

  olddirection = direction
  direction = Math.sin(sine_index).to_f
  direction = direction < 0 ? direction.floor : direction.ceil

  delta = rand(deltavariance) 
  yvalue += delta * direction

  if trend == :positive 
    yvalue = periodmin if yvalue < periodmin
    periodmin = yvalue if olddirection < direction
  elsif trend == :negative
    yvalue = periodmax if yvalue > periodmax
    periodmax = yvalue if olddirection > direction

  end

  data[sine_index] = yvalue
  sine_index += Math.sin(rand) # Math.sin(rand) will give random numbers from -1..1
  i += 1
end

code = <<-CODE
function drawVisualization() {
  // Create and populate the data table.
  var data = google.visualization.arrayToDataTable([
    ['x', 'Cats'],
    DATASTR
  ]);

  // Create and draw the visualization.
  new google.visualization.LineChart(document.getElementById('visualization')).
      draw(data, {curveType: "function",
                  width: 500, height: 400,
                  vAxis: {maxValue: 10}}
          );
}
CODE

datastr = data.collect{|k,v|  "[#{k},#{v}]"}.join(",")
code = code.gsub('DATASTR', datastr)
puts code
提赋 2024-12-29 10:13:04
double price=2000;
    while (true) {
        double min =  (price*-.02);
        double max =  (price*.02);
        double randomNum = ThreadLocalRandom.current().nextDouble(min, max+1);
        price=price+randomNum;
        System.out.println(price);
    }

它是在java中。只需将结果绘制在 Excel 列中即可查看图表。使用大量值在 Excel 中绘制。有趣的是它看起来与真实的股票数据有多么相似。

double price=2000;
    while (true) {
        double min =  (price*-.02);
        double max =  (price*.02);
        double randomNum = ThreadLocalRandom.current().nextDouble(min, max+1);
        price=price+randomNum;
        System.out.println(price);
    }

It is in java. Just plot the result in excel column to see the graph.Use a large set of values to plot in excel. It is intriguing to see the how similar it looks like real stock data.

窗影残 2024-12-29 10:13:04

基于上述算法的 Golang 代码,作者:@Jim Mischel

package main

import (
    "fmt"
    "math/rand"
)

func main() {

    var (
        change_percent, change_amount, new_price, old_price float64
    )
    volatility := 0.02
    old_price = 50

    for i := 0; i < 100; i++ {
        rnd := rand.Float64() // generate number, 0 <= x < 1.0
        // fmt.Printf("rnd %v ", rnd)
        change_percent = 2 * volatility * rnd
        // fmt.Printf("change_percent %v\n", change_percent)
        if change_percent > volatility {
            change_percent = change_percent - (2 * volatility)
        }
        change_amount = old_price * change_percent
        new_price = old_price + change_amount
        fmt.Printf("new_price %f\n", new_price)
        new_price = old_price
    }

}

Golang code based on the above algorithm by @Jim Mischel

package main

import (
    "fmt"
    "math/rand"
)

func main() {

    var (
        change_percent, change_amount, new_price, old_price float64
    )
    volatility := 0.02
    old_price = 50

    for i := 0; i < 100; i++ {
        rnd := rand.Float64() // generate number, 0 <= x < 1.0
        // fmt.Printf("rnd %v ", rnd)
        change_percent = 2 * volatility * rnd
        // fmt.Printf("change_percent %v\n", change_percent)
        if change_percent > volatility {
            change_percent = change_percent - (2 * volatility)
        }
        change_amount = old_price * change_percent
        new_price = old_price + change_amount
        fmt.Printf("new_price %f\n", new_price)
        new_price = old_price
    }

}

jJeQQOZ5 2024-12-29 10:13:04

这是我为我的使用而创建的代码。价格是为新烛台创建的,包括开盘价、最高价、最低价、收盘价和交易量。新价格是根据波动百分比生成的。我用了总计 5% 的价格。

该代码基于 C#。

public class PriceBar
{
    public DateTime Date { get; set; }
    public double Open { get; set; }
    public double High { get; set; }
    public double Low { get; set; }
    public double Close { get; set; }
    public long Volume { get; set; }
}

public static double GetRandomNumber(double minimum, double maximum)
{
    Random random = new Random();
    return random.NextDouble() * (maximum - minimum) + minimum;
}

public static void GenerateRandomBar(PriceBar newBar)
{
    double fluct = 0.025;
    double volFluct = 0.40;

    //Open is equal to the previous close
    newBar.Open = newBar.Close;
    newBar.Close = GetRandomNumber(newBar.Close - newBar.Close * fluct, newBar.Close + newBar.Close * fluct);
    newBar.High = GetRandomNumber(Math.Max(newBar.Close, newBar.Open), Math.Max(newBar.Close, newBar.Open) + Math.Abs(newBar.Close - newBar.Open) * fluct);
    newBar.Low = GetRandomNumber(Math.Min(newBar.Close, newBar.Open), Math.Min(newBar.Close, newBar.Open) - Math.Abs(newBar.Close - newBar.Open) * fluct);
    newBar.Volume = (long)GetRandomNumber(newBar.Volume * volFluct, newBar.Volume);
}

用法:

创建 PriceBar 的实例,填充前一个柱的价格。将 PriceBar 实例提供给函数 GenerateRandomBar()。它将返回一个带有新值的 PriceBar。

Here is the code that I created for my usage. The prices are created for new candle-stick that includes Open, High, Low, Close, and Volume. The new prices are generated based on % of volatility. I used total 5% for prices.

The code is C# based.

public class PriceBar
{
    public DateTime Date { get; set; }
    public double Open { get; set; }
    public double High { get; set; }
    public double Low { get; set; }
    public double Close { get; set; }
    public long Volume { get; set; }
}

public static double GetRandomNumber(double minimum, double maximum)
{
    Random random = new Random();
    return random.NextDouble() * (maximum - minimum) + minimum;
}

public static void GenerateRandomBar(PriceBar newBar)
{
    double fluct = 0.025;
    double volFluct = 0.40;

    //Open is equal to the previous close
    newBar.Open = newBar.Close;
    newBar.Close = GetRandomNumber(newBar.Close - newBar.Close * fluct, newBar.Close + newBar.Close * fluct);
    newBar.High = GetRandomNumber(Math.Max(newBar.Close, newBar.Open), Math.Max(newBar.Close, newBar.Open) + Math.Abs(newBar.Close - newBar.Open) * fluct);
    newBar.Low = GetRandomNumber(Math.Min(newBar.Close, newBar.Open), Math.Min(newBar.Close, newBar.Open) - Math.Abs(newBar.Close - newBar.Open) * fluct);
    newBar.Volume = (long)GetRandomNumber(newBar.Volume * volFluct, newBar.Volume);
}

Usage:

Create an instance of PriceBar, fill the previous bar's prices. Feed the PriceBar instance to the function GenerateRandomBar(). It will return a PriceBar with new values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文