Generate fake stock price option

What am I doing

I am making a chart of fictitious stock options. The price is updated every second using 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); } 

I use the values โ€‹โ€‹max_up and max_down (from 10 to 100) to gradually change the price and simulate some volatility.

For example, with max_up: 40 and max_down: 45, the price will gradually decline.

My question

But the problem is that the generated prices are too much volatile, even if max_up = max_down. The result is not applicable. (e.g. + 10 points in one day for a base price of 15,000).

The result of the evolution of prices per hour within 24 hours price evolution per hour in 24 hour

Is it possible that choosing a round ($ price, 4) and dividing by 10,000 instead of 1,000 would be better?

If anyone has an idea or advice to generate a โ€œnaturalโ€ evolution of prices, thanks in advance.

0
source share
2 answers

There are 86,400 seconds per day, so you need to divide by a much larger number. And instead of adding and subtracting, you can multiply the current price by a factor that is slightly larger or smaller than 1. This will 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); } 
+2
source

Based on this idea, you can use the actual volatility number. If you want, for example, volatility of 35% per year, you can find volatility per second. In pseudo code:

 vol_yr = 0.35 vol_month = vol_yr * sqrt(1.0/12) vol_second = vol_yr * sqrt(1.0/(252 * 86400)) # or 365 * 86400 

Then every second you โ€œflip a coinโ€ and either multiply or divide the current share price by (1 + vol_second). This is the principle of how binomial trees are created to evaluate exotic stock options.

0
source

Source: https://habr.com/ru/post/1397623/


All Articles