Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Define a function to generate Gaussian random numbers
- local function gaussianRandom()
- local u, v, s
- repeat
- u = 2 * math.random() - 1
- v = 2 * math.random() - 1
- s = u^2 + v^2
- until s ~= 0 and s < 1
- return u * math.sqrt(-2 * math.log(s) / s)
- end
- -- Calculate the next price of the stock based on various factors
- local function calculateNextPrice(currentPrice, dt, drift, volatility, meanPrice, externalFactors)
- -- Incorporate external factors
- local externalInfluence = 0
- for _, factor in ipairs(externalFactors) do
- externalInfluence = externalInfluence + factor.influence * factor.value
- end
- local randomShock = gaussianRandom()
- -- Slightly random drift component
- local driftComponent = (drift + (meanPrice - currentPrice) / currentPrice + externalInfluence + math.random(-0.03, 0.03)) * dt
- -- Capped and dynamic volatility
- local cappedVolatility = math.min(volatility, 0.3)
- volatility = volatility + math.random(-0.02, 0.02)
- volatility = math.clamp(volatility, 0.1, 0.3)
- local diffusion = cappedVolatility * randomShock * math.sqrt(dt)
- local changeFactor = math.exp(driftComponent + diffusion)
- return currentPrice * changeFactor
- end
- -- Determine the emoji to display based on price changes
- local function determineEmoji(previousPrice, currentPrice)
- local changeThreshold = 0.02
- if currentPrice >= previousPrice * (1 + changeThreshold) then
- return "🔼"
- elseif currentPrice <= previousPrice * (1 - changeThreshold) then
- return "🔻"
- else
- return "➖"
- end
- end
- -- Simulate the stock market
- local function simulateStockMarket()
- local stock = {
- name = "RoCoin (rC)",
- price = math.random(50, 250),
- drift = 0.05,
- volatility = 0.15,
- meanPrice = math.random(0, 1234567890) -- Initial mean price
- }
- local externalEvents = {
- {name = "EconomicNews", influence = 0.01, value = 0},
- {name = "MarketCrash", influence = -0.2, value = 0},
- {name = "ProductLaunch", influence = 0.15, value = 0},
- }
- local previousPrice = stock.price
- local meanPriceTrend = 0.01 -- Average speed of mean price change
- local meanPriceVolatility = 0.1 -- Volatility in the mean price change
- while true do
- local dt = 1
- -- Potentially trigger an external event
- if math.random() < 0.05 then
- local eventIndex = math.random(1, #externalEvents)
- externalEvents[eventIndex].value = math.random(-1, 1) * externalEvents[eventIndex].influence
- end
- -- Update the mean price with a random drift
- stock.meanPrice = stock.meanPrice + (meanPriceTrend * dt) + (gaussianRandom() * meanPriceVolatility * math.sqrt(dt))
- stock.meanPrice = math.clamp(stock.meanPrice, 20, 80)
- stock.price = calculateNextPrice(
- stock.price,
- dt,
- stock.drift,
- stock.volatility,
- stock.meanPrice,
- externalEvents
- )
- local change = stock.price - previousPrice
- local percentageChange = (change / previousPrice) * 100
- local emoji = determineEmoji(previousPrice, stock.price)
- print(string.format("%s: $%.2f %s (%.1f%%)", stock.name, stock.price, emoji, percentageChange))
- previousPrice = stock.price
- wait(0.5)
- end
- end
- simulateStockMarket()
Advertisement
Comments
-
- idk math so this is just for fun. this is not an educated remodeling of the stock market
Add Comment
Please, Sign In to add comment
Advertisement