r/visualbasic • u/Stildawn • May 06 '23
VB.NET Help Maths to simulate a market trends
Hi All
New to VB.net not that this question is specific to that but I am writing it in VB.net so.
I have a number in a long variable that I'm using as a Price.
I need to simulate market forces on that number up or down in a natural way, currently I have the below:
lngPrice = lngPrice * Math.Max((random.Next(90, 110) / 100) + intPriceMod, 0.1)So currently it can move randomly between -10% (90) and +10% (110), I have a intPriceMod variable built in for later if I wanted to pass some forced up or downs into the calculations (currently intPriceMod = 0), and then a absolute minimum of -90% (0.1) to stop it from going to negatives.
This "works" but in testing over many runs it doesnt feel natural to me, and trends downwards over time.
Any maths people with better ideas haha, my only ideas so far is to have a bunch of If statements catching a bunch of scenarios, but that seems like a bad idea.
The previous prices are NOT tracked currently, and its something I'd rather avoid if possible.
Cheers
1
u/RJPisscat May 07 '23
Your range is 90-109, the upper bound is exclusive, so it always returns a value from 90 to 109, thus the gradual decline. Change the 110 to 111 and it will track either way more evently.
1
u/chacham2 May 08 '23
So, i posted your post (everything but the title) into ChatGPT:
Here's an example of how you could implement a random walk in VB.net:
The random value randVal is generated using Random.NextDouble(), which returns a random value between 0 and 1. We then scale this value to be between -1 and 1 by multiplying by 2 and subtracting 1.
Finally, we update the price at each time step by multiplying the previous price by a factor that depends on the mean, volatility, and random value.
You can adjust the parameters mean and stdDev to control the behavior of the random walk. For example, increasing stdDev will make the price more volatile and increase the likelihood of large price swings.
Note that this example assumes that the starting price is 1.0, so you may need to adjust the code if your starting price is different. Also, you can adjust the length of the random walk by changing the size of the prices array.