是否有已知的技术可以生成逼真的虚假股票数据?
我最近编写了一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
一个简单的算法是使用一个简单的波动率数字来限制股票在给定时期(例如一天)内可以变化的程度。数字越高,波动性越大。因此,每天您可以通过以下方式计算新价格:
稳定股票的波动率可能为 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:
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
我有一本书分形市场分析(最近刚刚摆脱它),其中讨论了股票价格的统计特性。对于投资来说不是很有用,但它可能会对您有所帮助。
您需要使用所需的统计属性对随机过程进行建模。随机过程的两个示例是高斯白噪声和维纳过程(后者模拟布朗运动,也是小步随机游走的极限)。
如果我记得在《分形市场分析》一书中,
有一个断言,即股票价格的对数具有类似于所谓的“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.
有几个答案给出了相当教科书式的答案:使用几何布朗运动来模拟股票价格。但有一个主要原因可以认为这是错误的。真实股票价格的表现与几何布朗运动(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.
受 Peter P. 的回复启发,我写了一个快速的脏 JavaScript 版本。我需要创建每周、每年和总体趋势,因此它接受一系列参数并覆盖这些参数以获得更复杂的(假)趋势。
我在那里放了一个图表来显示结果: 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.
I put a chart on there to show the result: http://jsfiddle.net/z64Jr/3/
我想回复吉姆·米歇尔上面的帖子(https://stackoverflow.com/a/8597889/1360592),但自从我想包含代码,我被迫将我的回复放在这里。
根据 Jim Mischel 的算法,我进行了以下 Java 实现,它很好地满足了我的需求,生成的数字在绘制图表时产生了视觉上吸引人、逼真的股票行情价格。
Java:
请注意,正如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:
Note that, as wiggles pointed out in his comment, I needed to divide percentage by 100 when declaring the changeAmount variable.
看看雅虎财经,他们提供来自证券交易所和图表的免费延迟数据。
这是一篇关于使用 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/
我需要为我正在开发的一款模拟游戏创建一些虚拟市场数据。我需要数据看起来像市场数据,但仍保持在一定范围内,以便可以根据当天的起始价格、最高/最低价格进行预测。
最后,我组合了不同频率的正弦波,然后添加了一些随机性,结果不仅看起来不错,而且是一致的(你不会得到任何看起来奇怪的东西)。即使在可以感知到正弦波图案的地方,它看起来仍然没问题。
随机生成的市场数据
代码是用 BASIC 脚本语言编写的,但应该非常简单理解并转换为您想要的任何语言。获得标准化数据数组后,将这些值乘以您想要获得有界数据集的任何最大值。
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.
这是我对红宝石的尝试! :) 这将输出一个字符串,您可以复制并粘贴到谷歌图表中。我允许数据出现正向、负向或无趋势。该代码可能会针对随机性/规律性进行优化和/或调整。
Google 图表:https://code.google.com/apis/ajax /playground/?type=visualization#line_chart
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
它是在java中。只需将结果绘制在 Excel 列中即可查看图表。使用大量值在 Excel 中绘制。有趣的是它看起来与真实的股票数据有多么相似。
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.
基于上述算法的 Golang 代码,作者:@Jim Mischel
Golang code based on the above algorithm by @Jim Mischel
这是我为我的使用而创建的代码。价格是为新烛台创建的,包括开盘价、最高价、最低价、收盘价和交易量。新价格是根据波动百分比生成的。我用了总计 5% 的价格。
该代码基于 C#。
用法:
创建 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.
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.