 # Stock System

How would I go about making a stock system? I understand how to do the math operations and all of that but the only thing I need help with is the random drops and stuff. I need help perfecting an algorithm that will make this stock system as realistic as possible. Here is my criteria:

Stocks will go up steady in the long run but may have ups and downs daily. Example:

Stocks will never outrageously shoot up. They have realistic peaks and realistic dips that will keep them normal.
Day range and 52wk range.
Possibly use real quotes from sites and have LUA retrieve the values.
Limit shares so that admins can release new shares if needed.
Finally, the algorithm that would contain the math needed to do all the number shit.

Are you looking for it to just randomly increase or decrease?

[lua]
timer.Create( “stock_timer”, 3600, 0, function()

Stock = Stock + math.Rand(0,6)

end
[/lua]

But then that would only go up. So make the first integer a negative or add another random statement to subtract or add.

[lua]
timer.Create( “stock_timer”, 3600, 0, function()

Stock = Stock + math.Rand(-6,6)

end
[/lua]

It would increase and decrease at random times, but in the long run it would be a steady increase with a few dips here and there. That is a good timer but once it reaches a peak if it goes up too much, it needs to start decreasing then. Could I do it so that if Stock variable has grown more than 5%(peak) it would increase very slightly or start to decrease. How would I do that? Pretty much in that last sentence, I am saying in LUA terms, if Stock has grown more than 5% then do whatever. It can’t be the same number everytime though.

This should, in theory, work. Yet it hasn’t been tested.
[lua]local CurrentStock = 0 --Main data variable for the stocks
local PTicker = 0 --Variable used to determine how many times the stocks went up in a row
local MTicker = 0 --^^ except opposite
local MaxRow = 4 --Maximum amount of times stocks could increase or decrease in a row
local ShouldRigUp = false
local ShouldRigDown = false

timer.Create( “stock_timer”, 60, 0, function()

``````local Rand = math.Rand(-6, 6) --Default up or down

if ShouldRigUp == true then --if the stocks has been rigged to go up then
Rand = math.Rand(1, 6) --Grab a random number from 1 to 6
end

if ShouldRigDown == true then --if the stocks has been rigged to go down then
Rand = math.Rand(-6, -1)
end

CurrentStock = CurrentStock + Rand --Applies the stock change

if Rand &gt; 0 then --Checks whether the stock increased
PTicker = PTicker + 1 --if so increase PTicker by 1
MTicker = 0 --And reset MTicker
else --if it decreased
MTicker = MTicker + 1 --Do the opposite
PTicker = 0
end

if PTicker &gt;= MaxRow then --if the maximum amount of increases has been met
ShouldRigDown = true --Then rig the stocks to go down
elseif MTicker &gt;= MaxRow then --if the opposite has occursed then
ShouldRigUp = true --Rig stocks to go up
end

if PTicker != MaxRow then
ShouldRigDown = false
end

if MTicker != MaxRow then
ShouldRigUp = false
end
``````

end[/lua]