Advertisement
1m1m0

Market Prices "Emulator" Lua Model

Mar 2nd, 2024
49
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.11 KB | Money | 0 0
  1. -- Define a function to generate Gaussian random numbers
  2. local function gaussianRandom()
  3.     local u, v, s
  4.     repeat
  5.         u = 2 * math.random() - 1
  6.         v = 2 * math.random() - 1
  7.         s = u^2 + v^2
  8.     until s ~= 0 and s < 1
  9.     return u * math.sqrt(-2 * math.log(s) / s)
  10. end
  11.  
  12. -- Calculate the next price of the stock based on various factors
  13. local function calculateNextPrice(currentPrice, dt, drift, volatility, meanPrice, externalFactors)
  14.     -- Incorporate external factors
  15.     local externalInfluence = 0
  16.     for _, factor in ipairs(externalFactors) do
  17.         externalInfluence = externalInfluence + factor.influence * factor.value
  18.     end
  19.  
  20.     local randomShock = gaussianRandom()
  21.  
  22.     -- Slightly random drift component
  23.     local driftComponent = (drift + (meanPrice - currentPrice) / currentPrice + externalInfluence + math.random(-0.03, 0.03)) * dt
  24.  
  25.     -- Capped and dynamic volatility
  26.     local cappedVolatility = math.min(volatility, 0.3)
  27.     volatility = volatility + math.random(-0.02, 0.02)
  28.     volatility = math.clamp(volatility, 0.1, 0.3)
  29.  
  30.     local diffusion = cappedVolatility * randomShock * math.sqrt(dt)
  31.     local changeFactor = math.exp(driftComponent + diffusion)
  32.     return currentPrice * changeFactor
  33. end
  34.  
  35. -- Determine the emoji to display based on price changes
  36. local function determineEmoji(previousPrice, currentPrice)
  37.     local changeThreshold = 0.02
  38.     if currentPrice >= previousPrice * (1 + changeThreshold) then
  39.         return "🔼"
  40.     elseif currentPrice <= previousPrice * (1 - changeThreshold) then
  41.         return "🔻"
  42.     else
  43.         return "➖"
  44.     end
  45. end
  46.  
  47. -- Simulate the stock market
  48. local function simulateStockMarket()
  49.     local stock = {
  50.         name = "RoCoin (rC)",
  51.         price = math.random(50, 250),
  52.         drift = 0.05,
  53.         volatility = 0.15,
  54.         meanPrice = math.random(0, 1234567890) -- Initial mean price
  55.     }
  56.  
  57.     local externalEvents = {
  58.         {name = "EconomicNews", influence = 0.01, value = 0},
  59.         {name = "MarketCrash", influence = -0.2, value = 0},
  60.         {name = "ProductLaunch", influence = 0.15, value = 0},
  61.     }
  62.  
  63.     local previousPrice = stock.price
  64.  
  65.     local meanPriceTrend = 0.01 -- Average speed of mean price change
  66.     local meanPriceVolatility = 0.1 -- Volatility in the mean price change
  67.  
  68.     while true do
  69.         local dt = 1
  70.  
  71.         -- Potentially trigger an external event
  72.         if math.random() < 0.05 then
  73.             local eventIndex = math.random(1, #externalEvents)
  74.             externalEvents[eventIndex].value = math.random(-1, 1) * externalEvents[eventIndex].influence
  75.         end
  76.  
  77.         -- Update the mean price with a random drift
  78.         stock.meanPrice = stock.meanPrice + (meanPriceTrend * dt) + (gaussianRandom() * meanPriceVolatility * math.sqrt(dt))
  79.         stock.meanPrice = math.clamp(stock.meanPrice, 20, 80)
  80.  
  81.         stock.price = calculateNextPrice(
  82.             stock.price,
  83.             dt,
  84.             stock.drift,
  85.             stock.volatility,
  86.             stock.meanPrice,
  87.             externalEvents
  88.         )
  89.  
  90.         local change = stock.price - previousPrice
  91.         local percentageChange = (change / previousPrice) * 100
  92.  
  93.         local emoji = determineEmoji(previousPrice, stock.price)
  94.         print(string.format("%s: $%.2f %s (%.1f%%)", stock.name, stock.price, emoji, percentageChange))
  95.  
  96.         previousPrice = stock.price
  97.         wait(0.5)
  98.     end
  99. end
  100.  
  101. simulateStockMarket()
Advertisement
Comments
  • 1m1m0
    337 days
    # text 0.09 KB | 0 0
    1. 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