产生虚拟的股票期权价格变动 [英] Generate a fictitious stock option price variation
问题描述
我做什么
我正在制作虚拟股票期权图. 使用此功能
I am making graph of fictitious stock options. The price is updated each second, with this function
function stockVariation($price,$max_up,$max_down)
{
// Price > 1
if($price > 1)
{
// Calculate
$ratio=(mt_rand(0,$max_up/2)-mt_rand(0,$max_down/2))/1000;
$price+=$ratio;
}
// Price <=1 (we don't want 0 or negative price...)
else
$price+=mt_rand(1,$max_up)/1000;
return round($price,3);
}
我使用max_up和max_down值(从10到100)使价格逐渐变化并模拟一些波动性.
I use a max_up and max_down values (from 10 to 100) to make the price change progressively and simulate some volatility.
例如,对于max_up:40和max_down:45,价格将逐渐下降.
For example, with max_up : 40 and max_down : 45, the price will progressively go down.
我的问题
但是问题是,即使max_up = max_down,产生的价格也会波动太大. 结果是非自然的". (例如,以15,000的基本价格在一天中获得+10积分).
But the problem, is that prices generated are too much volatile, even if max_up = max_down. The result is "non-natural". (for example +10 points in one day for a base price of 15,000).
24小时内每小时价格演变的结果
Result of price evolution per hour in 24 hour
也许进行舍入($ price,4)并除以10000而不是1000会更好吗?
Perhaps making round($price,4) and divisions by 10 000 instead of 1 000, will be better ?
如果有人对产生自然的"价格变化有想法或建议,请先感谢.
If anyone have an idea or an advice to generate "natural" prices evolution, thanks in advance.
推荐答案
一天中有86400秒,因此您需要除以更大的数字.而且,您可能希望将当前价格乘以一个略大于或小于1的因子,而不是相加或相减.这将模拟一个百分比的增加或减少,而不是绝对的收益或损失.
There are 86400 seconds in a day, so you'll need to divide by a much larger number. And rather than adding and subtracting, you may want to multiply the current price by a factor that's slightly larger or smaller than 1. That would simulate a percentage increase or decrease, rather than an absolute gain or loss.
function stockVariation($price, $max_up, $max_down)
{
// Convert up/down to fractions of the current price.
// These will be very small positive numbers.
$random_up = mt_rand(0, $max_up) / $price;
$random_down = mt_rand(0, $max_down) / $price;
// Increase the price based on $max_up and decrease based on $max_down.
// This calculates the daily change that would result, which is slightly
// larger or smaller than 1.
$daily_change = (1 + $random_up) / (1 + $random_down);
// Since we're calling this function every second, we need to convert
// from change-per-day to change-per-second. This will make only a
// tiny change to $price.
$price = $price * $daily_change / 86400;
return round($price, 3);
}
这篇关于产生虚拟的股票期权价格变动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!